manager.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php namespace Laravel\Session;
  2. use Laravel\Str;
  3. use Laravel\Config;
  4. use Laravel\Session\Drivers\Driver;
  5. use Laravel\Session\Transporters\Transporter;
  6. class Manager {
  7. /**
  8. * The session driver instance.
  9. *
  10. * @var Driver
  11. */
  12. private $driver;
  13. /**
  14. * The session identifier transporter instance.
  15. *
  16. * @var Transporter
  17. */
  18. private $transporter;
  19. /**
  20. * The session payload instance.
  21. *
  22. * @var Payload
  23. */
  24. private $payload;
  25. /**
  26. * Indicates if the session exists in persistent storage.
  27. *
  28. * @var bool
  29. */
  30. private $exists = true;
  31. /**
  32. * Create a new session manager instance.
  33. *
  34. * @param Driver $driver
  35. * @param Transporter $transporter
  36. * @return void
  37. */
  38. public function __construct(Driver $driver, Transporter $transporter)
  39. {
  40. $this->driver = $driver;
  41. $this->transporter = $transporter;
  42. }
  43. /**
  44. * Get the session payload for the request.
  45. *
  46. * @param array $config
  47. * @return Payload
  48. */
  49. public function payload($config)
  50. {
  51. $session = $this->driver->load($this->transporter->get($config));
  52. // If the session is expired, a new session will be generated and all of the data from
  53. // the previous session will be lost. The new session will be assigned a random, long
  54. // string ID to uniquely identify it among the application's current users.
  55. if (is_null($session) or $this->expired($session, $config))
  56. {
  57. $this->exists = false;
  58. $session = array('id' => Str::random(40), 'data' => array());
  59. }
  60. $payload = new Payload($session);
  61. // If a CSRF token is not present in the session, we will generate one. These tokens
  62. // are generated per session to protect against Cross-Site Request Forgery attacks on
  63. // the application. It is up to the developer to take advantage of them using the token
  64. // methods on the Form class and the "csrf" route filter.
  65. if ( ! $payload->has('csrf_token'))
  66. {
  67. $payload->put('csrf_token', Str::random(16));
  68. }
  69. return $payload;
  70. }
  71. /**
  72. * Deteremine if the session is expired based on the last activity timestamp
  73. * and the session lifetime set in the configuration file.
  74. *
  75. * @param array $session
  76. * @param array $config
  77. * @return bool
  78. */
  79. private function expired($session, $config)
  80. {
  81. return (time() - $session['last_activity']) > ($config['lifetime'] * 60);
  82. }
  83. /**
  84. * Close the session handling for the request.
  85. *
  86. * @param Payload $payload
  87. * @param array $config
  88. * @param array $flash
  89. * @return void
  90. */
  91. public function close(Payload $payload, $config, $flash = array())
  92. {
  93. // If the session ID has been regenerated, we will need to inform the session driver
  94. // that the session will need to be persisted to the data store as a new session.
  95. if ($payload->regenerated)
  96. {
  97. $this->exists = false;
  98. }
  99. foreach ($flash as $key => $value)
  100. {
  101. $payload->flash($key, $value);
  102. }
  103. $this->driver->save($payload->age(), $config, $this->exists);
  104. $this->transporter->put($payload->session['id'], $config);
  105. // Some session drivers implement the Sweeper interface, which specified that the driver
  106. // must do its garbage collection manually. Alternatively, some drivers such as APC and
  107. // Memcached are not required to manually clean up their sessions.
  108. if (mt_rand(1, $config['sweepage'][1]) <= $config['sweepage'][0] and $this->driver instanceof Drivers\Sweeper)
  109. {
  110. $this->driver->sweep(time() - ($config['lifetime'] * 60));
  111. }
  112. }
  113. }