DatabaseSession.php 3.6 KB

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