Elgg  Version 1.10
NativeSessionStorage.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Http;
3 
37 
39  protected $started = false;
40 
42  protected $closed = false;
43 
82  public function __construct(array $options = array(), \Elgg\Http\SessionHandler $handler = null) {
83  $this->setOptions($options);
84  $this->setHandler($handler);
85  }
86 
90  public function start() {
91  if ($this->started && !$this->closed) {
92  return true;
93  }
94 
95  if (!session_start()) {
96  throw new \RuntimeException('Failed to start the session');
97  }
98 
99  $this->started = true;
100  $this->closed = false;
101 
102  return true;
103  }
104 
108  public function regenerate($destroy = false, $lifetime = null) {
109  if (null !== $lifetime) {
110  ini_set('session.cookie_lifetime', $lifetime);
111  }
112 
113  return session_regenerate_id($destroy);
114  }
115 
119  public function save() {
120  session_write_close();
121 
122  $this->closed = true;
123  }
124 
128  public function isStarted() {
129  return $this->started;
130  }
131 
135  public function getId() {
136  if (!$this->started) {
137  return ''; // returning empty is consistent with session_id() behavior
138  }
139 
140  return session_id();
141  }
142 
146  public function setId($id) {
147  if ($this->started) {
148  throw new \RuntimeException('Cannot change the ID of an active session');
149  }
150 
151  session_id($id);
152  }
153 
157  public function getName() {
158  return session_name();
159  }
160 
164  public function setName($name) {
165  session_name($name);
166  }
167 
171  public function has($name) {
172  if (!$this->started) {
173  $this->start();
174  }
175 
176  return array_key_exists($name, $_SESSION);
177  }
178 
182  public function get($name, $default = null) {
183  if (!$this->started) {
184  $this->start();
185  }
186  return array_key_exists($name, $_SESSION) ? $_SESSION[$name] : $default;
187  }
188 
192  public function set($name, $value) {
193  if (!$this->started) {
194  $this->start();
195  }
196  $_SESSION[$name] = $value;
197  }
198 
202  public function all() {
203  if (!$this->started) {
204  $this->start();
205  }
206  return $_SESSION;
207  }
208 
212  public function replace(array $attributes) {
213  if (!$this->started) {
214  $this->start();
215  }
216  $_SESSION = array();
217  foreach ($attributes as $key => $value) {
218  $this->set($key, $value);
219  }
220  }
221 
225  public function remove($name) {
226  if (!$this->started) {
227  $this->start();
228  }
229  $retval = null;
230  if (array_key_exists($name, $_SESSION)) {
231  $retval = $_SESSION[$name];
232  unset($_SESSION[$name]);
233  }
234 
235  return $retval;
236  }
237 
241  public function clear() {
242  if (!$this->started) {
243  $this->start();
244  }
245  $_SESSION = array();
246  }
247 
258  protected function setOptions(array $options) {
259  $validOptions = array_flip(array(
260  'cache_limiter', 'cookie_domain', 'cookie_httponly',
261  'cookie_lifetime', 'cookie_path', 'cookie_secure',
262  'entropy_file', 'entropy_length', 'gc_divisor',
263  'gc_maxlifetime', 'gc_probability', 'hash_bits_per_character',
264  'hash_function', 'name', 'referer_check',
265  'serialize_handler', 'use_cookies',
266  'use_only_cookies', 'use_trans_sid', 'upload_progress.enabled',
267  'upload_progress.cleanup', 'upload_progress.prefix', 'upload_progress.name',
268  'upload_progress.freq', 'upload_progress.min-freq', 'url_rewriter.tags',
269  ));
270 
271  foreach ($options as $key => $value) {
272  if (isset($validOptions[$key])) {
273  ini_set('session.' . $key, $value);
274  }
275  }
276  }
277 
284  protected function setHandler($handler) {
285  session_set_save_handler(
286  array($handler, 'open'),
287  array($handler, 'close'),
288  array($handler, 'read'),
289  array($handler, 'write'),
290  array($handler, 'destroy'),
291  array($handler, 'gc'));
292  }
293 
294 }
295 
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
__construct(array $options=array(),\Elgg\Http\SessionHandler $handler=null)
Constructor.
$value
Definition: longtext.php:29
$default
Definition: checkbox.php:36
save()
{Force the session to be saved and closed.This method must invoke session_write_close() unless this i...
setId($id)
{Sets the session ID.Session string void}
$options
Definition: index.php:14
Save menu items.
$key
Definition: summary.php:34
setOptions(array $options)
Sets session.
regenerate($destroy=false, $lifetime=null)
{Regenerates id that represents this storage.This method must invoke session_regenerate_id($destroy) ...
clear()
{Clears all attributes.void}
getId()
{Returns the session ID.string The session ID or empty.}
has($name)
{Checks if an attribute is defined.The attribute nameboolean}
getName()
{Returns the session name.string The session name.}
replace(array $attributes)
{Replaces all attributes.Attributes void}
setName($name)
{Sets the session name.Session name. void}
isStarted()
{Checks if the session is started.boolean True if started, false otherwise.}
start()
{Starts the session.boolean True if started. }
$handler
Definition: add.php:10
if(!$collection_name) $id
Definition: add.php:17
$attributes
Definition: ajax_loader.php:13
all()
{Returns all attributes.array Attributes}
setHandler($handler)
Set the session handler class with PHP.