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