00001 <?php
00002
00015
00016
00017
00018 class SessionManager {
00019
00021 var $sessionDao;
00022
00024 var $userSession;
00025
00031 function SessionManager(&$sessionDao) {
00032 $this->sessionDao = &$sessionDao;
00033
00034
00035 ini_set('session.use_trans_sid', 0);
00036 ini_set('session.save_handler', 'user');
00037 ini_set('session.serialize_handler', 'php');
00038 ini_set('session.use_cookies', 1);
00039 ini_set('session.name', Config::getVar('general', 'session_cookie_name'));
00040 ini_set('session.cookie_lifetime', 0);
00041 ini_set('session.cookie_path', Request::getBasePath() . '/');
00042 ini_set('session.gc_probability', 1);
00043 ini_set('session.gc_maxlifetime', 60 * 60);
00044 ini_set('session.auto_start', 1);
00045 ini_set('session.cache_limiter', 'none');
00046
00047 session_set_save_handler(
00048 array(&$this, 'open'),
00049 array(&$this, 'close'),
00050 array(&$this, 'read'),
00051 array(&$this, 'write'),
00052 array(&$this, 'destroy'),
00053 array(&$this, 'gc')
00054 );
00055
00056
00057 session_start();
00058 $sessionId = session_id();
00059
00060 $ip = Request::getRemoteAddr();
00061 $userAgent = Request::getUserAgent();
00062 $now = time();
00063
00064 if (!isset($this->userSession) || (Config::getVar('security', 'session_check_ip') && $this->userSession->getIpAddress() != $ip) || $this->userSession->getUserAgent() != $userAgent) {
00065 if (isset($this->userSession)) {
00066
00067 session_destroy();
00068 }
00069
00070
00071 $this->userSession = &new Session();
00072 $this->userSession->setId($sessionId);
00073 $this->userSession->setIpAddress($ip);
00074 $this->userSession->setUserAgent($userAgent);
00075 $this->userSession->setSecondsCreated($now);
00076 $this->userSession->setSecondsLastUsed($now);
00077 $this->userSession->setSessionData('');
00078
00079 $this->sessionDao->insertSession($this->userSession);
00080
00081 } else {
00082 if ($this->userSession->getRemember()) {
00083
00084 if (Config::getVar('general', 'session_lifetime') > 0) {
00085 $this->updateSessionLifetime(time() + Config::getVar('general', 'session_lifetime') * 86400);
00086 } else {
00087 $this->userSession->setRemember(0);
00088 $this->updateSessionLifetime(0);
00089 }
00090 }
00091
00092
00093 $this->userSession->setSecondsLastUsed($now);
00094 $this->sessionDao->updateSession($this->userSession);
00095 }
00096 }
00097
00102 function &getManager() {
00103 static $instance;
00104
00105 if (!isset($instance)) {
00106 $instance = new SessionManager(DAORegistry::getDAO('SessionDAO'));
00107 }
00108 return $instance;
00109 }
00110
00115 function &getUserSession() {
00116 return $this->userSession;
00117 }
00118
00124 function open() {
00125 return true;
00126 }
00127
00133 function close() {
00134 return true;
00135 }
00136
00142 function read($sessionId) {
00143 if (!isset($this->userSession)) {
00144 $this->userSession = &$this->sessionDao->getSession($sessionId);
00145 if (isset($this->userSession)) {
00146 $data = $this->userSession->getSessionData();
00147 }
00148 }
00149 return isset($data) ? $data : '';
00150 }
00151
00158 function write($sessionId, $data) {
00159 if (isset($this->userSession)) {
00160 $this->userSession->setSessionData($data);
00161 return $this->sessionDao->updateSession($this->userSession);
00162
00163 } else {
00164 return true;
00165 }
00166 }
00167
00173 function destroy($sessionId) {
00174 return $this->sessionDao->deleteSessionById($sessionId);
00175 }
00176
00183 function gc($maxlifetime) {
00184 return $this->sessionDao->deleteSessionByLastUsed(time() - 86400, Config::getVar('general', 'session_lifetime') <= 0 ? 0 : time() - Config::getVar('general', 'session_lifetime') * 86400);
00185 }
00186
00193 function updateSessionCookie($sessionId = false, $expireTime = 0) {
00194 return setcookie(session_name(), ($sessionId === false) ? session_id() : $sessionId, $expireTime, ini_get('session.cookie_path'));
00195 }
00196
00204 function regenerateSessionId() {
00205 $success = false;
00206 $currentSessionId = session_id();
00207
00208 if (function_exists('session_regenerate_id')) {
00209
00210 if (session_regenerate_id() && isset($this->userSession)) {
00211
00212 $this->sessionDao->deleteSessionById($currentSessionId);
00213 $this->userSession->setId(session_id());
00214 $this->sessionDao->insertSession($this->userSession);
00215 $this->updateSessionCookie();
00216 $success = true;
00217 }
00218
00219 } else {
00220
00221 do {
00222
00223 $newSessionId = md5(mt_rand());
00224 } while ($this->sessionDao->sessionExistsById($newSessionId));
00225
00226 if (isset($this->userSession)) {
00227
00228 $this->sessionDao->deleteSessionById($currentSessionId);
00229 $this->userSession->setId($newSessionId);
00230 $this->sessionDao->insertSession($this->userSession);
00231 $this->updateSessionCookie($newSessionId);
00232 $success = true;
00233 }
00234 }
00235
00236 return $success;
00237 }
00238
00244 function updateSessionLifetime($expireTime = 0) {
00245 return $this->updateSessionCookie(false, $expireTime);
00246 }
00247
00248 }
00249
00250 ?>