DatabaseSession.php 3.5 KB

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