]> git.mxchange.org Git - friendica.git/blobdiff - boot.php
Merge pull request #2353 from shtrom/issue757-https-reverse-proxy-headers-on-develop
[friendica.git] / boot.php
index 6a2acaa35e78bb33c833da1a470b938659f3eceb..318a87346771e7f609bcb9ad2cdfbbbdbfdb1904 100644 (file)
--- a/boot.php
+++ b/boot.php
@@ -6,11 +6,11 @@
 
 /**
  * Friendica
- * 
+ *
  * Friendica is a communications platform for integrated social communications
  * utilising decentralised communications and linkage to several indie social
  * projects - as well as popular mainstream providers.
- * 
+ *
  * Our mission is to free our friends and families from the clutches of
  * data-harvesting corporations, and pave the way to a future where social
  * communications are free and open and flow between alternate providers as
@@ -18,7 +18,7 @@
  */
 
 require_once('include/autoloader.php');
+
 require_once('include/config.php');
 require_once('include/network.php');
 require_once('include/plugin.php');
@@ -30,7 +30,7 @@ require_once('include/cache.php');
 require_once('library/Mobile_Detect/Mobile_Detect.php');
 require_once('include/features.php');
 require_once('include/identity.php');
-
+require_once('include/pidfile.php');
 require_once('update.php');
 require_once('include/dbstructure.php');
 
@@ -38,7 +38,7 @@ define ( 'FRIENDICA_PLATFORM',     'Friendica');
 define ( 'FRIENDICA_CODENAME',     'Asparagus');
 define ( 'FRIENDICA_VERSION',      '3.5-dev' );
 define ( 'DFRN_PROTOCOL_VERSION',  '2.23'    );
-define ( 'DB_UPDATE_VERSION',      1194      );
+define ( 'DB_UPDATE_VERSION',      1196      );
 
 /**
  * @brief Constant with a HTML line break.
@@ -387,6 +387,10 @@ define ( 'GRAVITY_COMMENT',      6);
 /* @}*/
 
 
+// Normally this constant is defined - but not if "pcntl" isn't installed
+if (!defined("SIGTERM"))
+       define("SIGTERM", 15);
+
 /**
  *
  * Reverse the effect of magic_quotes_gpc if it is enabled.
@@ -465,11 +469,12 @@ class App {
        public  $plugins;
        public  $apps = array();
        public  $identities;
-       public  $is_mobile;
-       public  $is_tablet;
+       public  $is_mobile = false;
+       public  $is_tablet = false;
        public  $is_friendica_app;
        public  $performance = array();
        public  $callstack = array();
+       public  $theme_info = array();
 
        public $nav_sel;
 
@@ -595,15 +600,6 @@ class App {
                if(x($_SERVER,'SERVER_NAME')) {
                        $this->hostname = $_SERVER['SERVER_NAME'];
 
-                       // See bug 437 - this didn't work so disabling it
-                       //if(stristr($this->hostname,'xn--')) {
-                               // PHP or webserver may have converted idn to punycode, so
-                               // convert punycode back to utf-8
-                       //      require_once('library/simplepie/idn/idna_convert.class.php');
-                       //      $x = new idna_convert();
-                       //      $this->hostname = $x->decode($_SERVER['SERVER_NAME']);
-                       //}
-
                        if(x($_SERVER,'SERVER_PORT') && $_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443)
                                $this->hostname .= ':' . $_SERVER['SERVER_PORT'];
                        /*
@@ -869,11 +865,14 @@ class App {
 
                $shortcut_icon = get_config("system", "shortcut_icon");
                if ($shortcut_icon == "")
-                       $shortcut_icon = $this->get_baseurl()."/images/friendica-32.png";
+                       $shortcut_icon = "images/friendica-32.png";
 
                $touch_icon = get_config("system", "touch_icon");
                if ($touch_icon == "")
-                       $touch_icon = $this->get_baseurl()."/images/friendica-128.png";
+                       $touch_icon = "images/friendica-128.png";
+
+               // get data wich is needed for infinite scroll on the network page
+               $invinite_scroll = infinite_scroll_data($this->module);
 
                $tpl = get_markup_template('head.tpl');
                $this->page['htmlhead'] = replace_macros($tpl,array(
@@ -887,7 +886,8 @@ class App {
                        '$update_interval' => $interval,
                        '$shortcut_icon' => $shortcut_icon,
                        '$touch_icon' => $touch_icon,
-                       '$stylesheet' => $stylesheet
+                       '$stylesheet' => $stylesheet,
+                       '$infinite_scroll' => $invinite_scroll,
                )) . $this->page['htmlhead'];
        }
 
@@ -952,6 +952,25 @@ class App {
        }
 
 
+       /**
+        * @brief Removes the baseurl from an url. This avoids some mixed content problems.
+        *
+        * @param string $url
+        *
+        * @return string The cleaned url
+        */
+       function remove_baseurl($url){
+
+               // Is the function called statically?
+               if (!is_object($this))
+                       return(self::$a->remove_baseurl($url));
+
+               $url = normalise_link($url);
+               $base = normalise_link($this->get_baseurl());
+               $url = str_replace($base."/", "", $url);
+               return $url;
+       }
+
        /**
         * @brief Register template engine class
         * 
@@ -1041,22 +1060,42 @@ class App {
        function save_timestamp($stamp, $value) {
                $duration = (float)(microtime(true)-$stamp);
 
+               if (!isset($this->performance[$value])) {
+                       // Prevent ugly E_NOTICE
+                       $this->performance[$value] = 0;
+               }
+
                $this->performance[$value] += (float)$duration;
                $this->performance["marktime"] += (float)$duration;
 
-               // Trace the different functions with their timestamps
-               $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 5);
+               $callstack = $this->callstack();
 
-               array_shift($trace);
+               if (!isset($this->callstack[$value][$callstack])) {
+                       // Prevent ugly E_NOTICE
+                       $this->callstack[$value][$callstack] = 0;
+               }
 
-               $function = array();
-               foreach ($trace AS $func)
-                       $function[] = $func["function"];
+               $this->callstack[$value][$callstack] += (float)$duration;
 
-               $function = implode(", ", $function);
+       }
 
-               $this->callstack[$value][$function] += (float)$duration;
+       /**
+        * @brief Returns a string with a callstack. Can be used for logging.
+        *
+        * @return string
+        */
+       function callstack() {
+               $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 6);
+
+               // We remove the first two items from the list since they contain data that we don't need.
+               array_shift($trace);
+               array_shift($trace);
 
