]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Config/Cache/ConfigCache.php
bugfix: add lost changes due merge
[friendica.git] / src / Core / Config / Cache / ConfigCache.php
index b1172c0c82639c8783ba9d260158dc6b96a92d91..b8175452bf4b8181c44d9db3c9d6ccd9f1ac7ef9 100644 (file)
@@ -2,14 +2,14 @@
 
 namespace Friendica\Core\Config\Cache;
 
+use ParagonIE\HiddenString\HiddenString;
+
 /**
  * The Friendica config cache for the application
  * Initial, all *.config.php files are loaded into this cache with the
- * ConfigCacheLoader ( @see ConfigCacheLoader )
- *
- * Is used for further caching operations too (depending on the ConfigAdapter )
+ * ConfigFileLoader ( @see ConfigFileLoader )
  */
-class ConfigCache implements IConfigCache, IPConfigCache
+class ConfigCache
 {
        /**
         * @var array
@@ -17,30 +17,42 @@ class ConfigCache implements IConfigCache, IPConfigCache
        private $config;
 
        /**
-        * @param array $config    A initial config array
+        * @var bool
         */
-       public function __construct(array $config = [])
+       private $hidePasswordOutput;
+
+       /**
+        * @param array $config             A initial config array
+        * @param bool  $hidePasswordOutput True, if cache variables should take extra care of password values
+        */
+       public function __construct(array $config = [], $hidePasswordOutput = true)
        {
+               $this->hidePasswordOutput = $hidePasswordOutput;
                $this->load($config);
        }
 
        /**
-        * {@inheritdoc}
+        * Tries to load the specified configuration array into the config array.
+        * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
+        *
+        * @param array $config
+        * @param bool  $overwrite Force value overwrite if the config key already exists
         */
        public function load(array $config, $overwrite = false)
        {
                $categories = array_keys($config);
 
                foreach ($categories as $category) {
-                       if (isset($config[$category]) && is_array($config[$category])) {
+                       if (is_array($config[$category])) {
                                $keys = array_keys($config[$category]);
 
                                foreach ($keys as $key) {
-                                       if (isset($config[$category][$key])) {
+                                       $value = $config[$category][$key];
+                                       if (isset($value)) {
                                                if ($overwrite) {
-                                                       $this->set($category, $key, $config[$category][$key]);
+                                                       $this->set($category, $key, $value);
                                                } else {
-                                                       $this->setDefault($category, $key, $config[$category][$key]);
+                                                       $this->setDefault($category, $key, $value);
                                                }
                                        }
                                }
@@ -49,26 +61,24 @@ class ConfigCache implements IConfigCache, IPConfigCache
        }
 
        /**
-        * {@inheritdoc}
+        * Gets a value from the config cache.
+        *
+        * @param string $cat Config category
+        * @param string $key Config key
+        *
+        * @return null|mixed Returns the value of the Config entry or null if not set
         */
        public function get($cat, $key = null)
        {
                if (isset($this->config[$cat][$key])) {
                        return $this->config[$cat][$key];
+               } elseif (!isset($key) && isset($this->config[$cat])) {
+                       return $this->config[$cat];
                } else {
-                       return '!<unset>!';
+                       return null;
                }
        }
 
-       /**
-        * {@inheritdoc}
-        */
-       public function has($cat, $key = null)
-       {
-               return isset($this->config[$cat][$key])
-                       && $this->config[$cat][$key] !== '!<unset>!';
-       }
-
        /**
         * Sets a default value in the config cache. Ignores already existing keys.
         *
@@ -84,33 +94,37 @@ class ConfigCache implements IConfigCache, IPConfigCache
        }
 
        /**
-        * {@inheritdoc}
+        * Sets a value in the config cache. Accepts raw output from the config table
+        *
+        * @param string $cat   Config category
+        * @param string $key   Config key
+        * @param mixed  $value Value to set
+        *
+        * @return bool True, if the value is set
         */
        public function set($cat, $key, $value)
        {
-               // Only arrays are serialized in database, so we have to unserialize sparingly
-               $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
-
                if (!isset($this->config[$cat])) {
                        $this->config[$cat] = [];
                }
 
-               $this->config[$cat][$key] = $value;
-
+               if ($this->hidePasswordOutput &&
+                   $key == 'password' &&
+                   is_string($value)) {
+                       $this->config[$cat][$key] = new HiddenString((string) $value);
+               } else {
+                       $this->config[$cat][$key] = $value;
+               }
                return true;
        }
 
        /**
-        * {@inheritdoc}
-        */
-       public function hasP($uid, $cat, $key = null)
-       {
-               return isset($this->config[$uid][$cat][$key])
-                       && $this->config[$uid][$cat][$key] !== '!<unset>!';
-       }
-
-       /**
-        * {@inheritdoc}
+        * Deletes a value from the config cache.
+        *
+        * @param string $cat Config category
+        * @param string $key Config key
+        *
+        * @return bool true, if deleted
         */
        public function delete($cat, $key)
        {
@@ -126,77 +140,40 @@ class ConfigCache implements IConfigCache, IPConfigCache
        }
 
        /**
-        * {@inheritdoc}
-        */
-       public function loadP($uid, array $config)
-       {
-               foreach ($config as $category => $values) {
-                       foreach ($values as $key => $value) {
-                               $this->setP($uid, $category, $key, $value);
-                       }
-               }
-       }
-
-       /**
-        * {@inheritdoc}
+        * Returns the whole configuration
+        *
+        * @return array The configuration
         */
-       public function getP($uid, $cat, $key = null)
+       public function getAll()
        {
-               if (isset($this->config[$uid][$cat][$key])) {
-                       return $this->config[$uid][$cat][$key];
-               } else {
-                       return '!<unset>!';
-               }
+               return $this->config;
        }
 
        /**
-        * {@inheritdoc}
+        * Returns an array with missing categories/Keys
+        *
+        * @param array $config The array to check
+        *
+        * @return array
         */
-       public function setP($uid, $cat, $key, $value)
+       public function keyDiff(array $config)
        {
-               // Only arrays are serialized in database, so we have to unserialize sparingly
-               $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
-
-               if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
-                       $this->config[$uid] = [];
-               }
-
-               if (!isset($this->config[$uid][$cat])) {
-                       $this->config[$uid][$cat] = [];
-               }
+               $return = [];
 
-               $this->config[$uid][$cat][$key] = $value;
+               $categories = array_keys($config);
 
-               return true;
-       }
+               foreach ($categories as $category) {
+                       if (is_array($config[$category])) {
+                               $keys = array_keys($config[$category]);
 
-       /**
-        * {@inheritdoc}
-        */
-       public function deleteP($uid, $cat, $key)
-       {
-               if (isset($this->config[$uid][$cat][$key])) {
-                       unset($this->config[$uid][$cat][$key]);
-                       if (count($this->config[$uid][$cat]) == 0) {
-                               unset($this->config[$uid][$cat]);
-                               if (count($this->config[$uid]) == 0) {
-                                       unset($this->config[$uid]);
+                               foreach ($keys as $key) {
+                                       if (!isset($this->config[$category][$key])) {
+                                               $return[$category][$key] = $config[$category][$key];
+                                       }
                                }
                        }
-
-                       return true;
-               } else {
-                       return false;
                }
-       }
 
-       /**
-        * Returns the whole configuration
-        *
-        * @return array The configuration
-        */
-       public function getAll()
-       {
-               return $this->config;
+               return $return;
        }
 }