]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Config/Cache/ConfigCache.php
Merge branch 'master' into develop
[friendica.git] / src / Core / Config / Cache / ConfigCache.php
index 4ebcc87482f044b278d0fdd626d47b97624fc9da..441cdee811cc550c3e0bb748c8fdb26887b0dc59 100644 (file)
@@ -2,12 +2,12 @@
 
 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
 {
@@ -16,11 +16,18 @@ class ConfigCache implements IConfigCache, IPConfigCache
         */
        private $config;
 
+       /**
+        * @var bool
+        */
+       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 = [])
+       public function __construct(array $config = [], $hidePasswordOutput = true)
        {
+               $this->hidePasswordOutput = $hidePasswordOutput;
                $this->load($config);
        }
 
@@ -32,12 +39,12 @@ class ConfigCache implements IConfigCache, IPConfigCache
                $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) {
                                        $value = $config[$category][$key];
-                                       if (isset($value) && $value !== '!<unset>!') {
+                                       if (isset($value)) {
                                                if ($overwrite) {
                                                        $this->set($category, $key, $value);
                                                } else {
@@ -56,22 +63,13 @@ class ConfigCache implements IConfigCache, IPConfigCache
        {
                if (isset($this->config[$cat][$key])) {
                        return $this->config[$cat][$key];
-               } elseif ($key == null && isset($this->config[$cat])) {
+               } 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>!') ||
-               ($key == null && isset($this->config[$cat]) && $this->config[$cat] !== '!<unset>!' && is_array($this->config[$cat]));
-       }
-
        /**
         * Sets a default value in the config cache. Ignores already existing keys.
         *
@@ -91,27 +89,20 @@ class ConfigCache implements IConfigCache, IPConfigCache
         */
        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' &&
+                   !empty($value) && 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>!') ||
-                       ($key == null && isset($this->config[$uid][$cat]) && $this->config[$uid][$cat] !== '!<unset>!' && is_array($this->config[$uid][$cat]));
-       }
-
        /**
         * {@inheritdoc}
         */
@@ -142,7 +133,7 @@ class ConfigCache implements IConfigCache, IPConfigCache
 
                                foreach ($keys as $key) {
                                        $value = $config[$category][$key];
-                                       if (isset($value) && $value !== '!<unset>!') {
+                                       if (isset($value)) {
                                                $this->setP($uid, $category, $key, $value);
                                        }
                                }
@@ -157,10 +148,10 @@ class ConfigCache implements IConfigCache, IPConfigCache
        {
                if (isset($this->config[$uid][$cat][$key])) {
                        return $this->config[$uid][$cat][$key];
-               } elseif ($key == null && isset($this->config[$uid][$cat])) {
+               } elseif (!isset($key) && isset($this->config[$uid][$cat])) {
                        return $this->config[$uid][$cat];
                } else {
-                       return '!<unset>!';
+                       return null;
                }
        }
 
@@ -169,9 +160,6 @@ class ConfigCache implements IConfigCache, IPConfigCache
         */
        public function setP($uid, $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[$uid]) || !is_array($this->config[$uid])) {
                        $this->config[$uid] = [];
                }
@@ -214,4 +202,32 @@ class ConfigCache implements IConfigCache, IPConfigCache
        {
                return $this->config;
        }
+
+       /**
+        * Returns an array with missing categories/Keys
+        *
+        * @param array $config The array to check
+        *
+        * @return array
+        */
+       public function keyDiff(array $config)
+       {
+               $return = [];
+
+               $categories = array_keys($config);
+
+               foreach ($categories as $category) {
+                       if (is_array($config[$category])) {
+                               $keys = array_keys($config[$category]);
+
+                               foreach ($keys as $key) {
+                                       if (!isset($this->config[$category][$key])) {
+                                               $return[$category][$key] = $config[$category][$key];
+                                       }
+                               }
+                       }
+               }
+
+               return $return;
+       }
 }