[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 /** 3 * SessionHandler:: defines an API for implementing custom session 4 * handlers for PHP. 5 * 6 * $Horde: framework/SessionHandler/SessionHandler.php,v 1.13.10.11 2006/07/14 08:54:59 jan Exp $ 7 * 8 * Copyright 2002-2006 Mike Cochrane <mike@graftonhall.co.nz> 9 * 10 * See the enclosed file COPYING for license information (LGPL). If you 11 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 12 * 13 * @author Mike Cochrane <mike@graftonhall.co.nz> 14 * @since Horde 3.0 15 * @package Horde_SessionHandler 16 */ 17 class SessionHandler { 18 19 /** 20 * Hash containing connection parameters. 21 * 22 * @var array 23 */ 24 var $_params = array(); 25 26 /** 27 * Constructs a new SessionHandler object. 28 * 29 * @param array $params A hash containing connection parameters. 30 */ 31 function SessionHandler($params = array()) 32 { 33 $this->_params = $params; 34 } 35 36 /** 37 * Attempts to return a concrete SessionHandler instance based on 38 * $driver. 39 * 40 * @param string $driver The type of concrete SessionHandler subclass to 41 * return. 42 * @param array $params A hash containing any additional configuration or 43 * connection parameters a subclass might need. 44 * 45 * @return mixed The newly created concrete SessionHandler instance, or 46 * false on an error. 47 */ 48 function &factory($driver, $params = null) 49 { 50 if (is_array($driver)) { 51 $app = $driver[0]; 52 $driver = $driver[1]; 53 } 54 55 $driver = basename($driver); 56 if ($driver == 'memcached') { 57 // Trap for old driver name. 58 $driver = 'memcache'; 59 } 60 61 $class = 'SessionHandler_' . $driver; 62 if (!class_exists($class)) { 63 if (!empty($app)) { 64 include_once $GLOBALS['registry']->get('fileroot', $app) . '/lib/SessionHandler/' . $driver . '.php'; 65 } else { 66 include_once 'Horde/SessionHandler/' . $driver . '.php'; 67 } 68 } 69 70 if (class_exists($class)) { 71 if (is_null($params)) { 72 include_once 'Horde.php'; 73 $params = Horde::getDriverConfig('sessionhandler', $driver); 74 } 75 $handler = new $class($params); 76 } else { 77 $handler = PEAR::raiseError('Class definition of ' . $class . ' not found.'); 78 } 79 80 return $handler; 81 } 82 83 /** 84 * Attempts to return a reference to a concrete SessionHandler 85 * instance based on $driver. It will only create a new instance 86 * if no SessionHandler instance with the same parameters 87 * currently exists. 88 * 89 * This method must be invoked as: $var = &SessionHandler::singleton() 90 * 91 * @param string $driver See SessionHandler::factory(). 92 * @param array $params See SessionHandler::factory(). 93 * 94 * @return mixed The created concrete SessionHandler instance, or false 95 * on error. 96 */ 97 function &singleton($driver, $params = null) 98 { 99 static $instances = array(); 100 101 $signature = serialize(array($driver, $params)); 102 if (empty($instances[$signature])) { 103 $instances[$signature] = &SessionHandler::factory($driver, $params); 104 } 105 106 return $instances[$signature]; 107 } 108 109 /** 110 * Open the SessionHandler backend. 111 * 112 * @abstract 113 * 114 * @param string $save_path The path to the session object. 115 * @param string $session_name The name of the session. 116 * 117 * @return boolean True on success, false otherwise. 118 */ 119 function open($save_path, $session_name) 120 { 121 return true; 122 } 123 124 /** 125 * Close the SessionHandler backend. 126 * 127 * @abstract 128 * 129 * @return boolean True on success, false otherwise. 130 */ 131 function close() 132 { 133 return true; 134 } 135 136 /** 137 * Read the data for a particular session identifier from the 138 * SessionHandler backend. 139 * 140 * @abstract 141 * 142 * @param string $id The session identifier. 143 * 144 * @return string The session data. 145 */ 146 function read($id) 147 { 148 return PEAR::raiseError(_("Not supported.")); 149 } 150 151 /** 152 * Write session data to the SessionHandler backend. 153 * 154 * @abstract 155 * 156 * @param string $id The session identifier. 157 * @param string $session_data The session data. 158 * 159 * @return boolean True on success, false otherwise. 160 */ 161 function write($id, $session_data) 162 { 163 return PEAR::raiseError(_("Not supported.")); 164 } 165 166 /** 167 * Destroy the data for a particular session identifier in the 168 * SessionHandler backend. 169 * 170 * @abstract 171 * 172 * @param string $id The session identifier. 173 * 174 * @return boolean True on success, false otherwise. 175 */ 176 function destroy($id) 177 { 178 return PEAR::raiseError(_("Not supported.")); 179 } 180 181 /** 182 * Garbage collect stale sessions from the SessionHandler backend. 183 * 184 * @abstract 185 * 186 * @param integer $maxlifetime The maximum age of a session. 187 * 188 * @return boolean True on success, false otherwise. 189 */ 190 function gc($maxlifetime = 300) 191 { 192 return PEAR::raiseError(_("Not supported.")); 193 } 194 195 /** 196 * Determines if a session belongs to an authenticated user. 197 * 198 * @access private 199 * 200 * @param string $session_data The session data itself. 201 * @param boolean $return_user If true, return the user session data. 202 * 203 * @return boolean|string True or the user's session data if the session 204 * belongs to an authenticated user. 205 */ 206 function _isAuthenticated($session_data, $return_data = false) 207 { 208 if (empty($session_data)) { 209 return false; 210 } 211 212 while ($session_data) { 213 $vars = explode('|', $session_data, 2); 214 $data = unserialize($vars[1]); 215 if ($vars[0] == '__auth') { 216 if (empty($data)) { 217 return false; 218 } 219 if (!empty($data['authenticated'])) { 220 return $return_data ? $data : true; 221 } 222 return false; 223 } 224 $session_data = substr($session_data, strlen($vars[0]) + strlen(serialize($data)) + 1); 225 } 226 } 227 228 /** 229 * Get a list of the valid session identifiers. 230 * 231 * @abstract 232 * 233 * @return array A list of valid session identifiers. 234 */ 235 function getSessionIDs() 236 { 237 return PEAR::raiseError(_("Not supported.")); 238 } 239 240 /** 241 * Determine the number of currently logged in users. 242 * 243 * @return integer A count of logged in users. 244 */ 245 function countAuthenticatedUsers() 246 { 247 $count = 0; 248 249 $sessions = $this->getSessionIDs(); 250 if (is_a($sessions, 'PEAR_Error')) { 251 return $sessions; 252 } 253 254 foreach ($sessions as $id) { 255 $data = $this->read($id); 256 if (!is_a($data, 'PEAR_Error') && 257 $this->_isAuthenticated($data)) { 258 $count++; 259 } 260 } 261 262 return $count; 263 } 264 265 /** 266 * Returns a list of currently logged in users. 267 * 268 * @return array A list of logged in users. 269 */ 270 function listAuthenticatedUsers($date = false) 271 { 272 $users = array(); 273 274 $sessions = $this->getSessionIDs(); 275 if (is_a($sessions, 'PEAR_Error')) { 276 return $sessions; 277 } 278 279 foreach ($sessions as $id) { 280 $data = $this->read($id); 281 if (is_a($data, 'PEAR_Error')) { 282 continue; 283 } 284 285 $data = $this->_isAuthenticated($data, true); 286 if ($data !== false) { 287 $user = $data['userId']; 288 if ($date) { 289 $user = date('r', $data['timestamp']) . ' ' . $user; 290 } 291 $users[] = $user; 292 } 293 } 294 295 return $users; 296 } 297 298 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |