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