1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie;
use ICanBoogie\Accessor\AccessorTrait;
use ICanBoogie\Session\CookieParams;
use ICanBoogie\Session\Flash;
use ICanBoogie\Session\NormalizeOptions;
use ICanBoogie\Session\SegmentCollection;
use ICanBoogie\Session\SegmentTrait;
/**
* Session.
*
* @property string $id Current session id.
* @property string $name Current session name.
* @property string $cache_limiter Current cache limiter.
* @property string $cache_expire Current cache expire.
* @property array $cookie_params Session cookie parameters.
* @property string $module_name Current session module.
* @property string $save_path Current session save path.
* @property-read int $status Current session status.
* @property-read bool $is_disabled Whether sessions are enabled, but none exists.
* @property-read bool $is_active Whether sessions are enabled, and one exists.
* @property-read bool $has_none Whether sessions are enabled, but none exists.
* @property-read bool $is_referenced Whether session id is referenced in the cookie.
* @property-read SegmentCollection $segments Session segments.
* @property SessionFlash $flash The session flash.
* @property-read string $token Current session token that can be used to prevent CSRF.
*
* @method void abort() Discard session array changes and finish session.
* @method void commit() Write session data and end session.
* @method bool decode(string $data) Decodes session data from a session encoded string.
* @method void destroy() Destroys all data registered to a session.
* @method string encode() Encodes the current session data as a session encoded string.
* @method bool regenerate_id($delete_old_session = false) Update the current session id with a newly generated one.
* @method void reset() Re-initialize session array with original values.
*/
class Session implements SessionOptions, SessionSegment
{
use AccessorTrait, SegmentTrait
{
SegmentTrait::__get insteadof AccessorTrait;
}
/**
* Name of the session token, may be used as form hidden input name.
*/
const TOKEN_NAME = '__SESSION_TOKEN__';
/**
* @return string
*
* @codeCoverageIgnore
*/
protected function get_id()
{
return session_id();
}
/**
* @param string $id
*/
protected function set_id($id)
{
session_id($id);
}
/**
* @return string
*/
protected function get_name()
{
return session_name();
}
/**
* @param string $name
*/
protected function set_name($name)
{
session_name($name);
}
/**
* @return string
*/
protected function get_token()
{
$token = &$this[self::TOKEN_NAME];
if (!$token)
{
$token = $this->generate_token();
}
return $token;
}
/**
* @return string
*/
protected function get_cache_limiter()
{
return session_cache_limiter();
}
/**
* @param string $cache_limiter
*/
protected function set_cache_limiter($cache_limiter)
{
session_cache_limiter($cache_limiter);
}
/**
* @return string
*/
protected function get_cache_expire()
{
return session_cache_expire();
}
/**
* @param string $cache_expire
*/
protected function set_cache_expire($cache_expire)
{
session_cache_expire($cache_expire);
}
/**
* @return string
*/
protected function get_module_name()
{
return session_module_name();
}
/**
* @param string $module
*/
protected function set_module_name($module)
{
session_module_name($module);
}
/**
* @return string
*/
protected function get_save_path()
{
return session_save_path();
}
/**
* @param string $path
*/
protected function set_save_path($path)
{
session_save_path($path);
}
/**
* @return array
*/
protected function get_cookie_params()
{
return session_get_cookie_params();
}
/**
* @param array $params
*/
protected function set_cookie_params(array $params)
{
$lifetime = CookieParams::DEFAULT_LIFETIME;
$path = CookieParams::DEFAULT_PATH;
$domain = CookieParams::DEFAULT_DOMAIN;
$secure = CookieParams::DEFAULT_SECURE;
$httponly = CookieParams::DEFAULT_HTTP_ONLY;
extract($params, EXTR_OVERWRITE);
session_set_cookie_params($lifetime, $path, $domain, $secure, $httponly);
}
/**
* Return the current session status.
*
* @return int
*/
protected function get_status()
{
return session_status();
}
/**
* Whether sessions are enabled, and one exists.
*
* @return bool
*/
protected function get_is_active()
{
return $this->status === PHP_SESSION_ACTIVE;
}
/**
* Whether sessions are disabled.
*
* @return bool
*/
protected function get_is_disabled()
{
return $this->status === PHP_SESSION_DISABLED;
}
/**
* Whether sessions are enabled, but none exists.
*
* @return bool
*/
protected function get_has_none()
{
return $this->status === PHP_SESSION_NONE;
}
/**
* Whether sessions id is referenced in the cookie.
*
* @return bool
*/
protected function get_is_referenced()
{
return !empty($_COOKIE[$this->name]);
}
/**
* @inheritdoc
*/
protected function &get_reference()
{
$this->start_or_reuse();
return $_SESSION;
}
/**
* @var SegmentCollection
*/
private $segments;
protected function get_segments()
{
return $this->segments ?: $this->segments = new SegmentCollection($this);
}
/**
* @var SessionFlash
*/
private $flash;
/**
* @inheritdoc
*/
protected function get_flash()
{
return $this->flash ?: $this->flash = new Flash($this);
}
/**
* @param array $options
*/
public function __construct(array $options = [])
{
$normalize_options = new NormalizeOptions;
foreach ($normalize_options($options) as $option => $value)
{
$this->$option = $value;
}
}
/**
* Forward selected method to session functions.
*
* @param string $name
* @param array $arguments
*/
public function __call($name, array $arguments)
{
$this->assert_is_forwardable($name);
call_user_func_array("session_$name", $arguments);
}
/**
* Assert that a method is forwardable to a session function.
*
* @param string $name
*
* @throws \BadMethodCallException if the method is not forwardable
*/
protected function assert_is_forwardable($name)
{
if (!in_array($name, [ 'abort', 'commit', 'decode', 'destroy', 'encode', 'regenerate_id', 'reset' ]))
{
$method = get_called_class() . "::$name()";
throw new \BadMethodCallException("Unknown method: $method.");
}
}
/**
* Initialize session data.
*
* **Note:** If PHP is running from CLI the `session_start()` method is not invoked but a fake
* `$_SESSION` is still created.
*
* @return bool
*
* @see session_start()
*
* @codeCoverageIgnore
*/
public function start()
{
if (PHP_SAPI === 'cli')
{
if (isset($_SESSION))
{
return false;
}
$_SESSION = &$_SESSION;
return true;
}
$started = session_start();
$this->regenerate_id();
return $started;
}
/**
* Start a new session or reuse the current one.
*/
public function start_or_reuse()
{
if ($this->is_active)
{
return;
}
$this->start();
}
/**
* Clear the session of all data.
*
* @see session_unset()
*
* @codeCoverageIgnore
*/
public function clear()
{
if (PHP_SAPI === 'cli')
{
$_SESSION = [];
return;
}
session_unset();
}
/**
* Update the current session id and token.
*
* @return bool `true` on success or `false` on failure.
*/
public function regenerate()
{
$this[self::TOKEN_NAME] = $this->generate_token();
return $this->regenerate_id(true);
}
/**
* Generate a session token.
*
* @return string
*/
protected function generate_token()
{
return hash('sha384', openssl_random_pseudo_bytes(4096));
}
/**
* Verify that a given token matches the session's token.
*
* @param string $token
*
* @return bool
*/
public function verify_token($token)
{
return $this[self::TOKEN_NAME] === $token;
}
}