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