Elgg  Version 1.10
ElggSession.php
Go to the documentation of this file.
1 <?php
2 
18 class ElggSession implements \ArrayAccess {
19 
21  protected $storage;
22 
24  protected $loggedInUser;
25 
32  public function __construct(\Elgg\Http\SessionStorage $storage) {
33  $this->storage = $storage;
34  $this->loggedInUser = null;
35  }
36 
44  public function start() {
45  $result = $this->storage->start();
46  $this->generateSessionToken();
47  return $result;
48  }
49 
57  public function migrate($destroy = false) {
58  return $this->storage->regenerate($destroy);
59  }
60 
69  public function invalidate() {
70  $this->storage->clear();
71  $this->loggedInUser = null;
72  $result = $this->migrate(true);
73  $this->generateSessionToken();
74  return $result;
75  }
76 
83  public function isStarted() {
84  return $this->storage->isStarted();
85  }
86 
93  public function getId() {
94  return $this->storage->getId();
95  }
96 
104  public function setId($id) {
105  $this->storage->setId($id);
106  }
107 
114  public function getName() {
115  return $this->storage->getName();
116  }
117 
125  public function setName($name) {
126  $this->storage->setName($name);
127  }
128 
136  public function get($name, $default = null) {
137  return $this->storage->get($name, $default);
138  }
139 
147  public function set($name, $value) {
148  $this->storage->set($name, $value);
149  }
150 
158  public function remove($name) {
159  return $this->storage->remove($name);
160  }
161 
169  public function del($key) {
170  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
171  $this->remove($key);
172  }
173 
181  public function has($name) {
182  return $this->storage->has($name);
183  }
184 
192  public function setLoggedInUser(\ElggUser $user) {
193  $this->set('guid', $user->guid);
194  $this->loggedInUser = $user;
195  }
196 
203  public function getLoggedInUser() {
204  return $this->loggedInUser;
205  }
206 
213  public function getLoggedInUserGuid() {
214  $user = $this->getLoggedInUser();
215  if ($user) {
216  return $user->guid;
217  }
218 
219  return 0;
220  }
221 
227  public function isAdminLoggedIn() {
228  $user = $this->getLoggedInUser();
229 
230  return $user && $user->isAdmin();
231  }
232 
238  public function isLoggedIn() {
239  return (bool)$this->getLoggedInUser();
240  }
241 
248  public function removeLoggedInUser() {
249  $this->loggedInUser = null;
250  $this->remove('guid');
251  }
252 
260  protected function generateSessionToken() {
261  // Generate a simple token that we store server side
262  if (!$this->has('__elgg_session')) {
263  $this->set('__elgg_session', md5(microtime() . rand()));
264  }
265  }
266 
275  public function __isset($key) {
276  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
277  // Note: We use offsetExists() for BC
278  return $this->offsetExists($key);
279  }
280 
290  public function offsetSet($key, $value) {
291  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
292  $this->set($key, $value);
293  }
294 
306  public function offsetGet($key) {
307  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
308 
309  if (in_array($key, array('user', 'id', 'name', 'username'))) {
310  elgg_deprecated_notice("Only 'guid' is stored in session for user now", 1.9);
311  if ($this->loggedInUser) {
312  switch ($key) {
313  case 'user':
314  return $this->loggedInUser;
315  break;
316  case 'id':
317  return $this->loggedInUser->guid;
318  break;
319  case 'name':
320  case 'username':
321  return $this->loggedInUser->$key;
322  break;
323  }
324  } else {
325  return null;
326  }
327  }
328 
329  if ($this->has($key)) {
330  return $this->get($key);
331  }
332 
333  $orig_value = null;
334  $value = _elgg_services()->hooks->trigger('session:get', $key, null, $orig_value);
335  if ($orig_value !== $value) {
336  elgg_deprecated_notice("Plugin hook session:get has been deprecated.", 1.9);
337  }
338 
339  $this->set($key, $value);
340  return $value;
341  }
342 
353  public function offsetUnset($key) {
354  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
355  $this->remove($key);
356  }
357 
368  public function offsetExists($offset) {
369  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
370 
371  if (in_array($offset, array('user', 'id', 'name', 'username'))) {
372  elgg_deprecated_notice("Only 'guid' is stored in session for user now", 1.9);
373  return (bool)$this->loggedInUser;
374  }
375 
376  if ($this->has($offset)) {
377  return true;
378  }
379 
380  // Note: We use offsetGet() for BC
381  if ($this->offsetGet($offset)) {
382  return true;
383  }
384 
385  return false;
386  }
387 }
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.
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:57
if(elgg_in_context('widget')) $offset
Definition: pagination.php:20
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:29
invalidate()
Invalidates the session.
Definition: ElggSession.php:69
if(!$autoload_available) _elgg_services()
Definition: autoloader.php:20
$default
Definition: checkbox.php:36
isStarted()
Has the session been started.
Definition: ElggSession.php:83
getId()
Get the session ID.
Definition: ElggSession.php:93
start()
Start the session.
Definition: ElggSession.php:44
getLoggedInUserGuid()
Return the current logged in user by guid.
del($key)
Alias to offsetUnset()
Save menu items.
$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)
Sends a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1055
__isset($key)
Test if property is set either as an attribute or metadata.
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.
__construct(\Elgg\Http\SessionStorage $storage)
Constructor.
Definition: ElggSession.php:32