BackendInterface.php
6 years ago
BaseSettingsTable.php
6 years ago
Cache.php
6 years ago
Config.php
6 years ago
MeasurableSettingsTable.php
6 years ago
NullBackend.php
6 years ago
PluginSettingsTable.php
6 years ago
SitesTable.php
6 years ago
PluginSettingsTable.php
208 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\Settings\Storage\Backend; |
| 11 | |
| 12 | use Piwik\Common; |
| 13 | use Piwik\Concurrency\Lock; |
| 14 | use Piwik\Container\StaticContainer; |
| 15 | use Piwik\Db; |
| 16 | use Exception; |
| 17 | use Piwik\Version; |
| 18 | |
| 19 | /** |
| 20 | * Plugin settings backend. Stores all settings in a "plugin_setting" database table. |
| 21 | * |
| 22 | * If a value that needs to be stored is an array, will insert a new row for each value of this array. |
| 23 | */ |
| 24 | class PluginSettingsTable extends BaseSettingsTable |
| 25 | { |
| 26 | /** |
| 27 | * @var string |
| 28 | */ |
| 29 | private $pluginName; |
| 30 | |
| 31 | /** |
| 32 | * @var string |
| 33 | */ |
| 34 | private $userLogin; |
| 35 | |
| 36 | public function __construct($pluginName, $userLogin) |
| 37 | { |
| 38 | parent::__construct(); |
| 39 | |
| 40 | if (empty($pluginName)) { |
| 41 | throw new Exception('No plugin name given for PluginSettingsTable backend'); |
| 42 | } |
| 43 | |
| 44 | if ($userLogin === false || $userLogin === null) { |
| 45 | throw new Exception('Invalid user login name given for PluginSettingsTable backend'); |
| 46 | } |
| 47 | |
| 48 | $this->pluginName = $pluginName; |
| 49 | $this->userLogin = $userLogin; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @inheritdoc |
| 54 | */ |
| 55 | public function getStorageId() |
| 56 | { |
| 57 | return 'PluginSettings_' . $this->pluginName . '_User_' . $this->userLogin; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Saves (persists) the current setting values in the database. |
| 62 | * @param array $values Key/value pairs of setting values to be written |
| 63 | */ |
| 64 | public function save($values) |
| 65 | { |
| 66 | $this->initDbIfNeeded(); |
| 67 | |
| 68 | $valuesKeep = array(); |
| 69 | |
| 70 | foreach ($values as $name => $value) { |
| 71 | if (!isset($value)) { |
| 72 | continue; |
| 73 | } |
| 74 | if (is_array($value) || is_object($value)) { |
| 75 | $jsonEncoded = 1; |
| 76 | $value = json_encode($value); |
| 77 | } else { |
| 78 | $jsonEncoded = 0; |
| 79 | if (is_bool($value)) { |
| 80 | // we are currently not storing booleans as json as it could result in trouble with the UI and regress |
| 81 | // preselecting the correct value |
| 82 | $value = (int) $value; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | $valuesKeep[] = array($this->pluginName, $this->userLogin, $name, $value, $jsonEncoded); |
| 87 | } |
| 88 | |
| 89 | $columns = array('plugin_name', 'user_login', 'setting_name', 'setting_value', 'json_encoded'); |
| 90 | |
| 91 | $table = $this->getTableName(); |
| 92 | $lockKey = $this->getStorageId(); |
| 93 | $this->lock->execute($lockKey, function() use ($valuesKeep, $table, $columns) { |
| 94 | $this->delete(); |
| 95 | // No values = nothing to save |
| 96 | if (!empty($valuesKeep)) { |
| 97 | Db\BatchInsert::tableInsertBatchSql($table, $columns, $valuesKeep); |
| 98 | } |
| 99 | }); |
| 100 | } |
| 101 | |
| 102 | private function jsonEncodedMissingError(Exception $e) |
| 103 | { |
| 104 | return strpos($e->getMessage(), 'json_encoded') !== false; |
| 105 | } |
| 106 | |
| 107 | public function load() |
| 108 | { |
| 109 | $this->initDbIfNeeded(); |
| 110 | |
| 111 | $sql = "SELECT `setting_name`, `setting_value`, `json_encoded` FROM " . $this->getTableName() . " WHERE plugin_name = ? and user_login = ?"; |
| 112 | $bind = array($this->pluginName, $this->userLogin); |
| 113 | |
| 114 | try { |
| 115 | $settings = $this->db->fetchAll($sql, $bind); |
| 116 | } catch (\Exception $e) { |
| 117 | // we catch an exception since json_encoded might not be present before matomo is updated to 3.5.0+ but the updater |
| 118 | // may run this query |
| 119 | if ($this->jsonEncodedMissingError($e)) { |
| 120 | $sql = "SELECT `setting_name`, `setting_value` FROM " . $this->getTableName() . " WHERE plugin_name = ? and user_login = ?"; |
| 121 | $settings = $this->db->fetchAll($sql, $bind); |
| 122 | } else { |
| 123 | throw $e; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | $flat = array(); |
| 128 | foreach ($settings as $setting) { |
| 129 | $name = $setting['setting_name']; |
| 130 | |
| 131 | if (!empty($setting['json_encoded'])) { |
| 132 | $flat[$name] = json_decode($setting['setting_value'], true); |
| 133 | } elseif (array_key_exists($name, $flat)) { |
| 134 | if (!is_array($flat[$name])) { |
| 135 | $flat[$name] = array($flat[$name]); |
| 136 | } |
| 137 | $flat[$name][] = $setting['setting_value']; |
| 138 | } else { |
| 139 | $flat[$name] = $setting['setting_value']; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return $flat; |
| 144 | } |
| 145 | |
| 146 | protected function getTableName() |
| 147 | { |
| 148 | return Common::prefixTable('plugin_setting'); |
| 149 | } |
| 150 | |
| 151 | public function delete() |
| 152 | { |
| 153 | $this->initDbIfNeeded(); |
| 154 | |
| 155 | $table = $this->getTableName(); |
| 156 | $sql = "DELETE FROM $table WHERE `plugin_name` = ? and `user_login` = ?"; |
| 157 | $bind = array($this->pluginName, $this->userLogin); |
| 158 | |
| 159 | $this->db->query($sql, $bind); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Unsets all settings for a user. The settings will be removed from the database. Used when |
| 164 | * a user is deleted. |
| 165 | * |
| 166 | * @internal |
| 167 | * @param string $userLogin |
| 168 | * @throws \Exception If the `$userLogin` is empty. Otherwise we would delete most plugin settings |
| 169 | */ |
| 170 | public static function removeAllUserSettingsForUser($userLogin) |
| 171 | { |
| 172 | if (empty($userLogin)) { |
| 173 | throw new Exception('No userLogin specified. Cannot remove all settings for this user'); |
| 174 | } |
| 175 | |
| 176 | try { |
| 177 | $table = Common::prefixTable('plugin_setting'); |
| 178 | Db::get()->query(sprintf('DELETE FROM %s WHERE user_login = ?', $table), array($userLogin)); |
| 179 | } catch (Exception $e) { |
| 180 | if ($e->getCode() != 42) { |
| 181 | // ignore table not found error, which might occur when updating from an older version of Piwik |
| 182 | throw $e; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Unsets all settings for a plugin. The settings will be removed from the database. Used when |
| 189 | * a plugin is uninstalled. |
| 190 | * |
| 191 | * @internal |
| 192 | * @param string $pluginName |
| 193 | * @throws \Exception If the `$userLogin` is empty. |
| 194 | */ |
| 195 | public static function removeAllSettingsForPlugin($pluginName) |
| 196 | { |
| 197 | try { |
| 198 | $table = Common::prefixTable('plugin_setting'); |
| 199 | Db::get()->query(sprintf('DELETE FROM %s WHERE plugin_name = ?', $table), array($pluginName)); |
| 200 | } catch (Exception $e) { |
| 201 | if ($e->getCode() != 42) { |
| 202 | // ignore table not found error, which might occur when updating from an older version of Piwik |
| 203 | throw $e; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 |