]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Cache/ConfigCache.php
Merge pull request #7250 from MrPetovan/bug/6410-normalize-message-button
[friendica.git] / src / Core / Config / Cache / ConfigCache.php
1 <?php
2
3 namespace Friendica\Core\Config\Cache;
4
5 use ParagonIE\HiddenString\HiddenString;
6
7 /**
8  * The Friendica config cache for the application
9  * Initial, all *.config.php files are loaded into this cache with the
10  * ConfigFileLoader ( @see ConfigFileLoader )
11  */
12 class ConfigCache implements IConfigCache, IPConfigCache
13 {
14         /**
15          * @var array
16          */
17         private $config;
18
19         /**
20          * @var bool
21          */
22         private $hidePasswordOutput;
23
24         /**
25          * @param array $config    A initial config array
26          * @param bool  $hidePasswordOutput True, if cache variables should take extra care of password values
27          */
28         public function __construct(array $config = [], $hidePasswordOutput = true)
29         {
30                 $this->hidePasswordOutput = $hidePasswordOutput;
31                 $this->load($config);
32         }
33
34         /**
35          * {@inheritdoc}
36          */
37         public function load(array $config, $overwrite = false)
38         {
39                 $categories = array_keys($config);
40
41                 foreach ($categories as $category) {
42                         if (is_array($config[$category])) {
43                                 $keys = array_keys($config[$category]);
44
45                                 foreach ($keys as $key) {
46                                         $value = $config[$category][$key];
47                                         if (isset($value)) {
48                                                 if ($overwrite) {
49                                                         $this->set($category, $key, $value);
50                                                 } else {
51                                                         $this->setDefault($category, $key, $value);
52                                                 }
53                                         }
54                                 }
55                         }
56                 }
57         }
58
59         /**
60          * {@inheritdoc}
61          */
62         public function get($cat, $key = null)
63         {
64                 if (isset($this->config[$cat][$key])) {
65                         return $this->config[$cat][$key];
66                 } elseif (!isset($key) && isset($this->config[$cat])) {
67                         return $this->config[$cat];
68                 } else {
69                         return null;
70                 }
71         }
72
73         /**
74          * Sets a default value in the config cache. Ignores already existing keys.
75          *
76          * @param string $cat Config category
77          * @param string $k   Config key
78          * @param mixed  $v   Default value to set
79          */
80         private function setDefault($cat, $k, $v)
81         {
82                 if (!isset($this->config[$cat][$k])) {
83                         $this->set($cat, $k, $v);
84                 }
85         }
86
87         /**
88          * {@inheritdoc}
89          */
90         public function set($cat, $key, $value)
91         {
92                 if (!isset($this->config[$cat])) {
93                         $this->config[$cat] = [];
94                 }
95
96                 if ($this->hidePasswordOutput &&
97                     $key == 'password' &&
98                     !empty($value) && is_string($value)) {
99                         $this->config[$cat][$key] = new HiddenString((string) $value);
100                 } else {
101                         $this->config[$cat][$key] = $value;
102                 }
103                 return true;
104         }
105
106         /**
107          * {@inheritdoc}
108          */
109         public function delete($cat, $key)
110         {
111                 if (isset($this->config[$cat][$key])) {
112                         unset($this->config[$cat][$key]);
113                         if (count($this->config[$cat]) == 0) {
114                                 unset($this->config[$cat]);
115                         }
116                         return true;
117                 } else {
118                         return false;
119                 }
120         }
121
122         /**
123          * {@inheritdoc}
124          */
125         public function loadP($uid, array $config)
126         {
127                 $categories = array_keys($config);
128
129                 foreach ($categories as $category) {
130                         if (isset($config[$category]) && is_array($config[$category])) {
131
132                                 $keys = array_keys($config[$category]);
133
134                                 foreach ($keys as $key) {
135                                         $value = $config[$category][$key];
136                                         if (isset($value)) {
137                                                 $this->setP($uid, $category, $key, $value);
138                                         }
139                                 }
140                         }
141                 }
142         }
143
144         /**
145          * {@inheritdoc}
146          */
147         public function getP($uid, $cat, $key = null)
148         {
149                 if (isset($this->config[$uid][$cat][$key])) {
150                         return $this->config[$uid][$cat][$key];
151                 } elseif (!isset($key) && isset($this->config[$uid][$cat])) {
152                         return $this->config[$uid][$cat];
153                 } else {
154                         return null;
155                 }
156         }
157
158         /**
159          * {@inheritdoc}
160          */
161         public function setP($uid, $cat, $key, $value)
162         {
163                 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
164                         $this->config[$uid] = [];
165                 }
166
167                 if (!isset($this->config[$uid][$cat])) {
168                         $this->config[$uid][$cat] = [];
169                 }
170
171                 $this->config[$uid][$cat][$key] = $value;
172
173                 return true;
174         }
175
176         /**
177          * {@inheritdoc}
178          */
179         public function deleteP($uid, $cat, $key)
180         {
181                 if (isset($this->config[$uid][$cat][$key])) {
182                         unset($this->config[$uid][$cat][$key]);
183                         if (count($this->config[$uid][$cat]) == 0) {
184                                 unset($this->config[$uid][$cat]);
185                                 if (count($this->config[$uid]) == 0) {
186                                         unset($this->config[$uid]);
187                                 }
188                         }
189
190                         return true;
191                 } else {
192                         return false;
193                 }
194         }
195
196         /**
197          * Returns the whole configuration
198          *
199          * @return array The configuration
200          */
201         public function getAll()
202         {
203                 return $this->config;
204         }
205
206         /**
207          * Returns an array with missing categories/Keys
208          *
209          * @param array $config The array to check
210          *
211          * @return array
212          */
213         public function keyDiff(array $config)
214         {
215                 $return = [];
216
217                 $categories = array_keys($config);
218
219                 foreach ($categories as $category) {
220                         if (is_array($config[$category])) {
221                                 $keys = array_keys($config[$category]);
222
223                                 foreach ($keys as $key) {
224                                         if (!isset($this->config[$category][$key])) {
225                                                 $return[$category][$key] = $config[$category][$key];
226                                         }
227                                 }
228                         }
229                 }
230
231                 return $return;
232         }
233 }