session.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. // -----------------------------------------------------
  23. // Create the session driver if we haven't already.
  24. // -----------------------------------------------------
  25. if (is_null(static::$driver))
  26. {
  27. static::$driver = Session\Factory::make(Config::get('session.driver'));
  28. }
  29. return static::$driver;
  30. }
  31. /**
  32. * Load the session for the user.
  33. *
  34. * @return void
  35. */
  36. public static function load()
  37. {
  38. // -----------------------------------------------------
  39. // If a valid ID is present, load the session.
  40. // -----------------------------------------------------
  41. if ( ! is_null($id = Cookie::get('laravel_session')))
  42. {
  43. static::$session = static::driver()->load($id);
  44. }
  45. // -----------------------------------------------------
  46. // If the session is invalid, start a new one.
  47. // -----------------------------------------------------
  48. if (is_null($id) or is_null(static::$session) or (time() - static::$session['last_activity']) > (Config::get('session.lifetime') * 60))
  49. {
  50. static::$session['id'] = Str::random(40);
  51. static::$session['data'] = array();
  52. }
  53. // -----------------------------------------------------
  54. // Generate a CSRF token if one does not exist.
  55. // -----------------------------------------------------
  56. if ( ! static::has('csrf_token'))
  57. {
  58. static::put('csrf_token', Str::random(16));
  59. }
  60. static::$session['last_activity'] = time();
  61. }
  62. /**
  63. * Determine if the session contains an item.
  64. *
  65. * @param string $key
  66. * @return bool
  67. */
  68. public static function has($key)
  69. {
  70. return array_key_exists($key, static::$session['data']) or
  71. array_key_exists(':old:'.$key, static::$session['data']) or
  72. array_key_exists(':new:'.$key, static::$session['data']);
  73. }
  74. /**
  75. * Get an item from the session.
  76. *
  77. * @param string $key
  78. * @return mixed
  79. */
  80. public static function get($key, $default = null)
  81. {
  82. if (static::has($key))
  83. {
  84. if (array_key_exists($key, static::$session['data']))
  85. {
  86. return static::$session['data'][$key];
  87. }
  88. // -----------------------------------------------------
  89. // Check the flash data for the item.
  90. // -----------------------------------------------------
  91. elseif (array_key_exists(':old:'.$key, static::$session['data']))
  92. {
  93. return static::$session['data'][':old:'.$key];
  94. }
  95. elseif (array_key_exists(':new:'.$key, static::$session['data']))
  96. {
  97. return static::$session['data'][':new:'.$key];
  98. }
  99. }
  100. return $default;
  101. }
  102. /**
  103. * Write an item to the session.
  104. *
  105. * @param string $key
  106. * @param mixed $value
  107. * @return void
  108. */
  109. public static function put($key, $value)
  110. {
  111. static::$session['data'][$key] = $value;
  112. }
  113. /**
  114. * Write a flash item to the session.
  115. *
  116. * @param string $key
  117. * @param mixed $value
  118. * @return void
  119. */
  120. public static function flash($key, $value)
  121. {
  122. static::put(':new:'.$key, $value);
  123. }
  124. /**
  125. * Remove an item from the session.
  126. *
  127. * @param string $key
  128. * @return void
  129. */
  130. public static function forget($key)
  131. {
  132. unset(static::$session['data'][$key]);
  133. }
  134. /**
  135. * Remove all items from the session.
  136. *
  137. * @return void
  138. */
  139. public static function flush()
  140. {
  141. static::$session['data'] = array();
  142. }
  143. /**
  144. * Regenerate the session ID.
  145. *
  146. * @return void
  147. */
  148. public static function regenerate()
  149. {
  150. static::driver()->delete(static::$session['id']);
  151. static::$session['id'] = Str::random(40);
  152. }
  153. /**
  154. * Close the session.
  155. *
  156. * @return void
  157. */
  158. public static function close()
  159. {
  160. // -----------------------------------------------------
  161. // Flash the old input data to the session.
  162. // -----------------------------------------------------
  163. static::flash('laravel_old_input', Input::get());
  164. // -----------------------------------------------------
  165. // Age the flash data.
  166. // -----------------------------------------------------
  167. static::age_flash();
  168. // -----------------------------------------------------
  169. // Save the session data to storage.
  170. // -----------------------------------------------------
  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. }