+               $callstack = array();
+               foreach ($trace AS $func)
+                       $callstack[] = $func["function"];
+
+               return implode(", ", $callstack);
        }
 
        function mark_timestamp($mark) {
@@ -1072,6 +1111,55 @@ class App {
                return($this->is_friendica_app);
        }
 
+       /**
+        * @brief Checks if the maximum load is reached
+        *
+        * @return bool Is the load reached?
+        */
+       function maxload_reached() {
+
+               $maxsysload = intval(get_config('system', 'maxloadavg'));
+               if ($maxsysload < 1)
+                       $maxsysload = 50;
+
+               $load = current_load();
+               if ($load) {
+                       if (intval($load) > $maxsysload) {
+                               logger('system: load '.$load.' too high.');
+                               return true;
+                       }
+               }
+               return false;
+       }
+
+       /**
+        * @brief Checks if the process is already running
+        *
+        * @param string $taskname The name of the task that will be used for the name of the lockfile
+        * @param string $task The path and name of the php script
+        * @param int $timeout The timeout after which a task should be killed
+        *
+        * @return bool Is the process running?
+        */
+       function is_already_running($taskname, $task = "", $timeout = 540) {
+
+               $lockpath = get_lockpath();
+               if ($lockpath != '') {
+                       $pidfile = new pidfile($lockpath, $taskname);
+                       if ($pidfile->is_already_running()) {
+                               logger("Already running");
+                               if ($pidfile->running_time() > $timeout) {
+                                       $pidfile->kill();
+                                       logger("killed stale process");
+                                       // Calling a new instance
+                                       if ($task != "")
+                                               proc_run('php', $task);
+                               }
+                               return true;
+                       }
+               }
+               return false;
+       }
 }
 
 /**
@@ -1423,7 +1511,7 @@ function login($register = false, $hiddens=false) {
 
        $noid = get_config('system','no_openid');
 
-       $dest_url = $a->get_baseurl(true) . '/' . $a->query_string;
+       $dest_url = $a->query_string;
 
        if(local_user()) {
                $tpl = get_markup_template("logout.tpl");
@@ -1440,20 +1528,20 @@ function login($register = false, $hiddens=false) {
 
        $o .= replace_macros($tpl, array(
 
-               '$dest_url'     => $dest_url,
-               '$logout'       => t('Logout'),
-               '$login'        => t('Login'),
+               '$dest_url'     => $dest_url,
+               '$logout'       => t('Logout'),
+               '$login'        => t('Login'),
 
-               '$lname'                => array('username', t('Nickname or Email address: ') , '', ''),
+               '$lname'        => array('username', t('Nickname or Email: ') , '', ''),
                '$lpassword'    => array('password', t('Password: '), '', ''),
                '$lremember'    => array('remember', t('Remember me'), 0,  ''),
 
-               '$openid'               => !$noid,
-               '$lopenid'      => array('openid_url', t('Or login using OpenID: '),'',''),
+               '$openid'       => !$noid,
+               '$lopenid'      => array('openid_url', t('Or login using OpenID: '),'',''),
 
-               '$hiddens'      => $hiddens,
+               '$hiddens'      => $hiddens,
 
-               '$register'     => $reg,
+               '$register'     => $reg,
 
                '$lostpass'     => t('Forgot your password?'),
                '$lostlink'     => t('Password Reset'),
@@ -1483,6 +1571,9 @@ function killme() {
  * @brief Redirect to another URL and terminate this process.
  */
 function goaway($s) {
+       if (!strstr(normalise_link($s), "http://"))
+               $s = App::get_baseurl()."/".$s;
+
        header("Location: $s");
        killme();
 }
