|
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 DB; |
|
32
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigContract; |
|
33
|
|
|
|
|
34
|
|
|
// adds the possibility to replace the default Config facade |
|
35
|
|
|
|
|
36
|
|
|
class Settings implements ConfigContract |
|
37
|
|
|
{ |
|
38
|
|
|
private $cache_time; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->cache_time = env('CACHE_LIFETIME', 60); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function set($key, $value = null) |
|
46
|
|
|
{ |
|
47
|
|
|
if (is_array($value)) { |
|
48
|
|
|
$value = self::arrayToPath($value, $key); |
|
|
|
|
|
|
49
|
|
|
foreach ($value as $k => $v) { |
|
50
|
|
|
DbConfig::updateOrCreate(['config_name' => $k], ['config_value' => $v]); |
|
|
|
|
|
|
51
|
|
|
Cache::put($k, $v, $this->cache_time); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
else { |
|
55
|
|
|
DbConfig::updateOrCreate(['config_name' => $key], ['config_value' => $value]); |
|
|
|
|
|
|
56
|
|
|
Cache::put($key, $value, $this->cache_time); |
|
57
|
|
|
} |
|
58
|
|
|
return $value; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
public function get($key, $default = null) |
|
63
|
|
|
{ |
|
64
|
|
|
// return value from cache or fetch it and return it |
|
65
|
|
|
return Cache::remember($key, $this->cache_time, function () use ($key, $default) { |
|
66
|
|
|
$db_data = DbConfig::key($key)->get(['config_name', 'config_value']); |
|
67
|
|
|
|
|
68
|
|
|
if (count($db_data) == 1 && $db_data->first()->config_name == $key) { |
|
69
|
|
|
// return a bare value if we are getting one item |
|
70
|
|
|
return $db_data->first()->config_value; |
|
71
|
|
|
} |
|
72
|
|
|
elseif (count($db_data) >= 1) { |
|
73
|
|
|
$result = self::collectionToArray($db_data, $key); |
|
74
|
|
|
$config = Config::get('config.' . $key, $default); |
|
75
|
|
|
if (!is_null($config)) { |
|
76
|
|
|
$result = array_replace_recursive($config, $result); |
|
77
|
|
|
} |
|
78
|
|
|
return $result; |
|
79
|
|
|
} |
|
80
|
|
|
else { |
|
81
|
|
|
return Config::get('config.' . $key, $default); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
}); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
public function has($key) |
|
89
|
|
|
{ |
|
90
|
|
|
return (Cache::has($key) || Config::has($key) || DbConfig::exactKey($key)->exists()); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function forget($key) |
|
94
|
|
|
{ |
|
95
|
|
|
// set to null to prevent falling back to Config |
|
96
|
|
|
DbConfig::key($key)->update(['config_value' => null]); |
|
97
|
|
|
Cache::forget($key); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function all() |
|
101
|
|
|
{ |
|
102
|
|
|
// no caching :( |
|
103
|
|
|
$config_settings = Config::all()['config']; |
|
104
|
|
|
$db_settings = self::collectionToArray(DbConfig::all()); |
|
105
|
|
|
return array_replace_recursive($config_settings, $db_settings); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Prepend a value onto an array configuration value. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $key |
|
112
|
|
|
* @param mixed $value |
|
113
|
|
|
* @return void |
|
114
|
|
|
*/ |
|
115
|
|
|
public function prepend($key, $value) |
|
116
|
|
|
{ |
|
117
|
|
|
// TODO: Implement prepend() method. |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Push a value onto an array configuration value. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $key |
|
124
|
|
|
* @param mixed $value |
|
125
|
|
|
* @return void |
|
126
|
|
|
*/ |
|
127
|
|
|
public function push($key, $value) |
|
128
|
|
|
{ |
|
129
|
|
|
// TODO: Implement push() method. |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
// ---- Local Utility functions ---- |
|
133
|
|
|
|
|
134
|
|
|
private static function collectionToArray($data, $prefix = "") |
|
135
|
|
|
{ |
|
136
|
|
|
$tree = array(); |
|
137
|
|
|
foreach ($data as $item) { |
|
138
|
|
|
$key = $item->config_name; |
|
139
|
|
|
if (substr($key, 0, strlen($prefix)) == $prefix) { |
|
140
|
|
|
$key = substr($key, strlen($prefix)); |
|
141
|
|
|
} |
|
142
|
|
|
$parts = explode('.', trim($key, '.')); |
|
143
|
|
|
|
|
144
|
|
|
$temp = &$tree; |
|
145
|
|
|
foreach ($parts as $part) { |
|
146
|
|
|
$temp = &$temp[$part]; |
|
147
|
|
|
} |
|
148
|
|
|
$temp = $item->config_value; |
|
149
|
|
|
unset($temp); |
|
150
|
|
|
} |
|
151
|
|
|
return $tree; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
private static function arrayToPath($array, $prefix = "") |
|
155
|
|
|
{ |
|
156
|
|
|
return self::recursive_keys($array, $prefix); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
private static function recursive_keys(array $array, $prefix = "", array $path = array()) |
|
160
|
|
|
{ |
|
161
|
|
|
if ($prefix != "") { |
|
162
|
|
|
$prefix = trim($prefix, '.') . '.'; |
|
163
|
|
|
} |
|
164
|
|
|
$result = array(); |
|
165
|
|
|
foreach ($array as $key => $val) { |
|
166
|
|
|
$currentPath = array_merge($path, array($key)); |
|
167
|
|
|
if (is_array($val)) { |
|
168
|
|
|
$result = array_merge($result, self::recursive_keys($val, $prefix, $currentPath)); |
|
169
|
|
|
} |
|
170
|
|
|
else { |
|
171
|
|
|
$result[$prefix . join('.', $currentPath)] = $val; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
return $result; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.