PageRenderTime 33ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/laravel/session/payload.php

http://github.com/ericbarnes/Status-Board
PHP | 346 lines | 126 code | 42 blank | 178 comment | 5 complexity | 38e822c975b14bc79f7f0b64853b58bf MD5 | raw file
Possible License(s): MIT
  1. <?php namespace Laravel\Session;
  2. use Closure;
  3. use Laravel\Str;
  4. use Laravel\Config;
  5. use Laravel\Cookie;
  6. use Laravel\Session;
  7. use Laravel\Session\Drivers\Driver;
  8. use Laravel\Session\Drivers\Sweeper;
  9. class Payload {
  10. /**
  11. * The session array that is stored by the driver.
  12. *
  13. * @var array
  14. */
  15. public $session;
  16. /**
  17. * The session driver used to retrieve and store the session payload.
  18. *
  19. * @var Driver
  20. */
  21. public $driver;
  22. /**
  23. * Indicates if the session already exists in storage.
  24. *
  25. * @var bool
  26. */
  27. public $exists = true;
  28. /**
  29. * Create a new session payload instance.
  30. *
  31. * @param Driver $driver
  32. * @return void
  33. */
  34. public function __construct(Driver $driver)
  35. {
  36. $this->driver = $driver;
  37. }
  38. /**
  39. * Load the session for the current request.
  40. *
  41. * @param string $id
  42. * @return void
  43. */
  44. public function load($id)
  45. {
  46. if ( ! is_null($id)) $this->session = $this->driver->load($id);
  47. // If the session doesn't exist or is invalid, we will create a new session
  48. // array and mark the session as being non-existent. Some drivers, such as
  49. // the database driver, need to know whether the session exists in storage
  50. // so they can know whether to insert or update the session.
  51. if (is_null($this->session) or static::expired($this->session))
  52. {
  53. $this->exists = false;
  54. $this->session = array('id' => Str::random(40), 'data' => array(
  55. ':new:' => array(),
  56. ':old:' => array(),
  57. ));
  58. }
  59. // A CSRF token is stored in every session. The token is used by the Form
  60. // class and the "csrf" filter to protect the application from cross-site
  61. // request forgery attacks. The token is simply a long, random string
  62. // which should be posted with each request to the application.
  63. if ( ! $this->has(Session::csrf_token))
  64. {
  65. $this->put(Session::csrf_token, Str::random(40));
  66. }
  67. }
  68. /**
  69. * Deteremine if the session payload instance is valid.
  70. *
  71. * The session is considered valid if it exists and has not expired.
  72. *
  73. * @param array $session
  74. * @return bool
  75. */
  76. protected static function expired($session)
  77. {
  78. $lifetime = Config::get('session.lifetime');
  79. return (time() - $session['last_activity']) > ($lifetime * 60);
  80. }
  81. /**
  82. * Determine if the session or flash data contains an item.
  83. *
  84. * @param string $key
  85. * @return bool
  86. */
  87. public function has($key)
  88. {
  89. return ( ! is_null($this->get($key)));
  90. }
  91. /**
  92. * Get an item from the session.
  93. *
  94. * The session flash data will also be checked for the requested item.
  95. *
  96. * <code>
  97. * // Get an item from the session
  98. * $name = Session::get('name');
  99. *
  100. * // Return a default value if the item doesn't exist
  101. * $name = Session::get('name', 'Taylor');
  102. * </code>
  103. *
  104. * @param string $key
  105. * @param mixed $default
  106. * @return mixed
  107. */
  108. public function get($key, $default = null)
  109. {
  110. $session = $this->session['data'];
  111. // We check for the item in the general session data first, and if it
  112. // does not exist in that data, we will attempt to find it in the new
  113. // and old flash data. If none of those arrays contain the requested
  114. // item, we will just return the default value.
  115. if ( ! is_null($value = array_get($session, $key)))
  116. {
  117. return $value;
  118. }
  119. elseif ( ! is_null($value = array_get($session[':new:'], $key)))
  120. {
  121. return $value;
  122. }
  123. elseif ( ! is_null($value = array_get($session[':old:'], $key)))
  124. {
  125. return $value;
  126. }
  127. return value($default);
  128. }
  129. /**
  130. * Write an item to the session.
  131. *
  132. * <code>
  133. * // Write an item to the session payload
  134. * Session::put('name', 'Taylor');
  135. * </code>
  136. *
  137. * @param string $key
  138. * @param mixed $value
  139. * @return void
  140. */
  141. public function put($key, $value)
  142. {
  143. array_set($this->session['data'], $key, $value);
  144. }
  145. /**
  146. * Write an item to the session flash data.
  147. *
  148. * Flash data only exists for the current and next request to the application.
  149. *
  150. * <code>
  151. * // Write an item to the session payload's flash data
  152. * Session::flash('name', 'Taylor');
  153. * </code>
  154. *
  155. * @param string $key
  156. * @param mixed $value
  157. * @return void
  158. */
  159. public function flash($key, $value)
  160. {
  161. array_set($this->session['data'][':new:'], $key, $value);
  162. }
  163. /**
  164. * Keep all of the session flash data from expiring after the request.
  165. *
  166. * @return void
  167. */
  168. public function reflash()
  169. {
  170. $old = $this->session['data'][':old:'];
  171. $this->session['data'][':new:'] = array_merge($this->session['data'][':new:'], $old);
  172. }
  173. /**
  174. * Keep a session flash item from expiring at the end of the request.
  175. *
  176. * <code>
  177. * // Keep the "name" item from expiring from the flash data
  178. * Session::keep('name');
  179. *
  180. * // Keep the "name" and "email" items from expiring from the flash data
  181. * Session::keep(array('name', 'email'));
  182. * </code>
  183. *
  184. * @param string|array $keys
  185. * @return void
  186. */
  187. public function keep($keys)
  188. {
  189. foreach ((array) $keys as $key)
  190. {
  191. $this->flash($key, $this->get($key));
  192. }
  193. }
  194. /**
  195. * Remove an item from the session data.
  196. *
  197. * @param string $key
  198. * @return void
  199. */
  200. public function forget($key)
  201. {
  202. array_forget($this->session['data'], $key);
  203. }
  204. /**
  205. * Remove all of the items from the session.
  206. *
  207. * The CSRF token will not be removed from the session.
  208. *
  209. * @return void
  210. */
  211. public function flush()
  212. {
  213. $token = $this->token();
  214. $session = array(Session::csrf_token => $token, ':new:' => array(), ':old:' => array());
  215. $this->session['data'] = $session;
  216. }
  217. /**
  218. * Assign a new, random ID to the session.
  219. *
  220. * @return void
  221. */
  222. public function regenerate()
  223. {
  224. $this->session['id'] = Str::random(40);
  225. $this->exists = false;
  226. }
  227. /**
  228. * Get the CSRF token that is stored in the session data.
  229. *
  230. * @return string
  231. */
  232. public function token()
  233. {
  234. return $this->get(Session::csrf_token);
  235. }
  236. /**
  237. * Get the last activity for the session.
  238. *
  239. * @return int
  240. */
  241. public function activity()
  242. {
  243. return $this->session['last_activity'];
  244. }
  245. /**
  246. * Store the session payload in storage.
  247. *
  248. * This method will be called automatically at the end of the request.
  249. *
  250. * @return void
  251. */
  252. public function save()
  253. {
  254. $this->session['last_activity'] = time();
  255. // Session flash data is only available during the request in which it
  256. // was flashed and the following request. We will age the data so that
  257. // it expires at the end of the user's next request.
  258. $this->age();
  259. $config = Config::get('session');
  260. // The responsibility of actually storing the session information in
  261. // persistent storage is delegated to the driver instance being used
  262. // by the session payload.
  263. //
  264. // This allows us to keep the payload very generic, while moving the
  265. // platform or storage mechanism code into the specialized drivers,
  266. // keeping our code very dry and organized.
  267. $this->driver->save($this->session, $config, $this->exists);
  268. // Next we'll write out the session cookie. This cookie contains the
  269. // ID of the session, and will be used to determine the owner of the
  270. // session on the user's subsequent requests to the application.
  271. $this->cookie($config);
  272. // Some session drivers implement the Sweeper interface, meaning that
  273. // they must clean up expired sessions manually. If the driver is a
  274. // sweeper, we need to determine if garbage collection should be
  275. // run for the request.
  276. $sweepage = $config['sweepage'];
  277. if ($this->driver instanceof Sweeper and (mt_rand(1, $sweepage[1]) <= $sweepage[0]))
  278. {
  279. $this->driver->sweep(time() - ($config['lifetime'] * 60));
  280. }
  281. }
  282. /**
  283. * Age the session flash data.
  284. *
  285. * @return void
  286. */
  287. protected function age()
  288. {
  289. $this->session['data'][':old:'] = $this->session['data'][':new:'];
  290. $this->session['data'][':new:'] = array();
  291. }
  292. /**
  293. * Send the session ID cookie to the browser.
  294. *
  295. * @param array $config
  296. * @return void
  297. */
  298. protected function cookie($config)
  299. {
  300. extract($config, EXTR_SKIP);
  301. $minutes = ( ! $expire_on_close) ? $lifetime : 0;
  302. Cookie::put($cookie, $this->session['id'], $minutes, $path, $domain, $secure);
  303. }
  304. }