PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/database/classes/kohana/session/database.php

https://bitbucket.org/seyar/ari100krat.local
PHP | 229 lines | 139 code | 38 blank | 52 comment | 12 complexity | 61a85f2f597d9cdaf040a2775b6e8aa2 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Database-based session class.
  4. *
  5. * Sample schema:
  6. *
  7. * CREATE TABLE `sessions` (
  8. * `session_id` VARCHAR( 24 ) NOT NULL,
  9. * `last_active` INT UNSIGNED NOT NULL,
  10. * `contents` TEXT NOT NULL,
  11. * PRIMARY KEY ( `session_id` ),
  12. * INDEX ( `last_active` )
  13. * ) ENGINE = MYISAM ;
  14. *
  15. * @package Kohana/Database
  16. * @category Session
  17. * @author Kohana Team
  18. * @copyright (c) 2008-2009 Kohana Team
  19. * @license http://kohanaphp.com/license
  20. */
  21. class Kohana_Session_Database extends Session {
  22. // Database instance
  23. protected $_db;
  24. // Database table name
  25. protected $_table = 'sessions';
  26. // Database column names
  27. protected $_columns = array(
  28. 'session_id' => 'session_id',
  29. 'last_active' => 'last_active',
  30. 'contents' => 'contents'
  31. );
  32. // Garbage collection requests
  33. protected $_gc = 500;
  34. // The current session id
  35. protected $_session_id;
  36. // The old session id
  37. protected $_update_id;
  38. public function __construct(array $config = NULL, $id = NULL)
  39. {
  40. if ( ! isset($config['group']))
  41. {
  42. // Use the default group
  43. $config['group'] = 'default';
  44. }
  45. // Load the database
  46. $this->_db = Database::instance($config['group']);
  47. if (isset($config['table']))
  48. {
  49. // Set the table name
  50. $this->_table = (string) $config['table'];
  51. }
  52. if (isset($config['gc']))
  53. {
  54. // Set the gc chance
  55. $this->_gc = (int) $config['gc'];
  56. }
  57. if (isset($config['columns']))
  58. {
  59. // Overload column names
  60. $this->_columns = $config['columns'];
  61. }
  62. parent::__construct($config, $id);
  63. if (mt_rand(0, $this->_gc) === $this->_gc)
  64. {
  65. // Run garbage collection
  66. // This will average out to run once every X requests
  67. $this->_gc();
  68. }
  69. }
  70. public function id()
  71. {
  72. return $this->_session_id;
  73. }
  74. protected function _read($id = NULL)
  75. {
  76. if ($id OR $id = Cookie::get($this->_name))
  77. {
  78. $result = DB::select(array($this->_columns['contents'], 'contents'))
  79. ->from($this->_table)
  80. ->where($this->_columns['session_id'], '=', ':id')
  81. ->limit(1)
  82. ->param(':id', $id)
  83. ->execute($this->_db);
  84. if ($result->count())
  85. {
  86. // Set the current session id
  87. $this->_session_id = $this->_update_id = $id;
  88. // Return the contents
  89. return $result->get('contents');
  90. }
  91. }
  92. // Create a new session id
  93. $this->_regenerate();
  94. return NULL;
  95. }
  96. protected function _regenerate()
  97. {
  98. // Create the query to find an ID
  99. $query = DB::select($this->_columns['session_id'])
  100. ->from($this->_table)
  101. ->where($this->_columns['session_id'], '=', ':id')
  102. ->limit(1)
  103. ->bind(':id', $id);
  104. do
  105. {
  106. // Create a new session id
  107. $id = str_replace('.', '-', uniqid(NULL, TRUE));
  108. // Get the the id from the database
  109. $result = $query->execute($this->_db);
  110. }
  111. while ($result->count());
  112. return $this->_session_id = $id;
  113. }
  114. protected function _write()
  115. {
  116. if ($this->_update_id === NULL)
  117. {
  118. // Insert a new row
  119. $query = DB::insert($this->_table, $this->_columns)
  120. ->values(array(':new_id', ':active', ':contents'));
  121. }
  122. else
  123. {
  124. // Update the row
  125. $query = DB::update($this->_table)
  126. ->value($this->_columns['last_active'], ':active')
  127. ->value($this->_columns['contents'], ':contents')
  128. ->where($this->_columns['session_id'], '=', ':old_id');
  129. if ($this->_update_id !== $this->_session_id)
  130. {
  131. // Also update the session id
  132. $query->value($this->_columns['session_id'], ':new_id');
  133. }
  134. }
  135. $query
  136. ->param(':new_id', $this->_session_id)
  137. ->param(':old_id', $this->_update_id)
  138. ->param(':active', $this->_data['last_active'])
  139. ->param(':contents', $this->__toString());
  140. // Execute the query
  141. $query->execute($this->_db);
  142. // The update and the session id are now the same
  143. $this->_update_id = $this->_session_id;
  144. // Update the cookie with the new session id
  145. Cookie::set($this->_name, $this->_session_id, $this->_lifetime);
  146. return TRUE;
  147. }
  148. protected function _destroy()
  149. {
  150. if ($this->_update_id === NULL)
  151. {
  152. // Session has not been created yet
  153. return TRUE;
  154. }
  155. // Delete the current session
  156. $query = DB::delete($this->_table)
  157. ->where($this->_columns['session_id'], '=', ':id')
  158. ->param(':id', $this->_update_id);
  159. try
  160. {
  161. // Execute the query
  162. $query->execute($this->_db);
  163. // Delete the cookie
  164. Cookie::delete($this->_name);
  165. }
  166. catch (Exception $e)
  167. {
  168. // An error occurred, the session has not been deleted
  169. return FALSE;
  170. }
  171. return TRUE;
  172. }
  173. protected function _gc()
  174. {
  175. if ($this->_lifetime)
  176. {
  177. // Expire sessions when their lifetime is up
  178. $expires = $this->_lifetime;
  179. }
  180. else
  181. {
  182. // Expire sessions after one month
  183. $expires = Date::MONTH;
  184. }
  185. // Delete all sessions that have expired
  186. DB::delete($this->_table)
  187. ->where($this->_columns['last_active'], '<', ':time')
  188. ->param(':time', time() - $expires)
  189. ->execute($this->_db);
  190. }
  191. } // End Session_Database