Elgg  Version 2.3
ElggSession.php
Go to the documentation of this file.
1 <?php
2 
6 
22 class ElggSession implements \ArrayAccess {
23 
27  protected $storage;
28 
32  protected $logged_in_user;
33 
37  protected $ignore_access = false;
38 
45  public function __construct(SessionInterface $storage) {
46  $this->storage = $storage;
47  }
48 
56  public function start() {
57  $result = $this->storage->start();
58  $this->generateSessionToken();
59  return $result;
60  }
61 
69  public function migrate($destroy = false) {
70  return $this->storage->migrate($destroy);
71  }
72 
81  public function invalidate() {
82  $this->storage->clear();
83  $this->logged_in_user = null;
84  $result = $this->migrate(true);
85  $this->generateSessionToken();
86  return $result;
87  }
88 
95  public function isStarted() {
96  return $this->storage->isStarted();
97  }
98 
105  public function getId() {
106  return $this->storage->getId();
107  }
108 
116  public function setId($id) {
117  $this->storage->setId($id);
118  }
119 
126  public function getName() {
127  return $this->storage->getName();
128  }
129 
137  public function setName($name) {
138  $this->storage->setName($name);
139  }
140 
148  public function get($name, $default = null) {
149  return $this->storage->get($name, $default);
150  }
151 
159  public function set($name, $value) {
160  $this->storage->set($name, $value);
161  }
162 
170  public function remove($name) {
171  return $this->storage->remove($name);
172  }
173 
181  public function del($key) {
182  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
183  $this->remove($key);
184  }
185 
193  public function has($name) {
194  return $this->storage->has($name);
195  }
196 
204  public function setLoggedInUser(\ElggUser $user) {
205  $current_user = $this->getLoggedInUser();
206  if ($current_user != $user) {
207  $this->set('guid', $user->guid);
208  $this->logged_in_user = $user;
209  _elgg_services()->entityCache->clear();
210  }
211  }
212 
219  public function getLoggedInUser() {
220  return $this->logged_in_user;
221  }
222 
229  public function getLoggedInUserGuid() {
230  $user = $this->getLoggedInUser();
231  return $user ? $user->guid : 0;
232  }
233 
239  public function isAdminLoggedIn() {
240  $user = $this->getLoggedInUser();
241 
242  return $user && $user->isAdmin();
243  }
244 
250  public function isLoggedIn() {
251  return (bool)$this->getLoggedInUser();
252  }
253 
260  public function removeLoggedInUser() {
261  $this->logged_in_user = null;
262  $this->remove('guid');
263  _elgg_services()->entityCache->clear();
264  }
265 
271  public function getIgnoreAccess() {
272  return $this->ignore_access;
273  }
274 
282  public function setIgnoreAccess($ignore = true) {
283  _elgg_services()->accessCache->clear();
284 
285  $prev = $this->ignore_access;
286  $this->ignore_access = $ignore;
287 
288  return $prev;
289  }
290 
291  // @codingStandardsIgnoreStart
300  public function get_ignore_access() {
301  return $this->getIgnoreAccess();
302  }
303  // @codingStandardsIgnoreEnd
304 
305  // @codingStandardsIgnoreStart
317  public function set_ignore_access($ignore = true) {
318  return $this->setIgnoreAccess($ignore);
319  }
320  // @codingStandardsIgnoreEnd
321 
330  protected function generateSessionToken() {
331  // Generate a simple token that we store server side
332  if (!$this->has('__elgg_session')) {
333  $this->set('__elgg_session', _elgg_services()->crypto->getRandomString(22));
334  }
335  }
336 
345  public function __isset($key) {
346  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
347  // Note: We use offsetExists() for BC
348  return $this->offsetExists($key);
349  }
350 
360  public function offsetSet($key, $value) {
361  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
362  $this->set($key, $value);
363  }
364 
376  public function offsetGet($key) {
377  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
378 
379  if (in_array($key, array('user', 'id', 'name', 'username'))) {
380  elgg_deprecated_notice("Only 'guid' is stored in session for user now", 1.9);
381  if ($this->logged_in_user) {
382  switch ($key) {
383  case 'user':
384  return $this->logged_in_user;
385  break;
386  case 'id':
387  return $this->logged_in_user->guid;
388  break;
389  case 'name':
390  case 'username':
391  return $this->logged_in_user->$key;
392  break;
393  }
394  } else {
395  return null;
396  }
397  }
398 
399  if ($this->has($key)) {
400  return $this->get($key);
401  }
402 
403  $orig_value = null;
404  $value = _elgg_services()->hooks->trigger('session:get', $key, null, $orig_value);
405  if ($orig_value !== $value) {
406  elgg_deprecated_notice("Plugin hook session:get has been deprecated.", 1.9);
407  }
408 
409  $this->set($key, $value);
410  return $value;
411  }
412 
423  public function offsetUnset($key) {
424  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
425  $this->remove($key);
426  }
427 
438  public function offsetExists($offset) {
439  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
440 
441  if (in_array($offset, array('user', 'id', 'name', 'username'))) {
442  elgg_deprecated_notice("Only 'guid' is stored in session for user now", 1.9);
443  return (bool)$this->logged_in_user;
444  }
445 
446  if ($this->has($offset)) {
447  return true;
448  }
449 
450  // Note: We use offsetGet() for BC
451  if ($this->offsetGet($offset)) {
452  return true;
453  }
454 
455  return false;
456  }
457 
463  public static function getMock() {
464  $storage = new MockArraySessionStorage();
465  $session = new Session($storage);
466  return new self($session);
467  }
468 }
setId($id)
Set the session ID.
setName($name)
Set the session name.
setLoggedInUser(\ElggUser $user)
Sets the logged in user.
has($name)
Has the attribute been defined.
set_ignore_access($ignore=true)
Alias of setIgnoreAccess()
get_ignore_access()
Alias of getIgnoreAccess()
offsetUnset($key)
Unset a value from the cache and the session.
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
generateSessionToken()
Adds a token to the session.
migrate($destroy=false)
Migrates the session to a new session id while maintaining session attributes.
Definition: ElggSession.php:69
getName()
Get the session name.
isLoggedIn()
Returns whether or not the user is currently logged in.
removeLoggedInUser()
Remove the logged in user.
$value
Definition: longtext.php:42
invalidate()
Invalidates the session.
Definition: ElggSession.php:81
if(!$count) $offset
Definition: pagination.php:26
$default
Definition: checkbox.php:34
isStarted()
Has the session been started.
Definition: ElggSession.php:95
getId()
Get the session ID.
start()
Start the session.
Definition: ElggSession.php:56
getLoggedInUserGuid()
Return the current logged in user by guid.
del($key)
Alias to offsetUnset()
getIgnoreAccess()
Get current ignore access setting.
$key
Definition: summary.php:34
getLoggedInUser()
Gets the logged in user.
isAdminLoggedIn()
Returns whether or not the viewer is currently logged in and an admin user.
offsetGet($key)
Get a variable from either the session, or if its not in the session attempt to get it from an api ca...
$user
Definition: ban.php:13
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1098
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
__construct(SessionInterface $storage)
Constructor.
Definition: ElggSession.php:45
setIgnoreAccess($ignore=true)
Set ignore access.
static getMock()
Get an isolated ElggSession that does not persist between requests.
__isset($key)
Test if property is set either as an attribute or metadata.
$session
Definition: login.php:9
if(!$collection_name) $id
Definition: add.php:17
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5
offsetExists($offset)
Return whether the value is set in either the session or the cache.
offsetSet($key, $value)
Set a value, go straight to session.