3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Core\Config;
24 use ParagonIE\HiddenString\HiddenString;
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 )
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 */
37 /** @var int Indicates that the cache entry is set by a server environment variable - High Priority */
39 /** @var int Indicates that the cache entry is fixed and must not be changed */
42 /** @var int Default value for a config source */
43 const SOURCE_DEFAULT = self::SOURCE_FILE;
58 private $hidePasswordOutput;
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
65 public function __construct(array $config = [], bool $hidePasswordOutput = true, $source = self::SOURCE_DEFAULT)
67 $this->hidePasswordOutput = $hidePasswordOutput;
68 $this->load($config, $source);
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.
75 * @param array $config
76 * @param int $source Indicates the source of the config entry
78 public function load(array $config, int $source = self::SOURCE_DEFAULT)
80 $categories = array_keys($config);
82 foreach ($categories as $category) {
83 if (is_array($config[$category])) {
84 $keys = array_keys($config[$category]);
86 foreach ($keys as $key) {
87 $value = $config[$category][$key];
89 $this->set($category, $key, $value, $source);
97 * Gets a value from the config cache.
99 * @param string $cat Config category
100 * @param string $key Config key
102 * @return null|mixed Returns the value of the Config entry or null if not set
104 public function get(string $cat, string $key = null)
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];
116 * Sets a value in the config cache. Accepts raw output from the config table
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
123 * @return bool True, if the value is set
125 public function set(string $cat, string $key, $value, $source = self::SOURCE_DEFAULT)
127 if (!isset($this->config[$cat])) {
128 $this->config[$cat] = [];
129 $this->source[$cat] = [];
132 if (isset($this->source[$cat][$key]) &&
133 $source < $this->source[$cat][$key]) {
137 if ($this->hidePasswordOutput &&
138 $key == 'password' &&
140 $this->config[$cat][$key] = new HiddenString((string)$value);
142 $this->config[$cat][$key] = $value;
145 $this->source[$cat][$key] = $source;
151 * Deletes a value from the config cache.
153 * @param string $cat Config category
154 * @param string $key Config key
156 * @return bool true, if deleted
158 public function delete(string $cat, string $key)
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]);
174 * Returns the whole configuration
176 * @return array The configuration
178 public function getAll()
180 return $this->config;
184 * Returns an array with missing categories/Keys
186 * @param array $config The array to check
190 public function keyDiff(array $config)
194 $categories = array_keys($config);
196 foreach ($categories as $category) {
197 if (is_array($config[$category])) {
198 $keys = array_keys($config[$category]);
200 foreach ($keys as $key) {
201 if (!isset($this->config[$category][$key])) {
202 $return[$category][$key] = $config[$category][$key];