]> git.mxchange.org Git - friendica.git/blobdiff - boot.php
Changed $a->get_baseurl() to App::get_baseurl()
[friendica.git] / boot.php
index 5df6520dbdaf92f178e0121f29de73ce35a3840c..ccff45f7343295d6b4c29648febce22a55e3735c 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',      1208      );
+define ( 'DB_UPDATE_VERSION',      1209      );
 
 /**
  * @brief Constant with a HTML line break.
@@ -530,6 +530,7 @@ class App {
        public  $videoheight = 350;
        public  $force_max_items = 0;
        public  $theme_thread_allow = true;
+       public  $theme_richtext_editor = true;
        public  $theme_events_in_profile = true;
 
        /**
@@ -609,6 +610,7 @@ class App {
                $this->performance["markstart"] = microtime(true);
 
                $this->callstack["database"] = array();
+               $this->callstack["database_write"] = array();
                $this->callstack["network"] = array();
                $this->callstack["file"] = array();
                $this->callstack["rendering"] = array();
@@ -765,7 +767,7 @@ class App {
 
        }
 
-       function get_basepath() {
+       public static function get_basepath() {
 
                $basepath = get_config("system", "basepath");
 
@@ -788,10 +790,18 @@ class App {
        /**
         * @brief Retrieves the Friendica instance base URL
         *
-        * Caches both SSL and non-SSL version for performance
+        * 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
         *
-        * @param bool $ssl
-        * @return string
+        * 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) {
 
@@ -800,33 +810,40 @@ class App {
                        return self::$a->get_baseurl($ssl);
                }
 
-               if (!isset($this->baseurl[$ssl ? 'https' : 'http'])) {
-                       $scheme = $this->scheme;
+               // Arbitrary values, the resulting url protocol can be different
+               $cache_index = $ssl ? 'https' : 'http';
 
-                       if ((x($this->config, 'system')) && (x($this->config['system'], 'ssl_policy'))) {
-                               if (intval($this->config['system']['ssl_policy']) === SSL_POLICY_FULL) {
-                                       $scheme = 'https';
-                               }
+               // Cached value found, nothing to process
+               if (isset($this->baseurl[$cache_index])) {
+                       return $this->baseurl[$cache_index];
+               }
 
-                               //      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.
+               $scheme = $this->scheme;
 
-                               if ($this->config['system']['ssl_policy'] == SSL_POLICY_SELFSIGN) {
-                                       if ($ssl) {
-                                               $scheme = 'https';
-                                       } else {
-                                               $scheme = 'http';
-                                       }
-                               }
+               if ((x($this->config, 'system')) && (x($this->config['system'], 'ssl_policy'))) {
+                       if (intval($this->config['system']['ssl_policy']) === SSL_POLICY_FULL) {
+                               $scheme = 'https';
                        }
 
-                       if (get_config('config', 'hostname') != '') {
-                               $this->hostname = get_config('config', 'hostname');
+                       //      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) {
+                                       $scheme = 'https';
+                               } else {
+                                       $scheme = 'http';
+                               }
                        }
+               }
 
-                       $this->baseurl[$ssl ? 'https' : 'http'] = $scheme . "://" . $this->hostname . ((isset($this->path) && strlen($this->path)) ? '/' . $this->path : '' );
+               if (get_config('config', 'hostname') != '') {
+                       $this->hostname = get_config('config', 'hostname');
                }
-               return $this->baseurl[$ssl ? 'https' : 'http'];
+
+               $this->baseurl[$cache_index] = $scheme . "://" . $this->hostname . ((isset($this->path) && strlen($this->path)) ? '/' . $this->path : '' );
+
+               return $this->baseurl[$cache_index];
        }
 
        /**
@@ -1011,7 +1028,7 @@ class App {
                } else {
                        $r = q("SELECT `contact`.`avatar-date` AS picdate FROM `contact` WHERE `contact`.`thumb` like '%%/%s'",
                                $common_filename);
-                       if(! dbm::is_result($r)){
+                       if (! dbm::is_result($r)) {
                                $this->cached_profile_image[$avatar_image] = $avatar_image;
                        } else {
                                $this->cached_profile_picdate[$common_filename] = "?rev=".urlencode($r[0]['picdate']);
@@ -1026,20 +1043,28 @@ class App {
        /**
         * @brief Removes the baseurl from an url. This avoids some mixed content problems.
         *
-        * @param string $url
+        * @param string $orig_url
         *
         * @return string The cleaned url
         */
