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\PConfig;
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($uid, array $config)
62 $categories = array_keys($config);
64 foreach ($categories as $category) {
65 if (isset($config[$category]) && is_array($config[$category])) {
67 $keys = array_keys($config[$category]);
69 foreach ($keys as $key) {
70 $value = $config[$category][$key];
72 $this->set($uid, $category, $key, $value);
80 * Retrieves a value from the user config cache
82 * @param int $uid User Id
83 * @param string $cat Config category
84 * @param string $key Config key
86 * @return null|string The value of the config entry or null if not set
88 public function get($uid, string $cat, string $key = null)
94 if (isset($this->config[$uid][$cat][$key])) {
95 return $this->config[$uid][$cat][$key];
96 } elseif (!isset($key) && isset($this->config[$uid][$cat])) {
97 return $this->config[$uid][$cat];
104 * Sets a value in the user config cache
106 * Accepts raw output from the pconfig table
108 * @param int $uid User Id
109 * @param string $cat Config category
110 * @param string $key Config key
111 * @param mixed $value Value to set
113 * @return bool Set successful
115 public function set($uid, string $cat, string $key, $value)
121 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
122 $this->config[$uid] = [];
125 if (!isset($this->config[$uid][$cat])) {
126 $this->config[$uid][$cat] = [];
129 if ($this->hidePasswordOutput &&
130 $key == 'password' &&
131 !empty($value) && is_string($value)) {
132 $this->config[$uid][$cat][$key] = new HiddenString((string)$value);
134 $this->config[$uid][$cat][$key] = $value;
142 * Deletes a value from the user config cache
144 * @param int $uid User Id
145 * @param string $cat Config category
146 * @param string $key Config key
148 * @return bool true, if deleted
150 public function delete($uid, string $cat, string $key)
156 if (isset($this->config[$uid][$cat][$key])) {
157 unset($this->config[$uid][$cat][$key]);
158 if (count($this->config[$uid][$cat]) == 0) {
159 unset($this->config[$uid][$cat]);
160 if (count($this->config[$uid]) == 0) {
161 unset($this->config[$uid]);
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];