Files
spa-api/system/session.php

232 lines
4.5 KiB
PHP
Raw Normal View History

2011-06-08 23:45:08 -05:00
<?php namespace System;
class Session {
/**
* The active session driver.
*
* @var Session\Driver
*/
public static $driver;
2011-06-08 23:45:08 -05:00
/**
* The session payload, which contains the session ID, data and last activity timestamp.
2011-06-08 23:45:08 -05:00
*
* @var array
*/
public static $session = array();
2011-06-08 23:45:08 -05:00
/**
* Get the session driver.
2011-06-08 23:45:08 -05:00
*
* @return Session\Driver
*/
public static function driver()
{
if (is_null(static::$driver))
{
switch (Config::get('session.driver'))
{
case 'cookie':
2011-08-06 20:49:53 -05:00
return static::$driver = new Session\Cookie;
case 'file':
2011-08-06 20:49:53 -05:00
return static::$driver = new Session\File;
case 'db':
2011-08-06 20:49:53 -05:00
return static::$driver = new Session\DB;
case 'memcached':
2011-08-06 20:49:53 -05:00
return static::$driver = new Session\Memcached;
case 'apc':
2011-08-06 20:49:53 -05:00
return static::$driver = new Session\APC;
default:
throw new \Exception("Session driver [$driver] is not supported.");
}
2011-06-08 23:45:08 -05:00
}
2011-08-08 09:58:44 -05:00
return static::$driver;
2011-06-08 23:45:08 -05:00
}
/**
2011-07-11 07:05:46 -07:00
* Load a user session by ID.
2011-06-08 23:45:08 -05:00
*
2011-07-11 07:05:46 -07:00
* @param string $id
2011-06-08 23:45:08 -05:00
* @return void
*/
2011-07-11 07:05:46 -07:00
public static function load($id)
2011-06-08 23:45:08 -05:00
{
2011-07-11 07:05:46 -07:00
static::$session = ( ! is_null($id)) ? static::driver()->load($id) : null;
2011-06-08 23:45:08 -05:00
if (static::invalid(static::$session)) static::$session = array('id' => Str::random(40), 'data' => array());
2011-06-08 23:45:08 -05:00
if ( ! static::has('csrf_token')) static::put('csrf_token', Str::random(16));
2011-06-08 23:45:08 -05:00
static::$session['last_activity'] = time();
}
/**
* Determine if a session is valid.
*
* A session is considered valid if it exists and has not expired.
*
* @param array $session
* @return bool
*/
private static function invalid($session)
{
return is_null($session) or (time() - $session['last_activity']) > (Config::get('session.lifetime') * 60);
}
2011-06-08 23:45:08 -05:00
/**
2011-06-16 22:45:33 -05:00
* Determine if the session or flash data contains an item.
2011-06-08 23:45:08 -05:00
*
2011-06-16 22:45:33 -05:00
* @param string $key
2011-06-08 23:45:08 -05:00
* @return bool
*/
2011-06-16 22:45:33 -05:00
public static function has($key)
2011-06-08 23:45:08 -05:00
{
return ( ! is_null(static::get($key)));
2011-06-08 23:45:08 -05:00
}
/**
* Get an item from the session or flash data.
2011-06-08 23:45:08 -05:00
*
* @param string $key
* @return mixed
*/
public static function get($key, $default = null)
{
foreach (array($key, ':old:'.$key, ':new:'.$key) as $possibility)
{
if (array_key_exists($possibility, static::$session['data'])) return static::$session['data'][$possibility];
2011-06-08 23:45:08 -05:00
}
return is_callable($default) ? call_user_func($default) : $default;
2011-06-08 23:45:08 -05:00
}
/**
* Write an item to the session.
*
* @param string $key
* @param mixed $value
* @return void
*/
public static function put($key, $value)
{
static::$session['data'][$key] = $value;
}
/**
* Write an item to the session flash data.
2011-06-08 23:45:08 -05:00
*
* @param string $key
* @param mixed $value
* @return void
*/
public static function flash($key, $value)
{
static::put(':new:'.$key, $value);
}
/**
* Remove an item from the session.
*
* @param string $key
* @return void
*/
public static function forget($key)
{
unset(static::$session['data'][$key]);
}
/**
* Remove all items from the session.
*
* @return void
*/
public static function flush()
{
static::$session['data'] = array();
}
/**
* Regenerate the session ID.
*
* @return void
*/
public static function regenerate()
{
static::driver()->delete(static::$session['id']);
2011-06-08 23:45:08 -05:00
static::$session['id'] = Str::random(40);
}
/**
* Close the session.
*
* The session will be stored in persistant storage and the session cookie will be
* session cookie will be sent to the browser. The old input data will also be
* stored in the session flash data.
*
2011-06-08 23:45:08 -05:00
* @return void
*/
public static function close()
{
static::flash('laravel_old_input', Input::get());
2011-06-14 17:27:11 -05:00
2011-06-08 23:45:08 -05:00
static::age_flash();
static::driver()->save(static::$session);
2011-08-08 09:58:44 -05:00
static::write_cookie();
2011-06-08 23:45:08 -05:00
if (mt_rand(1, 100) <= 2 and static::driver() instanceof Session\Sweeper)
2011-06-08 23:45:08 -05:00
{
2011-08-08 09:58:44 -05:00
static::driver()->sweep(time() - (Config::get('session.lifetime') * 60));
2011-06-08 23:45:08 -05:00
}
}
/**
* Age the session flash data.
*
* @return void
*/
private static function age_flash()
{
foreach (static::$session['data'] as $key => $value)
{
if (strpos($key, ':old:') === 0) static::forget($key);
2011-06-08 23:45:08 -05:00
}
foreach (static::$session['data'] as $key => $value)
{
if (strpos($key, ':new:') === 0)
{
static::put(':old:'.substr($key, 5), $value);
static::forget($key);
}
}
}
2011-08-08 09:58:44 -05:00
/**
* Write the session cookie.
*
* @return void
*/
private static function write_cookie()
{
if ( ! headers_sent())
{
2011-08-08 09:59:50 -05:00
extract(Config::get('session'));
2011-08-08 09:58:44 -05:00
$minutes = ($expire_on_close) ? 0 : $lifetime;
Cookie::put('laravel_session', static::$session['id'], $minutes, $path, $domain, $https, $http_only);
}
}
2011-06-08 23:45:08 -05:00
}