|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (C) 2016 Tony Murray <[email protected]> |
|
4
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
5
|
|
|
* it under the terms of the GNU General Public License as published by |
|
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
7
|
|
|
* (at your option) any later version. |
|
8
|
|
|
* |
|
9
|
|
|
* This program is distributed in the hope that it will be useful, |
|
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
|
12
|
|
|
* GNU General Public License for more details. |
|
13
|
|
|
* |
|
14
|
|
|
* You should have received a copy of the GNU General Public License |
|
15
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16
|
|
|
*/ |
|
17
|
|
|
/** |
|
18
|
|
|
* Settings.php |
|
19
|
|
|
* |
|
20
|
|
|
* @package LibreNMS |
|
21
|
|
|
* @author Tony Murray <[email protected]> |
|
22
|
|
|
* @copyright 2016 Tony Murray |
|
23
|
|
|
* @license @license http://opensource.org/licenses/GPL-3.0 GNU Public License v3 or later |
|
24
|
|
|
*/ |
|
25
|
|
|
namespace App; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
use App\Models\DbConfig; |
|
29
|
|
|
use Cache; |
|
30
|
|
|
use Config; |
|
31
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigContract; |
|
32
|
|
|
|
|
33
|
|
|
// adds the possibility to replace the default Config facade |
|
34
|
|
|
|
|
35
|
|
|
class Settings implements ConfigContract |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* The tag for the cache. |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
public static $cache_tag = "Settings"; |
|
43
|
|
|
/** |
|
44
|
|
|
* The amount of time in minutes to store values in cache |
|
45
|
|
|
* |
|
46
|
|
|
* @var int |
|
47
|
|
|
*/ |
|
48
|
|
|
private $cache_time; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Settings constructor. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct() |
|
54
|
|
|
{ |
|
55
|
|
|
$this->cache_time = env('CACHE_LIFETIME', 60); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Check if the key is defined in the Settings store. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $key Only full paths will return true. |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
|
|
public function has($key) |
|
65
|
|
|
{ |
|
66
|
|
|
return (Cache::tags(self::$cache_tag)->has($key) || Config::has('config.'.$key) || DbConfig::key($key)->exists()); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get all settings defined in the Settings store. |
|
71
|
|
|
* |
|
72
|
|
|
* @return array A nested array of all settings. |
|
73
|
|
|
*/ |
|
74
|
|
|
public function all() |
|
75
|
|
|
{ |
|
76
|
|
|
// no caching :( |
|
77
|
|
|
$config_settings = Config::all()['config']; |
|
78
|
|
|
$db_settings = self::collectionToArray(DbConfig::all()); |
|
79
|
|
|
return array_replace_recursive($config_settings, $db_settings); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Convert an Eloquent Collection into a nested array |
|
84
|
|
|
* |
|
85
|
|
|
* @param $data \Illuminate\Database\Eloquent\Collection The Collection. |
|
86
|
|
|
* @param string $prefix Path to prepend. Do not include trailing . |
|
87
|
|
|
* @return array The resulting nested array. |
|
88
|
|
|
*/ |
|
89
|
|
|
private static function collectionToArray($data, $prefix = "") |
|
90
|
|
|
{ |
|
91
|
|
|
$tree = array(); |
|
92
|
|
|
foreach ($data as $item) { |
|
93
|
|
|
$key = $item->config_name; |
|
94
|
|
|
if (starts_with($key, $prefix)) { |
|
95
|
|
|
$key = substr($key, strlen($prefix)); |
|
96
|
|
|
} |
|
97
|
|
|
$parts = explode('.', trim($key, '.')); |
|
98
|
|
|
|
|
99
|
|
|
$temp = &$tree; |
|
100
|
|
|
foreach ($parts as $part) { |
|
101
|
|
|
$temp = &$temp[$part]; |
|
102
|
|
|
} |
|
103
|
|
|
$temp = $item->config_value; |
|
104
|
|
|
unset($temp); |
|
105
|
|
|
} |
|
106
|
|
|
return $tree; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Prepend a value onto an array configuration value. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $key |
|
113
|
|
|
* @param mixed $value |
|
114
|
|
|
* @return void |
|
115
|
|
|
*/ |
|
116
|
|
|
public function prepend($key, $value) |
|
117
|
|
|
{ |
|
118
|
|
|
$var = $this->get($key); |
|
119
|
|
|
|
|
120
|
|
|
if (is_array($var)) { |
|
121
|
|
|
$this->forget($key); |
|
122
|
|
|
array_unshift($var, $value); |
|
123
|
|
|
$this->set($key, $var); |
|
124
|
|
|
} |
|
125
|
|
View Code Duplication |
else { |
|
|
|
|
|
|
126
|
|
|
$arr = [$value]; |
|
127
|
|
|
if (!is_null($var)) { |
|
128
|
|
|
$arr[] = $var; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$this->forget($key); |
|
132
|
|
|
$this->set($key, $arr); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get a value from the Settings store. |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $key A full or partial . separated key. |
|
140
|
|
|
* @param null $default If the key isn't found, return this value. By default undefined keys return null. |
|
141
|
|
|
* @return mixed If the $key is a full path, a bare value will be returned. If it is a partial path, a nested array will be retuned. |
|
142
|
|
|
*/ |
|
143
|
|
|
public function get($key, $default = null) |
|
144
|
|
|
{ |
|
145
|
|
|
// return value from cache or fetch it and return it |
|
146
|
|
|
return Cache::tags(self::$cache_tag)->remember($key, $this->cache_time, function() use ($key, $default) { |
|
147
|
|
|
|
|
148
|
|
|
// fetch the values from storage |
|
149
|
|
|
if (Config::has('config.'.$key)) { |
|
150
|
|
|
$config_data = Config::get('config.'.$key, $default); |
|
151
|
|
|
} |
|
152
|
|
|
$db_data = DbConfig::key($key)->get(['config_name', 'config_value']); |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
if (count($db_data) == 1 && $db_data->first()->config_name == $key) { |
|
155
|
|
|
// return a value if we are getting one item and it is the requested item (not recursing) |
|
156
|
|
|
return $db_data->first()->config_value; |
|
157
|
|
|
} |
|
158
|
|
|
elseif (count($db_data) >= 1) { |
|
159
|
|
|
// convert the collection to an array |
|
160
|
|
|
$result = self::collectionToArray($db_data, $key); |
|
161
|
|
|
|
|
162
|
|
|
// if we have config_data, merge them |
|
163
|
|
|
if (isset($config_data)) { |
|
164
|
|
|
return array_replace_recursive($config_data, $result); |
|
165
|
|
|
} |
|
166
|
|
|
else { |
|
167
|
|
|
return $result; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
// fall back to config.php/defaults.php |
|
172
|
|
|
if (isset($config_data) && (!is_array($config_data) || count($db_data) == 0)) { |
|
173
|
|
|
// return the value from config.php if it is a value |
|
174
|
|
|
return $config_data; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
// we couldn't find the key, return the default |
|
179
|
|
|
return $default; |
|
180
|
|
|
}); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Forget a key and all children |
|
185
|
|
|
* This cannot forget variables set in config.php |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $key Explicit key to forget |
|
188
|
|
|
*/ |
|
189
|
|
|
public function forget($key) |
|
190
|
|
|
{ |
|
191
|
|
|
// Cannot remove from config |
|
192
|
|
|
|
|
193
|
|
|
$count = DbConfig::key($key)->count(); |
|
|
|
|
|
|
194
|
|
|
if ($count == 1) { |
|
195
|
|
|
$this->flush($key); |
|
196
|
|
|
} |
|
197
|
|
|
else { |
|
198
|
|
|
$this->flush(); // possible optimization: selective flush |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
DbConfig::key($key)->delete(); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Clear the settings cache. |
|
206
|
|
|
* If path is set, only clear the path and it's parents. |
|
207
|
|
|
* This will not clear children. |
|
208
|
|
|
* |
|
209
|
|
|
* @param string $key The path to clear. |
|
210
|
|
|
*/ |
|
211
|
|
|
public function flush($key = null) |
|
212
|
|
|
{ |
|
213
|
|
|
if (is_null($key)) { |
|
214
|
|
|
// Clear all cache |
|
215
|
|
|
Cache::tags(self::$cache_tag)->flush(); |
|
216
|
|
|
} |
|
217
|
|
|
else { |
|
218
|
|
|
// Clear specific path |
|
219
|
|
|
$path = []; |
|
220
|
|
|
foreach (explode('.', $key) as $element) { |
|
221
|
|
|
$path[] = $element; |
|
222
|
|
|
Cache::tags(self::$cache_tag)->forget(join('.', $path)); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
// ---- Local Utility functions ---- |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Set a key value pair into the Settings store. |
|
231
|
|
|
* |
|
232
|
|
|
* @param string $key A . separated path to this setting |
|
233
|
|
|
* @param array|null $value A value or an array. If value is an array it will be converted to a . separate path(s) concatinated onto the given key |
|
234
|
|
|
* @throws \Exception |
|
235
|
|
|
*/ |
|
236
|
|
|
public function set($key, $value = null) |
|
237
|
|
|
{ |
|
238
|
|
|
if (is_array($value)) { |
|
239
|
|
|
// repeat until value contains no arrays |
|
240
|
|
|
foreach ($value as $k => $v) { |
|
241
|
|
|
if (!empty($key)) { |
|
242
|
|
|
if (is_string($k) && !str_contains($k, '.') && DbConfig::exactKey($key)->exists() && DbConfig::key($key)->count() == 1) { |
|
|
|
|
|
|
243
|
|
|
// check that we aren't trying to set an array onto an existing value only setting |
|
244
|
|
|
throw new \Exception("Attempting to set array value to existing non-array value at the key '".$key."'"); |
|
245
|
|
|
} else { |
|
246
|
|
|
// we are not at the leaf yet, add this chunk to the key and recurse |
|
247
|
|
|
$this->set($key.'.'.$k, $v); |
|
248
|
|
|
} |
|
249
|
|
|
} else { |
|
250
|
|
|
// a leaf, recurse one last time |
|
251
|
|
|
$this->set($k, $v); |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
else { |
|
256
|
|
|
// make sure we can save this |
|
257
|
|
|
if ($this->isReadOnly($key)) { |
|
258
|
|
|
throw new \Exception("The setting '".$key."' is read only"); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
// flush the cache and save the value in db and cache |
|
262
|
|
|
$this->flush($key); |
|
263
|
|
|
DbConfig::updateOrCreate(['config_name' => $key], ['config_value' => $value]); |
|
|
|
|
|
|
264
|
|
|
Cache::tags(self::$cache_tag)->put($key, $value, $this->cache_time); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Check if the key is read only. |
|
271
|
|
|
* When the user is not a global admin. |
|
272
|
|
|
* Currently, $key is unused |
|
273
|
|
|
* |
|
274
|
|
|
* @param string $key The path to check |
|
275
|
|
|
* @return boolean false or the source: config | auth |
|
276
|
|
|
*/ |
|
277
|
|
|
public function isReadOnly($key) |
|
|
|
|
|
|
278
|
|
|
{ |
|
279
|
|
|
$user = \Auth::user(); |
|
280
|
|
|
return (is_null($user) || !$user->isAdmin()); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Push a value onto an array configuration value. |
|
285
|
|
|
* |
|
286
|
|
|
* @param string $key |
|
287
|
|
|
* @param mixed $value |
|
288
|
|
|
* @return void |
|
289
|
|
|
*/ |
|
290
|
|
|
public function push($key, $value) |
|
291
|
|
|
{ |
|
292
|
|
|
$var = $this->get($key); |
|
293
|
|
|
if (is_array($var)) { |
|
294
|
|
|
$var[] = $value; |
|
295
|
|
|
$this->set($key, $var); |
|
296
|
|
|
} |
|
297
|
|
View Code Duplication |
else { |
|
|
|
|
|
|
298
|
|
|
$arr = array(); |
|
299
|
|
|
if (!is_null($var)) { |
|
300
|
|
|
$arr[] = $var; |
|
301
|
|
|
} |
|
302
|
|
|
$arr[] = $value; |
|
303
|
|
|
|
|
304
|
|
|
$this->forget($key); |
|
305
|
|
|
$this->set($key, $arr); |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: