| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 | 
							- <?php namespace System;
 
- class Session {
 
- 	/**
 
- 	 * The active session driver.
 
- 	 *
 
- 	 * @var Session\Driver
 
- 	 */
 
- 	private static $driver;
 
- 	/**
 
- 	 * The session.
 
- 	 *
 
- 	 * @var array
 
- 	 */
 
- 	private static $session = array();
 
- 	/**
 
- 	 * Get the session driver.
 
- 	 *
 
- 	 * @return Session\Driver
 
- 	 */
 
- 	public static function driver()
 
- 	{
 
- 		if (is_null(static::$driver))
 
- 		{
 
- 			static::$driver = Session\Factory::make(Config::get('session.driver'));
 
- 		}
 
- 		return static::$driver;
 
- 	}
 
- 	/**
 
- 	 * Load the session for the user.
 
- 	 *
 
- 	 * @return void
 
- 	 */
 
- 	public static function load()
 
- 	{
 
- 		if ( ! is_null($id = Cookie::get('laravel_session')))
 
- 		{
 
- 			static::$session = static::driver()->load($id);
 
- 		}
 
- 		// ---------------------------------------------------------
 
- 		// If the session is invalid or expired, start a new one.
 
- 		// ---------------------------------------------------------
 
- 		if (is_null($id) or is_null(static::$session) or static::expired(static::$session['last_activity']))
 
- 		{
 
- 			static::$session['id'] = Str::random(40);
 
- 			static::$session['data'] = array();
 
- 		}
 
- 		// ---------------------------------------------------------
 
- 		// Create a CSRF token for the session if necessary. This
 
- 		// token is used by the Form class and filters to protect
 
- 		// against cross-site request forgeries.
 
- 		// ---------------------------------------------------------
 
- 		if ( ! static::has('csrf_token'))
 
- 		{
 
- 			static::put('csrf_token', Str::random(16));
 
- 		}
 
- 		static::$session['last_activity'] = time();
 
- 	}
 
- 	/**
 
- 	 * Determine if a session has expired based on the last activity.
 
- 	 *
 
- 	 * @param  int  $last_activity
 
- 	 * @return bool
 
- 	 */
 
- 	private static function expired($last_activity)
 
- 	{
 
- 		return (time() - $last_activity) > (Config::get('session.lifetime') * 60);
 
- 	}
 
- 	/**
 
- 	 * Determine if the session or flash data contains an item.
 
- 	 *
 
- 	 * @param  string  $key
 
- 	 * @return bool
 
- 	 */
 
- 	public static function has($key)
 
- 	{
 
- 		return (array_key_exists($key, static::$session['data']) or
 
- 			    array_key_exists(':old:'.$key, static::$session['data']) or
 
- 			    array_key_exists(':new:'.$key, static::$session['data']));
 
- 	}
 
- 	/**
 
- 	 * Get an item from the session or flash data.
 
- 	 *
 
- 	 * @param  string  $key
 
- 	 * @return mixed
 
- 	 */
 
- 	public static function get($key, $default = null)
 
- 	{
 
- 		if (array_key_exists($key, static::$session['data']))
 
- 		{
 
- 			return static::$session['data'][$key];
 
- 		}
 
- 		elseif (array_key_exists(':old:'.$key, static::$session['data']))
 
- 		{
 
- 			return static::$session['data'][':old:'.$key];
 
- 		}
 
- 		elseif (array_key_exists(':new:'.$key, static::$session['data']))
 
- 		{
 
- 			return static::$session['data'][':new:'.$key];
 
- 		}
 
- 		return $default;
 
- 	}
 
- 	/**
 
- 	 * 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.
 
- 	 *
 
- 	 * @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()
 
- 	{
 
- 		// ---------------------------------------------------------
 
- 		// When regenerating the session ID, we go ahead and delete
 
- 		// the session data from storage. Then, we assign a new ID.
 
- 		//
 
- 		// The session will be re-written to storage at the end
 
- 		// of the request to the application.
 
- 		// ---------------------------------------------------------
 
- 		static::driver()->delete(static::$session['id']);
 
- 		static::$session['id'] = Str::random(40);
 
- 	}
 
- 	/**
 
- 	 * Close the session.
 
- 	 *
 
- 	 * @return void
 
- 	 */
 
- 	public static function close()
 
- 	{
 
- 		// ---------------------------------------------------------
 
- 		// Flash the old input data to the session. This allows
 
- 		// the Input::old method to retrieve input from the
 
- 		// previous request made by the user.
 
- 		// ---------------------------------------------------------
 
- 		static::flash('laravel_old_input', Input::get());
 
- 		static::age_flash();
 
- 		static::driver()->save(static::$session);
 
- 		// ---------------------------------------------------------
 
- 		// Send the session cookie the browser so we can remember
 
- 		// who the session belongs to on subsequent requests.
 
- 		// ---------------------------------------------------------
 
- 		if ( ! headers_sent())
 
- 		{
 
- 			$cookie = new Cookie('laravel_session', static::$session['id']);
 
- 			$cookie->lifetime = (Config::get('session.expire_on_close')) ? 0 : Config::get('session.lifetime');
 
- 			$cookie->path = Config::get('session.path');
 
- 			$cookie->domain = Config::get('session.domain');
 
- 			$cookie->secure = Config::get('session.https');
 
- 			$cookie->send();
 
- 		}
 
- 		// ---------------------------------------------------------
 
- 		// Perform session garbage collection (2% chance).
 
- 		// Session garbage collection removes all expired sessions.
 
- 		// ---------------------------------------------------------
 
- 		if (mt_rand(1, 100) <= 2)
 
- 		{
 
- 			static::driver()->sweep(time() - (Config::get('session.lifetime') * 60));
 
- 		}
 
- 	}
 
- 	/**
 
- 	 * Age the session flash data.
 
- 	 *
 
- 	 * @return void
 
- 	 */
 
- 	private static function age_flash()
 
- 	{
 
- 		// -----------------------------------------------------
 
- 		// Remove all of the :old: items from the session.
 
- 		// -----------------------------------------------------
 
- 		foreach (static::$session['data'] as $key => $value)
 
- 		{
 
- 			if (strpos($key, ':old:') === 0)
 
- 			{
 
- 				static::forget($key);
 
- 			}
 
- 		}
 
- 		// -----------------------------------------------------
 
- 		// Copy all of the :new: items to :old: items and then
 
- 		// remove the :new: items from the session.
 
- 		// -----------------------------------------------------
 
- 		foreach (static::$session['data'] as $key => $value)
 
- 		{
 
- 			if (strpos($key, ':new:') === 0)
 
- 			{
 
- 				static::put(':old:'.substr($key, 5), $value);
 
- 				static::forget($key);
 
- 			}
 
- 		}
 
- 	}
 
- }
 
 
  |