]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Cache.php
Merge pull request #9160 from MrPetovan/bug/9138-escape-field-input
[friendica.git] / src / Core / Config / Cache.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core\Config;
23
24 use ParagonIE\HiddenString\HiddenString;
25
26 /**
27  * The Friendica config cache for the application
28  * Initial, all *.config.php files are loaded into this cache with the
29  * ConfigFileLoader ( @see ConfigFileLoader )
30  */
31 class Cache
32 {
33         /**
34          * @var array
35          */
36         private $config;
37
38         /**
39          * @var bool
40          */
41         private $hidePasswordOutput;
42
43         /**
44          * @param array $config             A initial config array
45          * @param bool  $hidePasswordOutput True, if cache variables should take extra care of password values
46          */
47         public function __construct(array $config = [], bool $hidePasswordOutput = true)
48         {
49                 $this->hidePasswordOutput = $hidePasswordOutput;
50                 $this->load($config);
51         }
52
53         /**
54          * Tries to load the specified configuration array into the config array.
55          * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
56          *
57          * @param array $config
58          * @param bool  $overwrite Force value overwrite if the config key already exists
59          */
60         public function load(array $config, bool $overwrite = false)
61         {
62                 $categories = array_keys($config);
63
64                 foreach ($categories as $category) {
65                         if (is_array($config[$category])) {
66                                 $keys = array_keys($config[$category]);
67
68                                 foreach ($keys as $key) {
69                                         $value = $config[$category][$key];
70                                         if (isset($value)) {
71                                                 if ($overwrite) {
72                                                         $this->set($category, $key, $value);
73                                                 } else {
74                                                         $this->setDefault($category, $key, $value);
75                                                 }
76                                         }
77                                 }
78                         }
79                 }
80         }
81
82         /**
83          * Gets a value from the config cache.
84          *
85          * @param string $cat Config category
86          * @param string $key Config key
87          *
88          * @return null|mixed Returns the value of the Config entry or null if not set
89          */
90         public function get(string $cat, string $key = null)
91         {
92                 if (isset($this->config[$cat][$key])) {
93                         return $this->config[$cat][$key];
94                 } elseif (!isset($key) && isset($this->config[$cat])) {
95                         return $this->config[$cat];
96                 } else {
97                         return null;
98                 }
99         }
100
101         /**
102          * Sets a default value in the config cache. Ignores already existing keys.
103          *
104          * @param string $cat   Config category
105          * @param string $key   Config key
106          * @param mixed  $value Default value to set
107          */
108         private function setDefault(string $cat, string $key, $value)
109         {
110                 if (!isset($this->config[$cat][$key])) {
111                         $this->set($cat, $key, $value);
112                 }
113         }
114
115         /**
116          * Sets a value in the config cache. Accepts raw output from the config table
117          *
118          * @param string $cat   Config category
119          * @param string $key   Config key
120          * @param mixed  $value Value to set
121          *
122          * @return bool True, if the value is set
123          */
124         public function set(string $cat, string $key, $value)
125         {
126                 if (!isset($this->config[$cat])) {
127                         $this->config[$cat] = [];
128                 }
129
130                 if ($this->hidePasswordOutput &&
131                     $key == 'password' &&
132                     is_string($value)) {
133                         $this->config[$cat][$key] = new HiddenString((string)$value);
134                 } else {
135                         $this->config[$cat][$key] = $value;
136                 }
137                 return true;
138         }
139
140         /**
141          * Deletes a value from the config cache.
142          *
143          * @param string $cat Config category
144          * @param string $key Config key
145          *
146          * @return bool true, if deleted
147          */
148         public function delete(string $cat, string $key)
149         {
150                 if (isset($this->config[$cat][$key])) {
151                         unset($this->config[$cat][$key]);
152                         if (count($this->config[$cat]) == 0) {
153                                 unset($this->config[$cat]);
154                         }
155                         return true;
156                 } else {
157                         return false;
158                 }
159         }
160
161         /**
162          * Returns the whole configuration
163          *
164          * @return array The configuration
165          */
166         public function getAll()
167         {
168                 return $this->config;
169         }
170
171         /**
172          * Returns an array with missing categories/Keys
173          *
174          * @param array $config The array to check
175          *
176          * @return array
177          */
178         public function keyDiff(array $config)
179         {
180                 $return = [];
181
182                 $categories = array_keys($config);
183
184                 foreach ($categories as $category) {
185                         if (is_array($config[$category])) {
186                                 $keys = array_keys($config[$category]);
187
188                                 foreach ($keys as $key) {
189                                         if (!isset($this->config[$category][$key])) {
190                                                 $return[$category][$key] = $config[$category][$key];
191                                         }
192                                 }
193                         }
194                 }
195
196                 return $return;
197         }
198 }