manager.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. // Now that we should have a valid session, we can set the static session
  59. // property and check for session data such as the CSRF token. We will
  60. // also set the static driver and transporter properties, since they
  61. // will be used to close the session at the end of the request.
  62. static::$session = $session;
  63. if ( ! static::has('csrf_token')) static::put('csrf_token', Str::random(16));
  64. list(static::$driver, static::$transporter) = array($driver, $transporter);
  65. }
  66. /**
  67. * Determine if the session or flash data contains an item.
  68. *
  69. * @param string $key
  70. * @return bool
  71. */
  72. public static function has($key)
  73. {
  74. return ( ! is_null(static::get($key)));
  75. }
  76. /**
  77. * Get an item from the session.
  78. *
  79. * <code>
  80. * // Get an item from the session
  81. * $name = Session::get('name');
  82. *
  83. * // Return a default value if the item doesn't exist
  84. * $name = Session::get('name', 'Taylor');
  85. * </code>
  86. *
  87. * @param string $key
  88. * @param mixed $default
  89. * @return mixed
  90. */
  91. public static function get($key, $default = null)
  92. {
  93. foreach (array($key, ':old:'.$key, ':new:'.$key) as $possibility)
  94. {
  95. if (array_key_exists($possibility, static::$session['data']))
  96. {
  97. return static::$session['data'][$possibility];
  98. }
  99. }
  100. return ($default instanceof Closure) ? call_user_func($default) : $default;
  101. }
  102. /**
  103. * Write an item to the session.
  104. *
  105. * <code>
  106. * // Write an item to the session
  107. * Session::put('name', 'Taylor');
  108. * </code>
  109. *
  110. * @param string $key
  111. * @param mixed $value
  112. * @return void
  113. */
  114. public static function put($key, $value)
  115. {
  116. static::$session['data'][$key] = $value;
  117. }
  118. /**
  119. * Write an item to the session flash data.
  120. *
  121. * Flash data only exists for the next request. After that, it will
  122. * be removed from the session. Flash data is useful for temporary
  123. * status or welcome messages.
  124. *
  125. * <code>
  126. * // Flash an item to the session
  127. * Session::flash('name', 'Taylor');
  128. * </code>
  129. *
  130. * @param string $key
  131. * @param mixed $value
  132. * @return void
  133. */
  134. public static function flash($key, $value)
  135. {
  136. static::put(':new:'.$key, $value);
  137. }
  138. /**
  139. * Keep all of the session flash data from expiring at the end of the request.
  140. *
  141. * @return void
  142. */
  143. public static function reflash()
  144. {
  145. static::replace(':old:', ':new:', array_keys(static::$session['data']));
  146. }
  147. /**
  148. * Keep a session flash item from expiring at the end of the request.
  149. *
  150. * If a string is passed to the method, only that item will be kept.
  151. * An array may also be passed to the method, in which case all
  152. * items in the array will be kept.
  153. *
  154. * <code>
  155. * // Keep a session flash item from expiring
  156. * Session::keep('name');
  157. * </code>
  158. *
  159. * @param string|array $key
  160. * @return void
  161. */
  162. public static function keep($key)
  163. {
  164. if (is_array($key))
  165. {
  166. return array_map(array('Laravel\\Session\\Manager', 'keep'), $key);
  167. }
  168. static::flash($key, static::get($key));
  169. static::forget(':old:'.$key);
  170. }
  171. /**
  172. * Remove an item from the session.
  173. *
  174. * @param string $key
  175. * @return Driver
  176. */
  177. public static function forget($key)
  178. {
  179. unset(static::$session['data'][$key]);
  180. }
  181. /**
  182. * Remove all items from the session.
  183. *
  184. * @return void
  185. */
  186. public static function flush()
  187. {
  188. static::$session['data'] = array();
  189. }
  190. /**
  191. * Regenerate the session ID.
  192. *
  193. * @return void
  194. */
  195. public static function regenerate()
  196. {
  197. static::$session['id'] = Str::random(40);
  198. static::$regenerated = true;
  199. static::$exists = false;
  200. }
  201. /**
  202. * Age the session payload, preparing it for storage after a request.
  203. *
  204. * @return array
  205. */
  206. public static function age()
  207. {
  208. static::$session['last_activity'] = time();
  209. // To age the data, we will forget all of the old keys and then
  210. // rewrite the newly flashed items to have old keys, which will
  211. // be available for the next request.
  212. foreach (static::$session['data'] as $key => $value)
  213. {
  214. if (strpos($key, ':old:') === 0) static::forget($key);
  215. }
  216. static::replace(':new:', ':old:', array_keys(static::$session['data']));
  217. return static::$session;
  218. }
  219. /**
  220. * Readdress the session data by performing a string replacement on the keys.
  221. *
  222. * @param string $search
  223. * @param string $replace
  224. * @param array $keys
  225. * @return void
  226. */
  227. protected static function replace($search, $replace, $keys)
  228. {
  229. $keys = str_replace($search, $replace, $keys);
  230. static::$session['data'] = array_combine($keys, array_values(static::$session['data']));
  231. }
  232. /**
  233. * Close the session handling for the request.
  234. *
  235. * @return void
  236. */
  237. public static function close()
  238. {
  239. $config = Config::$items['session'];
  240. static::$driver->save(static::age(), $config, static::$exists);
  241. static::$transporter->put(static::$session['id'], $config);
  242. // Some session drivers may implement the Sweeper interface, meaning the
  243. // driver must do its garbage collection manually. Alternatively, some
  244. // drivers such as APC and Memcached are not required to manually
  245. // clean up their sessions.
  246. if (mt_rand(1, $config['sweepage'][1]) <= $config['sweepage'][0] and static::$driver instanceof Drivers\Sweeper)
  247. {
  248. static::$driver->sweep(time() - ($config['lifetime'] * 60));
  249. }
  250. }
  251. }