3 namespace Friendica\Core\PConfig;
5 use ParagonIE\HiddenString\HiddenString;
8 * The Friendica config cache for users
20 private $hidePasswordOutput;
23 * @param bool $hidePasswordOutput True, if cache variables should take extra care of password values
25 public function __construct(bool $hidePasswordOutput = true)
27 $this->hidePasswordOutput = $hidePasswordOutput;
31 * Tries to load the specified configuration array into the user specific config array.
32 * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
35 * @param array $config
37 public function load($uid, array $config)
43 $categories = array_keys($config);
45 foreach ($categories as $category) {
46 if (isset($config[$category]) && is_array($config[$category])) {
48 $keys = array_keys($config[$category]);
50 foreach ($keys as $key) {
51 $value = $config[$category][$key];
53 $this->set($uid, $category, $key, $value);
61 * Retrieves a value from the user config cache
63 * @param int $uid User Id
64 * @param string $cat Config category
65 * @param string $key Config key
67 * @return null|string The value of the config entry or null if not set
69 public function get($uid, string $cat, string $key = null)
75 if (isset($this->config[$uid][$cat][$key])) {
76 return $this->config[$uid][$cat][$key];
77 } elseif (!isset($key) && isset($this->config[$uid][$cat])) {
78 return $this->config[$uid][$cat];
85 * Sets a value in the user config cache
87 * Accepts raw output from the pconfig table
89 * @param int $uid User Id
90 * @param string $cat Config category
91 * @param string $key Config key
92 * @param mixed $value Value to set
94 * @return bool Set successful
96 public function set($uid, string $cat, string $key, $value)
102 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
103 $this->config[$uid] = [];
106 if (!isset($this->config[$uid][$cat])) {
107 $this->config[$uid][$cat] = [];
110 if ($this->hidePasswordOutput &&
111 $key == 'password' &&
112 !empty($value) && is_string($value)) {
113 $this->config[$uid][$cat][$key] = new HiddenString((string)$value);
115 $this->config[$uid][$cat][$key] = $value;
123 * Deletes a value from the user config cache
125 * @param int $uid User Id
126 * @param string $cat Config category
127 * @param string $key Config key
129 * @return bool true, if deleted
131 public function delete($uid, string $cat, string $key)
137 if (isset($this->config[$uid][$cat][$key])) {
138 unset($this->config[$uid][$cat][$key]);
139 if (count($this->config[$uid][$cat]) == 0) {
140 unset($this->config[$uid][$cat]);
141 if (count($this->config[$uid]) == 0) {
142 unset($this->config[$uid]);
153 * Returns the whole configuration
155 * @return array The configuration
157 public function getAll()
159 return $this->config;
163 * Returns an array with missing categories/Keys
165 * @param array $config The array to check
169 public function keyDiff(array $config)
173 $categories = array_keys($config);
175 foreach ($categories as $category) {
176 if (is_array($config[$category])) {
177 $keys = array_keys($config[$category]);
179 foreach ($keys as $key) {
180 if (!isset($this->config[$category][$key])) {
181 $return[$category][$key] = $config[$category][$key];