1. sfPostgreSQLSessionStorage.class.php
  2. /** * Provides support for session storage using a PostgreSQL brand database. * * parameters: see sfDatabaseSessionStorage * * @package symfony * @subpackage storage * @author Fabien Potencier * @author Sean Kerr * @version SVN: $Id: sfPostgreSQLSessionStorage.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $ */
  3. class sfPostgreSQLSessionStorage 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 = addslashes($id);
  21. // delete the record associated with this id
  22. $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.' = \''.$id.'\'';
  23. if (@pg_query($this->db, $sql))
  24. {
  25. return true;
  26. }
  27. // failed to destroy session
  28. throw new sfDatabaseException(sprintf('sfPostgreSQLSessionStorage cannot destroy session id "%s".', $id));
  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 associated with this id
  45. $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '.(time() - $lifetime);
  46. if (!@pg_query($this->db, $sql))
  47. {
  48. throw new sfDatabaseException('sfPostgreSQLSessionStorage cannot delete old sessions.');
  49. }
  50. return true;
  51. }
  52. /**
  53. * Reads a session.
  54. *
  55. * @param string $id A session ID
  56. *
  57. * @return string The session data if the session was read or created, otherwise an exception is thrown
  58. *
  59. * @throws <b>sfDatabaseException</b> If the session cannot be read
  60. */
  61. public function sessionRead($id)
  62. {
  63. // get table/column
  64. $db_table = $this->options['db_table'];
  65. $db_data_col = $this->options['db_data_col'];
  66. $db_id_col = $this->options['db_id_col'];
  67. $db_time_col = $this->options['db_time_col'];
  68. // cleanup the session id, just in case
  69. $id = addslashes($id);
  70. // delete the record associated with this id
  71. $sql = 'SELECT '.$db_data_col.' FROM '.$db_table.' WHERE '.$db_id_col.' = \''.$id.'\'';
  72. $result = @pg_query($this->db, $sql);
  73. if ($result != false && @pg_num_rows($result) == 1)
  74. {
  75. // found the session
  76. $data = pg_fetch_row($result);
  77. return $data[0];
  78. }
  79. else
  80. {
  81. // session does not exist, create it
  82. $sql = 'INSERT INTO '.$db_table.' ('.$db_id_col.', '.$db_data_col.', '.$db_time_col.') VALUES (\''.$id.'\', \'\', '.time().')';
  83. if (@pg_query($this->db, $sql))
  84. {
  85. return '';
  86. }
  87. // can't create record
  88. throw new sfDatabaseException(sprintf('sfPostgreSQLSessionStorage cannot create new record for id "%s".', $id));
  89. }
  90. }
  91. /**
  92. * Writes session data.
  93. *
  94. * @param string $id A session ID
  95. * @param string $data A serialized chunk of session data
  96. *
  97. * @return bool true, if the session was written, otherwise an exception is thrown
  98. *
  99. * @throws <b>sfDatabaseException</b> If the session data cannot be written
  100. */
  101. public function sessionWrite($id, $data)
  102. {
  103. // get table/column
  104. $db_table = $this->options['db_table'];
  105. $db_data_col = $this->options['db_data_col'];
  106. $db_id_col = $this->options['db_id_col'];
  107. $db_time_col = $this->options['db_time_col'];
  108. // cleanup the session id and data, just in case
  109. $id = addslashes($id);
  110. $data = addslashes($data);
  111. // delete the record associated with this id
  112. $sql = 'UPDATE '.$db_table.' SET '.$db_data_col.' = \''.$data.'\', '.$db_time_col.' = '.time().' WHERE '.$db_id_col.' = \''.$id.'\'';
  113. if (@pg_query($this->db, $sql))
  114. {
  115. return true;
  116. }
  117. // failed to write session data
  118. throw new sfDatabaseException(sprintf('sfPostgreSQLSessionStorage cannot write session data for id "%s".', $id));
  119. }
  120. }

Debug toolbar