]> git.mxchange.org Git - friendica.git/blobdiff - boot.php
Bugfix: Avoid warning when posting items and the worker isn't active
[friendica.git] / boot.php
index 348b786e4dfadec12a129da2ebb0bee7d162e2f9..88f7ad7829fff1e30d40290b2794d2834fd089a1 100644 (file)
--- a/boot.php
+++ b/boot.php
@@ -38,7 +38,7 @@ define ( 'FRIENDICA_PLATFORM',     'Friendica');
 define ( 'FRIENDICA_CODENAME',     'Asparagus');
 define ( 'FRIENDICA_VERSION',      '3.5.1-dev' );
 define ( 'DFRN_PROTOCOL_VERSION',  '2.23'    );
-define ( 'DB_UPDATE_VERSION',      1205      );
+define ( 'DB_UPDATE_VERSION',      1208      );
 
 /**
  * @brief Constant with a HTML line break.
@@ -127,6 +127,10 @@ define ( 'CACHE_MONTH',            0 );
 define ( 'CACHE_WEEK',             1 );
 define ( 'CACHE_DAY',              2 );
 define ( 'CACHE_HOUR',             3 );
+define ( 'CACHE_HALF_HOUR',        4 );
+define ( 'CACHE_QUARTER_HOUR',     5 );
+define ( 'CACHE_FIVE_MINUTES',     6 );
+define ( 'CACHE_MINUTE',           7 );
 /* @}*/
 
 /**
@@ -781,60 +785,100 @@ class App {
                return($this->scheme);
        }
 
+       /**
+        * @brief Retrieves the Friendica instance base URL
+        *
+        * This function assembles the base URL from multiple parts:
+        * - Protocol is determined either by the request or a combination of
+        * system.ssl_policy and the $ssl parameter.
+        * - Host name is determined either by system.hostname or inferred from request
+        * - Path is inferred from SCRIPT_NAME
+        *
+        * Caches the result (depending on $ssl value) for performance.
+        *
+        * Note: $ssl parameter value doesn't directly correlate with the resulting protocol
+        *
+        * @param bool $ssl Whether to append http or https under SSL_POLICY_SELFSIGN
+        * @return string Friendica server base URL
+        */
        function get_baseurl($ssl = false) {
 
                // Is the function called statically?
-               if (!is_object($this))
-                       return(self::$a->get_baseurl($ssl));
+               if (!is_object($this)) {
+                       return self::$a->get_baseurl($ssl);
+               }
+
+               // Arbitrary values, the resulting url protocol can be different
+               $cache_index = $ssl ? 'https' : 'http';
+
+               // Cached value found, nothing to process
+               if (isset($this->baseurl[$cache_index])) {
+                       return $this->baseurl[$cache_index];
+               }
 
                $scheme = $this->scheme;
 
-               if((x($this->config,'system')) && (x($this->config['system'],'ssl_policy'))) {
-                       if(intval($this->config['system']['ssl_policy']) === intval(SSL_POLICY_FULL))
+               if ((x($this->config, 'system')) && (x($this->config['system'], 'ssl_policy'))) {
+                       if (intval($this->config['system']['ssl_policy']) === SSL_POLICY_FULL) {
                                $scheme = 'https';
+                       }
 
                        //      Basically, we have $ssl = true on any links which can only be seen by a logged in user
                        //      (and also the login link). Anything seen by an outsider will have it turned off.
 
-                       if($this->config['system']['ssl_policy'] == SSL_POLICY_SELFSIGN) {
-                               if($ssl)
+                       if ($this->config['system']['ssl_policy'] == SSL_POLICY_SELFSIGN) {
+                               if ($ssl) {
                                        $scheme = 'https';
-                               else
+                               } else {
                                        $scheme = 'http';
+                               }
                        }
                }
 
-               if (get_config('config','hostname') != "")
-                       $this->hostname = get_config('config','hostname');
+               if (get_config('config', 'hostname') != '') {
+                       $this->hostname = get_config('config', 'hostname');
+               }
+
+               $this->baseurl[$cache_index] = $scheme . "://" . $this->hostname . ((isset($this->path) && strlen($this->path)) ? '/' . $this->path : '' );
 
-               $this->baseurl = $scheme . "://" . $this->hostname . ((isset($this->path) && strlen($this->path)) ? '/' . $this->path : '' );
-               return $this->baseurl;
+               return $this->baseurl[$cache_index];
        }
 
+       /**
+        * @brief Initializes the baseurl components
+        *
+        * Clears the baseurl cache to prevent inconstistencies
+        *
+        * @param string $url
+        */
        function set_baseurl($url) {
                $parsed = @parse_url($url);
 
-               $this->baseurl = $url;
+               $this->baseurl = [];
 
                if($parsed) {
                        $this->scheme = $parsed['scheme'];
 
                        $hostname = $parsed['host'];
-                       if(x($parsed,'port'))
+                       if (x($parsed, 'port')) {
                                $hostname .= ':' . $parsed['port'];
-                       if(x($parsed,'path'))
-                               $this->path = trim($parsed['path'],'\\/');
+                       }
+                       if (x($parsed, 'path')) {
+                               $this->path = trim($parsed['path'], '\\/');
+                       }
 
-                       if (file_exists(".htpreconfig.php"))
+                       if (file_exists(".htpreconfig.php")) {
                                @include(".htpreconfig.php");
+                       }
 
-                       if (get_config('config','hostname') != "")
-                               $this->hostname = get_config('config','hostname');
+                       if (get_config('config', 'hostname') != '') {
+                               $this->hostname = get_config('config', 'hostname');
+                       }
 
-                       if (!isset($this->hostname) OR ($this->hostname == ""))
+                       if (!isset($this->hostname) OR ($this->hostname == '')) {
                                $this->hostname = $hostname;
+                       }
                }
-
        }
 
        function get_hostname() {
@@ -1100,6 +1144,9 @@ class App {
        }
 
        function save_timestamp($stamp, $value) {
+               if (!isset($this->config['system']['profiler']) || !$this->config['system']['profiler'])
+                       return;
+
                $duration = (float)(microtime(true)-$stamp);
 
                if (!isset($this->performance[$value])) {
@@ -1131,23 +1178,33 @@ class App {
 
                $this->remove_inactive_processes();
 
+               q("START TRANSACTION");
+
                $r = q("SELECT `pid` FROM `process` WHERE `pid` = %d", intval(getmypid()));
-               if(!dbm::is_result($r))
+               if(!dbm::is_result($r)) {
                        q("INSERT INTO `process` (`pid`,`command`,`created`) VALUES (%d, '%s', '%s')",
                                intval(getmypid()),
                                dbesc($command),
                                dbesc(datetime_convert()));
+               }
+               q("COMMIT");
        }
 
        /**
         * @brief Remove inactive processes
         */
        function remove_inactive_processes() {
+               q("START TRANSACTION");
+
                $r = q("SELECT `pid` FROM `process`");
-               if(dbm::is_result($r))
-                       foreach ($r AS $process)
-                               if (!posix_kill($process["pid"], 0))
+               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");
        }
 
        /**
@@ -1176,11 +1233,6 @@ class App {
                return implode(", ", $callstack);
        }
 
-       function mark_timestamp($mark) {
-               //$this->performance["markstart"] -= microtime(true) - $this->performance["marktime"];
-               $this->performance["markstart"] = microtime(true) - $this->performance["markstart"] - $this->performance["marktime"];
-       }
-
        function get_useragent() {
                return(FRIENDICA_PLATFORM." '".FRIENDICA_CODENAME."' ".FRIENDICA_VERSION."-".DB_UPDATE_VERSION."; ".$this->get_baseurl());
        }
@@ -1325,7 +1377,7 @@ class App {
        function proc_run($args) {
 
                // Add the php path if it is a php call
-               if (count($args) && ($args[0] === 'php' OR is_int($args[0]))) {
+               if (count($args) && ($args[0] === 'php' OR !is_string($args[0]))) {
 
                        // If the last worker fork was less than 10 seconds before then don't fork another one.
                        // This should prevent the forking of masses of workers.
@@ -1845,11 +1897,12 @@ function get_max_import_size() {
  * @brief Wrap calls to proc_close(proc_open()) and call hook
  *     so plugins can take part in process :)
  *
- * @param (string|integer) $cmd program to run or priority
+ * @param (string|integer|array) $cmd program to run, priority or parameter array
  *
  * next args are passed as $cmd command line
  * e.g.: proc_run("ls","-la","/tmp");
  * or: proc_run(PRIORITY_HIGH, "include/notifier.php", "drop", $drop_id);
+ * or: proc_run(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "include/create_shadowentry.php", $post_id);
  *
  * @note $cmd and string args are surrounded with ""
  *
@@ -1860,24 +1913,31 @@ function proc_run($cmd){
 
        $a = get_app();
 
-       $args = func_get_args();
+       $proc_args = func_get_args();
 
-       $newargs = array();
-       if (!count($args))
+       $args = array();
+       if (!count($proc_args)) {
                return;
+       }
 
-       // expand any arrays
+       // Preserve the first parameter
+       // It could contain a command, the priority or an parameter array
+       // If we use the parameter array we have to protect it from the following function
+       $run_parameter = array_shift($proc_args);
 
-       foreach($args as $arg) {
-               if(is_array($arg)) {
-                       foreach($arg as $n) {
-                               $newargs[] = $n;
+       // expand any arrays
+       foreach ($proc_args as $arg) {
+               if (is_array($arg)) {
+                       foreach ($arg as $n) {
+                               $args[] = $n;
                        }
-               } else
-                       $newargs[] = $arg;
+               } else {
+                       $args[] = $arg;
+               }
        }
 
-       $args = $newargs;
+       // Now we add the run parameters back to the array
+       array_unshift($args, $run_parameter);
 
        $arr = array('args' => $args, 'run_cmd' => true);
 
@@ -1885,16 +1945,24 @@ function proc_run($cmd){
        if (!$arr['run_cmd'] OR !count($args))
                return;
 
-       if (!get_config("system", "worker") OR
-               (($args[0] != 'php') AND !is_int($args[0]))) {
+       if (!get_config("system", "worker") OR (is_string($run_parameter) AND ($run_parameter != 'php'))) {
                $a->proc_run($args);
                return;
        }
 
-       if (is_int($args[0]))
-               $priority = $args[0];
-       else
-               $priority = PRIORITY_MEDIUM;
+       $priority = PRIORITY_MEDIUM;
+       $dont_fork = get_config("system", "worker_dont_fork");
+
+       if (is_int($run_parameter)) {
+               $priority = $run_parameter;
+       } elseif (is_array($run_parameter)) {
+               if (isset($run_parameter['priority'])) {
+                       $priority = $run_parameter['priority'];
+               }
+               if (isset($run_parameter['dont_fork'])) {
+                       $dont_fork = $run_parameter['dont_fork'];
+               }
+       }
 
        $argv = $args;
        array_shift($argv);
@@ -1911,8 +1979,9 @@ function proc_run($cmd){
                        intval($priority));
 
        // Should we quit and wait for the poller to be called as a cronjob?
-       if (get_config("system", "worker_dont_fork"))
+       if ($dont_fork) {
                return;
+       }
 
        // Checking number of workers
        $workers = q("SELECT COUNT(*) AS `workers` FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");