]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Cache/ConfigCache.php
Introduce HiddenString for Config-Values
[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                         $this->config[$cat][$key] = new HiddenString($value);
99                 } else {
100                         $this->config[$cat][$key] = $value;
101                 }
102                 return true;
103         }
104
105         /**
106          * {@inheritdoc}
107          */
108         public function delete($cat, $key)
109         {
110                 if (isset($this->config[$cat][$key])) {
111                         unset($this->config[$cat][$key]);
112                         if (count($this->config[$cat]) == 0) {
113                                 unset($this->config[$cat]);
114                         }
115                         return true;
116                 } else {
117                         return false;
118                 }
119         }
120
121         /**
122          * {@inheritdoc}
123          */
124         public function loadP($uid, array $config)
125         {
126                 $categories = array_keys($config);
127
128                 foreach ($categories as $category) {
129                         if (isset($config[$category]) && is_array($config[$category])) {
130
131                                 $keys = array_keys($config[$category]);
132
133                                 foreach ($keys as $key) {
134                                         $value = $config[$category][$key];
135                                         if (isset($value)) {
136                                                 $this->setP($uid, $category, $key, $value);
137                                         }
138                                 }
139                         }
140                 }
141         }
142
143         /**
144          * {@inheritdoc}
145          */
146         public function getP($uid, $cat, $key = null)
147         {
148                 if (isset($this->config[$uid][$cat][$key])) {
149                         return $this->config[$uid][$cat][$key];
150                 } elseif (!isset($key) && isset($this->config[$uid][$cat])) {
151                         return $this->config[$uid][$cat];
152                 } else {
153                         return null;
154                 }
155         }
156
157         /**
158          * {@inheritdoc}
159          */
160         public function setP($uid, $cat, $key, $value)
161         {
162                 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
163                         $this->config[$uid] = [];
164                 }
165
166                 if (!isset($this->config[$uid][$cat])) {
167                         $this->config[$uid][$cat] = [];
168                 }
169
170                 $this->config[$uid][$cat][$key] = $value;
171
172                 return true;
173         }
174
175         /**
176          * {@inheritdoc}
177          */
178         public function deleteP($uid, $cat, $key)
179         {
180                 if (isset($this->config[$uid][$cat][$key])) {
181                         unset($this->config[$uid][$cat][$key]);
182                         if (count($this->config[$uid][$cat]) == 0) {
183                                 unset($this->config[$uid][$cat]);
184                                 if (count($this->config[$uid]) == 0) {
185                                         unset($this->config[$uid]);
186                                 }
187                         }
188
189                         return true;
190                 } else {
191                         return false;
192                 }
193         }
194
195         /**
196          * Returns the whole configuration
197          *
198          * @return array The configuration
199          */
200         public function getAll()
201         {
202                 return $this->config;
203         }
204
205         /**
206          * Returns an array with missing categories/Keys
207          *
208          * @param array $config The array to check
209          *
210          * @return array
211          */
212         public function keyDiff(array $config)
213         {
214                 $return = [];
215
216                 $categories = array_keys($config);
217
218                 foreach ($categories as $category) {
219                         if (is_array($config[$category])) {
220                                 $keys = array_keys($config[$category]);
221
222                                 foreach ($keys as $key) {
223                                         if (!isset($this->config[$category][$key])) {
224                                                 $return[$category][$key] = $config[$category][$key];
225                                         }
226                                 }
227                         }
228                 }
229
230                 return $return;
231         }
232 }