| 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 |
class sfCreoleSessionStorage extends sfDatabaseSessionStorage |
|---|
| 28 |
{ |
|---|
| 29 |
|
|---|
| 30 |
* Destroy a session. |
|---|
| 31 |
* |
|---|
| 32 |
* @param string $id A session ID. |
|---|
| 33 |
* |
|---|
| 34 |
* @return bool true, if the session was destroyed, otherwise an exception is thrown. |
|---|
| 35 |
* |
|---|
| 36 |
* @throws <b>DatabaseException</b> If the session cannot be destroyed. |
|---|
| 37 |
*/ |
|---|
| 38 |
public function sessionDestroy($id) |
|---|
| 39 |
{ |
|---|
| 40 |
|
|---|
| 41 |
$db_table = $this->options['db_table']; |
|---|
| 42 |
$db_id_col = $this->options['db_id_col']; |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
$sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.'= ?'; |
|---|
| 46 |
|
|---|
| 47 |
try |
|---|
| 48 |
{ |
|---|
| 49 |
$stmt = $this->con->prepareStatement($sql); |
|---|
| 50 |
$stmt->setString(1, $id); |
|---|
| 51 |
$stmt->executeUpdate(); |
|---|
| 52 |
} |
|---|
| 53 |
catch (SQLException $e) |
|---|
| 54 |
{ |
|---|
| 55 |
throw new sfDatabaseException(sprintf('Creole SQLException was thrown when trying to manipulate session data. Message: %s.', $e->getMessage())); |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
return true; |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
* Cleanup old sessions. |
|---|
| 63 |
* |
|---|
| 64 |
* @param int $lifetime The lifetime of a session. |
|---|
| 65 |
* |
|---|
| 66 |
* @return bool true, if old sessions have been cleaned, otherwise an exception is thrown. |
|---|
| 67 |
* |
|---|
| 68 |
* @throws <b>DatabaseException</b> If any old sessions cannot be cleaned. |
|---|
| 69 |
*/ |
|---|
| 70 |
public function sessionGC($lifetime) |
|---|
| 71 |
{ |
|---|
| 72 |
|
|---|
| 73 |
$db_table = $this->options['db_table']; |
|---|
| 74 |
$db_time_col = $this->options['db_time_col']; |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
$sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '.(time() - $lifetime); |
|---|
| 78 |
|
|---|
| 79 |
try |
|---|
| 80 |
{ |
|---|
| 81 |
$this->con->executeQuery($sql); |
|---|
| 82 |
} |
|---|
| 83 |
catch (SQLException $e) |
|---|
| 84 |
{ |
|---|
| 85 |
throw new sfDatabaseException(sprintf('Creole SQLException was thrown when trying to manipulate session data. Message: %s.', $e->getMessage())); |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
return true; |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
* Read a session. |
|---|
| 93 |
* |
|---|
| 94 |
* @param string $id A session ID. |
|---|
| 95 |
* |
|---|
| 96 |
* @return bool true, if the session was read, otherwise an exception is thrown. |
|---|
| 97 |
* |
|---|
| 98 |
* @throws <b>DatabaseException</b> If the session cannot be read. |
|---|
| 99 |
*/ |
|---|
| 100 |
public function sessionRead($id) |
|---|
| 101 |
{ |
|---|
| 102 |
|
|---|
| 103 |
$db_table = $this->options['db_table']; |
|---|
| 104 |
$db_data_col = $this->options['db_data_col']; |
|---|
| 105 |
$db_id_col = $this->options['db_id_col']; |
|---|
| 106 |
$db_time_col = $this->options['db_time_col']; |
|---|
| 107 |
|
|---|
| 108 |
try |
|---|
| 109 |
{ |
|---|
| 110 |
$sql = 'SELECT '.$db_data_col.' FROM '.$db_table.' WHERE '.$db_id_col.'=?'; |
|---|
| 111 |
|
|---|
| 112 |
$stmt = $this->con->prepareStatement($sql); |
|---|
| 113 |
$stmt->setString(1, $id); |
|---|
| 114 |
|
|---|
| 115 |
$dbRes = $stmt->executeQuery(ResultSet::FETCHMODE_NUM); |
|---|
| 116 |
|
|---|
| 117 |
if ($dbRes->next()) |
|---|
| 118 |
{ |
|---|
| 119 |
$data = $dbRes->getString(1); |
|---|
| 120 |
|
|---|
| 121 |
return $data; |
|---|
| 122 |
} |
|---|
| 123 |
else |
|---|
| 124 |
{ |
|---|
| 125 |
|
|---|
| 126 |
$sql = 'INSERT INTO '.$db_table.'('.$db_id_col.','.$db_data_col.','.$db_time_col.') VALUES (?,?,?)'; |
|---|
| 127 |
|
|---|
| 128 |
$stmt = $this->con->prepareStatement($sql); |
|---|
| 129 |
$stmt->setString(1, $id); |
|---|
| 130 |
$stmt->setString(2, ''); |
|---|
| 131 |
$stmt->setInt(3, time()); |
|---|
| 132 |
$stmt->executeUpdate(); |
|---|
| 133 |
|
|---|
| 134 |
return ''; |
|---|
| 135 |
} |
|---|
| 136 |
} |
|---|
| 137 |
catch (SQLException $e) |
|---|
| 138 |
{ |
|---|
| 139 |
throw new sfDatabaseException(sprintf('Creole SQLException was thrown when trying to manipulate session data. Message: %s.', $e->getMessage())); |
|---|
| 140 |
} |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
* Write session data. |
|---|
| 145 |
* |
|---|
| 146 |
* @param string $id A session ID. |
|---|
| 147 |
* @param string $data A serialized chunk of session data. |
|---|
| 148 |
* |
|---|
| 149 |
* @return bool true, if the session was written, otherwise an exception is |
|---|
| 150 |
* thrown. |
|---|
| 151 |
* |
|---|
| 152 |
* @throws <b>DatabaseException</b> If the session data cannot be written. |
|---|
| 153 |
*/ |
|---|
| 154 |
public function sessionWrite($id, $data) |
|---|
| 155 |
{ |
|---|
| 156 |
|
|---|
| 157 |
$db_table = $this->options['db_table']; |
|---|
| 158 |
$db_data_col = $this->options['db_data_col']; |
|---|
| 159 |
$db_id_col = $this->options['db_id_col']; |
|---|
| 160 |
$db_time_col = $this->options['db_time_col']; |
|---|
| 161 |
|
|---|
| 162 |
$sql = 'UPDATE '.$db_table.' SET '.$db_data_col.'=?, '.$db_time_col.' = '.time().' WHERE '.$db_id_col.'=?'; |
|---|
| 163 |
|
|---|
| 164 |
try |
|---|
| 165 |
{ |
|---|
| 166 |
$stmt = $this->con->prepareStatement($sql); |
|---|
| 167 |
$stmt->setString(1, $data); |
|---|
| 168 |
$stmt->setString(2, $id); |
|---|
| 169 |
$stmt->executeUpdate(); |
|---|
| 170 |
} |
|---|
| 171 |
catch (SQLException $e) |
|---|
| 172 |
{ |
|---|
| 173 |
throw new sfDatabaseException(sprintf('Creole SQLException was thrown when trying to manipulate session data. Message: %s.', $e->getMessage())); |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
return true; |
|---|
| 177 |
} |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|