DbTable.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Session\SaveHandler;
  7. use Magento\Framework\Exception\SessionException;
  8. use Magento\Framework\Phrase;
  9. /**
  10. * Data base session save handler
  11. */
  12. class DbTable extends \SessionHandler
  13. {
  14. /**
  15. * Session data table name
  16. *
  17. * @var string
  18. */
  19. protected $_sessionTable;
  20. /**
  21. * Database write connection
  22. *
  23. * @var \Magento\Framework\DB\Adapter\AdapterInterface
  24. */
  25. protected $connection;
  26. /**
  27. * Constructor
  28. *
  29. * @param \Magento\Framework\App\ResourceConnection $resource
  30. */
  31. public function __construct(\Magento\Framework\App\ResourceConnection $resource)
  32. {
  33. $this->_sessionTable = $resource->getTableName('session');
  34. $this->connection = $resource->getConnection();
  35. $this->checkConnection();
  36. }
  37. /**
  38. * Check DB connection
  39. *
  40. * @return void
  41. * @throws \Magento\Framework\Exception\SessionException
  42. */
  43. protected function checkConnection()
  44. {
  45. if (!$this->connection) {
  46. throw new SessionException(
  47. new Phrase("The write connection to the database isn't available. Please try again later.")
  48. );
  49. }
  50. if (!$this->connection->isTableExists($this->_sessionTable)) {
  51. throw new SessionException(
  52. new Phrase("The database storage table doesn't exist. Verify the table and try again.")
  53. );
  54. }
  55. }
  56. /**
  57. * Open session
  58. *
  59. * @param string $savePath ignored
  60. * @param string $sessionName ignored
  61. * @return bool
  62. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  63. */
  64. public function open($savePath, $sessionName)
  65. {
  66. return true;
  67. }
  68. /**
  69. * Close session
  70. *
  71. * @return bool
  72. */
  73. public function close()
  74. {
  75. return true;
  76. }
  77. /**
  78. * Fetch session data
  79. *
  80. * @param string $sessionId
  81. * @return string
  82. */
  83. public function read($sessionId)
  84. {
  85. // need to use write connection to get the most fresh DB sessions
  86. $select = $this->connection->select()->from(
  87. $this->_sessionTable,
  88. ['session_data']
  89. )->where(
  90. 'session_id = :session_id'
  91. );
  92. $bind = ['session_id' => $sessionId];
  93. $data = $this->connection->fetchOne($select, $bind);
  94. // check if session data is a base64 encoded string
  95. $decodedData = base64_decode($data, true);
  96. if ($decodedData !== false) {
  97. $data = $decodedData;
  98. }
  99. return $data;
  100. }
  101. /**
  102. * Update session
  103. *
  104. * @param string $sessionId
  105. * @param string $sessionData
  106. * @return bool
  107. */
  108. public function write($sessionId, $sessionData)
  109. {
  110. // need to use write connection to get the most fresh DB sessions
  111. $bindValues = ['session_id' => $sessionId];
  112. $select = $this->connection->select()->from($this->_sessionTable)->where('session_id = :session_id');
  113. $exists = $this->connection->fetchOne($select, $bindValues);
  114. // encode session serialized data to prevent insertion of incorrect symbols
  115. $sessionData = base64_encode($sessionData);
  116. $bind = ['session_expires' => time(), 'session_data' => $sessionData];
  117. if ($exists) {
  118. $this->connection->update($this->_sessionTable, $bind, ['session_id=?' => $sessionId]);
  119. } else {
  120. $bind['session_id'] = $sessionId;
  121. $this->connection->insert($this->_sessionTable, $bind);
  122. }
  123. return true;
  124. }
  125. /**
  126. * Destroy session
  127. *
  128. * @param string $sessionId
  129. * @return bool
  130. */
  131. public function destroy($sessionId)
  132. {
  133. $where = ['session_id = ?' => $sessionId];
  134. $this->connection->delete($this->_sessionTable, $where);
  135. return true;
  136. }
  137. /**
  138. * Garbage collection
  139. *
  140. * @param int $maxLifeTime
  141. * @return bool
  142. * @SuppressWarnings(PHPMD.ShortMethodName)
  143. */
  144. public function gc($maxLifeTime)
  145. {
  146. $where = ['session_expires < ?' => time() - $maxLifeTime];
  147. $this->connection->delete($this->_sessionTable, $where);
  148. return true;
  149. }
  150. }