DbTable.php
200 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | namespace Piwik\Session\SaveHandler; |
| 11 | |
| 12 | use Piwik\Common; |
| 13 | use Piwik\Db; |
| 14 | use Piwik\DbHelper; |
| 15 | use Exception; |
| 16 | use Piwik\Updater\Migration; |
| 17 | use Zend_Session; |
| 18 | use Zend_Session_SaveHandler_Interface; |
| 19 | |
| 20 | /** |
| 21 | * Database-backed session save handler |
| 22 | * |
| 23 | */ |
| 24 | class DbTable implements Zend_Session_SaveHandler_Interface |
| 25 | { |
| 26 | protected $config; |
| 27 | protected $maxLifetime; |
| 28 | |
| 29 | const TABLE_NAME = 'session'; |
| 30 | |
| 31 | /** |
| 32 | * @param array $config |
| 33 | */ |
| 34 | public function __construct($config) |
| 35 | { |
| 36 | $this->config = $config; |
| 37 | $this->maxLifetime = ini_get('session.gc_maxlifetime'); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Destructor |
| 42 | * |
| 43 | * @return void |
| 44 | */ |
| 45 | public function __destruct() |
| 46 | { |
| 47 | Zend_Session::writeClose(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Open Session - retrieve resources |
| 52 | * |
| 53 | * @param string $save_path |
| 54 | * @param string $name |
| 55 | * @return boolean |
| 56 | */ |
| 57 | public function open($save_path, $name) |
| 58 | { |
| 59 | Db::get()->getConnection(); |
| 60 | |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Close Session - free resources |
| 66 | * |
| 67 | * @return boolean |
| 68 | */ |
| 69 | public function close() |
| 70 | { |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Read session data |
| 76 | * |
| 77 | * @param string $id |
| 78 | * @return string |
| 79 | */ |
| 80 | public function read($id) |
| 81 | { |
| 82 | $sql = 'SELECT ' . $this->config['dataColumn'] . ' FROM ' . $this->config['name'] |
| 83 | . ' WHERE ' . $this->config['primary'] . ' = ?' |
| 84 | . ' AND ' . $this->config['modifiedColumn'] . ' + ' . $this->config['lifetimeColumn'] . ' >= ?'; |
| 85 | |
| 86 | $result = $this->fetchOne($sql, array($id, time())); |
| 87 | |
| 88 | if (!$result) { |
| 89 | $result = ''; |
| 90 | } |
| 91 | |
| 92 | return $result; |
| 93 | } |
| 94 | |
| 95 | private function fetchOne($sql, $bind) |
| 96 | { |
| 97 | try { |
| 98 | $result = Db::get()->fetchOne($sql, $bind); |
| 99 | } catch (Exception $e) { |
| 100 | if (Db::get()->isErrNo($e, Migration\Db::ERROR_CODE_TABLE_NOT_EXISTS)) { |
| 101 | $this->migrateToDbSessionTable(); |
| 102 | $result = Db::get()->fetchOne($sql, $bind); |
| 103 | } else { |
| 104 | throw $e; |
| 105 | } |
| 106 | } |
| 107 | return $result; |
| 108 | } |
| 109 | |
| 110 | private function query($sql, $bind) |
| 111 | { |
| 112 | try { |
| 113 | $result = Db::get()->query($sql, $bind); |
| 114 | } catch (Exception $e) { |
| 115 | if (Db::get()->isErrNo($e, Migration\Db::ERROR_CODE_TABLE_NOT_EXISTS)) { |
| 116 | $this->migrateToDbSessionTable(); |
| 117 | $result = Db::get()->query($sql, $bind); |
| 118 | } else { |
| 119 | throw $e; |
| 120 | } |
| 121 | } |
| 122 | return $result; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Write Session - commit data to resource |
| 127 | * |
| 128 | * @param string $id |
| 129 | * @param mixed $data |
| 130 | * @return boolean |
| 131 | */ |
| 132 | public function write($id, $data) |
| 133 | { |
| 134 | $sql = 'INSERT INTO ' . $this->config['name'] |
| 135 | . ' (' . $this->config['primary'] . ',' |
| 136 | . $this->config['modifiedColumn'] . ',' |
| 137 | . $this->config['lifetimeColumn'] . ',' |
| 138 | . $this->config['dataColumn'] . ')' |
| 139 | . ' VALUES (?,?,?,?)' |
| 140 | . ' ON DUPLICATE KEY UPDATE ' |
| 141 | . $this->config['modifiedColumn'] . ' = ?,' |
| 142 | . $this->config['lifetimeColumn'] . ' = ?,' |
| 143 | . $this->config['dataColumn'] . ' = ?'; |
| 144 | |
| 145 | $this->query($sql, array($id, time(), $this->maxLifetime, $data, time(), $this->maxLifetime, $data)); |
| 146 | |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Destroy Session - remove data from resource for |
| 152 | * given session id |
| 153 | * |
| 154 | * @param string $id |
| 155 | * @return boolean |
| 156 | */ |
| 157 | public function destroy($id) |
| 158 | { |
| 159 | $sql = 'DELETE FROM ' . $this->config['name'] . ' WHERE ' . $this->config['primary'] . ' = ?'; |
| 160 | |
| 161 | $this->query($sql, array($id)); |
| 162 | |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Garbage Collection - remove old session data older |
| 168 | * than $maxlifetime (in seconds) |
| 169 | * |
| 170 | * @param int $maxlifetime timestamp in seconds |
| 171 | * @return bool always true |
| 172 | */ |
| 173 | public function gc($maxlifetime) |
| 174 | { |
| 175 | $sql = 'DELETE FROM ' . $this->config['name'] |
| 176 | . ' WHERE ' . $this->config['modifiedColumn'] . ' + ' . $this->config['lifetimeColumn'] . ' < ?'; |
| 177 | |
| 178 | $this->query($sql, array(time())); |
| 179 | |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | private function migrateToDbSessionTable() |
| 184 | { |
| 185 | // happens when updating from Piwik 1.4 or earlier to Matomo 3.7+ |
| 186 | // in this case on update it will change the session handler to dbtable, but it hasn't performed |
| 187 | // the DB updates just yet which means the session table won't be available as it was only added in |
| 188 | // Piwik 1.5 => results in a sql error the session table does not exist |
| 189 | try { |
| 190 | $sql = DbHelper::getTableCreateSql(self::TABLE_NAME); |
| 191 | Db::query($sql); |
| 192 | } catch (Exception $e) { |
| 193 | if (!Db::get()->isErrNo($e, Migration\Db::ERROR_CODE_TABLE_EXISTS)) { |
| 194 | throw $e; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | } |
| 200 |