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