PageRenderTime 65ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/Ahineya/trn_dev
PHP | 239 lines | 144 code | 40 blank | 55 comment | 12 complexity | 6614706fe9472c127b3e9f71eb9f1370 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. /**
  149. * @return bool
  150. */
  151. protected function _restart()
  152. {
  153. $this->_regenerate();
  154. return TRUE;
  155. }
  156. protected function _destroy()
  157. {
  158. if ($this->_update_id === NULL)
  159. {
  160. // Session has not been created yet
  161. return TRUE;
  162. }
  163. // Delete the current session
  164. $query = DB::delete($this->_table)
  165. ->where($this->_columns['session_id'], '=', ':id')
  166. ->param(':id', $this->_update_id);
  167. try
  168. {
  169. // Execute the query
  170. $query->execute($this->_db);
  171. // Delete the cookie
  172. Cookie::delete($this->_name);
  173. }
  174. catch (Exception $e)
  175. {
  176. // An error occurred, the session has not been deleted
  177. return FALSE;
  178. }
  179. return TRUE;
  180. }
  181. protected function _gc()
  182. {
  183. if ($this->_lifetime)
  184. {
  185. // Expire sessions when their lifetime is up
  186. $expires = $this->_lifetime;
  187. }
  188. else
  189. {
  190. // Expire sessions after one month
  191. $expires = Date::MONTH;
  192. }
  193. // Delete all sessions that have expired
  194. DB::delete($this->_table)
  195. ->where($this->_columns['last_active'], '<', ':time')
  196. ->param(':time', time() - $expires)
  197. ->execute($this->_db);
  198. }
  199. } // End Session_Database