]> git.mxchange.org Git - friendica.git/blobdiff - src/App.php
No timeout problem anymore in preview.
[friendica.git] / src / App.php
index c52b52820b7e4d81fee59214b0d66cb743b7aca9..4b65afd797755f471b00b4fdbbee45b9f51269dc 100644 (file)
@@ -5,6 +5,14 @@ namespace Friendica;
 use Friendica\Core\Config;
 use Friendica\Core\PConfig;
 
+use Cache;
+use dba;
+use dbm;
+
+use Detection\MobileDetect;
+
+use Exception;
+
 /**
  *
  * class: App
@@ -93,6 +101,7 @@ class App {
         */
        public $template_engine_instance = array();
        public $process_id;
+       public $queue;
        private $ldelim = array(
                'internal' => '',
                'smarty3' => '{{'
@@ -192,7 +201,7 @@ class App {
                }
 
                if (! static::directory_usable($basepath, false)) {
-                       throw new \Exception('Basepath ' . $basepath . ' isn\'t usable.');
+                       throw new Exception('Basepath ' . $basepath . ' isn\'t usable.');
                }
 
                $this->basepath = rtrim($basepath, DIRECTORY_SEPARATOR);
@@ -276,7 +285,7 @@ class App {
                $this->pager['total'] = 0;
 
                // Detect mobile devices
-               $mobile_detect = new \Mobile_Detect();
+               $mobile_detect = new MobileDetect();
                $this->is_mobile = $mobile_detect->isMobile();
                $this->is_tablet = $mobile_detect->isTablet();
 
@@ -319,7 +328,27 @@ class App {
                        $basepath = $_SERVER['PWD'];
                }
 
-               return $basepath;
+               return self::realpath($basepath);
+       }
+
+       /**
+        * @brief Returns a normalized file path
+        *
+        * This is a wrapper for the "realpath" function.
+        * That function cannot detect the real path when some folders aren't readable.
+        * Since this could happen with some hosters we need to handle this.
+        *
+        * @param string $path The path that is about to be normalized
+        * @return string normalized path - when possible
+        */
+       public static function realpath($path) {
+               $normalized = realpath($path);
+
+               if (!is_bool($normalized)) {
+                       return $normalized;
+               } else {
+                       return $path;
+               }
        }
 
        function get_scheme() {
@@ -399,7 +428,7 @@ class App {
                                $this->hostname = Config::get('config', 'hostname');
                        }
 
-                       if (!isset($this->hostname) OR ( $this->hostname == '')) {
+                       if (!isset($this->hostname) || ( $this->hostname == '')) {
                                $this->hostname = $hostname;
                        }
                }
@@ -684,30 +713,30 @@ class App {
 
                $this->remove_inactive_processes();
 
-               q('START TRANSACTION');
+               dba::transaction();
 
                $r = q('SELECT `pid` FROM `process` WHERE `pid` = %d', intval(getmypid()));
-               if (!\dbm::is_result($r)) {
-                       q("INSERT INTO `process` (`pid`,`command`,`created`) VALUES (%d, '%s', '%s')", intval(getmypid()), dbesc($command), dbesc(datetime_convert()));
+               if (!dbm::is_result($r)) {
+                       dba::insert('process', array('pid' => getmypid(), 'command' => $command, 'created' => datetime_convert()));
                }
-               q('COMMIT');
+               dba::commit();
        }
 
        /**
         * @brief Remove inactive processes
         */
        function remove_inactive_processes() {
-               q('START TRANSACTION');
+               dba::transaction();
 
                $r = q('SELECT `pid` FROM `process`');
-               if (\dbm::is_result($r)) {
+               if (dbm::is_result($r)) {
                        foreach ($r AS $process) {
                                if (!posix_kill($process['pid'], 0)) {
                                        q('DELETE FROM `process` WHERE `pid` = %d', intval($process['pid']));
                                }
                        }
                }
-               q('COMMIT');
+               dba::commit();
        }
 
        /**
@@ -731,7 +760,11 @@ class App {
 
                $callstack = array();
                foreach ($trace AS $func) {
-                       $callstack[] = $func['function'];
+                       if (!empty($func['class'])) {
+                               $callstack[] = $func['class'].'::'.$func['function'];
+                       } else {
+                               $callstack[] = $func['function'];
+                       }
                }
 
                return implode(', ', $callstack);
@@ -790,6 +823,8 @@ class App {
         * @return bool Is the limit reached?
         */
        function max_processes_reached() {
+               // Deactivated, needs more investigating if this check really makes sense
+               return false;
 
                if ($this->is_backend()) {
                        $process = 'backend';
@@ -805,7 +840,7 @@ class App {
                        }
                }
 
-               $processlist = \dbm::processlist();
+               $processlist = dbm::processlist();
                if ($processlist['list'] != '') {
                        logger('Processcheck: Processes: ' . $processlist['amount'] . ' - Processlist: ' . $processlist['list'], LOGGER_DEBUG);
 
@@ -841,7 +876,7 @@ class App {
                        $meminfo[$key] = (int) ($meminfo[$key] / 1024);
                }
 
-               if (!isset($meminfo['MemAvailable']) OR ! isset($meminfo['MemFree'])) {
+               if (!isset($meminfo['MemAvailable']) || ! isset($meminfo['MemFree'])) {
                        return false;
                }
 
@@ -893,17 +928,17 @@ class App {
                        return;
                }
 
-               // If the last worker fork was less than 10 seconds before then don't fork another one.
+               // If the last worker fork was less than 2 seconds before then don't fork another one.
                // This should prevent the forking of masses of workers.
                $cachekey = 'app:proc_run:started';
-               $result = \Cache::get($cachekey);
+               $result = Cache::get($cachekey);
 
-               if (!is_null($result) AND ( time() - $result) < 10) {
+               if (!is_null($result) && ( time() - $result) < 2) {
                        return;
                }
 
                // Set the timestamp of the last proc_run
-               \Cache::set($cachekey, time(), CACHE_MINUTE);
+               Cache::set($cachekey, time(), CACHE_MINUTE);
 
                array_unshift($args, ((x($this->config, 'php_path')) && (strlen($this->config['php_path'])) ? $this->config['php_path'] : 'php'));
 
@@ -940,7 +975,7 @@ class App {
         * @return string system username
         */
        static function systemuser() {
-               if (!function_exists('posix_getpwuid') OR ! function_exists('posix_geteuid')) {
+               if (!function_exists('posix_getpwuid') || ! function_exists('posix_geteuid')) {
                        return '';
                }
 
@@ -971,7 +1006,7 @@ class App {
                        logger('Path "' . $directory . '" is not a directory for user ' . self::systemuser(), LOGGER_DEBUG);
                        return false;
                }
-               if ($check_writable AND !is_writable($directory)) {
+               if ($check_writable && !is_writable($directory)) {
                        logger('Path "' . $directory . '" is not writable for user ' . self::systemuser(), LOGGER_DEBUG);
                        return false;
                }