modsessionhandler.class.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. /**
  11. * Default database session handler class for MODX.
  12. *
  13. * @package modx
  14. */
  15. class modSessionHandler {
  16. /**
  17. * @var modX A reference to the modX instance controlling this session
  18. * handler.
  19. * @access public
  20. */
  21. public $modx= null;
  22. /**
  23. * @var int The maximum lifetime of the session
  24. */
  25. public $gcMaxLifetime= 0;
  26. /**
  27. * @var int The maximum lifetime of the cache of the session
  28. */
  29. public $cacheLifetime= false;
  30. /**
  31. * @var modSession The Session object
  32. */
  33. private $session = null;
  34. /**
  35. * Creates an instance of a modSessionHandler class.
  36. *
  37. * @param modX &$modx A reference to a {@link modX} instance.
  38. * @return modSessionHandler
  39. */
  40. function __construct(modX &$modx) {
  41. $this->modx= & $modx;
  42. $gcMaxlifetime = (integer) $this->modx->getOption('session_gc_maxlifetime');
  43. if ($gcMaxlifetime > 0) {
  44. $this->gcMaxLifetime= $gcMaxlifetime;
  45. } else {
  46. $this->gcMaxLifetime= (integer) @ini_get('session.gc_maxlifetime');
  47. }
  48. if ($this->modx->getOption('cache_db_session', null, false)) {
  49. $cacheLifetime = $this->modx->getOption('cache_db_session_lifetime', null, false);
  50. if ((integer) $cacheLifetime > 0) {
  51. $this->cacheLifetime = (integer) $cacheLifetime;
  52. } elseif ($cacheLifetime !== false && $this->gcMaxLifetime > 0) {
  53. $this->cacheLifetime = $this->gcMaxLifetime / 4;
  54. }
  55. }
  56. }
  57. /**
  58. * Opens the connection for the session handler.
  59. *
  60. * @access public
  61. * @return boolean Always returns true; actual connection is managed by
  62. * {@link modX}.
  63. */
  64. public function open() {
  65. return true;
  66. }
  67. /**
  68. * Closes the connection for the session handler.
  69. *
  70. * @access public
  71. * @return boolean Always returns true; actual connection is managed by
  72. * {@link modX}
  73. */
  74. public function close() {
  75. return true;
  76. }
  77. /**
  78. * Reads a specific {@link modSession} record's data.
  79. *
  80. * @access public
  81. * @param integer $id The pk of the {@link modSession} object.
  82. * @return string The data read from the {@link modSession} object.
  83. */
  84. public function read($id) {
  85. if ($this->_getSession($id)) {
  86. $data= $this->session->get('data');
  87. } else {
  88. $data= '';
  89. }
  90. return (string) $data;
  91. }
  92. /**
  93. * Writes data to a specific {@link modSession} object.
  94. *
  95. * @access public
  96. * @param integer $id The PK of the modSession object.
  97. * @param mixed $data The data to write to the session.
  98. * @return boolean True if successfully written.
  99. */
  100. public function write($id, $data) {
  101. $written= false;
  102. if ($this->_getSession($id, true)) {
  103. $this->session->set('data', $data);
  104. if ($this->session->isNew() || $this->session->isDirty('data') || ($this->cacheLifetime > 0 && (time() - strtotime($this->session->get('access'))) > $this->cacheLifetime)) {
  105. $this->session->set('access', time());
  106. }
  107. $written= $this->session->save($this->cacheLifetime);
  108. }
  109. return $written;
  110. }
  111. /**
  112. * Destroy a specific {@link modSession} record.
  113. *
  114. * @access public
  115. * @param integer $id
  116. * @return boolean True if the session record was destroyed.
  117. */
  118. public function destroy($id) {
  119. if ($this->_getSession($id)) {
  120. $destroyed= $this->session->remove();
  121. } else {
  122. $destroyed= true;
  123. }
  124. return $destroyed;
  125. }
  126. /**
  127. * Remove any expired sessions.
  128. *
  129. * @access public
  130. * @param integer $max The amount of time since now to expire any session
  131. * longer than.
  132. * @return boolean True if session records were removed.
  133. */
  134. public function gc($max) {
  135. $maxtime= time() - $this->gcMaxLifetime;
  136. $result = $this->modx->removeCollection('modSession', array("{$this->modx->escape('access')} < {$maxtime}"));
  137. return $result !== false;
  138. }
  139. /**
  140. * Gets the {@link modSession} object, respecting the cache flag represented by cacheLifetime.
  141. *
  142. * @access protected
  143. * @param integer $id The PK of the {@link modSession} record.
  144. * @param boolean $autoCreate If true, will automatically create the session
  145. * record if none is found.
  146. * @return modSession|null The modSession instance loaded from db or auto-created; null if it
  147. * could not be retrieved and/or created.
  148. */
  149. protected function _getSession($id, $autoCreate= false) {
  150. $this->session= $this->modx->getObject('modSession', array('id' => $id), $this->cacheLifetime);
  151. if ($autoCreate && !is_object($this->session)) {
  152. $this->session= $this->modx->newObject('modSession');
  153. $this->session->set('id', $id);
  154. }
  155. if (!($this->session instanceof modSession) || $id != $this->session->get('id') || !$this->session->validate()) {
  156. $this->modx->log(modX::LOG_LEVEL_INFO, 'There was an error retrieving or creating session id: ' . $id);
  157. }
  158. return $this->session;
  159. }
  160. }