3 namespace Friendica\Core\Config\Cache;
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(int $uid, array $config)
39 $categories = array_keys($config);
41 foreach ($categories as $category) {
42 if (isset($config[$category]) && is_array($config[$category])) {
44 $keys = array_keys($config[$category]);
46 foreach ($keys as $key) {
47 $value = $config[$category][$key];
49 $this->set($uid, $category, $key, $value);
57 * Retrieves a value from the user config cache
59 * @param int $uid User Id
60 * @param string $cat Config category
61 * @param string $key Config key
63 * @return null|string The value of the config entry or null if not set
65 public function get(int $uid, string $cat, string $key = null)
67 if (isset($this->config[$uid][$cat][$key])) {
68 return $this->config[$uid][$cat][$key];
69 } elseif (!isset($key) && isset($this->config[$uid][$cat])) {
70 return $this->config[$uid][$cat];
77 * Sets a value in the user config cache
79 * Accepts raw output from the pconfig table
81 * @param int $uid User Id
82 * @param string $cat Config category
83 * @param string $key Config key
84 * @param mixed $value Value to set
86 * @return bool Set successful
88 public function set(int $uid, string $cat, string $key, $value)
90 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
91 $this->config[$uid] = [];
94 if (!isset($this->config[$uid][$cat])) {
95 $this->config[$uid][$cat] = [];
98 if ($this->hidePasswordOutput &&
100 !empty($value) && is_string($value)) {
101 $this->config[$uid][$cat][$key] = new HiddenString((string)$value);
103 $this->config[$uid][$cat][$key] = $value;
111 * Deletes a value from the user config cache
113 * @param int $uid User Id
114 * @param string $cat Config category
115 * @param string $key Config key
117 * @return bool true, if deleted
119 public function delete(int $uid, string $cat, string $key)
121 if (isset($this->config[$uid][$cat][$key])) {
122 unset($this->config[$uid][$cat][$key]);
123 if (count($this->config[$uid][$cat]) == 0) {
124 unset($this->config[$uid][$cat]);
125 if (count($this->config[$uid]) == 0) {
126 unset($this->config[$uid]);
137 * Returns the whole configuration
139 * @return array The configuration
141 public function getAll()
143 return $this->config;
147 * Returns an array with missing categories/Keys
149 * @param array $config The array to check
153 public function keyDiff(array $config)
157 $categories = array_keys($config);
159 foreach ($categories as $category) {
160 if (is_array($config[$category])) {
161 $keys = array_keys($config[$category]);
163 foreach ($keys as $key) {
164 if (!isset($this->config[$category][$key])) {
165 $return[$category][$key] = $config[$category][$key];