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