session.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php namespace System;
  2. class Session {
  3. /**
  4. * The active session driver.
  5. *
  6. * @var Session\Driver
  7. */
  8. private static $driver;
  9. /**
  10. * The session.
  11. *
  12. * @var array
  13. */
  14. private static $session = array();
  15. /**
  16. * Get the session driver instance.
  17. *
  18. * @return Session\Driver
  19. */
  20. public static function driver()
  21. {
  22. if (is_null(static::$driver))
  23. {
  24. static::$driver = Session\Factory::make(Config::get('session.driver'));
  25. }
  26. return static::$driver;
  27. }
  28. /**
  29. * Load the session for the user.
  30. *
  31. * @return void
  32. */
  33. public static function load()
  34. {
  35. // -----------------------------------------------------
  36. // If a valid ID is present, load the session.
  37. // -----------------------------------------------------
  38. if ( ! is_null($id = Cookie::get('laravel_session')))
  39. {
  40. static::$session = static::driver()->load($id);
  41. }
  42. // -----------------------------------------------------
  43. // If the session is invalid, start a new one.
  44. // -----------------------------------------------------
  45. if (is_null($id) or is_null(static::$session) or (time() - static::$session['last_activity']) > (Config::get('session.lifetime') * 60))
  46. {
  47. static::$session['id'] = Str::random(40);
  48. static::$session['data'] = array();
  49. }
  50. // -----------------------------------------------------
  51. // Generate a CSRF token if one does not exist.
  52. // -----------------------------------------------------
  53. if ( ! static::has('csrf_token'))
  54. {
  55. static::put('csrf_token', Str::random(16));
  56. }
  57. static::$session['last_activity'] = time();
  58. }
  59. /**
  60. * Determine if the session contains an item.
  61. *
  62. * @param string $key
  63. * @return bool
  64. */
  65. public static function has($key)
  66. {
  67. return array_key_exists($key, static::$session['data']) or
  68. array_key_exists(':old:'.$key, static::$session['data']) or
  69. array_key_exists(':new:'.$key, static::$session['data']);
  70. }
  71. /**
  72. * Get an item from the session.
  73. *
  74. * @param string $key
  75. * @return mixed
  76. */
  77. public static function get($key, $default = null)
  78. {
  79. if (static::has($key))
  80. {
  81. if (array_key_exists($key, static::$session['data']))
  82. {
  83. return static::$session['data'][$key];
  84. }
  85. // -----------------------------------------------------
  86. // Check the flash data for the item.
  87. // -----------------------------------------------------
  88. elseif (array_key_exists(':old:'.$key, static::$session['data']))
  89. {
  90. return static::$session['data'][':old:'.$key];
  91. }
  92. elseif (array_key_exists(':new:'.$key, static::$session['data']))
  93. {
  94. return static::$session['data'][':new:'.$key];
  95. }
  96. }
  97. return $default;
  98. }
  99. /**
  100. * Get an item from the session and delete it.
  101. *
  102. * @param string $key
  103. * @return mixed
  104. */
  105. public static function once($key, $default = null)
  106. {
  107. $value = static::get($key, $default);
  108. static::forget($key);
  109. return $value;
  110. }
  111. /**
  112. * Write an item to the session.
  113. *
  114. * @param string $key
  115. * @param mixed $value
  116. * @return void
  117. */
  118. public static function put($key, $value)
  119. {
  120. static::$session['data'][$key] = $value;
  121. }
  122. /**
  123. * Write a flash item to the session.
  124. *
  125. * @param string $key
  126. * @param mixed $value
  127. * @return void
  128. */
  129. public static function flash($key, $value)
  130. {
  131. static::put(':new:'.$key, $value);
  132. }
  133. /**
  134. * Remove an item from the session.
  135. *
  136. * @param string $key
  137. * @return void
  138. */
  139. public static function forget($key)
  140. {
  141. unset(static::$session['data'][$key]);
  142. }
  143. /**
  144. * Remove all items from the session.
  145. *
  146. * @return void
  147. */
  148. public static function flush()
  149. {
  150. static::$session['data'] = array();
  151. }
  152. /**
  153. * Regenerate the session ID.
  154. *
  155. * @return void
  156. */
  157. public static function regenerate()
  158. {
  159. static::driver()->delete(static::$session['id']);
  160. static::$session['id'] = Str::random(40);
  161. }
  162. /**
  163. * Close the session.
  164. *
  165. * @return void
  166. */
  167. public static function close()
  168. {
  169. static::flash('laravel_old_input', Input::get());
  170. static::age_flash();
  171. static::driver()->save(static::$session);
  172. // -----------------------------------------------------
  173. // Set the session cookie.
  174. // -----------------------------------------------------
  175. if ( ! headers_sent())
  176. {
  177. $lifetime = (Config::get('session.expire_on_close')) ? 0 : Config::get('session.lifetime');
  178. Cookie::put('laravel_session', static::$session['id'], $lifetime, Config::get('session.path'), Config::get('session.domain'), Config::get('session.https'));
  179. }
  180. // -----------------------------------------------------
  181. // Perform session garbage collection (2% chance).
  182. // -----------------------------------------------------
  183. if (mt_rand(1, 100) <= 2)
  184. {
  185. static::driver()->sweep(time() - (Config::get('session.lifetime') * 60));
  186. }
  187. }
  188. /**
  189. * Age the session flash data.
  190. *
  191. * @return void
  192. */
  193. private static function age_flash()
  194. {
  195. // -----------------------------------------------------
  196. // Expire all of the old flash data.
  197. // -----------------------------------------------------
  198. foreach (static::$session['data'] as $key => $value)
  199. {
  200. if (strpos($key, ':old:') === 0)
  201. {
  202. static::forget($key);
  203. }
  204. }
  205. // -----------------------------------------------------
  206. // Age all of the new flash data.
  207. // -----------------------------------------------------
  208. foreach (static::$session['data'] as $key => $value)
  209. {
  210. if (strpos($key, ':new:') === 0)
  211. {
  212. // -----------------------------------------------------
  213. // Create an :old: flash item.
  214. // -----------------------------------------------------
  215. static::put(':old:'.substr($key, 5), $value);
  216. // -----------------------------------------------------
  217. // Forget the :new: flash item.
  218. // -----------------------------------------------------
  219. static::forget($key);
  220. }
  221. }
  222. }
  223. }