DatabaseSession.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.libs
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. /**
  20. * DatabaseSession provides methods to be used with CakeSession.
  21. *
  22. * @package cake.libs.session
  23. */
  24. class DatabaseSession implements CakeSessionHandlerInterface {
  25. /**
  26. * Constructor. Looks at Session configuration information and
  27. * sets up the session model.
  28. *
  29. * @return void
  30. */
  31. public function __construct() {
  32. $modelName = Configure::read('Session.handler.model');
  33. $database = Configure::read('Session.handler.database');
  34. $table = Configure::read('Session.handler.table');
  35. if (empty($database)) {
  36. $database = 'default';
  37. }
  38. $settings = array(
  39. 'class' => 'Session',
  40. 'alias' => 'Session',
  41. 'table' => 'cake_sessions',
  42. 'ds' => $database
  43. );
  44. if (!empty($modelName)) {
  45. $settings['class'] = $modelName;
  46. }
  47. if (!empty($table)) {
  48. $settings['table'] = $table;
  49. }
  50. ClassRegistry::init($settings);
  51. }
  52. /**
  53. * Method called on open of a database session.
  54. *
  55. * @return boolean Success
  56. * @access private
  57. */
  58. public function open() {
  59. return true;
  60. }
  61. /**
  62. * Method called on close of a database session.
  63. *
  64. * @return boolean Success
  65. * @access private
  66. */
  67. public function close() {
  68. $probability = mt_rand(1, 150);
  69. if ($probability <= 3) {
  70. DatabaseSession::gc();
  71. }
  72. return true;
  73. }
  74. /**
  75. * Method used to read from a database session.
  76. *
  77. * @param mixed $id The key of the value to read
  78. * @return mixed The value of the key or false if it does not exist
  79. * @access private
  80. */
  81. public function read($id) {
  82. $model = ClassRegistry::getObject('Session');
  83. $row = $model->find('first', array(
  84. 'conditions' => array($model->primaryKey => $id)
  85. ));
  86. if (empty($row[$model->alias]['data'])) {
  87. return false;
  88. }
  89. return $row[$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. * @access private
  98. */
  99. public function write($id, $data) {
  100. if (!$id) {
  101. return false;
  102. }
  103. $expires = time() + (Configure::read('Session.timeout') * 60);
  104. $Session = ClassRegistry::getObject('Session');
  105. $record = compact('id', 'data', 'expires');
  106. $record[$Session->primaryKey] = $id;
  107. return $Session->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. * @access private
  115. */
  116. public function destroy($id) {
  117. return ClassRegistry::getObject('Session')->delete($id);
  118. }
  119. /**
  120. * Helper function called on gc for database sessions.
  121. *
  122. * @param integer $expires Timestamp (defaults to current time)
  123. * @return boolean Success
  124. * @access private
  125. */
  126. public function gc($expires = null) {
  127. if (!$expires) {
  128. $expires = time();
  129. }
  130. $model = ClassRegistry::getObject('Session');
  131. return $model->deleteAll(array($model->alias . ".expires <" => $expires), false, false);
  132. }
  133. }