DatabaseSession.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. * @package Cake.Model.Datasource.Session
  15. * @since CakePHP(tm) v 2.0
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('CakeSessionHandlerInterface', 'Model/Datasource/Session');
  19. App::uses('ClassRegistry', 'Utility');
  20. /**
  21. * DatabaseSession provides methods to be used with CakeSession.
  22. *
  23. * @package Cake.Model.Datasource.Session
  24. */
  25. class DatabaseSession implements CakeSessionHandlerInterface {
  26. /**
  27. * Reference to the model handling the session data
  28. *
  29. * @var Model
  30. */
  31. protected $_model;
  32. /**
  33. * Number of seconds to mark the session as expired
  34. *
  35. * @var integer
  36. */
  37. protected $_timeout;
  38. /**
  39. * Constructor. Looks at Session configuration information and
  40. * sets up the session model.
  41. *
  42. */
  43. public function __construct() {
  44. $modelName = Configure::read('Session.handler.model');
  45. if (empty($modelName)) {
  46. $settings = array(
  47. 'class' => 'Session',
  48. 'alias' => 'Session',
  49. 'table' => 'cake_sessions',
  50. );
  51. } else {
  52. $settings = array(
  53. 'class' => $modelName,
  54. 'alias' => 'Session',
  55. );
  56. }
  57. $this->_model = ClassRegistry::init($settings);
  58. $this->_timeout = Configure::read('Session.timeout') * 60;
  59. }
  60. /**
  61. * Method called on open of a database session.
  62. *
  63. * @return boolean Success
  64. */
  65. public function open() {
  66. return true;
  67. }
  68. /**
  69. * Method called on close of a database session.
  70. *
  71. * @return boolean Success
  72. */
  73. public function close() {
  74. return true;
  75. }
  76. /**
  77. * Method used to read from a database session.
  78. *
  79. * @param integer|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. $row = $this->_model->find('first', array(
  84. 'conditions' => array($this->_model->primaryKey => $id)
  85. ));
  86. if (empty($row[$this->_model->alias]['data'])) {
  87. return false;
  88. }
  89. return $row[$this->_model->alias]['data'];
  90. }
  91. /**
  92. * Helper function called on write for database sessions.
  93. *
  94. * @param integer $id ID that uniquely identifies session in database
  95. * @param mixed $data The value of the data to be saved.
  96. * @return boolean True for successful write, false otherwise.
  97. */
  98. public function write($id, $data) {
  99. if (!$id) {
  100. return false;
  101. }
  102. $expires = time() + $this->_timeout;
  103. $record = compact('id', 'data', 'expires');
  104. $record[$this->_model->primaryKey] = $id;
  105. return $this->_model->save($record);
  106. }
  107. /**
  108. * Method called on the destruction of a database session.
  109. *
  110. * @param integer $id ID that uniquely identifies session in database
  111. * @return boolean True for successful delete, false otherwise.
  112. */
  113. public function destroy($id) {
  114. return $this->_model->delete($id);
  115. }
  116. /**
  117. * Helper function called on gc for database sessions.
  118. *
  119. * @param integer $expires Timestamp (defaults to current time)
  120. * @return boolean Success
  121. */
  122. public function gc($expires = null) {
  123. if (!$expires) {
  124. $expires = time();
  125. } else {
  126. $expires = time() - $expires;
  127. }
  128. return $this->_model->deleteAll(array($this->_model->alias . ".expires <" => $expires), false, false);
  129. }
  130. }