Elgg  Version 1.9
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 
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 removeLoggedInUser() {
214  $this->loggedInUser = null;
215  $this->remove('guid');
216  }
217 
225  protected function generateSessionToken() {
226  // Generate a simple token that we store server side
227  if (!$this->has('__elgg_session')) {
228  $this->set('__elgg_session', md5(microtime() . rand()));
229  }
230  }
231 
240  public function __isset($key) {
241  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
242  // Note: We use offsetExists() for BC
243  return $this->offsetExists($key);
244  }
245 
255  public function offsetSet($key, $value) {
256  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
257  $this->set($key, $value);
258  }
259 
271  public function offsetGet($key) {
272  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
273 
274  if (in_array($key, array('user', 'id', 'name', 'username'))) {
275  elgg_deprecated_notice("Only 'guid' is stored in session for user now", 1.9);
276  if ($this->loggedInUser) {
277  switch ($key) {
278  case 'user':
279  return $this->loggedInUser;
280  break;
281  case 'id':
282  return $this->loggedInUser->guid;
283  break;
284  case 'name':
285  case 'username':
286  return $this->loggedInUser->$key;
287  break;
288  }
289  } else {
290  return null;
291  }
292  }
293 
294  if ($this->has($key)) {
295  return $this->get($key);
296  }
297 
298  $orig_value = null;
299  $value = elgg_trigger_plugin_hook('session:get', $key, null, $orig_value);
300  if ($orig_value !== $value) {
301  elgg_deprecated_notice("Plugin hook session:get has been deprecated.", 1.9);
302  }
303 
304  $this->set($key, $value);
305  return $value;
306  }
307 
318  public function offsetUnset($key) {
319  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
320  $this->remove($key);
321  }
322 
333  public function offsetExists($offset) {
334  elgg_deprecated_notice(__METHOD__ . " has been deprecated.", 1.9);
335 
336  if (in_array($offset, array('user', 'id', 'name', 'username'))) {
337  elgg_deprecated_notice("Only 'guid' is stored in session for user now", 1.9);
338  return (bool)$this->loggedInUser;
339  }
340 
341  if ($this->has($offset)) {
342  return true;
343  }
344 
345  // Note: We use offsetGet() for BC
346  if ($this->offsetGet($offset)) {
347  return true;
348  }
349 
350  return false;
351  }
352 }
setId($id)
Set the session ID.
setName($name)
Set the session name.
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.
removeLoggedInUser()
Remove the logged in user.
$value
Definition: longtext.php:29
invalidate()
Invalidates the session.
Definition: ElggSession.php:69
$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
del($key)
Alias to offsetUnset()
$key
Definition: summary.php:34
getLoggedInUser()
Gets the logged in 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...
setLoggedInUser(ElggUser $user)
Sets the logged in user.
$user
Definition: ban.php:13
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Trigger a Plugin Hook and run all handler callbacks registered to that hook:type. ...
Definition: elgglib.php:925
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Sends a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1171
__isset($key)
Test if property is set either as an attribute or metadata.
__construct(Elgg_Http_SessionStorage $storage)
Constructor.
Definition: ElggSession.php:32
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.