3 * @copyright Copyright (C) 2010-2023, 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\PConfig\ValueObject;
24 use ParagonIE\HiddenString\HiddenString;
27 * The Friendica config cache for users
39 private $hidePasswordOutput;
42 * @param bool $hidePasswordOutput True, if cache variables should take extra care of password values
44 public function __construct(bool $hidePasswordOutput = true)
46 $this->hidePasswordOutput = $hidePasswordOutput;
50 * Tries to load the specified configuration array into the user specific config array.
51 * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
54 * @param array $config
56 public function load(int $uid, array $config)
62 $categories = array_keys($config);
64 foreach ($categories as $category) {
65 if (isset($config[$category]) && is_array($config[$category])) {
66 $keys = array_keys($config[$category]);
68 foreach ($keys as $key) {
69 $value = $config[$category][$key];
71 $this->set($uid, $category, $key, $value);
79 * Retrieves a value from the user config cache
81 * @param int $uid User Id
82 * @param string $cat Config category
83 * @param string|null $key Config key
85 * @return null|mixed The value of the config entry or null if not set
87 public function get(int $uid, string $cat, ?string $key = null)
93 if (isset($this->config[$uid][$cat][$key])) {
94 return $this->config[$uid][$cat][$key];
95 } elseif (!isset($key) && isset($this->config[$uid][$cat])) {
96 return $this->config[$uid][$cat];
103 * Sets a value in the user config cache
105 * Accepts raw output from the pconfig table
107 * @param int $uid User Id
108 * @param string $cat Config category
109 * @param string $key Config key
110 * @param mixed $value Value to set
112 * @return bool Set successful
114 public function set(int $uid, string $cat, string $key, $value): bool
120 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
121 $this->config[$uid] = [];
124 if (!isset($this->config[$uid][$cat])) {
125 $this->config[$uid][$cat] = [];
128 if ($this->hidePasswordOutput &&
129 $key == 'password' &&
130 !empty($value) && is_string($value)) {
131 $this->config[$uid][$cat][$key] = new HiddenString((string)$value);
133 $this->config[$uid][$cat][$key] = $value;
141 * Deletes a value from the user config cache
143 * @param int $uid User Id
144 * @param string $cat Config category
145 * @param string $key Config key
147 * @return bool true, if deleted
149 public function delete(int $uid, string $cat, string $key): bool
155 if (isset($this->config[$uid][$cat][$key])) {
156 unset($this->config[$uid][$cat][$key]);
157 if (count($this->config[$uid][$cat]) == 0) {
158 unset($this->config[$uid][$cat]);
159 if (count($this->config[$uid]) == 0) {
160 unset($this->config[$uid]);
171 * Returns the whole configuration
173 * @return string[][] The configuration
175 public function getAll(): array
177 return $this->config;
181 * Returns an array with missing categories/Keys
183 * @param string[][] $config The array to check
187 public function keyDiff(array $config): array
191 $categories = array_keys($config);
193 foreach ($categories as $category) {
194 if (is_array($config[$category])) {
195 $keys = array_keys($config[$category]);
197 foreach ($keys as $key) {
198 if (!isset($this->config[$category][$key])) {
199 $return[$category][$key] = $config[$category][$key];