manager.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php namespace Laravel\Session;
  2. use Closure;
  3. use Laravel\Str;
  4. use Laravel\Config;
  5. use Laravel\Cookie;
  6. use Laravel\Session\Drivers\Driver;
  7. use Laravel\Session\Drivers\Sweeper;
  8. if (Config::$items['application']['key'] === '')
  9. {
  10. throw new \Exception("An application key is required to use sessions.");
  11. }
  12. class Manager {
  13. /**
  14. * The session array that is stored by the driver.
  15. *
  16. * @var array
  17. */
  18. public $session;
  19. /**
  20. * Indicates if the session already exists in storage.
  21. *
  22. * @var bool
  23. */
  24. protected $exists = true;
  25. /**
  26. * Start the session handling for the current request.
  27. *
  28. * @param Driver $driver
  29. * @param string $id
  30. * @return void
  31. */
  32. public function __construct(Driver $driver, $id)
  33. {
  34. if ( ! is_null($id))
  35. {
  36. $this->session = $driver->load($id);
  37. }
  38. if (is_null($this->session) or $this->invalid())
  39. {
  40. $this->exists = false;
  41. $this->session = array('id' => Str::random(40), 'data' => array());
  42. }
  43. if ( ! $this->has('csrf_token'))
  44. {
  45. // A CSRF token is stored in every session. The token is used by the
  46. // Form class and the "csrf" filter to protect the application from
  47. // cross-site request forgery attacks. The token is simply a long,
  48. // random string which should be posted with each request.
  49. $this->put('csrf_token', Str::random(40));
  50. }
  51. }
  52. /**
  53. * Deteremine if the session payload instance is valid.
  54. *
  55. * The session is considered valid if it exists and has not expired.
  56. *
  57. * @return bool
  58. */
  59. protected function invalid()
  60. {
  61. $lifetime = Config::$items['session']['lifetime'];
  62. return (time() - $this->session['last_activity']) > ($lifetime * 60);
  63. }
  64. /**
  65. * Determine if session handling has been started for the request.
  66. *
  67. * @return bool
  68. */
  69. public function started()
  70. {
  71. return is_array($this->session);
  72. }
  73. /**
  74. * Determine if the session or flash data contains an item.
  75. *
  76. * @param string $key
  77. * @return bool
  78. */
  79. public function has($key)
  80. {
  81. return ( ! is_null($this->get($key)));
  82. }
  83. /**
  84. * Get an item from the session.
  85. *
  86. * The session flash data will also be checked for the requested item.
  87. *
  88. * <code>
  89. * // Get an item from the session
  90. * $name = Session::get('name');
  91. *
  92. * // Return a default value if the item doesn't exist
  93. * $name = Session::get('name', 'Taylor');
  94. * </code>
  95. *
  96. * @param string $key
  97. * @param mixed $default
  98. * @return mixed
  99. */
  100. public function get($key, $default = null)
  101. {
  102. foreach (array($key, ':old:'.$key, ':new:'.$key) as $possibility)
  103. {
  104. if (array_key_exists($possibility, $this->session['data']))
  105. {
  106. return $this->session['data'][$possibility];
  107. }
  108. }
  109. return ($default instanceof Closure) ? call_user_func($default) : $default;
  110. }
  111. /**
  112. * Write an item to the session.
  113. *
  114. * @param string $key
  115. * @param mixed $value
  116. * @return void
  117. */
  118. public function put($key, $value)
  119. {
  120. $this->session['data'][$key] = $value;
  121. }
  122. /**
  123. * Write an item to the session flash data.
  124. *
  125. * Flash data only exists for the next request to the application.
  126. *
  127. * @param string $key
  128. * @param mixed $value
  129. * @return void
  130. */
  131. public function flash($key, $value)
  132. {
  133. $this->put(':new:'.$key, $value);
  134. }
  135. /**
  136. * Keep all of the session flash data from expiring at the end of the request.
  137. *
  138. * @return void
  139. */
  140. public function reflash()
  141. {
  142. $flash = array();
  143. foreach ($this->session['data'] as $key => $value)
  144. {
  145. if (strpos($key, ':old:') === 0)
  146. {
  147. $flash[] = str_replace(':old:', '', $key);
  148. }
  149. }
  150. $this->keep($flash);
  151. }
  152. /**
  153. * Keep a session flash item from expiring at the end of the request.
  154. *
  155. * @param string|array $key
  156. * @return void
  157. */
  158. public function keep($keys)
  159. {
  160. foreach ((array) $keys as $key)
  161. {
  162. $this->flash($key, $this->get($key));
  163. }
  164. }
  165. /**
  166. * Remove an item from the session data.
  167. *
  168. * @param string $key
  169. * @return Driver
  170. */
  171. public function forget($key)
  172. {
  173. unset($this->session['data'][$key]);
  174. }
  175. /**
  176. * Remove all of the items from the session.
  177. *
  178. * @return void
  179. */
  180. public function flush()
  181. {
  182. $this->session['data'] = array();
  183. }
  184. /**
  185. * Assign a new, random ID to the session.
  186. *
  187. * @return void
  188. */
  189. public function regenerate()
  190. {
  191. $this->session['id'] = Str::random(40);
  192. $this->exists = false;
  193. }
  194. /**
  195. * Get the CSRF token that is stored in the session data.
  196. *
  197. * @return string
  198. */
  199. public function token()
  200. {
  201. return $this->get('csrf_token');
  202. }
  203. /**
  204. * Store the session payload in storage.
  205. *
  206. * @param Driver $driver
  207. * @return void
  208. */
  209. public function save(Driver $driver)
  210. {
  211. $this->session['last_activity'] = time();
  212. $this->age();
  213. $config = Config::$items['session'];
  214. $driver->save($this->session, $config, $this->exists);
  215. $this->cookie();
  216. // Some session drivers implement the Sweeper interface, meaning that they
  217. // must clean up expired sessions manually. If the driver is a sweeper, we
  218. // need to determine if garbage collection should be run for the request.
  219. // Since garbage collection can be expensive, the probability of it
  220. // occuring is controlled by the "sweepage" configuration option.
  221. if ($driver instanceof Sweeper and (mt_rand(1, $config['sweepage'][1]) <= $config['sweepage'][0]))
  222. {
  223. $driver->sweep(time() - ($config['lifetime'] * 60));
  224. }
  225. }
  226. /**
  227. * Age the session flash data.
  228. *
  229. * Session flash data is only available during the request in which it
  230. * was flashed, and the request after that. To "age" the data, we will
  231. * remove all of the :old: items and re-address the new items.
  232. *
  233. * @return void
  234. */
  235. protected function age()
  236. {
  237. foreach ($this->session['data'] as $key => $value)
  238. {
  239. if (strpos($key, ':old:') === 0)
  240. {
  241. $this->forget($key);
  242. }
  243. }
  244. // Now that all of the "old" keys have been removed from the session data,
  245. // we can re-address all of the newly flashed keys to have old addresses.
  246. // The array_combine method uses the first array for keys, and the second
  247. // array for values to construct a single array from both.
  248. $keys = str_replace(':new:', ':old:', array_keys($this->session['data']));
  249. $this->session['data'] = array_combine($keys, array_values($this->session['data']));
  250. }
  251. /**
  252. * Send the session ID cookie to the browser.
  253. *
  254. * @return void
  255. */
  256. protected function cookie()
  257. {
  258. $config = Config::$items['session'];
  259. extract($config, EXTR_SKIP);
  260. $minutes = ( ! $expire_on_close) ? $lifetime : 0;
  261. Cookie::put($cookie, $this->session['id'], $minutes, $path, $domain, $secure);
  262. }
  263. }