| 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 sfPDOSessionStorage 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>DatabaseException</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 |
$sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.'= ?'; |
|---|
| 43 |
|
|---|
| 44 |
try |
|---|
| 45 |
{ |
|---|
| 46 |
$stmt = $this->db->prepare($sql); |
|---|
| 47 |
$stmt->bindParam(1, $id, PDO::PARAM_STR); |
|---|
| 48 |
$stmt->execute(); |
|---|
| 49 |
} |
|---|
| 50 |
catch (PDOException $e) |
|---|
| 51 |
{ |
|---|
| 52 |
throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage())); |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
return true; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
* Cleans up old sessions. |
|---|
| 60 |
* |
|---|
| 61 |
* @param int $lifetime The lifetime of a session |
|---|
| 62 |
* |
|---|
| 63 |
* @return bool true, if old sessions have been cleaned, otherwise an exception is thrown |
|---|
| 64 |
* |
|---|
| 65 |
* @throws <b>DatabaseException</b> If any old sessions cannot be cleaned |
|---|
| 66 |
*/ |
|---|
| 67 |
public function sessionGC($lifetime) |
|---|
| 68 |
{ |
|---|
| 69 |
|
|---|
| 70 |
$db_table = $this->options['db_table']; |
|---|
| 71 |
$db_time_col = $this->options['db_time_col']; |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
$sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '.(time() - $lifetime); |
|---|
| 75 |
|
|---|
| 76 |
try |
|---|
| 77 |
{ |
|---|
| 78 |
$this->db->query($sql); |
|---|
| 79 |
} |
|---|
| 80 |
catch (PDOException $e) |
|---|
| 81 |
{ |
|---|
| 82 |
throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage())); |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
return true; |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
* Reads a session. |
|---|
| 90 |
* |
|---|
| 91 |
* @param string $id A session ID |
|---|
| 92 |
* |
|---|
| 93 |
* @return string The session data if the session was read or created, otherwise an exception is thrown |
|---|
| 94 |
* |
|---|
| 95 |
* @throws <b>DatabaseException</b> If the session cannot be read |
|---|
| 96 |
*/ |
|---|
| 97 |
public function sessionRead($id) |
|---|
| 98 |
{ |
|---|
| 99 |
|
|---|
| 100 |
$db_table = $this->options['db_table']; |
|---|
| 101 |
$db_data_col = $this->options['db_data_col']; |
|---|
| 102 |
$db_id_col = $this->options['db_id_col']; |
|---|
| 103 |
$db_time_col = $this->options['db_time_col']; |
|---|
| 104 |
|
|---|
| 105 |
try |
|---|
| 106 |
{ |
|---|
| 107 |
$sql = 'SELECT '.$db_data_col.' FROM '.$db_table.' WHERE '.$db_id_col.'=?'; |
|---|
| 108 |
|
|---|
| 109 |
$stmt = $this->db->prepare($sql); |
|---|
| 110 |
$stmt->bindParam(1, $id, PDO::PARAM_STR, 255); |
|---|
| 111 |
|
|---|
| 112 |
$stmt->execute(); |
|---|
| 113 |
if ($data = $stmt->fetchColumn()) |
|---|
| 114 |
{ |
|---|
| 115 |
return $data; |
|---|
| 116 |
} |
|---|
| 117 |
else |
|---|
| 118 |
{ |
|---|
| 119 |
|
|---|
| 120 |
$sql = 'INSERT INTO '.$db_table.'('.$db_id_col.', '.$db_data_col.', '.$db_time_col.') VALUES (?, ?, ?)'; |
|---|
| 121 |
|
|---|
| 122 |
$stmt = $this->db->prepare($sql); |
|---|
| 123 |
$stmt->bindParam(1, $id, PDO::PARAM_STR); |
|---|
| 124 |
$stmt->bindValue(2, '', PDO::PARAM_STR); |
|---|
| 125 |
$stmt->bindValue(3, time(), PDO::PARAM_INT); |
|---|
| 126 |
$stmt->execute(); |
|---|
| 127 |
|
|---|
| 128 |
return ''; |
|---|
| 129 |
} |
|---|
| 130 |
} |
|---|
| 131 |
catch (PDOException $e) |
|---|
| 132 |
{ |
|---|
| 133 |
throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage())); |
|---|
| 134 |
} |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
* Writes session data. |
|---|
| 139 |
* |
|---|
| 140 |
* @param string $id A session ID |
|---|
| 141 |
* @param string $data A serialized chunk of session data |
|---|
| 142 |
* |
|---|
| 143 |
* @return bool true, if the session was written, otherwise an exception is thrown |
|---|
| 144 |
* |
|---|
| 145 |
* @throws <b>DatabaseException</b> If the session data cannot be written |
|---|
| 146 |
*/ |
|---|
| 147 |
public function sessionWrite($id, $data) |
|---|
| 148 |
{ |
|---|
| 149 |
|
|---|
| 150 |
$db_table = $this->options['db_table']; |
|---|
| 151 |
$db_data_col = $this->options['db_data_col']; |
|---|
| 152 |
$db_id_col = $this->options['db_id_col']; |
|---|
| 153 |
$db_time_col = $this->options['db_time_col']; |
|---|
| 154 |
|
|---|
| 155 |
$sql = 'UPDATE '.$db_table.' SET '.$db_data_col.' = ?, '.$db_time_col.' = '.time().' WHERE '.$db_id_col.'= ?'; |
|---|
| 156 |
|
|---|
| 157 |
try |
|---|
| 158 |
{ |
|---|
| 159 |
$stmt = $this->db->prepare($sql); |
|---|
| 160 |
$stmt->bindParam(1, $data, PDO::PARAM_STR); |
|---|
| 161 |
$stmt->bindParam(2, $id, PDO::PARAM_STR); |
|---|
| 162 |
$stmt->execute(); |
|---|
| 163 |
} |
|---|
| 164 |
catch (PDOException $e) |
|---|
| 165 |
{ |
|---|
| 166 |
throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage())); |
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
return true; |
|---|
| 170 |
} |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|