PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php

https://gitlab.com/kimting254/wbms
PHP | 248 lines | 110 code | 35 blank | 103 comment | 12 complexity | e3445af76f484036e8d22ac7d04873fb MD5 | raw file
  1. <?php namespace Illuminate\Session\Middleware;
  2. use Closure;
  3. use Carbon\Carbon;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Session\SessionManager;
  6. use Illuminate\Session\SessionInterface;
  7. use Symfony\Component\HttpFoundation\Cookie;
  8. use Illuminate\Session\CookieSessionHandler;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Illuminate\Contracts\Routing\TerminableMiddleware;
  11. class StartSession implements TerminableMiddleware {
  12. /**
  13. * The session manager.
  14. *
  15. * @var \Illuminate\Session\SessionManager
  16. */
  17. protected $manager;
  18. /**
  19. * Indicates if the session was handled for the current request.
  20. *
  21. * @var bool
  22. */
  23. protected $sessionHandled = false;
  24. /**
  25. * Create a new session middleware.
  26. *
  27. * @param \Illuminate\Session\SessionManager $manager
  28. * @return void
  29. */
  30. public function __construct(SessionManager $manager)
  31. {
  32. $this->manager = $manager;
  33. }
  34. /**
  35. * Handle an incoming request.
  36. *
  37. * @param \Illuminate\Http\Request $request
  38. * @param \Closure $next
  39. * @return mixed
  40. */
  41. public function handle($request, Closure $next)
  42. {
  43. $this->sessionHandled = true;
  44. // If a session driver has been configured, we will need to start the session here
  45. // so that the data is ready for an application. Note that the Laravel sessions
  46. // do not make use of PHP "native" sessions in any way since they are crappy.
  47. if ($this->sessionConfigured())
  48. {
  49. $session = $this->startSession($request);
  50. $request->setSession($session);
  51. }
  52. $response = $next($request);
  53. // Again, if the session has been configured we will need to close out the session
  54. // so that the attributes may be persisted to some storage medium. We will also
  55. // add the session identifier cookie to the application response headers now.
  56. if ($this->sessionConfigured())
  57. {
  58. $this->storeCurrentUrl($request, $session);
  59. $this->collectGarbage($session);
  60. $this->addCookieToResponse($response, $session);
  61. }
  62. return $response;
  63. }
  64. /**
  65. * Perform any final actions for the request lifecycle.
  66. *
  67. * @param \Illuminate\Http\Request $request
  68. * @param \Symfony\Component\HttpFoundation\Response $response
  69. * @return void
  70. */
  71. public function terminate($request, $response)
  72. {
  73. if ($this->sessionHandled && $this->sessionConfigured() && ! $this->usingCookieSessions())
  74. {
  75. $this->manager->driver()->save();
  76. }
  77. }
  78. /**
  79. * Start the session for the given request.
  80. *
  81. * @param \Illuminate\Http\Request $request
  82. * @return \Illuminate\Session\SessionInterface
  83. */
  84. protected function startSession(Request $request)
  85. {
  86. with($session = $this->getSession($request))->setRequestOnHandler($request);
  87. $session->start();
  88. return $session;
  89. }
  90. /**
  91. * Get the session implementation from the manager.
  92. *
  93. * @param \Illuminate\Http\Request $request
  94. * @return \Illuminate\Session\SessionInterface
  95. */
  96. public function getSession(Request $request)
  97. {
  98. $session = $this->manager->driver();
  99. $session->setId($request->cookies->get($session->getName()));
  100. return $session;
  101. }
  102. /**
  103. * Store the current URL for the request if necessary.
  104. *
  105. * @param \Illuminate\Http\Request $request
  106. * @param \Illuminate\Session\SessionInterface $session
  107. * @return void
  108. */
  109. protected function storeCurrentUrl(Request $request, $session)
  110. {
  111. if ($request->method() === 'GET' && $request->route() && ! $request->ajax())
  112. {
  113. $session->setPreviousUrl($request->fullUrl());
  114. }
  115. }
  116. /**
  117. * Remove the garbage from the session if necessary.
  118. *
  119. * @param \Illuminate\Session\SessionInterface $session
  120. * @return void
  121. */
  122. protected function collectGarbage(SessionInterface $session)
  123. {
  124. $config = $this->manager->getSessionConfig();
  125. // Here we will see if this request hits the garbage collection lottery by hitting
  126. // the odds needed to perform garbage collection on any given request. If we do
  127. // hit it, we'll call this handler to let it delete all the expired sessions.
  128. if ($this->configHitsLottery($config))
  129. {
  130. $session->getHandler()->gc($this->getSessionLifetimeInSeconds());
  131. }
  132. }
  133. /**
  134. * Determine if the configuration odds hit the lottery.
  135. *
  136. * @param array $config
  137. * @return bool
  138. */
  139. protected function configHitsLottery(array $config)
  140. {
  141. return mt_rand(1, $config['lottery'][1]) <= $config['lottery'][0];
  142. }
  143. /**
  144. * Add the session cookie to the application response.
  145. *
  146. * @param \Symfony\Component\HttpFoundation\Response $response
  147. * @param \Illuminate\Session\SessionInterface $session
  148. * @return void
  149. */
  150. protected function addCookieToResponse(Response $response, SessionInterface $session)
  151. {
  152. if ($this->usingCookieSessions())
  153. {
  154. $this->manager->driver()->save();
  155. }
  156. if ($this->sessionIsPersistent($config = $this->manager->getSessionConfig()))
  157. {
  158. $response->headers->setCookie(new Cookie(
  159. $session->getName(), $session->getId(), $this->getCookieExpirationDate(),
  160. $config['path'], $config['domain'], array_get($config, 'secure', false)
  161. ));
  162. }
  163. }
  164. /**
  165. * Get the session lifetime in seconds.
  166. *
  167. * @return int
  168. */
  169. protected function getSessionLifetimeInSeconds()
  170. {
  171. return array_get($this->manager->getSessionConfig(), 'lifetime') * 60;
  172. }
  173. /**
  174. * Get the cookie lifetime in seconds.
  175. *
  176. * @return int
  177. */
  178. protected function getCookieExpirationDate()
  179. {
  180. $config = $this->manager->getSessionConfig();
  181. return $config['expire_on_close'] ? 0 : Carbon::now()->addMinutes($config['lifetime']);
  182. }
  183. /**
  184. * Determine if a session driver has been configured.
  185. *
  186. * @return bool
  187. */
  188. protected function sessionConfigured()
  189. {
  190. return ! is_null(array_get($this->manager->getSessionConfig(), 'driver'));
  191. }
  192. /**
  193. * Determine if the configured session driver is persistent.
  194. *
  195. * @param array|null $config
  196. * @return bool
  197. */
  198. protected function sessionIsPersistent(array $config = null)
  199. {
  200. $config = $config ?: $this->manager->getSessionConfig();
  201. return ! in_array($config['driver'], array(null, 'array'));
  202. }
  203. /**
  204. * Determine if the session is using cookie sessions.
  205. *
  206. * @return bool
  207. */
  208. protected function usingCookieSessions()
  209. {
  210. if ( ! $this->sessionConfigured()) return false;
  211. return $this->manager->driver()->getHandler() instanceof CookieSessionHandler;
  212. }
  213. }