SessionHandler.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace Aws\DynamoDb;
  3. /**
  4. * Provides an interface for using Amazon DynamoDB as a session store by hooking
  5. * into PHP's session handler hooks. Once registered, You may use the native
  6. * `$_SESSION` superglobal and session functions, and the sessions will be
  7. * stored automatically in DynamoDB. DynamoDB is a great session storage
  8. * solution due to its speed, scalability, and fault tolerance.
  9. *
  10. * For maximum performance, we recommend that you keep the size of your sessions
  11. * small. Locking is disabled by default, since it can drive up latencies and
  12. * costs under high traffic. Only turn it on if you need it.
  13. *
  14. * By far, the most expensive operation is garbage collection. Therefore, we
  15. * encourage you to carefully consider your session garbage collection strategy.
  16. * Note: the DynamoDB Session Handler does not allow garbage collection to be
  17. * triggered randomly. You must run garbage collection manually or through other
  18. * automated means using a cron job or similar scheduling technique.
  19. */
  20. class SessionHandler implements \SessionHandlerInterface
  21. {
  22. /** @var SessionConnectionInterface Session save logic.*/
  23. private $connection;
  24. /** @var string Session save path. */
  25. private $savePath;
  26. /** @var string Session name. */
  27. private $sessionName;
  28. /** @var string The last known session ID */
  29. private $openSessionId = '';
  30. /** @var string Stores serialized data for tracking changes. */
  31. private $dataRead = '';
  32. /** @var bool Keeps track of whether the session has been written. */
  33. private $sessionWritten = false;
  34. /**
  35. * Creates a new DynamoDB Session Handler.
  36. *
  37. * The configuration array accepts the following array keys and values:
  38. * - table_name: Name of table to store the sessions.
  39. * - hash_key: Name of hash key in table. Default: "id".
  40. * - data_attribute: Name of the data attribute in table. Default: "data".
  41. * - session_lifetime: Lifetime of inactive sessions expiration.
  42. * - session_lifetime_attribute: Name of the session life time attribute in table. Default: "expires".
  43. * - consistent_read: Whether or not to use consistent reads.
  44. * - batch_config: Batch options used for garbage collection.
  45. * - locking: Whether or not to use session locking.
  46. * - max_lock_wait_time: Max time (s) to wait for lock acquisition.
  47. * - min_lock_retry_microtime: Min time (µs) to wait between lock attempts.
  48. * - max_lock_retry_microtime: Max time (µs) to wait between lock attempts.
  49. *
  50. * You can find the full list of parameters and defaults within the trait
  51. * Aws\DynamoDb\SessionConnectionConfigTrait
  52. *
  53. * @param DynamoDbClient $client Client for doing DynamoDB operations
  54. * @param array $config Configuration for the Session Handler
  55. *
  56. * @return SessionHandler
  57. */
  58. public static function fromClient(DynamoDbClient $client, array $config = [])
  59. {
  60. $config += ['locking' => false];
  61. if ($config['locking']) {
  62. $connection = new LockingSessionConnection($client, $config);
  63. } else {
  64. $connection = new StandardSessionConnection($client, $config);
  65. }
  66. return new static($connection);
  67. }
  68. /**
  69. * @param SessionConnectionInterface $connection
  70. */
  71. public function __construct(SessionConnectionInterface $connection)
  72. {
  73. $this->connection = $connection;
  74. }
  75. /**
  76. * Register the DynamoDB session handler.
  77. *
  78. * @return bool Whether or not the handler was registered.
  79. * @codeCoverageIgnore
  80. */
  81. public function register()
  82. {
  83. return session_set_save_handler($this, true);
  84. }
  85. /**
  86. * Open a session for writing. Triggered by session_start().
  87. *
  88. * @param string $savePath Session save path.
  89. * @param string $sessionName Session name.
  90. *
  91. * @return bool Whether or not the operation succeeded.
  92. */
  93. #[\ReturnTypeWillChange]
  94. public function open($savePath, $sessionName)
  95. {
  96. $this->savePath = $savePath;
  97. $this->sessionName = $sessionName;
  98. return true;
  99. }
  100. /**
  101. * Close a session from writing.
  102. *
  103. * @return bool Success
  104. */
  105. #[\ReturnTypeWillChange]
  106. public function close()
  107. {
  108. $id = session_id();
  109. // Make sure the session is unlocked and the expiration time is updated,
  110. // even if the write did not occur
  111. if ($this->openSessionId !== $id || !$this->sessionWritten) {
  112. $result = $this->connection->write($this->formatId($id), '', false);
  113. $this->sessionWritten = (bool) $result;
  114. }
  115. return $this->sessionWritten;
  116. }
  117. /**
  118. * Read a session stored in DynamoDB.
  119. *
  120. * @param string $id Session ID.
  121. *
  122. * @return string Session data.
  123. */
  124. #[\ReturnTypeWillChange]
  125. public function read($id)
  126. {
  127. $this->openSessionId = $id;
  128. // PHP expects an empty string to be returned from this method if no
  129. // data is retrieved
  130. $this->dataRead = '';
  131. // Get session data using the selected locking strategy
  132. $item = $this->connection->read($this->formatId($id));
  133. $dataAttribute = $this->connection->getDataAttribute();
  134. $sessionLifetimeAttribute = $this->connection->getSessionLifetimeAttribute();
  135. // Return the data if it is not expired. If it is expired, remove it
  136. if (isset($item[$sessionLifetimeAttribute]) && isset($item[$dataAttribute])) {
  137. $this->dataRead = $item[$dataAttribute];
  138. if ($item[$sessionLifetimeAttribute] <= time()) {
  139. $this->dataRead = '';
  140. $this->destroy($id);
  141. }
  142. }
  143. return $this->dataRead;
  144. }
  145. /**
  146. * Write a session to DynamoDB.
  147. *
  148. * @param string $id Session ID.
  149. * @param string $data Serialized session data to write.
  150. *
  151. * @return bool Whether or not the operation succeeded.
  152. */
  153. #[\ReturnTypeWillChange]
  154. public function write($id, $data)
  155. {
  156. $changed = $id !== $this->openSessionId
  157. || $data !== $this->dataRead;
  158. $this->openSessionId = $id;
  159. // Write the session data using the selected locking strategy
  160. $this->sessionWritten = $this->connection
  161. ->write($this->formatId($id), $data, $changed);
  162. return $this->sessionWritten;
  163. }
  164. /**
  165. * Delete a session stored in DynamoDB.
  166. *
  167. * @param string $id Session ID.
  168. *
  169. * @return bool Whether or not the operation succeeded.
  170. */
  171. #[\ReturnTypeWillChange]
  172. public function destroy($id)
  173. {
  174. $this->openSessionId = $id;
  175. // Delete the session data using the selected locking strategy
  176. $this->sessionWritten
  177. = $this->connection->delete($this->formatId($id));
  178. return $this->sessionWritten;
  179. }
  180. /**
  181. * Satisfies the session handler interface, but does nothing. To do garbage
  182. * collection, you must manually call the garbageCollect() method.
  183. *
  184. * @param int $maxLifetime Ignored.
  185. *
  186. * @return bool Whether or not the operation succeeded.
  187. * @codeCoverageIgnore
  188. */
  189. #[\ReturnTypeWillChange]
  190. public function gc($maxLifetime)
  191. {
  192. // Garbage collection for a DynamoDB table must be triggered manually.
  193. return true;
  194. }
  195. /**
  196. * Triggers garbage collection on expired sessions.
  197. * @codeCoverageIgnore
  198. */
  199. public function garbageCollect()
  200. {
  201. $this->connection->deleteExpired();
  202. }
  203. /**
  204. * Prepend the session ID with the session name.
  205. *
  206. * @param string $id The session ID.
  207. *
  208. * @return string Prepared session ID.
  209. */
  210. private function formatId($id)
  211. {
  212. return trim($this->sessionName . '_' . $id, '_');
  213. }
  214. }