| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
class sfMySQLSessionStorage extends sfDatabaseSessionStorage |
|---|
| 25 |
{ |
|---|
| 26 |
|
|---|
| 27 |
* Destroys a session. |
|---|
| 28 |
* |
|---|
| 29 |
* @param string $id A session ID |
|---|
| 30 |
* |
|---|
| 31 |
* @return bool true, if the session was destroyed, otherwise an exception is thrown |
|---|
| 32 |
* |
|---|
| 33 |
* @throws <b>sfDatabaseException</b> If the session cannot be destroyed. |
|---|
| 34 |
*/ |
|---|
| 35 |
public function sessionDestroy($id) |
|---|
| 36 |
{ |
|---|
| 37 |
|
|---|
| 38 |
$db_table = $this->options['db_table']; |
|---|
| 39 |
$db_id_col = $this->options['db_id_col']; |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
$id = $this->db_escape($id); |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
$sql = "DELETE FROM $db_table WHERE $db_id_col = '$id'"; |
|---|
| 46 |
|
|---|
| 47 |
if ($this->db_query($sql)) |
|---|
| 48 |
{ |
|---|
| 49 |
return true; |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
throw new sfDatabaseException(sprintf('%s cannot destroy session id "%s" (%s).', get_class($this), $id, $this->db_error())); |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
* Cleans up old sessions. |
|---|
| 58 |
* |
|---|
| 59 |
* @param int $lifetime The lifetime of a session |
|---|
| 60 |
* |
|---|
| 61 |
* @return bool true, if old sessions have been cleaned, otherwise an exception is thrown |
|---|
| 62 |
* |
|---|
| 63 |
* @throws <b>sfDatabaseException</b> If any old sessions cannot be cleaned |
|---|
| 64 |
*/ |
|---|
| 65 |
public function sessionGC($lifetime) |
|---|
| 66 |
{ |
|---|
| 67 |
|
|---|
| 68 |
$db_table = $this->options['db_table']; |
|---|
| 69 |
$db_time_col = $this->options['db_time_col']; |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
$lifetime = $this->db_escape($lifetime); |
|---|
| 73 |
$sql = "DELETE FROM $db_table WHERE $db_time_col + $lifetime < UNIX_TIMESTAMP()"; |
|---|
| 74 |
|
|---|
| 75 |
if (!$this->db_query($sql)) |
|---|
| 76 |
{ |
|---|
| 77 |
throw new sfDatabaseException(sprintf('%s cannot delete old sessions (%s).', get_class($this), $this->db_error())); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
return true; |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
* Reads a session. |
|---|
| 85 |
* |
|---|
| 86 |
* @param string $id A session ID |
|---|
| 87 |
* |
|---|
| 88 |
* @return string The session data if the session was read or created, otherwise an exception is thrown |
|---|
| 89 |
* |
|---|
| 90 |
* @throws <b>sfDatabaseException</b> If the session cannot be read |
|---|
| 91 |
*/ |
|---|
| 92 |
public function sessionRead($id) |
|---|
| 93 |
{ |
|---|
| 94 |
|
|---|
| 95 |
$db_table = $this->options['db_table']; |
|---|
| 96 |
$db_data_col = $this->options['db_data_col']; |
|---|
| 97 |
$db_id_col = $this->options['db_id_col']; |
|---|
| 98 |
$db_time_col = $this->options['db_time_col']; |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
$id = $this->db_escape($id); |
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
$sql = "SELECT $db_data_col FROM $db_table WHERE $db_id_col = '$id'"; |
|---|
| 105 |
|
|---|
| 106 |
$result = $this->db_query($sql); |
|---|
| 107 |
|
|---|
| 108 |
if ($result != false && $this->db_num_rows($result) == 1) |
|---|
| 109 |
{ |
|---|
| 110 |
|
|---|
| 111 |
$data = $this->db_fetch_row($result); |
|---|
| 112 |
|
|---|
| 113 |
return $data[0]; |
|---|
| 114 |
} |
|---|
| 115 |
else |
|---|
| 116 |
{ |
|---|
| 117 |
|
|---|
| 118 |
$sql = "INSERT INTO $db_table ($db_id_col, $db_data_col, $db_time_col) VALUES ('$id', '', UNIX_TIMESTAMP())"; |
|---|
| 119 |
if ($this->db_query($sql)) |
|---|
| 120 |
{ |
|---|
| 121 |
return ''; |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
throw new sfDatabaseException(sprintf('%s cannot create new record for id "%s" (%s).', get_class($this), $id, $this->db_error())); |
|---|
| 126 |
} |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
* Writes session data. |
|---|
| 131 |
* |
|---|
| 132 |
* @param string $id A session ID |
|---|
| 133 |
* @param string $data A serialized chunk of session data |
|---|
| 134 |
* |
|---|
| 135 |
* @return bool true, if the session was written, otherwise an exception is thrown |
|---|
| 136 |
* |
|---|
| 137 |
* @throws <b>sfDatabaseException</b> If the session data cannot be written |
|---|
| 138 |
*/ |
|---|
| 139 |
public function sessionWrite($id, $data) |
|---|
| 140 |
{ |
|---|
| 141 |
|
|---|
| 142 |
$db_table = $this->options['db_table']; |
|---|
| 143 |
$db_data_col = $this->options['db_data_col']; |
|---|
| 144 |
$db_id_col = $this->options['db_id_col']; |
|---|
| 145 |
$db_time_col = $this->options['db_time_col']; |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
$id = $this->db_escape($id); |
|---|
| 149 |
$data = $this->db_escape($data); |
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
$sql = "UPDATE $db_table SET $db_data_col='$data', $db_time_col=UNIX_TIMESTAMP() WHERE $db_id_col='$id'"; |
|---|
| 153 |
|
|---|
| 154 |
if ($this->db_query($sql)) |
|---|
| 155 |
{ |
|---|
| 156 |
return true; |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
throw new sfDatabaseException(sprintf('%s cannot write session data for id "%s" (%s).', get_class($this), $id, $this->db_error())); |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
* Executes an SQL Query |
|---|
| 165 |
* |
|---|
| 166 |
* @param string $query The query to execute |
|---|
| 167 |
* @return mixed The result of the query |
|---|
| 168 |
*/ |
|---|
| 169 |
protected function db_query($query) |
|---|
| 170 |
{ |
|---|
| 171 |
return @mysql_query($query, $this->db); |
|---|
| 172 |
} |
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
* Escapes a string before using it in a query statement |
|---|
| 176 |
* |
|---|
| 177 |
* @param string $string The string to escape |
|---|
| 178 |
* @return string The escaped string |
|---|
| 179 |
*/ |
|---|
| 180 |
protected function db_escape($string) |
|---|
| 181 |
{ |
|---|
| 182 |
return mysql_real_escape_string($string, $this->db); |
|---|
| 183 |
} |
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
* Counts the rows in a query result |
|---|
| 187 |
* |
|---|
| 188 |
* @param resource $result Result of a query |
|---|
| 189 |
* @return int Number of rows |
|---|
| 190 |
*/ |
|---|
| 191 |
protected function db_num_rows($result) |
|---|
| 192 |
{ |
|---|
| 193 |
return mysql_num_rows($result); |
|---|
| 194 |
} |
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
* Extracts a row from a query result set |
|---|
| 198 |
* |
|---|
| 199 |
* @param resource $result Result of a query |
|---|
| 200 |
* @return array Extracted row as an indexed array |
|---|
| 201 |
*/ |
|---|
| 202 |
protected function db_fetch_row($result) |
|---|
| 203 |
{ |
|---|
| 204 |
return mysql_fetch_row($result); |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
* Returns the text of the error message from previous database operation |
|---|
| 209 |
* |
|---|
| 210 |
* @return string The error text from the last database function |
|---|
| 211 |
*/ |
|---|
| 212 |
protected function db_error() |
|---|
| 213 |
{ |
|---|
| 214 |
return mysql_error($this->db); |
|---|
| 215 |
} |
|---|
| 216 |
} |
|---|
| 217 |
|
|---|