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

Debug toolbar