]> git.mxchange.org Git - friendica.git/blobdiff - src/App.php
Merge pull request #5828 from nupplaphil/mode_class
[friendica.git] / src / App.php
index 3761004575a8157c38b107a556204f30b5140afb..9a09bcfce0ba8d425599c7ff735d75daa3c788b0 100644 (file)
@@ -31,21 +31,6 @@ require_once 'include/text.php';
  */
 class App
 {
-       const MODE_LOCALCONFIGPRESENT = 1;
-       const MODE_DBAVAILABLE = 2;
-       const MODE_DBCONFIGAVAILABLE = 4;
-       const MODE_MAINTENANCEDISABLED = 8;
-
-       /**
-        * @deprecated since version 2008.08 Use App->isInstallMode() instead to check for install mode.
-        */
-       const MODE_INSTALL = 0;
-
-       /**
-        * @deprecated since version 2008.08 Use the precise mode constant to check for a specific capability instead.
-        */
-       const MODE_NORMAL = App::MODE_LOCALCONFIGPRESENT | App::MODE_DBAVAILABLE | App::MODE_DBCONFIGAVAILABLE | App::MODE_MAINTENANCEDISABLED;
-
        public $module_loaded = false;
        public $module_class = null;
        public $query_string = '';
@@ -67,7 +52,6 @@ class App
        public $argv;
        public $argc;
        public $module;
-       public $mode = App::MODE_INSTALL;
        public $strings;
        public $basepath;
        public $urlpath;
@@ -326,13 +310,13 @@ class App
 
                $this->loadDatabase();
 
-               $this->determineMode();
+               App\Mode::determine($this->basepath);
 
                $this->determineUrlPath();
 
                Config::load();
 
-               if ($this->mode & self::MODE_DBAVAILABLE) {
+               if (App\Mode::has(App\Mode::DBAVAILABLE)) {
                        Core\Addon::loadHooks();
 
                        $this->loadAddonConfig();
@@ -406,13 +390,13 @@ class App
                }
 
                if (file_exists($this->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php')) {
-                       $this->loadConfigFile($this->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php');
+                       $this->loadConfigFile($this->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php', true);
                }
        }
 
        /**
         * Tries to load the specified configuration file into the App->config array.
-        * Overwrites previously set values.
+        * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
         *
         * The config format is INI and the template for configuration files is the following:
         *
@@ -425,9 +409,10 @@ class App
         * // Keep this line
         *
         * @param type $filepath
+        * @param bool $overwrite Force value overwrite if the config key already exists
         * @throws Exception
         */
-       public function loadConfigFile($filepath)
+       public function loadConfigFile($filepath, $overwrite = false)
        {
                if (!file_exists($filepath)) {
                        throw new Exception('Error parsing non-existent config file ' . $filepath);
@@ -443,7 +428,11 @@ class App
 
                foreach ($config as $category => $values) {
                        foreach ($values as $key => $value) {
-                               $this->setConfigValue($category, $key, $value);
+                               if ($overwrite) {
+                                       $this->setConfigValue($category, $key, $value);
+                               } else {
+                                       $this->setDefaultConfigValue($category, $key, $value);
+                               }
                        }
                }
        }
@@ -461,7 +450,7 @@ class App
 
                // Load the local addon config file to overwritten default addon config values
                if (file_exists($this->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'addon.ini.php')) {
-                       $this->loadConfigFile($this->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'addon.ini.php');
+                       $this->loadConfigFile($this->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'addon.ini.php', true);
                }
        }
 
@@ -513,45 +502,6 @@ class App
                }
        }
 
-       /**
-        * Sets the App mode
-        *
-        * - App::MODE_INSTALL    : Either the database connection can't be established or the config table doesn't exist
-        * - App::MODE_MAINTENANCE: The maintenance mode has been set
-        * - App::MODE_NORMAL     : Normal run with all features enabled
-        *
-        * @return type
-        */
-       private function determineMode()
-       {
-               $this->mode = 0;
-
-               if (!file_exists($this->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php')
-                       && !file_exists($this->basepath . DIRECTORY_SEPARATOR . '.htconfig.php')) {
-                       return;
-               }
-
-               $this->mode |= App::MODE_LOCALCONFIGPRESENT;
-
-               if (!DBA::connected()) {
-                       return;
-               }
-
-               $this->mode |= App::MODE_DBAVAILABLE;
-
-               if (DBA::fetchFirst("SHOW TABLES LIKE 'config'") === false) {
-                       return;
-               }
-
-               $this->mode |= App::MODE_DBCONFIGAVAILABLE;
-
-               if (Config::get('system', 'maintenance')) {
-                       return;
-               }
-
-               $this->mode |= App::MODE_MAINTENANCEDISABLED;
-       }
-
        public function loadDatabase()
        {
                if (DBA::connected()) {
@@ -591,16 +541,6 @@ class App
                $this->save_timestamp($stamp1, 'network');
        }
 
-       /**
-        * Install mode is when the local config file is missing or the DB schema hasn't been installed yet.
-        *
-        * @return bool
-        */
-       public function isInstallMode()
-       {
-               return !($this->mode & App::MODE_LOCALCONFIGPRESENT) || !($this->mode & App::MODE_DBCONFIGAVAILABLE);
-       }
-
        /**
         * @brief Returns the base filesystem path of the App
         *
@@ -1149,7 +1089,11 @@ class App
 
                $meminfo = [];
                foreach ($memdata as $line) {
-                       list($key, $val) = explode(':', $line);
+                       $data = explode(':', $line);
+                       if (count($data) != 2) {
+                               continue;
+                       }
+                       list($key, $val) = $data;
                        $meminfo[$key] = (int) trim(str_replace('kB', '', $val));
                        $meminfo[$key] = (int) ($meminfo[$key] / 1024);
                }
@@ -1315,6 +1259,20 @@ class App
                return $return;
        }
 
+       /**
+        * Sets a default value in the config cache. Ignores already existing keys.
+        *
+        * @param string $cat Config category
+        * @param string $k   Config key
+        * @param mixed  $v   Default value to set
+        */
+       private function setDefaultConfigValue($cat, $k, $v)
+       {
+               if (!isset($this->config[$cat][$k])) {
+                       $this->setConfigValue($cat, $k, $v);
+               }
+       }
+
        /**
         * Sets a value in the config cache. Accepts raw output from the config table
         *
@@ -1444,7 +1402,7 @@ class App
         */
        public function getCurrentTheme()
        {
-               if ($this->isInstallMode()) {
+               if (App\Mode::isInstall()) {
                        return '';
                }