]> git.mxchange.org Git - friendica.git/blobdiff - include/Core/Config.php
Prevent a memory Access Violation when the database isn't connected
[friendica.git] / include / Core / Config.php
index 761d7f490ec6d34c7917bae4546692691ca01b6f..6ef394f2f1cb8cd65aa233eb5d1e1e65b340fb8e 100644 (file)
@@ -2,7 +2,7 @@
 namespace Friendica\Core;
 /**
  * @file include/Core/Config.php
- * 
+ *
  *  @brief Contains the class with methods for system configuration
  */
 
@@ -32,7 +32,7 @@ class Config {
        public static function load($family) {
                global $a;
 
-               $r = q("SELECT `v`, `k` FROM `config` WHERE `cat` = '%s'", dbesc($family));
+               $r = q("SELECT `v`, `k` FROM `config` WHERE `cat` = '%s' ORDER BY `cat`, `k`, `id`", dbesc($family));
                if(count($r)) {
                        foreach($r as $rr) {
                                $k = $rr['k'];
@@ -70,11 +70,11 @@ class Config {
         *  If true the config is loaded from the db and not from the cache (default: false)
         * @return mixed Stored value or null if it does not exist
         */
-       public static function get($family, $key, $default_value=null, $refresh = false) {
+       public static function get($family, $key, $default_value = null, $refresh = false) {
 
                global $a;
 
-               if(! $instore) {
+               if(! $refresh) {
                        // Looking if the whole family isn't set
                        if(isset($a->config[$family])) {
                                if($a->config[$family] === '!<unset>!') {
@@ -90,7 +90,7 @@ class Config {
                        }
                }
 
-               $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
+               $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' ORDER BY `id` DESC LIMIT 1",
                        dbesc($family),
                        dbesc($key)
                );
@@ -123,42 +123,25 @@ class Config {
         *  The value to store
         * @return mixed Stored $value or false if the database update failed
         */
-       public static function set($family,$key,$value) {
+       public static function set($family, $key, $value) {
                global $a;
 
-               // If $a->config[$family] has been previously set to '!<unset>!', then
-               // $a->config[$family][$key] will evaluate to $a->config[$family][0], and
-               // $a->config[$family][$key] = $value will be equivalent to
-               // $a->config[$family][0] = $value[0] (this causes infuriating bugs),
-               // so unset the family before assigning a value to a family's key
-               if($a->config[$family] === '!<unset>!')
-                       unset($a->config[$family]);
+               $a->config[$family][$key] = $value;
 
                // manage array value
-               $dbvalue = (is_array($value)?serialize($value):$value);
-               $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
-               if(is_null(self::get($family,$key,null,true))) {
-                       $a->config[$family][$key] = $value;
-                       $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
-                               dbesc($family),
-                               dbesc($key),
-                               dbesc($dbvalue)
-                       );
-                       if($ret)
-                               return $value;
-                       return $ret;
-               }
+               $dbvalue = is_array($value) ? serialize($value) : $value;
+               $dbvalue = is_bool($dbvalue) ? intval($dbvalue) : $dbvalue;
 
-               $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'",
-                       dbesc($dbvalue),
+               $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' )
+ON DUPLICATE KEY UPDATE `v` = '%s'",
                        dbesc($family),
-                       dbesc($key)
+                       dbesc($key),
+                       dbesc($dbvalue),
+                       dbesc($dbvalue)
                );
-
-               $a->config[$family][$key] = $value;
-
-               if($ret)
+               if ($ret) {
                        return $value;
+               }
                return $ret;
        }