3 * @copyright Copyright (C) 2020, Friendica
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 */
40 /** @var int Default value for a config source */
41 const SOURCE_DEFAULT = self::SOURCE_FILE;
56 private $hidePasswordOutput;
59 * @param array $config A initial config array
60 * @param bool $hidePasswordOutput True, if cache variables should take extra care of password values
61 * @param int $source Sets a source of the initial config values
63 public function __construct(array $config = [], bool $hidePasswordOutput = true, $source = self::SOURCE_DEFAULT)
65 $this->hidePasswordOutput = $hidePasswordOutput;
66 $this->load($config, $source);
70 * Tries to load the specified configuration array into the config array.
71 * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
73 * @param array $config
74 * @param int $source Indicates the source of the config entry
76 public function load(array $config, int $source = self::SOURCE_DEFAULT)
78 $categories = array_keys($config);
80 foreach ($categories as $category) {
81 if (is_array($config[$category])) {
82 $keys = array_keys($config[$category]);
84 foreach ($keys as $key) {
85 $value = $config[$category][$key];
87 $this->set($category, $key, $value, $source);
95 * Gets a value from the config cache.
97 * @param string $cat Config category
98 * @param string $key Config key
100 * @return null|mixed Returns the value of the Config entry or null if not set
102 public function get(string $cat, string $key = null)
104 if (isset($this->config[$cat][$key])) {
105 return $this->config[$cat][$key];
106 } else if (!isset($key) && isset($this->config[$cat])) {
107 return $this->config[$cat];
114 * Sets a value in the config cache. Accepts raw output from the config table
116 * @param string $cat Config category
117 * @param string $key Config key
118 * @param mixed $value Value to set
119 * @param int $source The source of the current config key
121 * @return bool True, if the value is set
123 public function set(string $cat, string $key, $value, $source = self::SOURCE_DEFAULT)
125 if (!isset($this->config[$cat])) {
126 $this->config[$cat] = [];
127 $this->source[$cat] = [];
130 if (isset($this->source[$cat][$key]) &&
131 $source < $this->source[$cat][$key]) {
135 if ($this->hidePasswordOutput &&
136 $key == 'password' &&
138 $this->config[$cat][$key] = new HiddenString((string)$value);
140 $this->config[$cat][$key] = $value;
143 $this->source[$cat][$key] = $source;
149 * Deletes a value from the config cache.
151 * @param string $cat Config category
152 * @param string $key Config key
154 * @return bool true, if deleted
156 public function delete(string $cat, string $key)
158 if (isset($this->config[$cat][$key])) {
159 unset($this->config[$cat][$key]);
160 unset($this->source[$cat][$key]);
161 if (count($this->config[$cat]) == 0) {
162 unset($this->config[$cat]);
163 unset($this->source[$cat]);
172 * Returns the whole configuration
174 * @return array The configuration
176 public function getAll()
178 return $this->config;
182 * Returns an array with missing categories/Keys
184 * @param array $config The array to check
188 public function keyDiff(array $config)
192 $categories = array_keys($config);
194 foreach ($categories as $category) {
195 if (is_array($config[$category])) {
196 $keys = array_keys($config[$category]);
198 foreach ($keys as $key) {
199 if (!isset($this->config[$category][$key])) {
200 $return[$category][$key] = $config[$category][$key];