PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/SessionHandler/lib/Horde/SessionHandler/Storage/Sql.php

https://github.com/sgtcarneiro/horde
PHP | 208 lines | 112 code | 22 blank | 74 comment | 3 complexity | db6c487874c0ace87269477ad2f6670e MD5 | raw file
  1. <?php
  2. /**
  3. * SessionHandler storage implementation for SQL databases.
  4. *
  5. * Uses the following SQL table structure:
  6. * <pre>
  7. * CREATE TABLE horde_sessionhandler (
  8. * VARCHAR(32) NOT NULL,
  9. * session_lastmodified INT NOT NULL,
  10. * session_data LONGBLOB,
  11. * -- Or, on some DBMS systems:
  12. * -- session_data IMAGE,
  13. *
  14. * PRIMARY KEY (session_id)
  15. * );
  16. *
  17. * CREATE INDEX session_lastmodified_idx ON horde_sessionhandler (session_lastmodified);
  18. * </pre>
  19. *
  20. * Copyright 2002-2011 The Horde Project (http://www.horde.org/)
  21. *
  22. * See the enclosed file COPYING for license information (LGPL). If you
  23. * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  24. *
  25. * @author Mike Cochrane <mike@graftonhall.co.nz>
  26. * @category Horde
  27. * @license http://www.fsf.org/copyleft/lgpl.html LGPL
  28. * @package SessionHandler
  29. */
  30. class Horde_SessionHandler_Storage_Sql extends Horde_SessionHandler_Storage
  31. {
  32. /**
  33. * Handle for the current database connection.
  34. *
  35. * @var Horde_Db_Adapter
  36. */
  37. protected $_db;
  38. /**
  39. * Constructor.
  40. *
  41. * @param array $params Parameters:
  42. * <pre>
  43. * 'db' - (Horde_Db_Adapter) [REQUIRED] The DB instance.
  44. * 'table' - (string) The name of the sessions table.
  45. * DEFAULT: 'horde_sessionhandler'
  46. * </pre>
  47. *
  48. * @throws InvalidArgumentException
  49. */
  50. public function __construct(array $params = array())
  51. {
  52. if (!isset($params['db'])) {
  53. throw new InvalidArgumentException('Missing db parameter.');
  54. }
  55. $this->_db = $params['db'];
  56. unset($params['db']);
  57. parent::__construct(array_merge(array(
  58. 'table' => 'horde_sessionhandler'
  59. ), $params));
  60. }
  61. /**
  62. */
  63. public function open($save_path = null, $session_name = null)
  64. {
  65. }
  66. /**
  67. */
  68. public function close()
  69. {
  70. /* Close any open transactions. */
  71. if ($this->_db->transactionStarted()) {
  72. try {
  73. $this->_db->commitDbTransaction();
  74. } catch (Horde_Db_Exception $e) {
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. /**
  81. */
  82. public function read($id)
  83. {
  84. /* Begin a transaction. */
  85. // TODO: Rowlocking in Mysql
  86. $this->_db->beginDbTransaction();
  87. /* Build query. */
  88. $query = sprintf('SELECT session_data FROM %s WHERE session_id = ?',
  89. $this->_params['table']);
  90. $values = array($id);
  91. /* Execute the query. */
  92. try {
  93. return $this->_db->selectValue($query, $values);
  94. } catch (Horde_Db_Exception $e) {
  95. return false;
  96. }
  97. }
  98. /**
  99. */
  100. public function write($id, $session_data)
  101. {
  102. if (!$this->_db->isActive()) { $this->_db->reconnect(); }
  103. /* Build the SQL query. */
  104. $query = sprintf('SELECT session_id FROM %s WHERE session_id = ?',
  105. $this->_params['table']);
  106. $values = array($id);
  107. /* Execute the query. */
  108. try {
  109. $result = $this->_db->selectValue($query, $values);
  110. } catch (Horde_Db_Exception $e) {
  111. return false;
  112. }
  113. /* Build the replace SQL query. */
  114. $query = sprintf('REPLACE INTO %s ' .
  115. '(session_id, session_data, session_lastmodified) ' .
  116. 'VALUES (?, ?, ?)',
  117. $this->_params['table']);
  118. $values = array(
  119. $id,
  120. $session_data,
  121. time()
  122. );
  123. /* Execute the replace query. */
  124. try {
  125. $this->_db->update($query, $values);
  126. $this->_db->commitDbTransaction();
  127. } catch (Horde_Db_Exception $e) {
  128. try {
  129. $this->_db->rollbackDbTransaction();
  130. } catch (Horde_Db_Exception $e) {
  131. }
  132. return false;
  133. }
  134. return true;
  135. }
  136. /**
  137. */
  138. public function destroy($id)
  139. {
  140. /* Build the SQL query. */
  141. $query = sprintf('DELETE FROM %s WHERE session_id = ?',
  142. $this->_params['table']);
  143. $values = array($id);
  144. /* Execute the query. */
  145. try {
  146. $this->_db->delete($query, $values);
  147. $this->_db->commitDbTransaction();
  148. } catch (Horde_Db_Exception $e) {
  149. return false;
  150. }
  151. return true;
  152. }
  153. /**
  154. */
  155. public function gc($maxlifetime = 300)
  156. {
  157. /* Build the SQL query. */
  158. $query = sprintf('DELETE FROM %s WHERE session_lastmodified < ?',
  159. $this->_params['table']);
  160. $values = array(time() - $maxlifetime);
  161. /* Execute the query. */
  162. try {
  163. $this->_db->delete($query, $values);
  164. } catch (Horde_Db_Exception $e) {
  165. return false;
  166. }
  167. return true;
  168. }
  169. /**
  170. */
  171. public function getSessionIDs()
  172. {
  173. $this->open();
  174. /* Build the SQL query. */
  175. $query = sprintf('SELECT session_id FROM %s' .
  176. ' WHERE session_lastmodified >= ?',
  177. $this->_params['table']);
  178. $values = array(time() - ini_get('session.gc_maxlifetime'));
  179. /* Execute the query. */
  180. try {
  181. return $this->_db->selectValues($query, $values);
  182. } catch (Horde_Db_Exception $e) {
  183. return array();
  184. }
  185. }
  186. }