]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig/Cache.php
Remove confirm template obsolete uses (except for contacts)
[friendica.git] / src / Core / PConfig / 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\PConfig;
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($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
67                                 $keys = array_keys($config[$category]);
68
69                                 foreach ($keys as $key) {
70                                         $value = $config[$category][$key];
71                                         if (isset($value)) {
72                                                 $this->set($uid, $category, $key, $value);
73                                         }
74                                 }
75                         }
76                 }
77         }
78
79         /**
80          * Retrieves a value from the user config cache
81          *
82          * @param int    $uid User Id
83          * @param string $cat Config category
84          * @param string $key Config key
85          *
86          * @return null|string The value of the config entry or null if not set
87          */
88         public function get($uid, string $cat, string $key = null)
89         {
90                 if (!is_int($uid)) {
91                         return null;
92                 }
93
94                 if (isset($this->config[$uid][$cat][$key])) {
95                         return $this->config[$uid][$cat][$key];
96                 } elseif (!isset($key) && isset($this->config[$uid][$cat])) {
97                         return $this->config[$uid][$cat];
98                 } else {
99                         return null;
100                 }
101         }
102
103         /**
104          * Sets a value in the user config cache
105          *
106          * Accepts raw output from the pconfig table
107          *
108          * @param int    $uid   User Id
109          * @param string $cat   Config category
110          * @param string $key   Config key
111          * @param mixed  $value Value to set
112          *
113          * @return bool Set successful
114          */
115         public function set($uid, string $cat, string $key, $value)
116         {
117                 if (!is_int($uid)) {
118                         return false;
119                 }
120
121                 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
122                         $this->config[$uid] = [];
123                 }
124
125                 if (!isset($this->config[$uid][$cat])) {
126                         $this->config[$uid][$cat] = [];
127                 }
128
129                 if ($this->hidePasswordOutput &&
130                     $key == 'password' &&
131                     !empty($value) && is_string($value)) {
132                         $this->config[$uid][$cat][$key] = new HiddenString((string)$value);
133                 } else {
134                         $this->config[$uid][$cat][$key] = $value;
135                 }
136
137
138                 return true;
139         }
140
141         /**
142          * Deletes a value from the user config cache
143          *
144          * @param int    $uid User Id
145          * @param string $cat Config category
146          * @param string $key Config key
147          *
148          * @return bool true, if deleted
149          */
150         public function delete($uid, string $cat, string $key)
151         {
152                 if (!is_int($uid)) {
153                         return false;
154                 }
155
156                 if (isset($this->config[$uid][$cat][$key])) {
157                         unset($this->config[$uid][$cat][$key]);
158                         if (count($this->config[$uid][$cat]) == 0) {
159                                 unset($this->config[$uid][$cat]);
160                                 if (count($this->config[$uid]) == 0) {
161                                         unset($this->config[$uid]);
162                                 }
163                         }
164
165                         return true;
166                 } else {
167                         return false;
168                 }
169         }
170
171         /**
172          * Returns the whole configuration
173          *
174          * @return array The configuration
175          */
176         public function getAll()
177         {
178                 return $this->config;
179         }
180
181         /**
182          * Returns an array with missing categories/Keys
183          *
184          * @param array $config The array to check
185          *
186          * @return array
187          */
188         public function keyDiff(array $config)
189         {
190                 $return = [];
191
192                 $categories = array_keys($config);
193
194                 foreach ($categories as $category) {
195                         if (is_array($config[$category])) {
196                                 $keys = array_keys($config[$category]);
197
198                                 foreach ($keys as $key) {
199                                         if (!isset($this->config[$category][$key])) {
200                                                 $return[$category][$key] = $config[$category][$key];
201                                         }
202                                 }
203                         }
204                 }
205
206                 return $return;
207         }
208 }