]> git.mxchange.org Git - friendica.git/blobdiff - src/App.php
Yet more global classes to escape
[friendica.git] / src / App.php
index 41ee5d3a02bd77b6866c44578988198e522bd5de..efb60c0386bd4502a07d07cc29db552310486216 100644 (file)
@@ -40,6 +40,7 @@ class App {
        public $module;\r
        public $pager;\r
        public $strings;\r
+       public $basepath;\r
        public $path;\r
        public $hooks;\r
        public $timezone;\r
@@ -112,8 +113,10 @@ class App {
 \r
        /**\r
         * @brief App constructor.\r
+        *\r
+        * @param string $basepath Path to the app base folder\r
         */\r
-       function __construct() {\r
+       function __construct($basepath) {\r
 \r
                global $default_timezone;\r
 \r
@@ -154,13 +157,6 @@ class App {
 \r
                startup();\r
 \r
-               set_include_path(\r
-                       get_include_path() . PATH_SEPARATOR\r
-                       . 'include' . PATH_SEPARATOR\r
-                       . 'library' . PATH_SEPARATOR\r
-                       . 'library/langdet' . PATH_SEPARATOR\r
-                       . '.');\r
-\r
                $this->scheme = 'http';\r
 \r
                if ((x($_SERVER, 'HTTPS') && $_SERVER['HTTPS']) ||\r
@@ -195,6 +191,20 @@ class App {
                        $this->hostname = $hostname;\r
                }\r
 \r
+               if (! static::directory_usable($basepath)) {\r
+                       throw new Exception('Basepath ' . $basepath . ' isn\'t usable.');\r
+               }\r
+\r
+               $this->basepath = rtrim($basepath, DIRECTORY_SEPARATOR);\r
+\r
+               set_include_path(\r
+                       get_include_path() . PATH_SEPARATOR\r
+                       . $this->basepath . DIRECTORY_SEPARATOR . 'include' . PATH_SEPARATOR\r
+                       . $this->basepath . DIRECTORY_SEPARATOR . 'library' . PATH_SEPARATOR\r
+                       . $this->basepath . DIRECTORY_SEPARATOR . 'library/langdet' . PATH_SEPARATOR\r
+                       . $this->basepath);\r
+\r
+\r
                if (is_array($_SERVER['argv']) && $_SERVER['argc'] > 1 && substr(end($_SERVER['argv']), 0, 4) == 'http') {\r
                        $this->set_baseurl(array_pop($_SERVER['argv']));\r
                        $_SERVER['argc'] --;\r
@@ -284,18 +294,28 @@ class App {
                self::$a = $this;\r
        }\r
 \r
+       /**\r
+        * @brief Returns the base filesystem path of the App\r
+        *\r
+        * It first checks for the internal variable, then for DOCUMENT_ROOT and\r
+        * finally for PWD\r
+        *\r
+        * @return string\r
+        */\r
        public static function get_basepath() {\r
-               $basepath = get_config('system', 'basepath');\r
+               if (isset($this)) {\r
+                       $basepath = $this->basepath;\r
+               }\r
 \r
-               if ($basepath == '') {\r
-                       $basepath = dirname(__FILE__);\r
+               if (! $basepath) {\r
+                       $basepath = Config::get('system', 'basepath');\r
                }\r
 \r
-               if ($basepath == '') {\r
+               if (! $basepath && x($_SERVER, 'DOCUMENT_ROOT')) {\r
                        $basepath = $_SERVER['DOCUMENT_ROOT'];\r
                }\r
 \r
-               if ($basepath == '') {\r
+               if (! $basepath && x($_SERVER, 'PWD')) {\r
                        $basepath = $_SERVER['PWD'];\r
                }\r
 \r
@@ -375,8 +395,8 @@ class App {
                                include '.htpreconfig.php';\r
                        }\r
 \r
-                       if (get_config('config', 'hostname') != '') {\r
-                               $this->hostname = get_config('config', 'hostname');\r
+                       if (Config::get('config', 'hostname') != '') {\r
+                               $this->hostname = Config::get('config', 'hostname');\r
                        }\r
 \r
                        if (!isset($this->hostname) OR ( $this->hostname == '')) {\r
@@ -386,8 +406,8 @@ class App {
        }\r
 \r
        function get_hostname() {\r
-               if (get_config('config', 'hostname') != '') {\r
-                       $this->hostname = get_config('config', 'hostname');\r
+               if (Config::get('config', 'hostname') != '') {\r
+                       $this->hostname = Config::get('config', 'hostname');\r
                }\r
 \r
                return $this->hostname;\r
@@ -420,7 +440,7 @@ class App {
        }\r
 \r
        function init_pagehead() {\r
-               $interval = ((local_user()) ? get_pconfig(local_user(), 'system', 'update_interval') : 40000);\r
+               $interval = ((local_user()) ? PConfig::get(local_user(), 'system', 'update_interval') : 40000);\r
 \r
                // If the update is 'deactivated' set it to the highest integer number (~24 days)\r
                if ($interval < 0) {\r
@@ -457,12 +477,12 @@ class App {
                        $stylesheet = '$stylesheet';\r
                }\r
 \r
-               $shortcut_icon = get_config('system', 'shortcut_icon');\r
+               $shortcut_icon = Config::get('system', 'shortcut_icon');\r
                if ($shortcut_icon == '') {\r
                        $shortcut_icon = 'images/friendica-32.png';\r
                }\r
 \r
-               $touch_icon = get_config('system', 'touch_icon');\r
+               $touch_icon = Config::get('system', 'touch_icon');\r
                if ($touch_icon == '') {\r
                        $touch_icon = 'images/friendica-128.png';\r
                }\r
@@ -667,7 +687,7 @@ class App {
                q('START TRANSACTION');\r
 \r
                $r = q('SELECT `pid` FROM `process` WHERE `pid` = %d', intval(getmypid()));\r
-               if (!dbm::is_result($r)) {\r
+               if (!\dbm::is_result($r)) {\r
                        q("INSERT INTO `process` (`pid`,`command`,`created`) VALUES (%d, '%s', '%s')", intval(getmypid()), dbesc($command), dbesc(datetime_convert()));\r
                }\r
                q('COMMIT');\r
@@ -680,7 +700,7 @@ class App {
                q('START TRANSACTION');\r
 \r
                $r = q('SELECT `pid` FROM `process`');\r
-               if (dbm::is_result($r)) {\r
+               if (\dbm::is_result($r)) {\r
                        foreach ($r AS $process) {\r
                                if (!posix_kill($process['pid'], 0)) {\r
                                        q('DELETE FROM `process` WHERE `pid` = %d', intval($process['pid']));\r
@@ -773,19 +793,19 @@ class App {
 \r
                if ($this->is_backend()) {\r
                        $process = 'backend';\r
-                       $max_processes = get_config('system', 'max_processes_backend');\r
+                       $max_processes = Config::get('system', 'max_processes_backend');\r
                        if (intval($max_processes) == 0) {\r
                                $max_processes = 5;\r
                        }\r
                } else {\r
                        $process = 'frontend';\r
-                       $max_processes = get_config('system', 'max_processes_frontend');\r
+                       $max_processes = Config::get('system', 'max_processes_frontend');\r
                        if (intval($max_processes) == 0) {\r
                                $max_processes = 20;\r
                        }\r
                }\r
 \r
-               $processlist = dbm::processlist();\r
+               $processlist = \dbm::processlist();\r
                if ($processlist['list'] != '') {\r
                        logger('Processcheck: Processes: ' . $processlist['amount'] . ' - Processlist: ' . $processlist['list'], LOGGER_DEBUG);\r
 \r
@@ -845,13 +865,13 @@ class App {
 \r
                if ($this->is_backend()) {\r
                        $process = 'backend';\r
-                       $maxsysload = intval(get_config('system', 'maxloadavg'));\r
+                       $maxsysload = intval(Config::get('system', 'maxloadavg'));\r
                        if ($maxsysload < 1) {\r
                                $maxsysload = 50;\r
                        }\r
                } else {\r
                        $process = 'frontend';\r
-                       $maxsysload = intval(get_config('system', 'maxloadavg_frontend'));\r
+                       $maxsysload = intval(Config::get('system', 'maxloadavg_frontend'));\r
                        if ($maxsysload < 1) {\r
                                $maxsysload = 50;\r
                        }\r
@@ -876,14 +896,14 @@ class App {
                // If the last worker fork was less than 10 seconds before then don't fork another one.\r
                // This should prevent the forking of masses of workers.\r
                $cachekey = 'app:proc_run:started';\r
-               $result = Cache::get($cachekey);\r
+               $result = \Cache::get($cachekey);\r
 \r
                if (!is_null($result) AND ( time() - $result) < 10) {\r
                        return;\r
                }\r
 \r
                // Set the timestamp of the last proc_run\r
-               Cache::set($cachekey, time(), CACHE_MINUTE);\r
+               \Cache::set($cachekey, time(), CACHE_MINUTE);\r
 \r
                array_unshift($args, ((x($this->config, 'php_path')) && (strlen($this->config['php_path'])) ? $this->config['php_path'] : 'php'));\r
 \r
@@ -900,10 +920,10 @@ class App {
                        return;\r
                }\r
 \r
-               if (get_config('system', 'proc_windows')) {\r
-                       $resource = proc_open('cmd /c start /b ' . $cmdline, array(), $foo, dirname(__FILE__));\r
+               if (Config::get('system', 'proc_windows')) {\r
+                       $resource = proc_open('cmd /c start /b ' . $cmdline, array(), $foo, $this->get_basepath());\r
                } else {\r
-                       $resource = proc_open($cmdline . ' &', array(), $foo, dirname(__FILE__));\r
+                       $resource = proc_open($cmdline . ' &', array(), $foo, $this->get_basepath());\r
                }\r
                if (!is_resource($resource)) {\r
                        logger('We got no resource for command ' . $cmdline, LOGGER_DEBUG);\r
@@ -957,5 +977,4 @@ class App {
                }\r
                return true;\r
        }\r
-\r
 }\r