manager.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php namespace Laravel\Session;
  2. use Closure;
  3. use Laravel\Str;
  4. use Laravel\Config;
  5. use Laravel\Session\Drivers\Driver;
  6. use Laravel\Session\Transporters\Transporter;
  7. class Manager {
  8. /**
  9. * The current session payload.
  10. *
  11. * @var array
  12. */
  13. public static $session = array();
  14. /**
  15. * Indicates if the session exists in persistent storage.
  16. *
  17. * @var bool
  18. */
  19. public static $exists = true;
  20. /**
  21. * Indicates if the session ID has been regenerated.
  22. *
  23. * @var bool
  24. */
  25. public static $regenerated = false;
  26. /**
  27. * The driver being used by the session.
  28. *
  29. * @var Drivers\Driver
  30. */
  31. protected static $driver;
  32. /**
  33. * The session ID transporter used by the session.
  34. *
  35. * @var Transporters\Transpoter
  36. */
  37. protected static $transporter;
  38. /**
  39. * Start the session handling for the current request.
  40. *
  41. * @param Drivers\Driver $driver
  42. * @param Transporters\Transporter $transporter
  43. * @return Payload
  44. */
  45. public static function start(Driver $driver, Transporter $transporter)
  46. {
  47. $config = Config::$items['session'];
  48. $session = $driver->load($transporter->get($config));
  49. // If the session is expired, a new session will be generated and all of
  50. // the data from the previous session will be lost. The new session will
  51. // be assigned a random, long string ID to uniquely identify it among
  52. // the application's current users.
  53. if (is_null($session) or (time() - $session['last_activity']) > ($config['lifetime'] * 60))
  54. {
  55. static::$exists = false;
  56. $session = array('id' => Str::random(40), 'data' => array());
  57. }
  58. static::$session = $session;
  59. // If a CSRF token is not present in the session, we will generate one.
  60. // These tokens are generated per session to protect against Cross-Site
  61. // Request Forgery attacks on the application.
  62. if ( ! static::has('csrf_token'))
  63. {
  64. static::put('csrf_token', Str::random(16));
  65. }
  66. list(static::$driver, static::$transporter) = array($driver, $transporter);
  67. }
  68. /**
  69. * Determine if the session or flash data contains an item.
  70. *
  71. * @param string $key
  72. * @return bool
  73. */
  74. public static function has($key)
  75. {
  76. return ( ! is_null(static::get($key)));
  77. }
  78. /**
  79. * Get an item from the session.
  80. *
  81. * <code>
  82. * // Get an item from the session
  83. * $name = Session::get('name');
  84. *
  85. * // Return a default value if the item doesn't exist
  86. * $name = Session::get('name', 'Taylor');
  87. * </code>
  88. *
  89. * @param string $key
  90. * @param mixed $default
  91. * @return mixed
  92. */
  93. public static function get($key, $default = null)
  94. {
  95. foreach (array($key, ':old:'.$key, ':new:'.$key) as $possibility)
  96. {
  97. if (array_key_exists($possibility, static::$session['data']))
  98. {
  99. return static::$session['data'][$possibility];
  100. }
  101. }
  102. return ($default instanceof Closure) ? call_user_func($default) : $default;
  103. }
  104. /**
  105. * Write an item to the session.
  106. *
  107. * <code>
  108. * // Write an item to the session
  109. * Session::put('name', 'Taylor');
  110. * </code>
  111. *
  112. * @param string $key
  113. * @param mixed $value
  114. * @return void
  115. */
  116. public static function put($key, $value)
  117. {
  118. static::$session['data'][$key] = $value;
  119. }
  120. /**
  121. * Write an item to the session flash data.
  122. *
  123. * Flash data only exists for the next request. After that, it will
  124. * be removed from the session. Flash data is useful for temporary
  125. * status or welcome messages.
  126. *
  127. * <code>
  128. * // Flash an item to the session
  129. * Session::flash('name', 'Taylor');
  130. * </code>
  131. *
  132. * @param string $key
  133. * @param mixed $value
  134. * @return void
  135. */
  136. public static function flash($key, $value)
  137. {
  138. static::put(':new:'.$key, $value);
  139. }
  140. /**
  141. * Keep all of the session flash data from expiring at the end of the request.
  142. *
  143. * @return void
  144. */
  145. public static function reflash()
  146. {
  147. static::replace(':old:', ':new:', array_keys(static::$session['data']));
  148. }
  149. /**
  150. * Keep a session flash item from expiring at the end of the request.
  151. *
  152. * If a string is passed to the method, only that item will be kept.
  153. * An array may also be passed to the method, in which case all
  154. * items in the array will be kept.
  155. *
  156. * <code>
  157. * // Keep a session flash item from expiring
  158. * Session::keep('name');
  159. * </code>
  160. *
  161. * @param string|array $key
  162. * @return void
  163. */
  164. public static function keep($key)
  165. {
  166. if (is_array($key))
  167. {
  168. return array_map(array('Laravel\\Session\\Manager', 'keep'), $key);
  169. }
  170. static::flash($key, static::get($key));
  171. static::forget(':old:'.$key);
  172. }
  173. /**
  174. * Remove an item from the session.
  175. *
  176. * @param string $key
  177. * @return Driver
  178. */
  179. public static function forget($key)
  180. {
  181. unset(static::$session['data'][$key]);
  182. }
  183. /**
  184. * Remove all items from the session.
  185. *
  186. * @return void
  187. */
  188. public static function flush()
  189. {
  190. static::$session['data'] = array();
  191. }
  192. /**
  193. * Regenerate the session ID.
  194. *
  195. * @return void
  196. */
  197. public static function regenerate()
  198. {
  199. static::$session['id'] = Str::random(40);
  200. static::$regenerated = true;
  201. static::$exists = false;
  202. }
  203. /**
  204. * Age the session payload, preparing it for storage after a request.
  205. *
  206. * @return array
  207. */
  208. public static function age()
  209. {
  210. static::$session['last_activity'] = time();
  211. // To age the data, we will forget all of the old keys and then
  212. // rewrite the newly flashed items to have old keys, which will
  213. // be available for the next request.
  214. foreach (static::$session['data'] as $key => $value)
  215. {
  216. if (strpos($key, ':old:') === 0) static::forget($key);
  217. }
  218. static::replace(':new:', ':old:', array_keys(static::$session['data']));
  219. return static::$session;
  220. }
  221. /**
  222. * Readdress the session data by performing a string replacement on the keys.
  223. *
  224. * @param string $search
  225. * @param string $replace
  226. * @param array $keys
  227. * @return void
  228. */
  229. protected static function replace($search, $replace, $keys)
  230. {
  231. $keys = str_replace($search, $replace, $keys);
  232. static::$session['data'] = array_combine($keys, array_values(static::$session['data']));
  233. }
  234. /**
  235. * Close the session handling for the request.
  236. *
  237. * @param array $flash
  238. * @return void
  239. */
  240. public static function close($flash = array())
  241. {
  242. $config = Config::$items['session'];
  243. foreach ($flash as $key => $value)
  244. {
  245. static::flash($key, $value);
  246. }
  247. static::$driver->save(static::age(), $config, static::$exists);
  248. static::$transporter->put(static::$session['id'], $config);
  249. // Some session drivers may implement the Sweeper interface, meaning the
  250. // driver must do its garbage collection manually. Alternatively, some
  251. // drivers such as APC and Memcached are not required to manually
  252. // clean up their sessions.
  253. if (mt_rand(1, $config['sweepage'][1]) <= $config['sweepage'][0] and static::$driver instanceof Drivers\Sweeper)
  254. {
  255. static::$driver->sweep(time() - ($config['lifetime'] * 60));
  256. }
  257. }
  258. }