sfDatabaseSessionStorage.class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) 2004-2006 Sean Kerr <sean@code-box.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Base class for all sfStorage that uses a sfDatabase object as a storage.
  12. *
  13. * @package symfony
  14. * @subpackage storage
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. * @author Sean Kerr <sean@code-box.org>
  17. * @version SVN: $Id: sfDatabaseSessionStorage.class.php 17521 2009-04-22 07:49:24Z dwhittle $
  18. */
  19. abstract class sfDatabaseSessionStorage extends sfSessionStorage
  20. {
  21. protected
  22. $db = null,
  23. $con = null;
  24. /**
  25. * Available options:
  26. *
  27. * * db_table: The database table in which session data will be stored
  28. * * database: The sfDatabase object to use
  29. * * db_id_col: The database column in which the session id will be stored (sess_id by default)
  30. * * db_data_col: The database column in which the session data will be stored (sess_data by default)
  31. * * db_time_col: The database column in which the session timestamp will be stored (sess_time by default)
  32. *
  33. * @param array $options An associative array of options
  34. *
  35. * @see sfSessionStorage
  36. */
  37. public function initialize($options = array())
  38. {
  39. $options = array_merge(array(
  40. 'db_id_col' => 'sess_id',
  41. 'db_data_col' => 'sess_data',
  42. 'db_time_col' => 'sess_time',
  43. ), $options);
  44. // disable auto_start
  45. $options['auto_start'] = false;
  46. // initialize the parent
  47. parent::initialize($options);
  48. if (!isset($this->options['db_table']))
  49. {
  50. throw new sfInitializationException('You must provide a "db_table" option to sfDatabaseSessionStorage.');
  51. }
  52. if (!isset($this->options['database']))
  53. {
  54. throw new sfInitializationException('You must provide a "database" option to sfDatabaseSessionStorage.');
  55. }
  56. // use this object as the session handler
  57. session_set_save_handler(array($this, 'sessionOpen'),
  58. array($this, 'sessionClose'),
  59. array($this, 'sessionRead'),
  60. array($this, 'sessionWrite'),
  61. array($this, 'sessionDestroy'),
  62. array($this, 'sessionGC'));
  63. // start our session
  64. session_start();
  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. * Opens a session.
  78. *
  79. * @param string $path (ignored)
  80. * @param string $name (ignored)
  81. *
  82. * @return boolean true, if the session was opened, otherwise an exception is thrown
  83. *
  84. * @throws <b>DatabaseException</b> If a connection with the database does not exist or cannot be created
  85. */
  86. public function sessionOpen($path = null, $name = null)
  87. {
  88. // what database are we using?
  89. $database = $this->options['database'];
  90. // get the database resource
  91. $this->db = $database->getResource();
  92. $this->con = $database->getConnection();
  93. if (is_null($this->db) && is_null($this->con))
  94. {
  95. throw new sfDatabaseException('Database connection doesn\'t exist. Unable to open session.');
  96. }
  97. return true;
  98. }
  99. /**
  100. * Destroys a session.
  101. *
  102. * @param string $id A session ID
  103. *
  104. * @return bool true, if the session was destroyed, otherwise an exception is thrown
  105. *
  106. * @throws <b>DatabaseException</b> If the session cannot be destroyed
  107. */
  108. abstract public function sessionDestroy($id);
  109. /**
  110. * Cleans up old sessions.
  111. *
  112. * @param int $lifetime The lifetime of a session
  113. *
  114. * @return bool true, if old sessions have been cleaned, otherwise an exception is thrown
  115. *
  116. * @throws <b>DatabaseException</b> If any old sessions cannot be cleaned
  117. */
  118. abstract public function sessionGC($lifetime);
  119. /**
  120. * Reads a session.
  121. *
  122. * @param string $id A session ID
  123. *
  124. * @return bool true, if the session was read, otherwise an exception is thrown
  125. *
  126. * @throws <b>DatabaseException</b> If the session cannot be read
  127. */
  128. abstract public function sessionRead($id);
  129. /**
  130. * Writes session data.
  131. *
  132. * @param string $id A session ID
  133. * @param string $data A serialized chunk of session data
  134. *
  135. * @return bool true, if the session was written, otherwise an exception is thrown
  136. *
  137. * @throws <b>DatabaseException</b> If the session data cannot be written
  138. */
  139. abstract public function sessionWrite($id, $data);
  140. /**
  141. * Regenerates id that represents this storage.
  142. *
  143. * @param boolean $destroy Destroy session when regenerating?
  144. *
  145. * @return boolean True if session regenerated, false if error
  146. *
  147. */
  148. public function regenerate($destroy = false)
  149. {
  150. if (self::$sessionIdRegenerated)
  151. {
  152. return;
  153. }
  154. $currentId = session_id();
  155. parent::regenerate($destroy);
  156. $newId = session_id();
  157. $this->sessionRead($newId);
  158. return $this->sessionWrite($newId, $this->sessionRead($currentId));
  159. }
  160. /**
  161. * Executes the shutdown procedure.
  162. *
  163. */
  164. public function shutdown()
  165. {
  166. parent::shutdown();
  167. }
  168. }