SessionHandler.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. public function open($savePath, $sessionName)
  94. {
  95. $this->savePath = $savePath;
  96. $this->sessionName = $sessionName;
  97. return true;
  98. }
  99. /**
  100. * Close a session from writing.
  101. *
  102. * @return bool Success
  103. */
  104. public function close()
  105. {
  106. $id = session_id();
  107. // Make sure the session is unlocked and the expiration time is updated,
  108. // even if the write did not occur
  109. if ($this->openSessionId !== $id || !$this->sessionWritten) {
  110. $result = $this->connection->write($this->formatId($id), '', false);
  111. $this->sessionWritten = (bool) $result;
  112. }
  113. return $this->sessionWritten;
  114. }
  115. /**
  116. * Read a session stored in DynamoDB.
  117. *
  118. * @param string $id Session ID.
  119. *
  120. * @return string Session data.
  121. */
  122. public function read($id)
  123. {
  124. $this->openSessionId = $id;
  125. // PHP expects an empty string to be returned from this method if no
  126. // data is retrieved
  127. $this->dataRead = '';
  128. // Get session data using the selected locking strategy
  129. $item = $this->connection->read($this->formatId($id));
  130. $dataAttribute = $this->connection->getDataAttribute();
  131. $sessionLifetimeAttribute = $this->connection->getSessionLifetimeAttribute();
  132. // Return the data if it is not expired. If it is expired, remove it
  133. if (isset($item[$sessionLifetimeAttribute]) && isset($item[$dataAttribute])) {
  134. $this->dataRead = $item[$dataAttribute];
  135. if ($item[$sessionLifetimeAttribute] <= time()) {
  136. $this->dataRead = '';
  137. $this->destroy($id);
  138. }
  139. }
  140. return $this->dataRead;
  141. }
  142. /**
  143. * Write a session to DynamoDB.
  144. *
  145. * @param string $id Session ID.
  146. * @param string $data Serialized session data to write.
  147. *
  148. * @return bool Whether or not the operation succeeded.
  149. */
  150. public function write($id, $data)
  151. {
  152. $changed = $id !== $this->openSessionId
  153. || $data !== $this->dataRead;
  154. $this->openSessionId = $id;
  155. // Write the session data using the selected locking strategy
  156. $this->sessionWritten = $this->connection
  157. ->write($this->formatId($id), $data, $changed);
  158. return $this->sessionWritten;
  159. }
  160. /**
  161. * Delete a session stored in DynamoDB.
  162. *
  163. * @param string $id Session ID.
  164. *
  165. * @return bool Whether or not the operation succeeded.
  166. */
  167. public function destroy($id)
  168. {
  169. $this->openSessionId = $id;
  170. // Delete the session data using the selected locking strategy
  171. $this->sessionWritten
  172. = $this->connection->delete($this->formatId($id));
  173. return $this->sessionWritten;
  174. }
  175. /**
  176. * Satisfies the session handler interface, but does nothing. To do garbage
  177. * collection, you must manually call the garbageCollect() method.
  178. *
  179. * @param int $maxLifetime Ignored.
  180. *
  181. * @return bool Whether or not the operation succeeded.
  182. * @codeCoverageIgnore
  183. */
  184. public function gc($maxLifetime)
  185. {
  186. // Garbage collection for a DynamoDB table must be triggered manually.
  187. return true;
  188. }
  189. /**
  190. * Triggers garbage collection on expired sessions.
  191. * @codeCoverageIgnore
  192. */
  193. public function garbageCollect()
  194. {
  195. $this->connection->deleteExpired();
  196. }
  197. /**
  198. * Prepend the session ID with the session name.
  199. *
  200. * @param string $id The session ID.
  201. *
  202. * @return string Prepared session ID.
  203. */
  204. private function formatId($id)
  205. {
  206. return trim($this->sessionName . '_' . $id, '_');
  207. }
  208. }