DatabaseSession.php 3.5 KB

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