PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.2.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.2.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 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