PageRenderTime 38ms CodeModel.GetById 20ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

/framework/core/session/SessionDb.php

http://zoop.googlecode.com/
PHP | 116 lines | 94 code | 18 blank | 4 comment | 6 complexity | 9d13d0c448bb40cc9f84172fb7d8eecb MD5 | raw file
  1<?php
  2class SessionDb
  3{
  4	var $db;
  5	var $style;
  6	var $saveChanges;
  7	var $sessionName;
  8	var $sessionId;
  9	
 10	public function __construct($params)
 11	{
 12		$this->db = DbModule::getConnection($params['db_connection']);
 13		$this->style = $params['style'];
 14	}
 15	
 16	//	user functions
 17	function start()
 18	{
 19		$this->saveChanges = 0;
 20		session_set_save_handler(array($this, 'open'), array($this, 'close'), array($this, 'read'), array($this, 'write'), array($this, 'destroy'), array($this, 'gc'));
 21		session_start();
 22	}
 23	
 24	function get($key = '__default__')
 25	{
 26		$row = $this->db->selsertRow('session_data', array('value'), array('session_id' => $this->sessionId, 'key' => $key));
 27		
 28		if($row['value'])
 29			return $row['value'];
 30		else
 31			return NULL;
 32	}
 33	
 34	function getWithLock($key = '__default__')
 35	{
 36		
 37	}
 38	
 39	function set($value, $key = '__default__')
 40	{
 41		$this->db->upsertRow('session_data', array('session_id' => $this->sessionId, 'key' => $key), array('value' => $value));
 42	}
 43	
 44	function saveChanges()
 45	{
 46		if(session_style != 'serialize_manaul')
 47			trigger_error("this function is only used with session_style = 'serialize_manaual'");
 48		$this->saveChanges = 1;
 49	}
 50	
 51	function saveChangesUnsafe()
 52	{
 53		if($this->style != 'write_manual')
 54			trigger_error("this function is only used with session_style = 'write_manaual'");
 55		$this->saveChanges = 1;
 56	}
 57	
 58	//	callbacks
 59	function open($savePath, $sessionName)
 60	{
 61		$this->sessionName = $sessionName;
 62		return true;
 63	}
 64	
 65	function close()
 66	{
 67		return true;
 68	}
 69	
 70	function read($sessionId)
 71	{
 72		$this->sessionId = $sessionId;
 73		//	make sure the session_base record is in there
 74		$this->db->upsertRow('session_base', array('session_id' => $sessionId), array('last_active:keyword' => 'CURRENT_TIMESTAMP'));
 75		
 76		$data = $this->get();
 77		
 78		//	get the default row
 79		return $data;
 80	}
 81	
 82	function write($sessionId, $sessionData)
 83	{
 84		switch($this->style)
 85		{
 86			case 'serialize_manual':
 87			case 'write_manual':
 88				if($this->saveChanges)
 89					$this->set($sessionData);
 90				break;
 91			default:
 92				trigger_error("session style '" . session_style . "' not yet implemented");
 93				break;
 94		}
 95		
 96		return true;
 97	}
 98	
 99	function destroy($sessionId)
100	{
101		$this->db->beginTransaction();
102		$this->db->deleteRows("delete from session_data where session_id = :session_id", array('session_id' => $sessionId));
103		$this->db->deleteRow("delete from session_base where session_id = :session_id", array('session_id' => $sessionId));
104		$this->db->commitTransaction();
105		return true;
106	}
107	
108	function gc($maxLifetime)
109	{
110		$this->db->beginTransaction();
111		$this->db->deleteRows("delete from session_base where extract(epoch from CURRENT_TIMESTAMP - last_active) > :maxLifetime", array('maxLifetime' => $maxLifetime));
112		$this->db->deleteRows("delete from session_data where session_id not in (select session_id from session_base)", array());
113		$this->db->commitTransaction();
114		return true;
115	}
116}