| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
class sfPostgreSQLSessionStorage extends sfDatabaseSessionStorage |
|---|
| 24 |
{ |
|---|
| 25 |
|
|---|
| 26 |
* Destroys a session. |
|---|
| 27 |
* |
|---|
| 28 |
* @param string $id A session ID |
|---|
| 29 |
* |
|---|
| 30 |
* @return bool true, if the session was destroyed, otherwise an exception is thrown |
|---|
| 31 |
* |
|---|
| 32 |
* @throws <b>sfDatabaseException</b> If the session cannot be destroyed |
|---|
| 33 |
*/ |
|---|
| 34 |
public function sessionDestroy($id) |
|---|
| 35 |
{ |
|---|
| 36 |
|
|---|
| 37 |
$db_table = $this->options['db_table']; |
|---|
| 38 |
$db_id_col = $this->options['db_id_col']; |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
$id = addslashes($id); |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
$sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.' = \''.$id.'\''; |
|---|
| 45 |
|
|---|
| 46 |
if (@pg_query($this->db, $sql)) |
|---|
| 47 |
{ |
|---|
| 48 |
return true; |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
throw new sfDatabaseException(sprintf('sfPostgreSQLSessionStorage cannot destroy session id "%s".', $id)); |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
* Cleans up old sessions. |
|---|
| 57 |
* |
|---|
| 58 |
* @param int $lifetime The lifetime of a session |
|---|
| 59 |
* |
|---|
| 60 |
* @return bool true, if old sessions have been cleaned, otherwise an exception is thrown |
|---|
| 61 |
* |
|---|
| 62 |
* @throws <b>sfDatabaseException</b> If any old sessions cannot be cleaned |
|---|
| 63 |
*/ |
|---|
| 64 |
public function sessionGC($lifetime) |
|---|
| 65 |
{ |
|---|
| 66 |
|
|---|
| 67 |
$db_table = $this->options['db_table']; |
|---|
| 68 |
$db_time_col = $this->options['db_time_col']; |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
$sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '.(time() - $lifetime); |
|---|
| 72 |
|
|---|
| 73 |
if (!@pg_query($this->db, $sql)) |
|---|
| 74 |
{ |
|---|
| 75 |
throw new sfDatabaseException('sfPostgreSQLSessionStorage cannot delete old sessions.'); |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
return true; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
* Reads a session. |
|---|
| 83 |
* |
|---|
| 84 |
* @param string $id A session ID |
|---|
| 85 |
* |
|---|
| 86 |
* @return string The session data if the session was read or created, otherwise an exception is thrown |
|---|
| 87 |
* |
|---|
| 88 |
* @throws <b>sfDatabaseException</b> If the session cannot be read |
|---|
| 89 |
*/ |
|---|
| 90 |
public function sessionRead($id) |
|---|
| 91 |
{ |
|---|
| 92 |
|
|---|
| 93 |
$db_table = $this->options['db_table']; |
|---|
| 94 |
$db_data_col = $this->options['db_data_col']; |
|---|
| 95 |
$db_id_col = $this->options['db_id_col']; |
|---|
| 96 |
$db_time_col = $this->options['db_time_col']; |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
$id = addslashes($id); |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
$sql = 'SELECT '.$db_data_col.' FROM '.$db_table.' WHERE '.$db_id_col.' = \''.$id.'\''; |
|---|
| 103 |
|
|---|
| 104 |
$result = @pg_query($this->db, $sql); |
|---|
| 105 |
|
|---|
| 106 |
if ($result != false && @pg_num_rows($result) == 1) |
|---|
| 107 |
{ |
|---|
| 108 |
|
|---|
| 109 |
$data = pg_fetch_row($result); |
|---|
| 110 |
|
|---|
| 111 |
return $data[0]; |
|---|
| 112 |
} |
|---|
| 113 |
else |
|---|
| 114 |
{ |
|---|
| 115 |
|
|---|
| 116 |
$sql = 'INSERT INTO '.$db_table.' ('.$db_id_col.', '.$db_data_col.', '.$db_time_col.') VALUES (\''.$id.'\', \'\', '.time().')'; |
|---|
| 117 |
|
|---|
| 118 |
if (@pg_query($this->db, $sql)) |
|---|
| 119 |
{ |
|---|
| 120 |
return ''; |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
throw new sfDatabaseException(sprintf('sfPostgreSQLSessionStorage cannot create new record for id "%s".', $id)); |
|---|
| 125 |
} |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
* Writes session data. |
|---|
| 130 |
* |
|---|
| 131 |
* @param string $id A session ID |
|---|
| 132 |
* @param string $data A serialized chunk of session data |
|---|
| 133 |
* |
|---|
| 134 |
* @return bool true, if the session was written, otherwise an exception is thrown |
|---|
| 135 |
* |
|---|
| 136 |
* @throws <b>sfDatabaseException</b> If the session data cannot be written |
|---|
| 137 |
*/ |
|---|
| 138 |
public function sessionWrite($id, $data) |
|---|
| 139 |
{ |
|---|
| 140 |
|
|---|
| 141 |
$db_table = $this->options['db_table']; |
|---|
| 142 |
$db_data_col = $this->options['db_data_col']; |
|---|
| 143 |
$db_id_col = $this->options['db_id_col']; |
|---|
| 144 |
$db_time_col = $this->options['db_time_col']; |
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
$id = addslashes($id); |
|---|
| 148 |
$data = addslashes($data); |
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
$sql = 'UPDATE '.$db_table.' SET '.$db_data_col.' = \''.$data.'\', '.$db_time_col.' = '.time().' WHERE '.$db_id_col.' = \''.$id.'\''; |
|---|
| 152 |
|
|---|
| 153 |
if (@pg_query($this->db, $sql)) |
|---|
| 154 |
{ |
|---|
| 155 |
return true; |
|---|
| 156 |
} |
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
throw new sfDatabaseException(sprintf('sfPostgreSQLSessionStorage cannot write session data for id "%s".', $id)); |
|---|
| 160 |
} |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|