1. sfMySQLSessionStorage.class.php
  2. /** * Provides support for session storage using a MySQL brand database. * * parameters: see sfDatabaseSessionStorage * * @package symfony * @subpackage storage * @author Fabien Potencier * @author Sean Kerr * @author Julien Garand * @version SVN: $Id: sfMySQLSessionStorage.class.php 24590 2009-11-30 18:28:13Z FabianLange $ */
  3. class sfMySQLSessionStorage extends sfDatabaseSessionStorage
  4. {
  5. /**
  6. * Destroys a session.
  7. *
  8. * @param string $id A session ID
  9. *
  10. * @return bool true, if the session was destroyed, otherwise an exception is thrown
  11. *
  12. * @throws <b>sfDatabaseException</b> If the session cannot be destroyed.
  13. */
  14. public function sessionDestroy($id)
  15. {
  16. // get table/column
  17. $db_table = $this->options['db_table'];
  18. $db_id_col = $this->options['db_id_col'];
  19. // cleanup the session id, just in case
  20. $id = $this->db_escape($id);
  21. // delete the record associated with this id
  22. $sql = "DELETE FROM $db_table WHERE $db_id_col = '$id'";
  23. if ($this->db_query($sql))
  24. {
  25. return true;
  26. }
  27. // failed to destroy session
  28. throw new sfDatabaseException(sprintf('%s cannot destroy session id "%s" (%s).', get_class($this), $id, $this->db_error()));
  29. }
  30. /**
  31. * Cleans up old sessions.
  32. *
  33. * @param int $lifetime The lifetime of a session
  34. *
  35. * @return bool true, if old sessions have been cleaned, otherwise an exception is thrown
  36. *
  37. * @throws <b>sfDatabaseException</b> If any old sessions cannot be cleaned
  38. */
  39. public function sessionGC($lifetime)
  40. {
  41. // get table/column
  42. $db_table = $this->options['db_table'];
  43. $db_time_col = $this->options['db_time_col'];
  44. // delete the record older than the authorised session life time
  45. $lifetime = $this->db_escape($lifetime); // We never know...
  46. $sql = "DELETE FROM $db_table WHERE $db_time_col + $lifetime < UNIX_TIMESTAMP()";
  47. if (!$this->db_query($sql))
  48. {
  49. throw new sfDatabaseException(sprintf('%s cannot delete old sessions (%s).', get_class($this), $this->db_error()));
  50. }
  51. return true;
  52. }
  53. /**
  54. * Reads a session.
  55. *
  56. * @param string $id A session ID
  57. *
  58. * @return string The session data if the session was read or created, otherwise an exception is thrown
  59. *
  60. * @throws <b>sfDatabaseException</b> If the session cannot be read
  61. */
  62. public function sessionRead($id)
  63. {
  64. // get table/column
  65. $db_table = $this->options['db_table'];
  66. $db_data_col = $this->options['db_data_col'];
  67. $db_id_col = $this->options['db_id_col'];
  68. $db_time_col = $this->options['db_time_col'];
  69. // cleanup the session id, just in case
  70. $id = $this->db_escape($id);
  71. // get the record associated with this id
  72. $sql = "SELECT $db_data_col FROM $db_table WHERE $db_id_col = '$id'";
  73. $result = $this->db_query($sql);
  74. if ($result != false && $this->db_num_rows($result) == 1)
  75. {
  76. // found the session
  77. $data = $this->db_fetch_row($result);
  78. return $data[0];
  79. }
  80. else
  81. {
  82. // session does not exist, create it
  83. $sql = "INSERT INTO $db_table ($db_id_col, $db_data_col, $db_time_col) VALUES ('$id', '', UNIX_TIMESTAMP())";
  84. if ($this->db_query($sql))
  85. {
  86. return '';
  87. }
  88. // can't create record
  89. throw new sfDatabaseException(sprintf('%s cannot create new record for id "%s" (%s).', get_class($this), $id, $this->db_error()));
  90. }
  91. }
  92. /**
  93. * Writes session data.
  94. *
  95. * @param string $id A session ID
  96. * @param string $data A serialized chunk of session data
  97. *
  98. * @return bool true, if the session was written, otherwise an exception is thrown
  99. *
  100. * @throws <b>sfDatabaseException</b> If the session data cannot be written
  101. */
  102. public function sessionWrite($id, $data)
  103. {
  104. // get table/column
  105. $db_table = $this->options['db_table'];
  106. $db_data_col = $this->options['db_data_col'];
  107. $db_id_col = $this->options['db_id_col'];
  108. $db_time_col = $this->options['db_time_col'];
  109. // cleanup the session id and data, just in case
  110. $id = $this->db_escape($id);
  111. $data = $this->db_escape($data);
  112. // update the record associated with this id
  113. $sql = "UPDATE $db_table SET $db_data_col='$data', $db_time_col=UNIX_TIMESTAMP() WHERE $db_id_col='$id'";
  114. if ($this->db_query($sql))
  115. {
  116. return true;
  117. }
  118. // failed to write session data
  119. throw new sfDatabaseException(sprintf('%s cannot write session data for id "%s" (%s).', get_class($this), $id, $this->db_error()));
  120. }
  121. /**
  122. * Executes an SQL Query
  123. *
  124. * @param string $query The query to execute
  125. * @return mixed The result of the query
  126. */
  127. protected function db_query($query)
  128. {
  129. return @mysql_query($query, $this->db);
  130. }
  131. /**
  132. * Escapes a string before using it in a query statement
  133. *
  134. * @param string $string The string to escape
  135. * @return string The escaped string
  136. */
  137. protected function db_escape($string)
  138. {
  139. return mysql_real_escape_string($string, $this->db);
  140. }
  141. /**
  142. * Counts the rows in a query result
  143. *
  144. * @param resource $result Result of a query
  145. * @return int Number of rows
  146. */
  147. protected function db_num_rows($result)
  148. {
  149. return mysql_num_rows($result);
  150. }
  151. /**
  152. * Extracts a row from a query result set
  153. *
  154. * @param resource $result Result of a query
  155. * @return array Extracted row as an indexed array
  156. */
  157. protected function db_fetch_row($result)
  158. {
  159. return mysql_fetch_row($result);
  160. }
  161. /**
  162. * Returns the text of the error message from previous database operation
  163. *
  164. * @return string The error text from the last database function
  165. */
  166. protected function db_error()
  167. {
  168. return mysql_error($this->db);
  169. }
  170. }

Debug toolbar