DatabaseSession.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. /**
  20. * DatabaseSession provides methods to be used with CakeSession.
  21. *
  22. * @package Cake.Model.Datasource.Session
  23. */
  24. class DatabaseSession implements CakeSessionHandlerInterface {
  25. /**
  26. * Constructor. Looks at Session configuration information and
  27. * sets up the session model.
  28. *
  29. */
  30. public function __construct() {
  31. $modelName = Configure::read('Session.handler.model');
  32. if (empty($modelName)) {
  33. $settings = array(
  34. 'class' =>'Session',
  35. 'alias' => 'Session',
  36. 'table' => 'cake_sessions',
  37. );
  38. } else {
  39. $settings = array(
  40. 'class' =>$modelName,
  41. 'alias' => 'Session',
  42. );
  43. }
  44. ClassRegistry::init($settings);
  45. }
  46. /**
  47. * Method called on open of a database session.
  48. *
  49. * @return boolean Success
  50. */
  51. public function open() {
  52. return true;
  53. }
  54. /**
  55. * Method called on close of a database session.
  56. *
  57. * @return boolean Success
  58. */
  59. public function close() {
  60. $probability = mt_rand(1, 150);
  61. if ($probability <= 3) {
  62. $this->gc();
  63. }
  64. return true;
  65. }
  66. /**
  67. * Method used to read from a database session.
  68. *
  69. * @param mixed $id The key of the value to read
  70. * @return mixed The value of the key or false if it does not exist
  71. */
  72. public function read($id) {
  73. $model = ClassRegistry::getObject('Session');
  74. $row = $model->find('first', array(
  75. 'conditions' => array($model->primaryKey => $id)
  76. ));
  77. if (empty($row[$model->alias]['data'])) {
  78. return false;
  79. }
  80. return $row[$model->alias]['data'];
  81. }
  82. /**
  83. * Helper function called on write for database sessions.
  84. *
  85. * @param integer $id ID that uniquely identifies session in database
  86. * @param mixed $data The value of the data to be saved.
  87. * @return boolean True for successful write, false otherwise.
  88. */
  89. public function write($id, $data) {
  90. if (!$id) {
  91. return false;
  92. }
  93. $expires = time() + (Configure::read('Session.timeout') * 60);
  94. $Session = ClassRegistry::getObject('Session');
  95. $record = compact('id', 'data', 'expires');
  96. $record[$Session->primaryKey] = $id;
  97. return $Session->save($record);
  98. }
  99. /**
  100. * Method called on the destruction of a database session.
  101. *
  102. * @param integer $id ID that uniquely identifies session in database
  103. * @return boolean True for successful delete, false otherwise.
  104. */
  105. public function destroy($id) {
  106. return ClassRegistry::getObject('Session')->delete($id);
  107. }
  108. /**
  109. * Helper function called on gc for database sessions.
  110. *
  111. * @param integer $expires Timestamp (defaults to current time)
  112. * @return boolean Success
  113. */
  114. public function gc($expires = null) {
  115. if (!$expires) {
  116. $expires = time();
  117. }
  118. $model = ClassRegistry::getObject('Session');
  119. return $model->deleteAll(array($model->alias . ".expires <" => $expires), false, false);
  120. }
  121. }