DatabaseSession.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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-2011, 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-2011, 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. /**
  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 int
  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. $probability = mt_rand(1, 150);
  75. if ($probability <= 3) {
  76. $this->gc();
  77. }
  78. return true;
  79. }
  80. /**
  81. * Method used to read from a database session.
  82. *
  83. * @param mixed $id The key of the value to read
  84. * @return mixed The value of the key or false if it does not exist
  85. */
  86. public function read($id) {
  87. $row = $this->_model->find('first', array(
  88. 'conditions' => array($this->_model->primaryKey => $id)
  89. ));
  90. if (empty($row[$this->_model->alias]['data'])) {
  91. return false;
  92. }
  93. return $row[$this->_model->alias]['data'];
  94. }
  95. /**
  96. * Helper function called on write for database sessions.
  97. *
  98. * @param integer $id ID that uniquely identifies session in database
  99. * @param mixed $data The value of the data to be saved.
  100. * @return boolean True for successful write, false otherwise.
  101. */
  102. public function write($id, $data) {
  103. if (!$id) {
  104. return false;
  105. }
  106. $expires = time() + $this->_timeout;
  107. $record = compact('id', 'data', 'expires');
  108. $record[$this->_model->primaryKey] = $id;
  109. return $this->_model->save($record);
  110. }
  111. /**
  112. * Method called on the destruction of a database session.
  113. *
  114. * @param integer $id ID that uniquely identifies session in database
  115. * @return boolean True for successful delete, false otherwise.
  116. */
  117. public function destroy($id) {
  118. return $this->_model->delete($id);
  119. }
  120. /**
  121. * Helper function called on gc for database sessions.
  122. *
  123. * @param integer $expires Timestamp (defaults to current time)
  124. * @return boolean Success
  125. */
  126. public function gc($expires = null) {
  127. if (!$expires) {
  128. $expires = time();
  129. }
  130. return $this->_model->deleteAll(array($this->_model->alias . ".expires <" => $expires), false, false);
  131. }
  132. /**
  133. * Closes the session before the objects handling it become unavailable
  134. *
  135. * @return void
  136. */
  137. public function __destruct() {
  138. try {
  139. session_write_close();
  140. } catch (Exception $e) {}
  141. }
  142. }