]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig/ValueObject/Cache.php
spelling: names
[friendica.git] / src / Core / PConfig / ValueObject / Cache.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
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\PConfig\ValueObject;
23
24 use ParagonIE\HiddenString\HiddenString;
25
26 /**
27  * The Friendica config cache for users
28  */
29 class Cache
30 {
31         /**
32          * @var array
33          */
34         private $config = [];
35
36         /**
37          * @var bool
38          */
39         private $hidePasswordOutput;
40
41         /**
42          * @param bool $hidePasswordOutput True, if cache variables should take extra care of password values
43          */
44         public function __construct(bool $hidePasswordOutput = true)
45         {
46                 $this->hidePasswordOutput = $hidePasswordOutput;
47         }
48
49         /**
50          * Tries to load the specified configuration array into the user specific config array.
51          * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
52          *
53          * @param int   $uid
54          * @param array $config
55          */
56         public function load(int $uid, array $config)
57         {
58                 if (!is_int($uid)) {
59                         return;
60                 }
61
62                 $categories = array_keys($config);
63
64                 foreach ($categories as $category) {
65                         if (isset($config[$category]) && 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                                                 $this->set($uid, $category, $key, $value);
72                                         }
73                                 }
74                         }
75                 }
76         }
77
78         /**
79          * Retrieves a value from the user config cache
80          *
81          * @param int    $uid User Id
82          * @param string $cat Config category
83          * @param string|null $key Config key
84          *
85          * @return null|mixed The value of the config entry or null if not set
86          */
87         public function get(int $uid, string $cat, ?string $key = null)
88         {
89                 if (!is_int($uid)) {
90                         return null;
91                 }
92
93                 if (isset($this->config[$uid][$cat][$key])) {
94                         return $this->config[$uid][$cat][$key];
95                 } elseif (!isset($key) && isset($this->config[$uid][$cat])) {
96                         return $this->config[$uid][$cat];
97                 } else {
98                         return null;
99                 }
100         }
101
102         /**
103          * Sets a value in the user config cache
104          *
105          * Accepts raw output from the pconfig table
106          *
107          * @param int    $uid   User Id
108          * @param string $cat   Config category
109          * @param string $key   Config key
110          * @param mixed  $value Value to set
111          *
112          * @return bool Set successful
113          */
114         public function set(int $uid, string $cat, string $key, $value): bool
115         {
116                 if (!is_int($uid)) {
117                         return false;
118                 }
119
120                 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
121                         $this->config[$uid] = [];
122                 }
123
124                 if (!isset($this->config[$uid][$cat])) {
125                         $this->config[$uid][$cat] = [];
126                 }
127
128                 if ($this->hidePasswordOutput &&
129                         $key == 'password' &&
130                         !empty($value) && is_string($value)) {
131                         $this->config[$uid][$cat][$key] = new HiddenString((string)$value);
132                 } else {
133                         $this->config[$uid][$cat][$key] = $value;
134                 }
135
136
137                 return true;
138         }
139
140         /**
141          * Deletes a value from the user config cache
142          *
143          * @param int    $uid User Id
144          * @param string $cat Config category
145          * @param string $key Config key
146          *
147          * @return bool true, if deleted
148          */
149         public function delete(int $uid, string $cat, string $key): bool
150         {
151                 if (!is_int($uid)) {
152                         return false;
153                 }
154
155                 if (isset($this->config[$uid][$cat][$key])) {
156                         unset($this->config[$uid][$cat][$key]);
157                         if (count($this->config[$uid][$cat]) == 0) {
158                                 unset($this->config[$uid][$cat]);
159                                 if (count($this->config[$uid]) == 0) {
160                                         unset($this->config[$uid]);
161                                 }
162                         }
163
164                         return true;
165                 } else {
166                         return false;
167                 }
168         }
169
170         /**
171          * Returns the whole configuration
172          *
173          * @return string[][] The configuration
174          */
175         public function getAll(): array
176         {
177                 return $this->config;
178         }
179
180         /**
181          * Returns an array with missing categories/Keys
182          *
183          * @param string[][] $config The array to check
184          *
185          * @return string[][]
186          */
187         public function keyDiff(array $config): array
188         {
189                 $return = [];
190
191                 $categories = array_keys($config);
192
193                 foreach ($categories as $category) {
194                         if (is_array($config[$category])) {
195                                 $keys = array_keys($config[$category]);
196
197                                 foreach ($keys as $key) {
198                                         if (!isset($this->config[$category][$key])) {
199                                                 $return[$category][$key] = $config[$category][$key];
200                                         }
201                                 }
202                         }
203                 }
204
205                 return $return;
206         }
207 }