]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Config/Cache/ConfigCache.php
Hardening ConfigUpgrade
[friendica.git] / src / Core / Config / Cache / ConfigCache.php
index 54da327dba9e55b3d5c719a195d7a179ced2a4fe..85525b4767c3e143d9fb06d04920205a27d644db 100644 (file)
@@ -5,9 +5,7 @@ namespace Friendica\Core\Config\Cache;
 /**
  * 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
 {
@@ -32,15 +30,16 @@ 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) {
-                                       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);
                                                }
                                        }
                                }
@@ -55,22 +54,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.
         *
@@ -90,9 +80,6 @@ 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] = [];
                }
@@ -102,15 +89,6 @@ class ConfigCache implements IConfigCache, IPConfigCache
                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}
         */
@@ -132,9 +110,19 @@ class ConfigCache implements IConfigCache, IPConfigCache
         */
        public function loadP($uid, array $config)
        {
-               foreach ($config as $category => $values) {
-                       foreach ($values as $key => $value) {
-                               $this->setP($uid, $category, $key, $value);
+               $categories = array_keys($config);
+
+               foreach ($categories as $category) {
+                       if (isset($config[$category]) && is_array($config[$category])) {
+
+                               $keys = array_keys($config[$category]);
+
+                               foreach ($keys as $key) {
+                                       $value = $config[$category][$key];
+                                       if (isset($value)) {
+                                               $this->setP($uid, $category, $key, $value);
+                                       }
+                               }
                        }
                }
        }
@@ -146,10 +134,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;
                }
        }
 
@@ -158,9 +146,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] = [];
                }
@@ -203,4 +188,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;
+    }
 }