-       function remove_baseurl($url){
+       function remove_baseurl($orig_url){
 
                // Is the function called statically?
-               if (!is_object($this))
-                       return(self::$a->remove_baseurl($url));
+               if (!is_object($this)) {
+                       return(self::$a->remove_baseurl($orig_url));
+               }
 
-               $url = normalise_link($url);
+               // Remove the hostname from the url if it is an internal link
+               $nurl = normalise_link($orig_url);
                $base = normalise_link($this->get_baseurl());
-               $url = str_replace($base."/", "", $url);
-               return $url;
+               $url = str_replace($base."/", "", $nurl);
+
+               // if it is an external link return the orignal value
+               if ($url == normalise_link($orig_url)) {
+                       return $orig_url;
+               } else {
+                       return $url;
+               }
        }
 
        /**
@@ -1166,7 +1191,7 @@ class App {
                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),
@@ -1182,7 +1207,7 @@ class App {
                q("START 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"]));
@@ -1269,10 +1294,6 @@ class App {
         */
        function max_processes_reached() {
 
-               // Is the function called statically?
-               if (!is_object($this))
-                       return(self::$a->max_processes_reached());
-
                if ($this->is_backend()) {
                        $process = "backend";
                        $max_processes = get_config('system', 'max_processes_backend');
@@ -1304,10 +1325,6 @@ class App {
         */
        function maxload_reached() {
 
-               // Is the function called statically?
-               if (!is_object($this))
-                       return(self::$a->maxload_reached());
-
                if ($this->is_backend()) {
                        $process = "backend";
                        $maxsysload = intval(get_config('system', 'maxloadavg'));
@@ -1361,8 +1378,12 @@ class App {
 
        function proc_run($args) {
 
+               if (!function_exists("proc_open")) {
+                       return;
+               }
+
                // 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.
@@ -1450,17 +1471,18 @@ function system_unavailable() {
 
 
 function clean_urls() {
-       global $a;
+       $a = get_app();
        //      if($a->config['system']['clean_urls'])
        return true;
        //      return false;
 }
 
 function z_path() {
-       global $a;
-       $base = $a->get_baseurl();
+       $base = App::get_baseurl();
+
        if(! clean_urls())
                $base .= '/?q=';
+
        return $base;
 }
 
@@ -1470,10 +1492,10 @@ function z_path() {
  * @see App::get_baseurl()
  *
  * @return string
+ * @TODO Maybe super-flous and deprecated? Seems to only wrap App::get_baseurl()
  */
 function z_root() {
-       global $a;
-       return $a->get_baseurl();
+       return App::get_baseurl();
 }
 
 /**
@@ -1515,7 +1537,7 @@ function check_db() {
  * Sets the base url for use in cmdline programs which don't have
  * $_SERVER variables
  */
-function check_url(&$a) {
+function check_url(App &$a) {
 
        $url = get_config('system','url');
 
@@ -1526,9 +1548,9 @@ function check_url(&$a) {
        // We will only change the url to an ip address if there is no existing setting
 
        if(! x($url))
-               $url = set_config('system','url',$a->get_baseurl());
-       if((! link_compare($url,$a->get_baseurl())) && (! preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/",$a->get_hostname)))
-               $url = set_config('system','url',$a->get_baseurl());
+               $url = set_config('system','url',App::get_baseurl());
+       if((! link_compare($url,App::get_baseurl())) && (! preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/",$a->get_hostname)))
+               $url = set_config('system','url',App::get_baseurl());
 
        return;
 }
@@ -1537,7 +1559,7 @@ function check_url(&$a) {
 /**
  * @brief Automatic database updates
  */
-function update_db(&$a) {
+function update_db(App &$a) {
        $build = get_config('system','build');
        if(! x($build))
                $build = set_config('system','build',DB_UPDATE_VERSION);
@@ -1653,10 +1675,10 @@ function run_update_function($x) {
  * @param App $a
  *
         */
-function check_plugins(&$a) {
+function check_plugins(App &$a) {
 
        $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
-       if(dbm::is_result($r))
+       if (dbm::is_result($r))
                $installed = $r;
        else
                $installed = array();
@@ -1874,7 +1896,7 @@ function info($s) {
  * @return int
  */
 function get_max_import_size() {
-       global $a;
+       $a = get_app();
        return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
 }
 
@@ -1882,11 +1904,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 ""
  *
@@ -1897,24 +1920,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);
 
@@ -1922,16 +1952,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);
@@ -1948,8 +1986,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'");
@@ -1983,7 +2022,7 @@ function current_theme(){
                $r = q("select theme from user where uid = %d limit 1",
                        intval($a->profile_uid)
                );
-               if(dbm::is_result($r))
+               if (dbm::is_result($r))
                        $page_theme = $r[0]['theme'];
        }
 
@@ -2054,7 +2093,7 @@ function current_theme(){
  * @return string
  */
 function current_theme_url() {
-       global $a;
+       $a = get_app();
 
        $t = current_theme();
 
@@ -2096,7 +2135,7 @@ function feed_birthday($uid,$tz) {
                        intval($uid)
        );
 
-       if(dbm::is_result($p)) {
+       if (dbm::is_result($p)) {
                $tmp_dob = substr($p[0]['dob'],5);
                if(intval($tmp_dob)) {
                        $y = datetime_convert($tz,$tz,'now','Y');
@@ -2319,6 +2358,36 @@ function get_lockpath() {
        return "";
 }
 
+/**
+ * @brief Returns the path where spool files are stored
+ *
+ * @return string Spool path
+ */
+function get_spoolpath() {
+       $spoolpath = get_config('system','spoolpath');
+       if (($spoolpath != "") AND is_dir($spoolpath) AND is_writable($spoolpath)) {
+               return($spoolpath);
+       }
+
+       $temppath = get_temppath();
+
+       if ($temppath != "") {
+               $spoolpath = $temppath."/spool";
+
+               if (!is_dir($spoolpath)) {
+                       mkdir($spoolpath);
+               } elseif (!is_writable($spoolpath)) {
+                       $spoolpath = $temppath;
+               }
+
+               if (is_dir($spoolpath) AND is_writable($spoolpath)) {
+                       set_config("system", "spoolpath", $spoolpath);
+                       return($spoolpath);
+               }
+       }
+       return "";
+}
+
 function get_temppath() {
        $a = get_app();
 
@@ -2341,7 +2410,8 @@ function get_temppath() {
        return("");
 }
 
-function set_template_engine(&$a, $engine = 'internal') {
+/// @deprecated
+function set_template_engine(App &$a, $engine = 'internal') {
 /// @note This function is no longer necessary, but keep it as a wrapper to the class method
 /// to avoid breaking themes again unnecessarily