| [ Index ] |
PHP Cross Reference of Akelos Framework |
[Summary view] [Print] [Text view]
1 <?php 2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ 3 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 // +----------------------------------------------------------------------+ 10 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> 17 */ 18 19 if(!defined('AK_SESSION_CLASS_INCLUDED')){ define('AK_SESSION_CLASS_INCLUDED',true); // Class overriding trick 20 21 22 require_once (AK_LIB_DIR.DS.'Ak.php'); 23 require_once (AK_LIB_DIR.DS.'AkObject.php'); 24 25 26 /** 27 * Memcache based session. 28 * 29 * This class enables saving sessions into a database or memcache. 30 * see: config/DEFAULT-DB-sessions.yml or config/DEFAULT-MEMCACHE-sessions.yml 31 * 32 * This can 33 * be usefull for multiple server sites, and to have more 34 * control over sessions. 35 * 36 * <code> 37 * 38 * require_once(AK_LIB_DIR.DS.'AkSession.php'); 39 * $SessionHandler = &AkSession::initHandler(); 40 * 41 * </code> 42 * 43 * @author Bermi Ferrer <bermi at akelos com> 44 * @author Arno Schneider <arno at bermilabs com> 45 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org 46 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html> 47 * @since 0.9 48 * @version $Revision 0.9 $ 49 */ 50 class AkSession extends AkObject 51 { 52 53 /** 54 * Session driver 55 * 56 * Stores session data using cache handlers 57 * 58 * @access protected 59 * @var object $_driverInstance 60 */ 61 var $_driverInstance; 62 63 var $sessions_enabled; 64 /** 65 * Original session value for avoiding hitting the cache system in case nothing has changed 66 * 67 * @access private 68 * @var string $_db 69 */ 70 var $_original_sess_value = ''; 71 72 function initHandler() 73 { 74 $settings = Ak::getSettings('sessions', false); 75 $SessionHandler = &AkSession::lookupStore($settings); 76 return $SessionHandler; 77 } 78 79 function &lookupStore($options = null) 80 { 81 static $session_store; 82 $false = false; 83 if ($options === true && !empty($session_store)) { 84 return $session_store; 85 } else if (is_array($options) && 86 isset($options['enabled']) && $options['enabled']==true && 87 isset($options['handler']) && 88 isset($options['handler']['type'])) { 89 $type = $options['handler']['type']; 90 $options = isset($options['handler']['options'])?$options['handler']['options']:array(); 91 } else if (is_string($options) || is_int($options)) { 92 $type = $options; 93 $options = array(); 94 } else { 95 return $false; 96 } 97 $session_store = new AkSession(); 98 $session_store->init($options,$type); 99 if ($session_store->sessions_enabled) { 100 return $session_store; 101 } 102 return $false; 103 } 104 105 function init($options = array(),$type = null) 106 { 107 $options = is_int($options) ? array('lifeTime'=>$options) : (is_array($options) ? $options : array()); 108 109 switch ($type) { 110 case 1: 111 $this->sessions_enabled = false; 112 if(isset($options['save_path'])) { 113 session_save_path($options['save_path']); 114 } 115 break; 116 case 2: 117 require_once (AK_LIB_DIR.'/AkCache/AkAdodbCache.php'); 118 $this->_driverInstance =& new AkAdodbCache(); 119 $res = $this->_driverInstance->init($options); 120 $this->sessions_enabled = $res; 121 break; 122 case 3: 123 require_once (AK_LIB_DIR.'/AkCache/AkMemcache.php'); 124 $this->_driverInstance =& new AkMemcache(); 125 $res = $this->_driverInstance->init($options); 126 $this->sessions_enabled = $res; 127 break; 128 default: 129 $this->sessions_enabled = false; 130 break; 131 } 132 if ($this->sessions_enabled) { 133 $this->sessionLife = $options['lifeTime']; 134 session_set_save_handler ( 135 array(&$this, '_open'), 136 array(&$this, '_close'), 137 array(&$this, '_read'), 138 array(&$this, '_write'), 139 array(&$this, '_destroy'), 140 array(&$this, '_gc') 141 ); 142 143 } 144 } 145 /** 146 * $this->sessionLife setter 147 * 148 * Use this method to set $this->sessionLife value 149 * 150 * @access public 151 * @see get$sessionLife 152 * @param integer $sessionLife Secconds for the session to expire. 153 * @return bool Returns true if $this->sessionLife has been set 154 * correctly. 155 */ 156 function setSessionLife($sessionLife) 157 { 158 $this->sessionLife = $sessionLife; 159 160 } 161 162 // ---- Protected methods ---- // 163 164 /** 165 * Session open handler 166 * 167 * @access protected 168 * @return boolean 169 */ 170 function _open() 171 { 172 return true; 173 } 174 175 /** 176 * Session close handler 177 * 178 * @access protected 179 * @return boolean 180 */ 181 function _close() 182 { 183 /** 184 * @todo Get from cached vars last time garbage collection was made to avoid hitting db 185 * on every request 186 */ 187 $this->_gc(); 188 return true; 189 } 190 191 /** 192 * Session read handler 193 * 194 * @access protected 195 * @param string $id Session Id 196 * @return string 197 */ 198 function _read($id) 199 { 200 $result = $this->_driverInstance->get($id,'AK_SESSIONS'); 201 return is_null($result) ? '' : (string)$result; 202 } 203 204 /** 205 * Session write handler 206 * 207 * @access protected 208 * @param string $id 209 * @param string $data 210 * @return boolean 211 */ 212 function _write($id, $data) 213 { 214 // We don't want to hit the cache handler if nothing has changed 215 if($this->_original_sess_value != $data){ 216 $ret = $this->_driverInstance->save($data, $id,'AK_SESSIONS'); 217 if(!$ret){ 218 return false; 219 }else{ 220 return true; 221 } 222 }else { 223 return true; 224 } 225 } 226 227 /** 228 * Session destroy handler 229 * 230 * @access protected 231 * @param string $id 232 * @return boolean 233 */ 234 function _destroy($id) 235 { 236 return (bool)$this->_driverInstance->remove($id,'AK_SESSIONS'); 237 } 238 239 /** 240 * Session garbage collection handler 241 * 242 * @access protected 243 * @return boolean 244 */ 245 function _gc() 246 { 247 return (bool)$this->_driverInstance->clean('AK_SESSIONS','old'); 248 } 249 250 251 } 252 253 } 254 // END OF AK_SESSION_CLASS_INCLUDED 255 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Oct 27 12:43:49 2008 | Cross-referenced by PHPXref 0.6 |