sfDatabaseSessionStorage.class.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 15544 2009-02-16 21:46:00Z 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 and connection
  91. $databaseClass = get_class($database);
  92. if($databaseClass == 'sfPropelDatabase')
  93. {
  94. $this->db = Propel::getConnection();
  95. }
  96. elseif($databaseClass == 'sfDoctrineDatabase')
  97. {
  98. $this->db = $database->getConnection();
  99. }
  100. else
  101. {
  102. $this->db = $database->getResource();
  103. }
  104. $this->con = $database->getConnection();
  105. if (is_null($this->db) && is_null($this->con))
  106. {
  107. throw new sfDatabaseException('Database connection does not exist. Unable to open session.');
  108. }
  109. return true;
  110. }
  111. /**
  112. * Destroys a session.
  113. *
  114. * @param string $id A session ID
  115. *
  116. * @return bool true, if the session was destroyed, otherwise an exception is thrown
  117. *
  118. * @throws <b>DatabaseException</b> If the session cannot be destroyed
  119. */
  120. abstract public function sessionDestroy($id);
  121. /**
  122. * Cleans up old sessions.
  123. *
  124. * @param int $lifetime The lifetime of a session
  125. *
  126. * @return bool true, if old sessions have been cleaned, otherwise an exception is thrown
  127. *
  128. * @throws <b>DatabaseException</b> If any old sessions cannot be cleaned
  129. */
  130. abstract public function sessionGC($lifetime);
  131. /**
  132. * Reads a session.
  133. *
  134. * @param string $id A session ID
  135. *
  136. * @return bool true, if the session was read, otherwise an exception is thrown
  137. *
  138. * @throws <b>DatabaseException</b> If the session cannot be read
  139. */
  140. abstract public function sessionRead($id);
  141. /**
  142. * Writes session data.
  143. *
  144. * @param string $id A session ID
  145. * @param string $data A serialized chunk of session data
  146. *
  147. * @return bool true, if the session was written, otherwise an exception is thrown
  148. *
  149. * @throws <b>DatabaseException</b> If the session data cannot be written
  150. */
  151. abstract public function sessionWrite($id, $data);
  152. /**
  153. * Regenerates id that represents this storage.
  154. *
  155. * @param boolean $destroy Destroy session when regenerating?
  156. *
  157. * @return boolean True if session regenerated, false if error
  158. *
  159. */
  160. public function regenerate($destroy = false)
  161. {
  162. if (self::$sessionIdRegenerated)
  163. {
  164. return;
  165. }
  166. $currentId = session_id();
  167. parent::regenerate($destroy);
  168. $newId = session_id();
  169. $this->sessionRead($newId);
  170. return $this->sessionWrite($newId, $this->sessionRead($currentId));
  171. }
  172. /**
  173. * Executes the shutdown procedure.
  174. *
  175. */
  176. public function shutdown()
  177. {
  178. }
  179. }