Fixes #149
[akelos.git] / lib / AkDbSession.php
blob496809027a77a099b787494261672568a4c98734
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 // +----------------------------------------------------------------------+
5 // | Akelos Framework - http://www.akelos.org |
6 // +----------------------------------------------------------------------+
7 // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
8 // | Released under the GNU Lesser General Public License, see LICENSE.txt|
9 // +----------------------------------------------------------------------+
11 /**
12 * @package ActionController
13 * @subpackage Sessions
14 * @author Bermi Ferrer <bermi a.t akelos c.om>
15 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
16 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
19 if(!defined('AK_DBSESSION_CLASS_INCLUDED')){ define('AK_DBSESSION_CLASS_INCLUDED',true); // Class overriding trick
22 require_once(AK_LIB_DIR.DS.'Ak.php');
23 require_once(AK_LIB_DIR.DS.'AkObject.php');
26 /**
27 * Database based session.
29 * This class enables saving sessions into a database. This can
30 * be usefull for multiple server sites, and to have more
31 * control over sessions.
33 * <code>
35 * require_once(AK_LIB_DIR.'/AkDbSession.php');
37 * $AkDbSession = new AkDbSession();
38 * $AkDbSession->sessionLife = AK_SESSION_EXPIRE;
39 * session_set_save_handler (
40 * array(&$AkDbSession, '_open'),
41 * array(&$AkDbSession, '_close'),
42 * array(&$AkDbSession, '_read'),
43 * array(&$AkDbSession, '_write'),
44 * array(&$AkDbSession, '_destroy'),
45 * array(&$AkDbSession, '_gc')
46 * );
48 * </code>
50 * @author Bermi Ferrer <bermi at akelos com>
51 * @copyright Copyright (c) 2002-2005, Akelos Media, S.L. http://www.akelos.org
52 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
53 * @since 0.1
54 * @version $Revision 0.1 $
56 class AkDbSession extends AkObject
58 /**
59 * Secconds for the session to expire.
61 * @see setSessionLife
62 * @access public
63 * @var integer $sessionLife
65 var $sessionLife = AK_SESSION_EXPIRE;
67 /**
68 * Database instance handler
70 * Stores a reference to an ADODB database instance.
72 * @access protected
73 * @var object $_db
75 var $_db;
77 /**
78 * Original session value for avoiding hitting the database in case nothing has changed
80 * @access private
81 * @var string $_db
83 var $_original_sess_value = '';
85 /**
86 * $this->sessionLife setter
88 * Use this method to set $this->sessionLife value
90 * @access public
91 * @see get$sessionLife
92 * @param integer $sessionLife Secconds for the session to expire.
93 * @return bool Returns true if $this->sessionLife has been set
94 * correctly.
96 function setSessionLife($sessionLife)
98 $this->sessionLife = $sessionLife;
102 // ---- Protected methods ---- //
105 * Session open handler
107 * @access protected
108 * @return boolean
110 function _open()
112 $this->_db =& Ak::db();
113 return true;
117 * Session close handler
119 * @access protected
120 * @return boolean
122 function _close()
125 * @todo Get from cached vars last time garbage collection was made to avoid hitting db
126 * on every request
128 $this->_gc();
129 return true;
133 * Session read handler
135 * @access protected
136 * @param string $id Session Id
137 * @return string
139 function _read($id)
141 $result = $this->_db->selectValue("SELECT value FROM sessions WHERE id = ".$this->_db->quote_string($id));
142 return is_null($result) ? '' : (string)$result;
146 * Session write handler
148 * @access protected
149 * @param string $id
150 * @param string $data
151 * @return boolean
153 function _write($id, $data)
155 // We don't want to hit the db if nothing has changed
156 if($this->_original_sess_value != $data){
158 * @todo replace with dbAdapter-method
160 $ret = $this->_db->connection->Replace('sessions', array('id'=>$this->_db->quote_string($id),'expire'=>$this->_db->quote_datetime(time()),'value'=>$this->_db->quote_string($data)), 'id');
161 if($ret == 0){
162 return false;
163 }else{
164 return true;
166 }else {
167 return true;
172 * Session destroy handler
174 * @access protected
175 * @param string $id
176 * @return boolean
178 function _destroy($id)
180 return (bool)$this->_db->delete('DELETE FROM sessions WHERE id = '.$this->_db->quote_string($id));
184 * Session garbage collection handler
186 * @access protected
187 * @return boolean
189 function _gc()
191 return (bool)$this->_db->delete('DELETE FROM sessions WHERE expire < '.$this->_db->quote_datetime(time()-$this->sessionLife));
197 }// End of if(!defined('AK_DBSESSION_CLASS_INCLUDED')){