| 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 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
class sfPDOSessionStorage extends sfSessionStorage |
|---|
| 35 |
{ |
|---|
| 36 |
|
|---|
| 37 |
* PDO connection |
|---|
| 38 |
* @var Connection |
|---|
| 39 |
*/ |
|---|
| 40 |
protected $db; |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
* Initializes this Storage instance. |
|---|
| 44 |
* |
|---|
| 45 |
* @param sfContext A sfContext instance |
|---|
| 46 |
* @param array An associative array of initialization parameters |
|---|
| 47 |
* |
|---|
| 48 |
* @return boolean true, if initialization completes successfully, otherwise false |
|---|
| 49 |
* |
|---|
| 50 |
* @throws <b>InitializationException</b> If an error occurs while initializing this Storage |
|---|
| 51 |
*/ |
|---|
| 52 |
public function initialize($context, $parameters = null) |
|---|
| 53 |
{ |
|---|
| 54 |
|
|---|
| 55 |
$parameters['auto_start'] = false; |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
parent::initialize($context, $parameters); |
|---|
| 59 |
|
|---|
| 60 |
if (!$this->getParameterHolder()->has('db_table')) |
|---|
| 61 |
{ |
|---|
| 62 |
|
|---|
| 63 |
$error = 'Factory configuration file is missing required "db_table" parameter for the Storage category'; |
|---|
| 64 |
|
|---|
| 65 |
throw new sfInitializationException($error); |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
session_set_save_handler(array($this, 'sessionOpen'), |
|---|
| 70 |
array($this, 'sessionClose'), |
|---|
| 71 |
array($this, 'sessionRead'), |
|---|
| 72 |
array($this, 'sessionWrite'), |
|---|
| 73 |
array($this, 'sessionDestroy'), |
|---|
| 74 |
array($this, 'sessionGC')); |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
session_start(); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
* Closes a session. |
|---|
| 82 |
* |
|---|
| 83 |
* @return boolean true, if the session was closed, otherwise false |
|---|
| 84 |
*/ |
|---|
| 85 |
public function sessionClose() |
|---|
| 86 |
{ |
|---|
| 87 |
|
|---|
| 88 |
return true; |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
* Destroys a session. |
|---|
| 93 |
* |
|---|
| 94 |
* @param string A session ID |
|---|
| 95 |
* |
|---|
| 96 |
* @return boolean true, if the session was destroyed, otherwise an exception is thrown |
|---|
| 97 |
* |
|---|
| 98 |
* @throws <b>DatabaseException</b> If the session cannot be destroyed |
|---|
| 99 |
*/ |
|---|
| 100 |
public function sessionDestroy($id) |
|---|
| 101 |
{ |
|---|
| 102 |
|
|---|
| 103 |
$db_table = $this->getParameterHolder()->get('db_table'); |
|---|
| 104 |
$db_id_col = $this->getParameterHolder()->get('db_id_col', 'sess_id'); |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
$sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.'= ?'; |
|---|
| 108 |
|
|---|
| 109 |
try |
|---|
| 110 |
{ |
|---|
| 111 |
$stmt = $this->db->prepare($sql); |
|---|
| 112 |
$stmt->bindParam(1, $id, PDO::PARAM_STR); |
|---|
| 113 |
$stmt->execute(); |
|---|
| 114 |
} |
|---|
| 115 |
catch (PDOException $e) |
|---|
| 116 |
{ |
|---|
| 117 |
$error = sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()); |
|---|
| 118 |
|
|---|
| 119 |
throw new sfDatabaseException($error); |
|---|
| 120 |
} |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
* Cleans up old sessions. |
|---|
| 125 |
* |
|---|
| 126 |
* @param int The lifetime of a session |
|---|
| 127 |
* |
|---|
| 128 |
* @return boolean true, if old sessions have been cleaned, otherwise an exception is thrown |
|---|
| 129 |
* |
|---|
| 130 |
* @throws <b>DatabaseException</b> If any old sessions cannot be cleaned |
|---|
| 131 |
*/ |
|---|
| 132 |
public function sessionGC($lifetime) |
|---|
| 133 |
{ |
|---|
| 134 |
|
|---|
| 135 |
$time = time() - $lifetime; |
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
$db_table = $this->getParameterHolder()->get('db_table'); |
|---|
| 139 |
$db_time_col = $this->getParameterHolder()->get('db_time_col', 'sess_time'); |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
$sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '.$time; |
|---|
| 143 |
|
|---|
| 144 |
try |
|---|
| 145 |
{ |
|---|
| 146 |
$this->db->query($sql); |
|---|
| 147 |
return true; |
|---|
| 148 |
} |
|---|
| 149 |
catch (PDOException $e) |
|---|
| 150 |
{ |
|---|
| 151 |
$error = sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()); |
|---|
| 152 |
|
|---|
| 153 |
throw new sfDatabaseException($error); |
|---|
| 154 |
} |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
* Opens a session. |
|---|
| 159 |
* |
|---|
| 160 |
* @param string |
|---|
| 161 |
* @param string |
|---|
| 162 |
* |
|---|
| 163 |
* @return boolean true, if the session was opened, otherwise an exception is thrown |
|---|
| 164 |
* |
|---|
| 165 |
* @throws <b>DatabaseException</b> If a connection with the database does not exist or cannot be created |
|---|
| 166 |
*/ |
|---|
| 167 |
public function sessionOpen($path, $name) |
|---|
| 168 |
{ |
|---|
| 169 |
|
|---|
| 170 |
$database = $this->getParameterHolder()->get('database', 'default'); |
|---|
| 171 |
|
|---|
| 172 |
$this->db = $this->getContext()->getDatabaseConnection($database); |
|---|
| 173 |
if ($this->db == null || !$this->db instanceof PDO) |
|---|
| 174 |
{ |
|---|
| 175 |
$error = 'PDO dabatase connection doesn\'t exist. Unable to open session.'; |
|---|
| 176 |
|
|---|
| 177 |
throw new sfDatabaseException($error); |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
return true; |
|---|
| 181 |
} |
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
* Reads a session. |
|---|
| 185 |
* |
|---|
| 186 |
* @param string A session ID |
|---|
| 187 |
* |
|---|
| 188 |
* @return boolean true, if the session was read, otherwise an exception is thrown |
|---|
| 189 |
* |
|---|
| 190 |
* @throws <b>DatabaseException</b> If the session cannot be read |
|---|
| 191 |
*/ |
|---|
| 192 |
public function sessionRead($id) |
|---|
| 193 |
{ |
|---|
| 194 |
|
|---|
| 195 |
$db_table = $this->getParameterHolder()->get('db_table'); |
|---|
| 196 |
$db_data_col = $this->getParameterHolder()->get('db_data_col', 'sess_data'); |
|---|
| 197 |
$db_id_col = $this->getParameterHolder()->get('db_id_col', 'sess_id'); |
|---|
| 198 |
$db_time_col = $this->getParameterHolder()->get('db_time_col', 'sess_time'); |
|---|
| 199 |
|
|---|
| 200 |
try |
|---|
| 201 |
{ |
|---|
| 202 |
$sql = 'SELECT '.$db_data_col.' FROM '.$db_table.' WHERE '.$db_id_col.'=?'; |
|---|
| 203 |
|
|---|
| 204 |
$stmt = $this->db->prepare($sql); |
|---|
| 205 |
$stmt->bindParam(1, $id, PDO::PARAM_STR, 255); |
|---|
| 206 |
|
|---|
| 207 |
$stmt->execute(); |
|---|
| 208 |
if ($data = $stmt->fetchColumn()) |
|---|
| 209 |
{ |
|---|
| 210 |
return $data; |
|---|
| 211 |
} |
|---|
| 212 |
else |
|---|
| 213 |
{ |
|---|
| 214 |
|
|---|
| 215 |
$sql = 'INSERT INTO '.$db_table.'('.$db_id_col.', '.$db_data_col.', '.$db_time_col.') VALUES (?, ?, ?)'; |
|---|
| 216 |
|
|---|
| 217 |
$stmt = $this->db->prepare($sql); |
|---|
| 218 |
$stmt->bindParam(1, $id, PDO::PARAM_STR); |
|---|
| 219 |
$stmt->bindValue(2, '', PDO::PARAM_STR); |
|---|
| 220 |
$stmt->bindValue(3, time(), PDO::PARAM_INT); |
|---|
| 221 |
$stmt->execute(); |
|---|
| 222 |
|
|---|
| 223 |
return ''; |
|---|
| 224 |
} |
|---|
| 225 |
} |
|---|
| 226 |
catch (PDOException $e) |
|---|
| 227 |
{ |
|---|
| 228 |
$error = sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()); |
|---|
| 229 |
|
|---|
| 230 |
throw new sfDatabaseException($error); |
|---|
| 231 |
} |
|---|
| 232 |
} |
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
* Writes session data. |
|---|
| 236 |
* |
|---|
| 237 |
* @param string A session ID |
|---|
| 238 |
* @param string A serialized chunk of session data |
|---|
| 239 |
* |
|---|
| 240 |
* @return boolean true, if the session was written, otherwise an exception is thrown |
|---|
| 241 |
* |
|---|
| 242 |
* @throws <b>DatabaseException</b> If the session data cannot be written |
|---|
| 243 |
*/ |
|---|
| 244 |
public function sessionWrite($id, $data) |
|---|
| 245 |
{ |
|---|
| 246 |
|
|---|
| 247 |
$db_table = $this->getParameterHolder()->get('db_table'); |
|---|
| 248 |
$db_data_col = $this->getParameterHolder()->get('db_data_col', 'sess_data'); |
|---|
| 249 |
$db_id_col = $this->getParameterHolder()->get('db_id_col', 'sess_id'); |
|---|
| 250 |
$db_time_col = $this->getParameterHolder()->get('db_time_col', 'sess_time'); |
|---|
| 251 |
|
|---|
| 252 |
$sql = 'UPDATE '.$db_table.' SET '.$db_data_col.' = ?, '.$db_time_col.' = '.time().' WHERE '.$db_id_col.'= ?'; |
|---|
| 253 |
|
|---|
| 254 |
try |
|---|
| 255 |
{ |
|---|
| 256 |
$stmt = $this->db->prepare($sql); |
|---|
| 257 |
$stmt->bindParam(1, $data, PDO::PARAM_STR); |
|---|
| 258 |
$stmt->bindParam(2, $id, PDO::PARAM_STR); |
|---|
| 259 |
$stmt->execute(); |
|---|
| 260 |
return true; |
|---|
| 261 |
} |
|---|
| 262 |
|
|---|
| 263 |
catch (PDOException $e) |
|---|
| 264 |
{ |
|---|
| 265 |
$error = sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()); |
|---|
| 266 |
|
|---|
| 267 |
throw new sfDatabaseException($error); |
|---|
| 268 |
} |
|---|
| 269 |
|
|---|
| 270 |
return false; |
|---|
| 271 |
} |
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
* Executes the shutdown procedure. |
|---|
| 275 |
* |
|---|
| 276 |
*/ |
|---|
| 277 |
public function shutdown() |
|---|
| 278 |
{ |
|---|
| 279 |
} |
|---|
| 280 |
} |
|---|
| 281 |
|
|---|