PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Network/Session/DatabaseSession.php

https://github.com/binondord/cakephp
PHP | 156 lines | 67 code | 16 blank | 73 comment | 5 complexity | e3ce97c4005e4e347596ac1d9abf95f5 MD5 | raw file
  1. <?php
  2. /**
  3. * Database Session save handler. Allows saving session information into a model.
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since 2.0.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Network\Session;
  18. use Cake\ORM\Entity;
  19. use Cake\ORM\TableRegistry;
  20. use SessionHandlerInterface;
  21. /**
  22. * DatabaseSession provides methods to be used with Session.
  23. *
  24. */
  25. class DatabaseSession implements SessionHandlerInterface
  26. {
  27. /**
  28. * Reference to the table handling the session data
  29. *
  30. * @var \Cake\ORM\Table
  31. */
  32. protected $_table;
  33. /**
  34. * Number of seconds to mark the session as expired
  35. *
  36. * @var int
  37. */
  38. protected $_timeout;
  39. /**
  40. * Constructor. Looks at Session configuration information and
  41. * sets up the session model.
  42. *
  43. * @param array $config The configuration for this engine. It requires the 'model'
  44. * key to be present corresponding to the Table to use for managing the sessions.
  45. */
  46. public function __construct(array $config = [])
  47. {
  48. if (empty($config['model'])) {
  49. $config = TableRegistry::exists('Sessions') ? [] : ['table' => 'sessions'];
  50. $this->_table = TableRegistry::get('Sessions', $config);
  51. } else {
  52. $this->_table = TableRegistry::get($config['model']);
  53. }
  54. $this->_timeout = ini_get('session.gc_maxlifetime');
  55. }
  56. /**
  57. * Method called on open of a database session.
  58. *
  59. * @param string $savePath The path where to store/retrieve the session.
  60. * @param string $name The session name.
  61. * @return bool Success
  62. */
  63. public function open($savePath, $name)
  64. {
  65. return true;
  66. }
  67. /**
  68. * Method called on close of a database session.
  69. *
  70. * @return bool Success
  71. */
  72. public function close()
  73. {
  74. return true;
  75. }
  76. /**
  77. * Method used to read from a database session.
  78. *
  79. * @param int|string $id The key of the value to read
  80. * @return mixed The value of the key or false if it does not exist
  81. */
  82. public function read($id)
  83. {
  84. $result = $this->_table
  85. ->find('all')
  86. ->select(['data'])
  87. ->where([$this->_table->primaryKey() => $id])
  88. ->hydrate(false)
  89. ->first();
  90. if (empty($result)) {
  91. return false;
  92. }
  93. if (is_string($result['data'])) {
  94. return $result['data'];
  95. }
  96. return stream_get_contents($result['data']);
  97. }
  98. /**
  99. * Helper function called on write for database sessions.
  100. *
  101. * @param int $id ID that uniquely identifies session in database
  102. * @param mixed $data The value of the data to be saved.
  103. * @return bool True for successful write, false otherwise.
  104. */
  105. public function write($id, $data)
  106. {
  107. if (!$id) {
  108. return false;
  109. }
  110. $expires = time() + $this->_timeout;
  111. $record = compact('data', 'expires');
  112. $record[$this->_table->primaryKey()] = $id;
  113. $result = $this->_table->save(new Entity($record));
  114. return (bool)$result;
  115. }
  116. /**
  117. * Method called on the destruction of a database session.
  118. *
  119. * @param int $id ID that uniquely identifies session in database
  120. * @return bool True for successful delete, false otherwise.
  121. */
  122. public function destroy($id)
  123. {
  124. return (bool)$this->_table->delete(new Entity(
  125. [$this->_table->primaryKey() => $id],
  126. ['markNew' => false]
  127. ));
  128. }
  129. /**
  130. * Helper function called on gc for database sessions.
  131. *
  132. * @param string $maxlifetime Sessions that have not updated for the last maxlifetime seconds will be removed.
  133. * @return bool True on success, false on failure.
  134. */
  135. public function gc($maxlifetime)
  136. {
  137. $this->_table->deleteAll(['expires <' => time() - $maxlifetime]);
  138. return true;
  139. }
  140. }