Rev 730 | Rev 1086 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
511 | daniel-mar | 5 | * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft |
2 | daniel-mar | 6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
8 | * you may not use this file except in compliance with the License. |
||
9 | * You may obtain a copy of the License at |
||
10 | * |
||
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | * |
||
13 | * Unless required by applicable law or agreed to in writing, software |
||
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | * See the License for the specific language governing permissions and |
||
17 | * limitations under the License. |
||
18 | */ |
||
19 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
511 | daniel-mar | 21 | |
261 | daniel-mar | 22 | // OIDplusConfig contains settings that are stored in the database. |
294 | daniel-mar | 23 | // Not to be confused with OIDplusBaseConfig which is the basic ("static") |
24 | // configuration stored in userdata/baseconfig/config.inc.php, |
||
261 | daniel-mar | 25 | // e.g. database access credentials. |
730 | daniel-mar | 26 | class OIDplusConfig extends OIDplusBaseClass implements OIDplusGetterSetterInterface { |
2 | daniel-mar | 27 | |
269 | daniel-mar | 28 | /*public*/ const PROTECTION_EDITABLE = 0; |
29 | /*public*/ const PROTECTION_READONLY = 1; |
||
30 | /*public*/ const PROTECTION_HIDDEN = 2; |
||
263 | daniel-mar | 31 | |
326 | daniel-mar | 32 | protected $configTableReadOnce = false; // this ensures that all $values and $descriptions were read |
33 | |||
75 | daniel-mar | 34 | protected $values = array(); |
295 | daniel-mar | 35 | protected $descriptions = array(); |
682 | daniel-mar | 36 | protected $protectSettings = array(); |
37 | protected $visibleSettings = array(); |
||
263 | daniel-mar | 38 | protected $validateCallbacks = array(); |
13 | daniel-mar | 39 | |
263 | daniel-mar | 40 | public function prepareConfigKey($name, $description, $init_value, $protection, $validateCallback) { |
326 | daniel-mar | 41 | // Check if the protection flag is valid |
263 | daniel-mar | 42 | switch ($protection) { |
43 | case OIDplusConfig::PROTECTION_EDITABLE: |
||
44 | $protected = 0; |
||
45 | $visible = 1; |
||
46 | break; |
||
47 | case OIDplusConfig::PROTECTION_READONLY: |
||
48 | $protected = 1; |
||
49 | $visible = 1; |
||
50 | break; |
||
51 | case OIDplusConfig::PROTECTION_HIDDEN: |
||
52 | $protected = 1; |
||
53 | $visible = 0; |
||
54 | break; |
||
55 | default: |
||
360 | daniel-mar | 56 | throw new OIDplusException(_L('Invalid protection flag, use OIDplusConfig::PROTECTION_* constants')); |
263 | daniel-mar | 57 | } |
295 | daniel-mar | 58 | |
326 | daniel-mar | 59 | // Check length limitations given by the database tables |
166 | daniel-mar | 60 | if (strlen($name) > 50) { |
360 | daniel-mar | 61 | throw new OIDplusException(_L('Config key name "%1" is too long (max %2).',$name,50)); |
166 | daniel-mar | 62 | } |
63 | if (strlen($description) > 255) { |
||
360 | daniel-mar | 64 | throw new OIDplusException(_L('Description for config key "%1" is too long (max %2).',$name,255)); |
166 | daniel-mar | 65 | } |
326 | daniel-mar | 66 | |
67 | // Read all values and descriptions from the database once. |
||
150 | daniel-mar | 68 | $this->buildConfigArray(); |
326 | daniel-mar | 69 | |
70 | // Figure out if we need to create/update something at database level |
||
150 | daniel-mar | 71 | if (!isset($this->values[$name])) { |
326 | daniel-mar | 72 | // Case A: The config setting does not exist in the database. So we create it now. |
614 | daniel-mar | 73 | try { |
74 | OIDplus::db()->query("insert into ###config (name, description, value, protected, visible) values (?, ?, ?, ?, ?)", array($name, $description, $init_value, $protected, $visible)); |
||
1050 | daniel-mar | 75 | } catch (\Exception $e) { |
614 | daniel-mar | 76 | // After a software update that introduced a new config setting, |
77 | // there will be a race-condition at this place, because |
||
78 | // jsTree and content are loading simultaneously! |
||
79 | // So we ignore the error here. |
||
80 | } |
||
155 | daniel-mar | 81 | $this->values[$name] = $init_value; |
295 | daniel-mar | 82 | $this->descriptions[$name] = $description; |
682 | daniel-mar | 83 | $this->protectSettings[$name] = $protected; |
84 | $this->visibleSettings[$name] = $visible; |
||
326 | daniel-mar | 85 | } else { |
86 | // Case B: The config setting exists ... |
||
87 | if ($this->descriptions[$name] != $description) { |
||
88 | // ... but the human readable description is different. |
||
89 | // We want to give the plugin authors the possibility to automatically update the config descriptions for their plugins |
||
90 | // So we just edit the description |
||
91 | OIDplus::db()->query("update ###config set description = ? where name = ?", array($description, $name)); |
||
92 | $this->descriptions[$name] = $description; |
||
93 | } |
||
682 | daniel-mar | 94 | if ($this->protectSettings[$name] != $protected) { |
95 | OIDplus::db()->query("update ###config set protected = ? where name = ?", array($protected, $name)); |
||
96 | $this->protectSettings[$name] = $protected; |
||
97 | } |
||
98 | if ($this->visibleSettings[$name] != $visible) { |
||
99 | OIDplus::db()->query("update ###config set visible = ? where name = ?", array($visible, $name)); |
||
100 | $this->visibleSettings[$name] = $visible; |
||
101 | } |
||
150 | daniel-mar | 102 | } |
326 | daniel-mar | 103 | |
104 | // Register the validation callback |
||
263 | daniel-mar | 105 | if (!is_null($validateCallback)) { |
106 | $this->validateCallbacks[$name] = $validateCallback; |
||
107 | } |
||
13 | daniel-mar | 108 | } |
109 | |||
326 | daniel-mar | 110 | public function clearCache() { |
111 | $this->configTableReadOnce = false; |
||
112 | $this->buildConfigArray(); |
||
113 | } |
||
114 | |||
150 | daniel-mar | 115 | protected function buildConfigArray() { |
326 | daniel-mar | 116 | if ($this->configTableReadOnce) return; |
117 | |||
118 | $this->values = array(); |
||
119 | $this->descriptions = array(); |
||
682 | daniel-mar | 120 | $this->protectSettings = array(); |
121 | $this->visibleSettings = array(); |
||
122 | $res = OIDplus::db()->query("select name, description, protected, visible, value from ###config"); |
||
326 | daniel-mar | 123 | while ($row = $res->fetch_object()) { |
124 | $this->values[$row->name] = $row->value; |
||
125 | $this->descriptions[$row->name] = $row->description; |
||
682 | daniel-mar | 126 | $this->protectSettings[$row->name] = $row->protected; |
127 | $this->visibleSettings[$row->name] = $row->visible; |
||
75 | daniel-mar | 128 | } |
326 | daniel-mar | 129 | |
130 | $this->configTableReadOnce = true; |
||
150 | daniel-mar | 131 | } |
75 | daniel-mar | 132 | |
261 | daniel-mar | 133 | public function getValue($name, $default=null) { |
326 | daniel-mar | 134 | // Read all config settings once and write them in array $this->values |
150 | daniel-mar | 135 | $this->buildConfigArray(); |
326 | daniel-mar | 136 | |
137 | // Now we can see if our desired attribute is available |
||
60 | daniel-mar | 138 | if (isset($this->values[$name])) { |
139 | return $this->values[$name]; |
||
140 | } else { |
||
261 | daniel-mar | 141 | return $default; |
60 | daniel-mar | 142 | } |
46 | daniel-mar | 143 | } |
144 | |||
74 | daniel-mar | 145 | public function exists($name) { |
326 | daniel-mar | 146 | return !is_null($this->getValue($name, null)); |
74 | daniel-mar | 147 | } |
148 | |||
14 | daniel-mar | 149 | public function setValue($name, $value) { |
574 | daniel-mar | 150 | // Read all config settings once and write them in array $this->values |
151 | $this->buildConfigArray(); |
||
152 | |||
326 | daniel-mar | 153 | if (isset($this->values[$name])) { |
574 | daniel-mar | 154 | // Avoid unnecessary database writes |
326 | daniel-mar | 155 | if ($this->values[$name] == $value) return; |
574 | daniel-mar | 156 | } else { |
157 | throw new OIDplusException(_L('Config value "%1" cannot be written because it was not prepared!', $name)); |
||
326 | daniel-mar | 158 | } |
159 | |||
263 | daniel-mar | 160 | // Give plugins the possibility to stop the process by throwing an Exception (e.g. if the value is invalid) |
326 | daniel-mar | 161 | // Required is that the plugin previously prepared the config setting using prepareConfigKey() |
263 | daniel-mar | 162 | if (isset($this->validateCallbacks[$name])) { |
163 | $this->validateCallbacks[$name]($value); |
||
14 | daniel-mar | 164 | } |
257 | daniel-mar | 165 | |
14 | daniel-mar | 166 | // Now change the value in the database |
261 | daniel-mar | 167 | OIDplus::db()->query("update ###config set value = ? where name = ?", array($value, $name)); |
66 | daniel-mar | 168 | $this->values[$name] = $value; |
14 | daniel-mar | 169 | } |
170 | |||
672 | daniel-mar | 171 | public function setValueNoCallback($name, $value) { |
172 | // Read all config settings once and write them in array $this->values |
||
173 | $this->buildConfigArray(); |
||
174 | |||
175 | if (isset($this->values[$name])) { |
||
176 | // Avoid unnecessary database writes |
||
177 | if ($this->values[$name] == $value) return; |
||
178 | } else { |
||
179 | throw new OIDplusException(_L('Config value "%1" cannot be written because it was not prepared!', $name)); |
||
180 | } |
||
181 | |||
182 | // Now change the value in the database |
||
183 | OIDplus::db()->query("update ###config set value = ? where name = ?", array($value, $name)); |
||
184 | $this->values[$name] = $value; |
||
185 | } |
||
186 | |||
569 | daniel-mar | 187 | public function delete($name) { |
326 | daniel-mar | 188 | if ($this->configTableReadOnce) { |
189 | if (isset($this->values[$name])) { |
||
190 | OIDplus::db()->query("delete from ###config where name = ?", array($name)); |
||
191 | } |
||
192 | } else { |
||
193 | // We do not know if the value exists. |
||
194 | // buildConfigArray() would do many reads which are unnecessary. |
||
195 | // So we just do a MySQL command to delete the stuff: |
||
294 | daniel-mar | 196 | OIDplus::db()->query("delete from ###config where name = ?", array($name)); |
197 | } |
||
326 | daniel-mar | 198 | |
199 | unset($this->values[$name]); |
||
200 | unset($this->descriptions[$name]); |
||
201 | unset($this->validateCallbacks[$name]); |
||
682 | daniel-mar | 202 | unset($this->protectSettings[$name]); |
203 | unset($this->visibleSettings[$name]); |
||
294 | daniel-mar | 204 | } |
205 | |||
569 | daniel-mar | 206 | } |