@@ -1742,9 +1833,9 @@ function current_theme_url() {
 
        $opts = (($a->profile_uid) ? '?f=&puid=' . $a->profile_uid : '');
        if (file_exists('view/theme/' . $t . '/style.php'))
-               return($a->get_baseurl() . '/view/theme/' . $t . '/style.pcss' . $opts);
+               return('view/theme/'.$t.'/style.pcss'.$opts);
 
-       return($a->get_baseurl() . '/view/theme/' . $t . '/style.css');
+       return('view/theme/'.$t.'/style.css');
 }
 
 function feed_birthday($uid,$tz) {
@@ -1810,31 +1901,6 @@ function is_site_admin() {
        return false;
 }
 
-
-function load_contact_links($uid) {
-
-       $a = get_app();
-
-       $ret = array();
-
-       if(! $uid || x($a->contacts,'empty'))
-               return;
-
-       $r = q("SELECT `id`,`network`,`url`,`thumb`, `rel` FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `thumb` != ''",
-                       intval($uid)
-       );
-       if(count($r)) {
-               foreach($r as $rr){
-                       $url = normalise_link($rr['url']);
-                       $ret[$url] = $rr;
-               }
-       } else
-               $ret['empty'] = true;
-
-       $a->contacts = $ret;
-       return;
-}
-
 /**
  * @brief Returns querystring as string from a mapped array.
  *
@@ -2123,3 +2189,43 @@ function argv($x) {
 
        return '';
 }
+
+/**
+ * @brief Get the data which is needed for infinite scroll
+ * 
+ * For invinite scroll we need the page number of the actual page
+ * and the the URI where the content of the next page comes from.
+ * This data is needed for the js part in main.js.
+ * Note: infinite scroll does only work for the network page (module)
+ * 
+ * @param string $module The name of the module (e.g. "network")
+ * @return array Of infinite scroll data
+ *     'pageno' => $pageno The number of the actual page
+ *     'reload_uri' => $reload_uri The URI of the content we have to load
+ */
+function infinite_scroll_data($module) {
+
+       if (get_pconfig(local_user(),'system','infinite_scroll')
+               AND ($module == "network") AND ($_GET["mode"] != "minimal")) {
+
+               // get the page number
+               if (is_string($_GET["page"]))
+                       $pageno = $_GET["page"];
+               else
+                       $pageno = 1;
+
+               $reload_uri = "";
+
+               // try to get the uri from which we load the content
+               foreach ($_GET AS $param => $value)
+                       if (($param != "page") AND ($param != "q"))
+                               $reload_uri .= "&".$param."=".urlencode($value);
+
+               if (($a->page_offset != "") AND !strstr($reload_uri, "&offset="))
+                       $reload_uri .= "&offset=".urlencode($a->page_offset);
+
+               $arr = array("pageno" => $pageno, "reload_uri" => $reload_uri);
+
+               return $arr;
+       }
+}