sfPostgreSQLSessionStorage.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. * Provides support for session storage using a PostgreSQL brand database.
  12. *
  13. * <b>parameters:</b> see sfDatabaseSessionStorage
  14. *
  15. * @package symfony
  16. * @subpackage storage
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. * @author Sean Kerr <sean@code-box.org>
  19. * @version SVN: $Id: sfPostgreSQLSessionStorage.class.php 10589 2008-08-01 16:00:48Z nicolas $
  20. */
  21. class sfPostgreSQLSessionStorage extends sfDatabaseSessionStorage
  22. {
  23. /**
  24. * Destroys a session.
  25. *
  26. * @param string $id A session ID
  27. *
  28. * @return bool true, if the session was destroyed, otherwise an exception is thrown
  29. *
  30. * @throws <b>sfDatabaseException</b> If the session cannot be destroyed
  31. */
  32. public function sessionDestroy($id)
  33. {
  34. // get table/column
  35. $db_table = $this->options['db_table'];
  36. $db_id_col = $this->options['db_id_col'];
  37. // cleanup the session id, just in case
  38. $id = addslashes($id);
  39. // delete the record associated with this id
  40. $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.' = \''.$id.'\'';
  41. if (@pg_query($this->db, $sql))
  42. {
  43. return true;
  44. }
  45. // failed to destroy session
  46. throw new sfDatabaseException(sprintf('sfPostgreSQLSessionStorage cannot destroy session id "%s".', $id));
  47. }
  48. /**
  49. * Cleans up old sessions.
  50. *
  51. * @param int $lifetime The lifetime of a session
  52. *
  53. * @return bool true, if old sessions have been cleaned, otherwise an exception is thrown
  54. *
  55. * @throws <b>sfDatabaseException</b> If any old sessions cannot be cleaned
  56. */
  57. public function sessionGC($lifetime)
  58. {
  59. // get table/column
  60. $db_table = $this->options['db_table'];
  61. $db_time_col = $this->options['db_time_col'];
  62. // delete the record associated with this id
  63. $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '.(time() - $lifetime);
  64. if (!@pg_query($this->db, $sql))
  65. {
  66. throw new sfDatabaseException('sfPostgreSQLSessionStorage cannot delete old sessions.');
  67. }
  68. return true;
  69. }
  70. /**
  71. * Reads a session.
  72. *
  73. * @param string $id A session ID
  74. *
  75. * @return string The session data if the session was read or created, otherwise an exception is thrown
  76. *
  77. * @throws <b>sfDatabaseException</b> If the session cannot be read
  78. */
  79. public function sessionRead($id)
  80. {
  81. // get table/column
  82. $db_table = $this->options['db_table'];
  83. $db_data_col = $this->options['db_data_col'];
  84. $db_id_col = $this->options['db_id_col'];
  85. $db_time_col = $this->options['db_time_col'];
  86. // cleanup the session id, just in case
  87. $id = addslashes($id);
  88. // delete the record associated with this id
  89. $sql = 'SELECT '.$db_data_col.' FROM '.$db_table.' WHERE '.$db_id_col.' = \''.$id.'\'';
  90. $result = @pg_query($this->db, $sql);
  91. if ($result != false && @pg_num_rows($result) == 1)
  92. {
  93. // found the session
  94. $data = pg_fetch_row($result);
  95. return $data[0];
  96. }
  97. else
  98. {
  99. // session does not exist, create it
  100. $sql = 'INSERT INTO '.$db_table.' ('.$db_id_col.', '.$db_data_col.', '.$db_time_col.') VALUES (\''.$id.'\', \'\', '.time().')';
  101. if (@pg_query($this->db, $sql))
  102. {
  103. return '';
  104. }
  105. // can't create record
  106. throw new sfDatabaseException(sprintf('sfPostgreSQLSessionStorage cannot create new record for id "%s".', $id));
  107. }
  108. }
  109. /**
  110. * Writes session data.
  111. *
  112. * @param string $id A session ID
  113. * @param string $data A serialized chunk of session data
  114. *
  115. * @return bool true, if the session was written, otherwise an exception is thrown
  116. *
  117. * @throws <b>sfDatabaseException</b> If the session data cannot be written
  118. */
  119. public function sessionWrite($id, $data)
  120. {
  121. // get table/column
  122. $db_table = $this->options['db_table'];
  123. $db_data_col = $this->options['db_data_col'];
  124. $db_id_col = $this->options['db_id_col'];
  125. $db_time_col = $this->options['db_time_col'];
  126. // cleanup the session id and data, just in case
  127. $id = addslashes($id);
  128. $data = addslashes($data);
  129. // delete the record associated with this id
  130. $sql = 'UPDATE '.$db_table.' SET '.$db_data_col.' = \''.$data.'\', '.$db_time_col.' = '.time().' WHERE '.$db_id_col.' = \''.$id.'\'';
  131. if (@pg_query($this->db, $sql))
  132. {
  133. return true;
  134. }
  135. // failed to write session data
  136. throw new sfDatabaseException(sprintf('sfPostgreSQLSessionStorage cannot write session data for id "%s".', $id));
  137. }
  138. }