manager.php 2.97 KB
Newer Older
1
<?php namespace Laravel\Session;
2

3 4 5 6
use Laravel\Str;
use Laravel\Config;
use Laravel\Session\Drivers\Driver;
use Laravel\Session\Transporters\Transporter;
7 8

class Manager {
9 10

	/**
11
	 * The session driver instance.
12
	 *
13
	 * @var Driver
14
	 */
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
	private $driver;

	/**
	 * The session identifier transporter instance.
	 *
	 * @var Transporter
	 */
	private $transporter;

	/**
	 * The session payload instance.
	 *
	 * @var Payload
	 */
	private $payload;
30 31 32 33

	/**
	 * Create a new session manager instance.
	 *
34 35
	 * @param  Driver       $driver
	 * @param  Transporter  $transporter
36 37
	 * @return void
	 */
38
	public function __construct(Driver $driver, Transporter $transporter)
39 40 41 42 43 44 45 46
	{
		$this->driver = $driver;
		$this->transporter = $transporter;
	}

	/**
	 * Get the session payload for the request.
	 *
47
	 * @param  array    $config
48 49
	 * @return Payload
	 */
50
	public function payload($config)
51
	{
52
		$session = $this->driver->load($this->transporter->get($config));
53 54 55 56

		// If the session is expired, a new session will be generated and all of the data from
		// the previous session will be lost. The new session will be assigned a random, long
		// string ID to uniquely identify it among the application's current users.
57
		if (is_null($session) or $this->expired($session, $config))
58 59 60 61 62 63 64 65 66 67
		{
			$session = array('id' => Str::random(40), 'data' => array());
		}

		$payload = new Payload($session);

		// If a CSRF token is not present in the session, we will generate one. These tokens
		// are generated per session to protect against Cross-Site Request Forgery attacks on
		// the application. It is up to the developer to take advantage of them using the token
		// methods on the Form class and the "csrf" route filter.
68 69 70 71
		if ( ! $payload->has('csrf_token'))
		{
			$payload->put('csrf_token', Str::random(16));
		}
72 73

		return $payload;
74 75 76
	}

	/**
77 78
	 * Deteremine if the session is expired based on the last activity timestamp
	 * and the session lifetime set in the configuration file.
79
	 *
80 81
	 * @param  array  $session
	 * @param  array  $config
82 83
	 * @return bool
	 */
84
	private function expired($session, $config)
85
	{
86
		return (time() - $session['last_activity']) > ($config['lifetime'] * 60);
87 88 89 90
	}

	/**
	 * Close the session handling for the request.
Taylor Otwell committed
91
	 *
92
	 * @param  Payload  $payload
93
	 * @param  array    $config
Taylor Otwell committed
94
	 * @param  array    $flash
95 96
	 * @return void
	 */
Taylor Otwell committed
97
	public function close(Payload $payload, $config, $flash = array())
98
	{
Taylor Otwell committed
99 100 101 102 103
		foreach ($flash as $key => $value)
		{
			$this->driver->flash($key, $value);
		}

104 105 106 107 108 109 110
		$this->driver->save($payload->age(), $config);

		$this->transporter->put($payload->session['id'], $config);

		// Some session drivers implement the Sweeper interface, which specified that the driver
		// must do its garbage collection manually. Alternatively, some drivers such as APC and
		// Memcached are not required to manually clean up their sessions.
111
		if (mt_rand(1, $config['sweepage'][1]) <= $config['sweepage'][0] and $this->driver instanceof Drivers\Sweeper)
112 113 114 115 116
		{
			$this->driver->sweep(time() - ($config['lifetime'] * 60));
		}
	}

117
}