PageRenderTime 47ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/symfony/2.0.0pr4/src/vendor/symfony/src/Symfony/Component/HttpFoundation/SessionStorage/PdoSessionStorage.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 222 lines | 112 code | 30 blank | 80 comment | 5 complexity | feff074a4e59593845bf6189302fda39 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  1. <?php
  2. namespace Symfony\Component\HttpFoundation\SessionStorage;
  3. /*
  4. * This file is part of the Symfony framework.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * PdoSessionStorage.
  13. *
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. */
  16. class PdoSessionStorage extends NativeSessionStorage
  17. {
  18. protected $db;
  19. /**
  20. * @throws \InvalidArgumentException When "db_table" option is not provided
  21. */
  22. public function __construct(\PDO $db, $options = null)
  23. {
  24. $this->db = $db;
  25. $options = array_merge(array(
  26. 'db_id_col' => 'sess_id',
  27. 'db_data_col' => 'sess_data',
  28. 'db_time_col' => 'sess_time',
  29. ), $options);
  30. if (!array_key_exists('db_table', $options)) {
  31. throw new \InvalidArgumentException('You must provide the "db_table" option for a PdoSessionStorage.');
  32. }
  33. parent::__construct($options);
  34. }
  35. /**
  36. * Starts the session.
  37. */
  38. public function start()
  39. {
  40. if (self::$sessionStarted) {
  41. return;
  42. }
  43. // use this object as the session handler
  44. session_set_save_handler(
  45. array($this, 'sessionOpen'),
  46. array($this, 'sessionClose'),
  47. array($this, 'sessionRead'),
  48. array($this, 'sessionWrite'),
  49. array($this, 'sessionDestroy'),
  50. array($this, 'sessionGC')
  51. );
  52. parent::start();
  53. }
  54. /**
  55. * Opens a session.
  56. *
  57. * @param string $path (ignored)
  58. * @param string $name (ignored)
  59. *
  60. * @return boolean true, if the session was opened, otherwise an exception is thrown
  61. */
  62. public function sessionOpen($path = null, $name = null)
  63. {
  64. return true;
  65. }
  66. /**
  67. * Closes a session.
  68. *
  69. * @return boolean true, if the session was closed, otherwise false
  70. */
  71. public function sessionClose()
  72. {
  73. // do nothing
  74. return true;
  75. }
  76. /**
  77. * Destroys a session.
  78. *
  79. * @param string $id A session ID
  80. *
  81. * @return bool true, if the session was destroyed, otherwise an exception is thrown
  82. *
  83. * @throws \RuntimeException If the session cannot be destroyed
  84. */
  85. public function sessionDestroy($id)
  86. {
  87. // get table/column
  88. $db_table = $this->options['db_table'];
  89. $db_id_col = $this->options['db_id_col'];
  90. // delete the record associated with this id
  91. $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.'= ?';
  92. try {
  93. $stmt = $this->db->prepare($sql);
  94. $stmt->bindParam(1, $id, \PDO::PARAM_STR);
  95. $stmt->execute();
  96. } catch (\PDOException $e) {
  97. throw new \RuntimeException(sprintf('PDOException was thrown when trying to manipulate session data: %s', $e->getMessage()), 0, $e);
  98. }
  99. return true;
  100. }
  101. /**
  102. * Cleans up old sessions.
  103. *
  104. * @param int $lifetime The lifetime of a session
  105. *
  106. * @return bool true, if old sessions have been cleaned, otherwise an exception is thrown
  107. *
  108. * @throws \RuntimeException If any old sessions cannot be cleaned
  109. */
  110. public function sessionGC($lifetime)
  111. {
  112. // get table/column
  113. $db_table = $this->options['db_table'];
  114. $db_time_col = $this->options['db_time_col'];
  115. // delete the record associated with this id
  116. $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '.(time() - $lifetime);
  117. try {
  118. $this->db->query($sql);
  119. } catch (\PDOException $e) {
  120. throw new \RuntimeException(sprintf('PDOException was thrown when trying to manipulate session data: %s', $e->getMessage()), 0, $e);
  121. }
  122. return true;
  123. }
  124. /**
  125. * Reads a session.
  126. *
  127. * @param string $id A session ID
  128. *
  129. * @return string The session data if the session was read or created, otherwise an exception is thrown
  130. *
  131. * @throws \RuntimeException If the session cannot be read
  132. */
  133. public function sessionRead($id)
  134. {
  135. // get table/columns
  136. $db_table = $this->options['db_table'];
  137. $db_data_col = $this->options['db_data_col'];
  138. $db_id_col = $this->options['db_id_col'];
  139. $db_time_col = $this->options['db_time_col'];
  140. try {
  141. $sql = 'SELECT '.$db_data_col.' FROM '.$db_table.' WHERE '.$db_id_col.'=?';
  142. $stmt = $this->db->prepare($sql);
  143. $stmt->bindParam(1, $id, \PDO::PARAM_STR, 255);
  144. $stmt->execute();
  145. // it is recommended to use fetchAll so that PDO can close the DB cursor
  146. // we anyway expect either no rows, or one row with one column. fetchColumn, seems to be buggy #4777
  147. $sessionRows = $stmt->fetchAll(\PDO::FETCH_NUM);
  148. if (count($sessionRows) == 1) {
  149. return $sessionRows[0][0];
  150. } else {
  151. // session does not exist, create it
  152. $sql = 'INSERT INTO '.$db_table.'('.$db_id_col.', '.$db_data_col.', '.$db_time_col.') VALUES (?, ?, ?)';
  153. $stmt = $this->db->prepare($sql);
  154. $stmt->bindParam(1, $id, \PDO::PARAM_STR);
  155. $stmt->bindValue(2, '', \PDO::PARAM_STR);
  156. $stmt->bindValue(3, time(), \PDO::PARAM_INT);
  157. $stmt->execute();
  158. return '';
  159. }
  160. } catch (\PDOException $e) {
  161. throw new \RuntimeException(sprintf('PDOException was thrown when trying to manipulate session data: %s', $e->getMessage()), 0, $e);
  162. }
  163. }
  164. /**
  165. * Writes session data.
  166. *
  167. * @param string $id A session ID
  168. * @param string $data A serialized chunk of session data
  169. *
  170. * @return bool true, if the session was written, otherwise an exception is thrown
  171. *
  172. * @throws \RuntimeException If the session data cannot be written
  173. */
  174. public function sessionWrite($id, $data)
  175. {
  176. // get table/column
  177. $db_table = $this->options['db_table'];
  178. $db_data_col = $this->options['db_data_col'];
  179. $db_id_col = $this->options['db_id_col'];
  180. $db_time_col = $this->options['db_time_col'];
  181. $sql = 'UPDATE '.$db_table.' SET '.$db_data_col.' = ?, '.$db_time_col.' = '.time().' WHERE '.$db_id_col.'= ?';
  182. try {
  183. $stmt = $this->db->prepare($sql);
  184. $stmt->bindParam(1, $data, \PDO::PARAM_STR);
  185. $stmt->bindParam(2, $id, \PDO::PARAM_STR);
  186. $stmt->execute();
  187. } catch (\PDOException $e) {
  188. throw new \RuntimeException(sprintf('PDOException was thrown when trying to manipulate session data: %s', $e->getMessage()), 0, $e);
  189. }
  190. return true;
  191. }
  192. }