]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Cache/PConfigCache.php
Merge pull request #7371 from nupplaphil/task/split_cache
[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($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                 $categories = array_keys($config);
40
41                 foreach ($categories as $category) {
42                         if (isset($config[$category]) && is_array($config[$category])) {
43
44                                 $keys = array_keys($config[$category]);
45
46                                 foreach ($keys as $key) {
47                                         $value = $config[$category][$key];
48                                         if (isset($value)) {
49                                                 $this->set($uid, $category, $key, $value);
50                                         }
51                                 }
52                         }
53                 }
54         }
55
56         /**
57          * Retrieves a value from the user config cache
58          *
59          * @param int    $uid     User Id
60          * @param string $cat     Config category
61          * @param string $key     Config key
62          *
63          * @return null|string The value of the config entry or null if not set
64          */
65         public function get($uid, $cat, $key = null)
66         {
67                 if (isset($this->config[$uid][$cat][$key])) {
68                         return $this->config[$uid][$cat][$key];
69                 } elseif (!isset($key) && isset($this->config[$uid][$cat])) {
70                         return $this->config[$uid][$cat];
71                 } else {
72                         return null;
73                 }
74         }
75
76         /**
77          * Sets a value in the user config cache
78          *
79          * Accepts raw output from the pconfig table
80          *
81          * @param int    $uid   User Id
82          * @param string $cat   Config category
83          * @param string $key   Config key
84          * @param mixed  $value Value to set
85          *
86          * @return bool Set successful
87          */
88         public function set($uid, $cat, $key, $value)
89         {
90                 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
91                         $this->config[$uid] = [];
92                 }
93
94                 if (!isset($this->config[$uid][$cat])) {
95                         $this->config[$uid][$cat] = [];
96                 }
97
98                 if ($this->hidePasswordOutput &&
99                     $key == 'password' &&
100                     !empty($value) && is_string($value)) {
101                         $this->config[$uid][$cat][$key] = new HiddenString((string) $value);
102                 } else {
103                         $this->config[$uid][$cat][$key] = $value;
104                 }
105
106
107                 return true;
108         }
109
110         /**
111          * Deletes a value from the user config cache
112          *
113          * @param int    $uid User Id
114          * @param string $cat Config category
115          * @param string $key Config key
116          *
117          * @return bool true, if deleted
118          */
119         public function delete($uid, $cat, $key)
120         {
121                 if (isset($this->config[$uid][$cat][$key])) {
122                         unset($this->config[$uid][$cat][$key]);
123                         if (count($this->config[$uid][$cat]) == 0) {
124                                 unset($this->config[$uid][$cat]);
125                                 if (count($this->config[$uid]) == 0) {
126                                         unset($this->config[$uid]);
127                                 }
128                         }
129
130                         return true;
131                 } else {
132                         return false;
133                 }
134         }
135
136         /**
137          * Returns the whole configuration
138          *
139          * @return array The configuration
140          */
141         public function getAll()
142         {
143                 return $this->config;
144         }
145
146         /**
147          * Returns an array with missing categories/Keys
148          *
149          * @param array $config The array to check
150          *
151          * @return array
152          */
153         public function keyDiff(array $config)
154         {
155                 $return = [];
156
157                 $categories = array_keys($config);
158
159                 foreach ($categories as $category) {
160                         if (is_array($config[$category])) {
161                                 $keys = array_keys($config[$category]);
162
163                                 foreach ($keys as $key) {
164                                         if (!isset($this->config[$category][$key])) {
165                                                 $return[$category][$key] = $config[$category][$key];
166                                         }
167                                 }
168                         }
169                 }
170
171                 return $return;
172         }
173 }