session.php 6.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php namespace System;

class Session {

	/**
	 * The active session driver.
	 *
	 * @var Session\Driver
	 */
	private static $driver;

	/**
	 * The session.
	 *
	 * @var array
	 */
	private static $session = array();

	/**
20
	 * Get the session driver.
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
	 *
	 * @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);
		}

46 47 48 49
		// ---------------------------------------------------------
		// 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']))
50 51 52 53 54
		{
			static::$session['id'] = Str::random(40);
			static::$session['data'] = array();
		}

55 56 57 58 59
		// ---------------------------------------------------------
		// 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.
		// ---------------------------------------------------------
60 61 62 63 64 65 66 67 68
		if ( ! static::has('csrf_token'))
		{
			static::put('csrf_token', Str::random(16));
		}

		static::$session['last_activity'] = time();
	}

	/**
69 70 71 72 73 74 75 76 77 78 79
	 * 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);
	}

	/**
Taylor Otwell committed
80
	 * Determine if the session or flash data contains an item.
81
	 *
Taylor Otwell committed
82
	 * @param  string  $key
83 84
	 * @return bool
	 */
Taylor Otwell committed
85
	public static function has($key)
86
	{
Taylor Otwell committed
87 88 89
		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']));
90 91 92
	}

	/**
93
	 * Get an item from the session or flash data.
94 95 96 97 98 99
	 *
	 * @param  string  $key
	 * @return mixed
	 */
	public static function get($key, $default = null)
	{
100
		if (array_key_exists($key, static::$session['data']))
101
		{
102 103 104 105 106 107 108 109 110
			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];
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
		}

		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;
	}

	/**
129
	 * Write an item to the session flash data.
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
	 *
	 * @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()
	{
168 169 170 171 172 173 174
		// ---------------------------------------------------------
		// 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.
		// ---------------------------------------------------------
175
		static::driver()->delete(static::$session['id']);
176

177 178 179 180 181 182 183 184 185 186
		static::$session['id'] = Str::random(40);
	}

	/**
	 * Close the session.
	 *
	 * @return void
	 */
	public static function close()
	{
187 188 189 190 191
		// ---------------------------------------------------------
		// 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.
		// ---------------------------------------------------------
192
		static::flash('laravel_old_input', Input::get());
193

194 195 196 197
		static::age_flash();

		static::driver()->save(static::$session);

198 199 200 201
		// ---------------------------------------------------------
		// Send the session cookie the browser so we can remember
		// who the session belongs to on subsequent requests.
		// ---------------------------------------------------------
202 203
		if ( ! headers_sent())
		{
204 205
			$cookie = new Cookie('laravel_session', static::$session['id']);

206
			$cookie->lifetime = (Config::get('session.expire_on_close')) ? 0 : Config::get('session.lifetime');
207 208 209 210 211
			$cookie->path = Config::get('session.path');
			$cookie->domain = Config::get('session.domain');
			$cookie->secure = Config::get('session.https');

			$cookie->send();
212 213
		}

214
		// ---------------------------------------------------------
215
		// Perform session garbage collection (2% chance).
216 217
		// Session garbage collection removes all expired sessions.
		// ---------------------------------------------------------
218 219
		if (mt_rand(1, 100) <= 2)
		{
220
			static::driver()->sweep(time() - (Config::get('session.lifetime') * 60));
221 222 223 224 225 226 227 228 229 230 231
		}
	}

	/**
	 * Age the session flash data.
	 *
	 * @return void
	 */
	private static function age_flash()
	{
		// -----------------------------------------------------
232
		// Remove all of the :old: items from the session.
233 234 235 236 237 238 239 240 241 242
		// -----------------------------------------------------
		foreach (static::$session['data'] as $key => $value)
		{
			if (strpos($key, ':old:') === 0)
			{
				static::forget($key);
			}
		}

		// -----------------------------------------------------
243 244
		// Copy all of the :new: items to :old: items and then
		// remove the :new: items from the session.
245 246 247 248 249 250 251 252 253 254 255 256 257
		// -----------------------------------------------------
		foreach (static::$session['data'] as $key => $value)
		{
			if (strpos($key, ':new:') === 0)
			{
				static::put(':old:'.substr($key, 5), $value);

				static::forget($key);
			}
		}
	}

}