Open Monograph Press  3.3.0
SessionManager.inc.php
1 <?php
2 
17 
19  var $sessionDao;
20 
23 
31  function __construct($sessionDao, $request) {
32  $this->sessionDao = $sessionDao;
33 
34  // Configure PHP session parameters
35  ini_set('session.use_trans_sid', 0);
36  ini_set('session.serialize_handler', 'php');
37  ini_set('session.use_cookies', 1);
38  ini_set('session.name', Config::getVar('general', 'session_cookie_name')); // Cookie name
39  ini_set('session.cookie_lifetime', 0);
40  ini_set('session.cookie_path', Config::getVar('general', 'session_cookie_path', $request->getBasePath() . '/'));
41  ini_set('session.cookie_domain', $request->getServerHost(null, false));
42  ini_set('session.gc_probability', 1);
43  ini_set('session.gc_maxlifetime', 60 * 60);
44  ini_set('session.auto_start', 1);
45  ini_set('session.cache_limiter', 'none');
46 
47  session_set_save_handler(
48  array($this, 'open'),
49  array($this, 'close'),
50  array($this, 'read'),
51  array($this, 'write'),
52  array($this, 'destroy'),
53  array($this, 'gc')
54  );
55 
56  // Initialize the session. This calls SessionManager::read() and
57  // sets $this->userSession if a session is present.
58  session_start();
59  $sessionId = session_id();
60 
61  $ip = $request->getRemoteAddr();
62  $userAgent = $request->getUserAgent();
63  $now = time();
64 
65  // Check if the session is tied to the parent domain
66  if (isset($this->userSession) && $this->userSession->getDomain() && $this->userSession->getDomain() != $request->getServerHost(null, false)) {
67  // if current host contains . and the session domain (is a subdomain of the session domain), adjust the session's domain parameter to the parent
68  if (strtolower(substr($request->getServerHost(null, false), -1 - strlen($this->userSession->getDomain()))) == '.'.strtolower($this->userSession->getDomain())) {
69  ini_set('session.cookie_domain', $this->userSession->getDomain());
70  }
71  }
72 
73  if (!isset($this->userSession) || (Config::getVar('security', 'session_check_ip') && $this->userSession->getIpAddress() != $ip) || $this->userSession->getUserAgent() != substr($userAgent, 0, 255)) {
74  if (isset($this->userSession)) {
75  // Destroy old session
76  session_destroy();
77  }
78 
79  // Create new session
80  $this->userSession = $this->sessionDao->newDataObject();
81  $this->userSession->setId($sessionId);
82  $this->userSession->setIpAddress($ip);
83  $this->userSession->setUserAgent($userAgent);
84  $this->userSession->setSecondsCreated($now);
85  $this->userSession->setSecondsLastUsed($now);
86  $this->userSession->setDomain(ini_get('session.cookie_domain'));
87  $this->userSession->setSessionData('');
88 
89  $this->sessionDao->insertObject($this->userSession);
90 
91  } else {
92  if ($this->userSession->getRemember()) {
93  // Update session timestamp for remembered sessions so it doesn't expire in the middle of a browser session
94  if (Config::getVar('general', 'session_lifetime') > 0) {
95  $this->updateSessionLifetime(time() + Config::getVar('general', 'session_lifetime') * 86400);
96  } else {
97  $this->userSession->setRemember(0);
98  $this->updateSessionLifetime(0);
99  }
100  }
101 
102  // Update existing session's timestamp; will be saved when write is called
103  $this->userSession->setSecondsLastUsed($now);
104  }
105 
106  // Adding session_write_close as a shutdown function. This is a PHP
107  // space workaround for the "Class '...' not found" bug in installations
108  // having the APC opcode cache installed
109  // Bugzilla: https://pkp.sfu.ca/bugzilla/show_bug.cgi?id=8151
110  // PHP Bug tracker: https://bugs.php.net/bug.php?id=58739
111  register_shutdown_function('session_write_close');
112  }
113 
118  static function getManager() {
119  // Reference required
120  $instance =& Registry::get('sessionManager', true, null);
121 
122  if (is_null($instance)) {
123  $application = Registry::get('application');
124  assert(!is_null($application));
125  $request = $application->getRequest();
126  assert(!is_null($request));
127 
128  // Implicitly set session manager by ref in the registry
129  $instance = new SessionManager(DAORegistry::getDAO('SessionDAO'), $request);
130  }
131 
132  return $instance;
133  }
134 
139  function getUserSession() {
140  return $this->userSession;
141  }
142 
148  function open() {
149  return true;
150  }
151 
157  function close() {
158  return true;
159  }
160 
166  function read($sessionId) {
167  if (!isset($this->userSession)) {
168  $this->userSession = $this->sessionDao->getSession($sessionId);
169  if (isset($this->userSession)) {
170  $data = $this->userSession->getSessionData();
171  }
172  }
173  return isset($data) ? $data : '';
174  }
175 
182  function write($sessionId, $data) {
183  if (isset($this->userSession)) {
184  $this->userSession->setSessionData($data);
185  return $this->sessionDao->updateObject($this->userSession);
186 
187  } else {
188  return true;
189  }
190  }
191 
197  function destroy($sessionId) {
198  return $this->sessionDao->deleteById($sessionId);
199  }
200 
207  function gc($maxlifetime) {
208  return $this->sessionDao->deleteByLastUsed(time() - 86400, Config::getVar('general', 'session_lifetime') <= 0 ? 0 : time() - Config::getVar('general', 'session_lifetime') * 86400);
209  }
210 
217  function updateSessionCookie($sessionId = false, $expireTime = 0) {
218  $domain = ini_get('session.cookie_domain');
219  // Specific domains must contain at least one '.' (e.g. Chrome)
220  if (strpos($domain, '.') === false) $domain = false;
221 
222  // Clear cookies with no domain #8921
223  if ($domain) {
224  setcookie(session_name(), "", 0, ini_get('session.cookie_path'), false);
225  }
226 
227  return setcookie(
228  session_name(),
229  ($sessionId === false) ? session_id() : $sessionId,
230  $expireTime,
231  ini_get('session.cookie_path'),
232  $domain
233  );
234  }
235 
243  function regenerateSessionId() {
244  $success = false;
245  $currentSessionId = session_id();
246 
247  if (session_regenerate_id() && isset($this->userSession)) {
248  // Delete old session and insert new session
249  $this->sessionDao->deleteById($currentSessionId);
250  $this->userSession->setId(session_id());
251  $this->sessionDao->insertObject($this->userSession);
252  $this->updateSessionCookie(); // TODO: this might not be needed on >= 4.3.3
253  $success = true;
254  }
255 
256  return $success;
257  }
258 
264  function updateSessionLifetime($expireTime = 0) {
265  return $this->updateSessionCookie(false, $expireTime);
266  }
267 }
268 
269 
SessionManager\close
close()
Definition: SessionManager.inc.php:163
SessionManager\getManager
static getManager()
Definition: SessionManager.inc.php:124
SessionManager\$userSession
$userSession
Definition: SessionManager.inc.php:28
SessionManager\updateSessionCookie
updateSessionCookie($sessionId=false, $expireTime=0)
Definition: SessionManager.inc.php:223
$application
$application
Definition: index.php:61
SessionManager\__construct
__construct($sessionDao, $request)
Definition: SessionManager.inc.php:37
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
SessionManager\regenerateSessionId
regenerateSessionId()
Definition: SessionManager.inc.php:249
SessionManager\write
write($sessionId, $data)
Definition: SessionManager.inc.php:188
SessionManager\getUserSession
getUserSession()
Definition: SessionManager.inc.php:145
SessionManager
Implements PHP methods for a custom session storage handler (see http://php.net/session).
Definition: SessionManager.inc.php:16
SessionManager\open
open()
Definition: SessionManager.inc.php:154
SessionManager\read
read($sessionId)
Definition: SessionManager.inc.php:172
Registry\get
static & get($key, $createIfEmpty=false, $createWithDefault=null)
Definition: Registry.inc.php:35
Config\getVar
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
SessionManager\$sessionDao
$sessionDao
Definition: SessionManager.inc.php:22
SessionManager\updateSessionLifetime
updateSessionLifetime($expireTime=0)
Definition: SessionManager.inc.php:270
SessionManager\gc
gc($maxlifetime)
Definition: SessionManager.inc.php:213
SessionManager\destroy
destroy($sessionId)
Definition: SessionManager.inc.php:203