Elgg  Version 1.11
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  $this->set('guid', $user->guid);
206  $this->logged_in_user = $user;
207  }
208 
215  public function getLoggedInUser() {
216  return $this->logged_in_user;
217  }
218 
225  public function getLoggedInUserGuid() {
226  $user = $this->getLoggedInUser();
227  return $user ? $user->guid : 0;
228  }
229 
235  public function isAdminLoggedIn() {
236  $user = $this->getLoggedInUser();
237 
238  return $user && $user->isAdmin();
239  }
240 
246  public function isLoggedIn() {
247  return (bool)$this->getLoggedInUser();
248  }
249 
256  public function removeLoggedInUser() {
257  $this->logged_in_user = null;
258  $this->remove('guid');
259  }
260 
266  public function getIgnoreAccess() {
267  return $this->ignore_access;
268  }
269 
277  public function setIgnoreAccess($ignore = true) {
278  _elgg_services()->accessCache->clear();
279 
280  $prev = $this->ignore_access;
281  $this->ignore_access = $ignore;
282 
283  return $prev;
284  }
285 
286  // @codingStandardsIgnoreStart
295  public function get_ignore_access() {
296  return $this->getIgnoreAccess();
297  }
298  // @codingStandardsIgnoreEnd
299 
300  // @codingStandardsIgnoreStart
312  public function set_ignore_access($ignore = true) {
313  return $this->setIgnoreAccess($ignore);
314  }
315  // @codingStandardsIgnoreEnd
316 
325  protected function generateSessionToken() {
326  // Generate a simple token that we store server side
327  if (!$this->has('__elgg_session')) {
328  $this->set('__elgg_session', _elgg_services()->crypto->getRandomString(22));
329  }
330  }
331 
340  public function __isset($key) {
341  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
342  // Note: We use offsetExists() for BC
343  return $this->offsetExists($key);
344  }
345 
355  public function offsetSet($key, $value) {
356  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
357  $this->set($key, $value);
358  }
359 
371  public function offsetGet($key) {
372  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
373 
374  if (in_array($key, array('user', 'id', 'name', 'username'))) {
375  elgg_deprecated_notice("Only 'guid' is stored in session for user now", 1.9);
376  if ($this->logged_in_user) {
377  switch ($key) {
378  case 'user':
379  return $this->logged_in_user;
380  break;
381  case 'id':
382  return $this->logged_in_user->guid;
383  break;
384  case 'name':
385  case 'username':
386  return $this->logged_in_user->$key;
387  break;
388  }
389  } else {
390  return null;
391  }
392  }
393 
394  if ($this->has($key)) {
395  return $this->get($key);
396  }
397 
398  $orig_value = null;
399  $value = _elgg_services()->hooks->trigger('session:get', $key, null, $orig_value);
400  if ($orig_value !== $value) {
401  elgg_deprecated_notice("Plugin hook session:get has been deprecated.", 1.9);
402  }
403 
404  $this->set($key, $value);
405  return $value;
406  }
407 
418  public function offsetUnset($key) {
419  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
420  $this->remove($key);
421  }
422 
433  public function offsetExists($offset) {
434  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
435 
436  if (in_array($offset, array('user', 'id', 'name', 'username'))) {
437  elgg_deprecated_notice("Only 'guid' is stored in session for user now", 1.9);
438  return (bool)$this->logged_in_user;
439  }
440 
441  if ($this->has($offset)) {
442  return true;
443  }
444 
445  // Note: We use offsetGet() for BC
446  if ($this->offsetGet($offset)) {
447  return true;
448  }
449 
450  return false;
451  }
452 
458  public static function getMock() {
459  $storage = new MockArraySessionStorage();
460  $session = new Session($storage);
461  return new self($session);
462  }
463 }
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:26
invalidate()
Invalidates the session.
Definition: ElggSession.php:81
if(!$count) $offset
Definition: pagination.php:25
$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.
_elgg_services()
Definition: autoloader.php:14
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:1006
__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
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.