Rearranging scripts to reduce the hassle of updating local application whenever scrip...
[akelos.git] / lib / AkDbSession.php
bloba451d64242b8c69164cb2d2913011bcf7bac1dcc
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 // {{{ properties
61 // --- Public properties --- //
64 /**
65 * Secconds for the session to expire.
67 * @see setSessionLife
68 * @access public
69 * @var integer $sessionLife
71 var $sessionLife = AK_SESSION_EXPIRE;
74 // --- Protected properties --- //
77 /**
78 * Database instance handler
80 * Stores a reference to an ADODB database instance.
82 * @access protected
83 * @var object $_db
85 var $_db;
87 // }}}
90 /**
91 * Original session value for avoiding hitting the database in case nothing has changed
93 * @access private
94 * @var string $_db
96 var $_original_sess_value = '';
98 // }}}
101 // ------ CLASS METHODS ------ //
105 // ---- Setters ---- //
108 // {{{ setSessionLife()
111 * $this->sessionLife setter
113 * Use this method to set $this->sessionLife value
115 * @access public
116 * @see get$sessionLife
117 * @param integer $sessionLife Secconds for the session to expire.
118 * @return bool Returns true if $this->sessionLife has been set
119 * correctly.
121 function setSessionLife($sessionLife)
123 $this->sessionLife = $sessionLife;
127 // }}}
130 // ---- Protected methods ---- //
133 // {{{ _open()
136 * Session open handler
138 * @access protected
139 * @return boolean
141 function _open()
143 $this->_db =& Ak::db();
144 return true;
147 // }}}
148 // {{{ _close()
151 * Session close handler
153 * @access protected
154 * @return boolean
156 function _close()
159 * @todo Get from cached vars last time garbage collection was made to avoid hitting db
160 * on every request
162 $this->_gc();
163 return true;
166 // }}}
167 // {{{ _read()
170 * Session read handler
172 * @access protected
173 * @param string $id Session Id
174 * @return string
176 function _read($id)
178 $query_result = $this->_db->Execute("SELECT value FROM sessions WHERE id = ".$this->_db->qstr($id));
179 if(!$query_result && AK_DEBUG){
180 trigger_error($this->_db->ErrorMsg(), E_USER_NOTICE);
181 }else{
182 $this->_original_sess_value = (string)$query_result->fields[0];
183 return $this->_original_sess_value;
185 return '';
188 // }}}
189 // {{{ _write()
192 * Session write handler
194 * @access protected
195 * @param string $id
196 * @param string $data
197 * @return boolean
199 function _write($id, $data)
201 // We don't want to hit the db if nothing has changed
202 if($this->_original_sess_value != $data){
203 $ret = $this->_db->Replace('sessions', array('id'=>$this->_db->qstr($id),'expire'=>$this->_db->DBTimeStamp(time()),'value'=>$this->_db->qstr($data)), 'id');
204 if($ret == 0){
205 return false;
206 }else{
207 return true;
209 }else {
210 return true;
214 // }}}
215 // {{{ _destroy()
218 * Session destroy handler
220 * @access protected
221 * @param string $id
222 * @return boolean
224 function _destroy($id)
226 if(!$this->_db->Execute('DELETE FROM sessions WHERE id = '.$this->_db->qstr($id)) && AK_DEBUG){
227 trigger_error($this->_db->ErrorMsg(), E_USER_NOTICE);
229 return (bool)@$this->_db->Affected_Rows();
232 // }}}
233 // {{{ _gc()
236 * Session garbage collection handler
238 * @access protected
239 * @return boolean
241 function _gc()
243 if(!$this->_db->Execute('DELETE FROM sessions WHERE expire < '.$this->_db->DBTimeStamp(time()-$this->sessionLife)) && AK_DEBUG){
244 trigger_error($this->_db->ErrorMsg(), E_USER_NOTICE);
246 return (bool)$this->_db->Affected_Rows();
249 // }}}
254 }// End of if(!defined('AK_DBSESSION_CLASS_INCLUDED')){