PageRenderTime 34ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Model/Datasource/Session/DatabaseSession.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 152 lines | 62 code | 13 blank | 77 comment | 6 complexity | 471e976b5d22ceeb20ce4eff5805dce8 MD5 | raw file
  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. * Reference to the model handling the session data
  27. *
  28. * @var Model
  29. */
  30. protected $_model;
  31. /**
  32. * Number of seconds to mark the session as expired
  33. *
  34. * @var int
  35. */
  36. protected $_timeout;
  37. /**
  38. * Constructor. Looks at Session configuration information and
  39. * sets up the session model.
  40. *
  41. */
  42. public function __construct() {
  43. $modelName = Configure::read('Session.handler.model');
  44. if (empty($modelName)) {
  45. $settings = array(
  46. 'class' => 'Session',
  47. 'alias' => 'Session',
  48. 'table' => 'cake_sessions',
  49. );
  50. } else {
  51. $settings = array(
  52. 'class' => $modelName,
  53. 'alias' => 'Session',
  54. );
  55. }
  56. $this->_model = ClassRegistry::init($settings);
  57. $this->_timeout = Configure::read('Session.timeout') * 60;
  58. }
  59. /**
  60. * Method called on open of a database session.
  61. *
  62. * @return boolean Success
  63. */
  64. public function open() {
  65. return true;
  66. }
  67. /**
  68. * Method called on close of a database session.
  69. *
  70. * @return boolean Success
  71. */
  72. public function close() {
  73. $probability = mt_rand(1, 150);
  74. if ($probability <= 3) {
  75. $this->gc();
  76. }
  77. return true;
  78. }
  79. /**
  80. * Method used to read from a database session.
  81. *
  82. * @param mixed $id The key of the value to read
  83. * @return mixed The value of the key or false if it does not exist
  84. */
  85. public function read($id) {
  86. $row = $this->_model->find('first', array(
  87. 'conditions' => array($this->_model->primaryKey => $id)
  88. ));
  89. if (empty($row[$this->_model->alias]['data'])) {
  90. return false;
  91. }
  92. return $row[$this->_model->alias]['data'];
  93. }
  94. /**
  95. * Helper function called on write for database sessions.
  96. *
  97. * @param integer $id ID that uniquely identifies session in database
  98. * @param mixed $data The value of the data to be saved.
  99. * @return boolean True for successful write, false otherwise.
  100. */
  101. public function write($id, $data) {
  102. if (!$id) {
  103. return false;
  104. }
  105. $expires = time() + $this->_timeout;
  106. $record = compact('id', 'data', 'expires');
  107. $record[$this->_model->primaryKey] = $id;
  108. return $this->_model->save($record);
  109. }
  110. /**
  111. * Method called on the destruction of a database session.
  112. *
  113. * @param integer $id ID that uniquely identifies session in database
  114. * @return boolean True for successful delete, false otherwise.
  115. */
  116. public function destroy($id) {
  117. return $this->_model->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. */
  125. public function gc($expires = null) {
  126. if (!$expires) {
  127. $expires = time();
  128. }
  129. return $this->_model->deleteAll(array($this->_model->alias . ".expires <" => $expires), false, false);
  130. }
  131. /**
  132. * Closes the session before the objects handling it become unavailable
  133. *
  134. * @return void
  135. */
  136. public function __destruct() {
  137. session_write_close();
  138. }
  139. }