]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Cache.php
Merge pull request #9373 from nupplaphil/task/server_env
[friendica.git] / src / Core / Config / 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\Config;
23
24 use ParagonIE\HiddenString\HiddenString;
25
26 /**
27  * The Friendica config cache for the application
28  * Initial, all *.config.php files are loaded into this cache with the
29  * ConfigFileLoader ( @see ConfigFileLoader )
30  */
31 class Cache
32 {
33         /** @var int Indicates that the cache entry is set by file - Low Priority */
34         const SOURCE_FILE = 0;
35         /** @var int Indicates that the cache entry is set by the DB config table - Middle Priority */
36         const SOURCE_DB = 1;
37         /** @var int Indicates that the cache entry is set by a server environment variable - High Priority */
38         const SOURCE_ENV = 3;
39         /** @var int Indicates that the cache entry is fixed and must not be changed */
40         const SOURCE_FIX = 4;
41
42         /** @var int Default value for a config source */
43         const SOURCE_DEFAULT = self::SOURCE_FILE;
44
45         /**
46          * @var array
47          */
48         private $config;
49
50         /**
51          * @var int[][]
52          */
53         private $source = [];
54
55         /**
56          * @var bool
57          */
58         private $hidePasswordOutput;
59
60         /**
61          * @param array $config             A initial config array
62          * @param bool  $hidePasswordOutput True, if cache variables should take extra care of password values
63          * @param int   $source             Sets a source of the initial config values
64          */
65         public function __construct(array $config = [], bool $hidePasswordOutput = true, $source = self::SOURCE_DEFAULT)
66         {
67                 $this->hidePasswordOutput = $hidePasswordOutput;
68                 $this->load($config, $source);
69         }
70
71         /**
72          * Tries to load the specified configuration array into the config array.
73          * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
74          *
75          * @param array $config
76          * @param int   $source Indicates the source of the config entry
77          */
78         public function load(array $config, int $source = self::SOURCE_DEFAULT)
79         {
80                 $categories = array_keys($config);
81
82                 foreach ($categories as $category) {
83                         if (is_array($config[$category])) {
84                                 $keys = array_keys($config[$category]);
85
86                                 foreach ($keys as $key) {
87                                         $value = $config[$category][$key];
88                                         if (isset($value)) {
89                                                 $this->set($category, $key, $value, $source);
90                                         }
91                                 }
92                         }
93                 }
94         }
95
96         /**
97          * Gets a value from the config cache.
98          *
99          * @param string $cat Config category
100          * @param string $key Config key
101          *
102          * @return null|mixed Returns the value of the Config entry or null if not set
103          */
104         public function get(string $cat, string $key = null)
105         {
106                 if (isset($this->config[$cat][$key])) {
107                         return $this->config[$cat][$key];
108                 } else if (!isset($key) && isset($this->config[$cat])) {
109                         return $this->config[$cat];
110                 } else {
111                         return null;
112                 }
113         }
114
115         /**
116          * Sets a value in the config cache. Accepts raw output from the config table
117          *
118          * @param string $cat    Config category
119          * @param string $key    Config key
120          * @param mixed  $value  Value to set
121          * @param int    $source The source of the current config key
122          *
123          * @return bool True, if the value is set
124          */
125         public function set(string $cat, string $key, $value, $source = self::SOURCE_DEFAULT)
126         {
127                 if (!isset($this->config[$cat])) {
128                         $this->config[$cat] = [];
129                         $this->source[$cat] = [];
130                 }
131
132                 if (isset($this->source[$cat][$key]) &&
133                         $source < $this->source[$cat][$key]) {
134                         return false;
135                 }
136
137                 if ($this->hidePasswordOutput &&
138                         $key == 'password' &&
139                         is_string($value)) {
140                         $this->config[$cat][$key] = new HiddenString((string)$value);
141                 } else {
142                         $this->config[$cat][$key] = $value;
143                 }
144
145                 $this->source[$cat][$key] = $source;
146
147                 return true;
148         }
149
150         /**
151          * Deletes a value from the config cache.
152          *
153          * @param string $cat Config category
154          * @param string $key Config key
155          *
156          * @return bool true, if deleted
157          */
158         public function delete(string $cat, string $key)
159         {
160                 if (isset($this->config[$cat][$key])) {
161                         unset($this->config[$cat][$key]);
162                         unset($this->source[$cat][$key]);
163                         if (count($this->config[$cat]) == 0) {
164                                 unset($this->config[$cat]);
165                                 unset($this->source[$cat]);
166                         }
167                         return true;
168                 } else {
169                         return false;
170                 }
171         }
172
173         /**
174          * Returns the whole configuration
175          *
176          * @return array The configuration
177          */
178         public function getAll()
179         {
180                 return $this->config;
181         }
182
183         /**
184          * Returns an array with missing categories/Keys
185          *
186          * @param array $config The array to check
187          *
188          * @return array
189          */
190         public function keyDiff(array $config)
191         {
192                 $return = [];
193
194                 $categories = array_keys($config);
195
196                 foreach ($categories as $category) {
197                         if (is_array($config[$category])) {
198                                 $keys = array_keys($config[$category]);
199
200                                 foreach ($keys as $key) {
201                                         if (!isset($this->config[$category][$key])) {
202                                                 $return[$category][$key] = $config[$category][$key];
203                                         }
204                                 }
205                         }
206                 }
207
208                 return $return;
209         }
210 }