Open Journal Systems  3.3.0
Session.inc.php
1 <?php
2 
22 class Session extends DataObject {
23 
25  var $user;
26 
27 
33  function getSessionVar($key) {
34  return isset($_SESSION[$key]) ? $_SESSION[$key] : null;
35  }
36 
43  function setSessionVar($key, $value) {
44  $_SESSION[$key] = $value;
45  return $value;
46  }
47 
52  function unsetSessionVar($key) {
53  if (isset($_SESSION[$key])) {
54  unset($_SESSION[$key]);
55  }
56  }
57 
58  //
59  // Get/set methods
60  //
61 
66  function getUserId() {
67  return $this->getData('userId');
68  }
69 
74  function setUserId($userId) {
75  if (!isset($userId) || empty($userId)) {
76  $this->user = null;
77  $userId = null;
78 
79  } else if ($userId != $this->getData('userId')) {
80  $userDao = DAORegistry::getDAO('UserDAO'); /* @var $userDao UserDAO */
81  $this->user = $userDao->getById($userId);
82  if (!isset($this->user)) {
83  $userId = null;
84  }
85  }
86  $this->setData('userId', $userId);
87  }
88 
93  function getIpAddress() {
94  return $this->getData('ipAddress');
95  }
96 
101  function setIpAddress($ipAddress) {
102  $this->setData('ipAddress', $ipAddress);
103  }
104 
109  function getUserAgent() {
110  return $this->getData('userAgent');
111  }
112 
117  function setUserAgent($userAgent) {
118  $this->setData('userAgent', $userAgent);
119  }
120 
125  function getSecondsCreated() {
126  return $this->getData('created');
127  }
128 
133  function setSecondsCreated($created) {
134  $this->setData('created', $created);
135  }
136 
141  function getSecondsLastUsed() {
142  return $this->getData('lastUsed');
143  }
144 
149  function setSecondsLastUsed($lastUsed) {
150  $this->setData('lastUsed', $lastUsed);
151  }
152 
157  function getRemember() {
158  return $this->getData('remember');
159  }
160 
165  function setRemember($remember) {
166  $this->setData('remember', $remember);
167  }
168 
173  function getSessionData() {
174  return $this->getData('data');
175  }
176 
181  function setSessionData($data) {
182  $this->setData('data', $data);
183  }
184 
189  function getDomain() {
190  return $this->getData('domain');
191  }
192 
197  function setDomain($data) {
198  $this->setData('domain', $data);
199  }
200 
205  function &getUser() {
206  return $this->user;
207  }
208 
213  function getCSRFToken() {
214  $csrf = $this->getSessionVar('csrf');
215  if (!is_array($csrf) || time() > $csrf['timestamp'] + (60*60)) { // 1 hour token expiry
216  // Generate random data
217  if (function_exists('openssl_random_pseudo_bytes')) $data = openssl_random_pseudo_bytes(128);
218  elseif (function_exists('random_bytes')) $data = random_bytes(128);
219  else $data = sha1(mt_rand());
220 
221  // Hash the data
222  $token = null;
223  $salt = Config::getVar('security', 'salt');
224  $algos = hash_algos();
225  foreach (array('sha256', 'sha1', 'md5') as $algo) {
226  if (in_array($algo, $algos)) {
227  $token = hash_hmac($algo, $data, $salt);
228  }
229  }
230  if (!$token) $token = md5($data . $salt);
231 
232  $csrf = $this->setSessionVar('csrf', array(
233  'timestamp' => time(),
234  'token' => $token,
235  ));
236  } else {
237  // Extend timeout of CSRF token
238  $csrf['timestamp'] = time();
239  $this->setSessionVar('csrf', $csrf);
240  }
241  return $csrf['token'];
242  }
243 }
244 
245 
DataObject\getData
& getData($key, $locale=null)
Definition: DataObject.inc.php:100
DataObject
Any class with an associated DAO should extend this class.
Definition: DataObject.inc.php:18
Session\setIpAddress
setIpAddress($ipAddress)
Definition: Session.inc.php:101
Session\unsetSessionVar
unsetSessionVar($key)
Definition: Session.inc.php:52
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
Session\setSessionData
setSessionData($data)
Definition: Session.inc.php:181
Session\setRemember
setRemember($remember)
Definition: Session.inc.php:165
Session\$user
$user
Definition: Session.inc.php:25
Session\getCSRFToken
getCSRFToken()
Definition: Session.inc.php:213
Session\getSessionVar
getSessionVar($key)
Definition: Session.inc.php:33
Session
Maintains user state information from one request to the next.
Definition: Session.inc.php:22
Session\getUserAgent
getUserAgent()
Definition: Session.inc.php:109
Session\setUserId
setUserId($userId)
Definition: Session.inc.php:74
Session\getRemember
getRemember()
Definition: Session.inc.php:157
Session\setSecondsLastUsed
setSecondsLastUsed($lastUsed)
Definition: Session.inc.php:149
Session\getUser
& getUser()
Definition: Session.inc.php:205
Session\getDomain
getDomain()
Definition: Session.inc.php:189
Config\getVar
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
Session\getIpAddress
getIpAddress()
Definition: Session.inc.php:93
Session\getUserId
getUserId()
Definition: Session.inc.php:66
Session\setSecondsCreated
setSecondsCreated($created)
Definition: Session.inc.php:133
Session\getSecondsLastUsed
getSecondsLastUsed()
Definition: Session.inc.php:141
Session\setUserAgent
setUserAgent($userAgent)
Definition: Session.inc.php:117
Session\setSessionVar
setSessionVar($key, $value)
Definition: Session.inc.php:43
Session\getSessionData
getSessionData()
Definition: Session.inc.php:173
Session\setDomain
setDomain($data)
Definition: Session.inc.php:197
DataObject\setData
setData($key, $value, $locale=null)
Definition: DataObject.inc.php:132
Session\getSecondsCreated
getSecondsCreated()
Definition: Session.inc.php:125