]> git.mxchange.org Git - friendica.git/blobdiff - boot.php
Version 3.5.4-rc
[friendica.git] / boot.php
index 15341c0fed29199c676e8739753b50f55f980bc8..eecc14d58401b7a7983f8886eb9e3ee7373bd739 100644 (file)
--- a/boot.php
+++ b/boot.php
@@ -21,7 +21,9 @@
 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php');
 
 use Friendica\App;
+use Friendica\Core\System;
 use Friendica\Core\Config;
+use Friendica\Util\Lock;
 
 require_once 'include/config.php';
 require_once 'include/network.php';
@@ -39,9 +41,9 @@ require_once 'include/poller.php';
 
 define ( 'FRIENDICA_PLATFORM',     'Friendica');
 define ( 'FRIENDICA_CODENAME',     'Asparagus');
-define ( 'FRIENDICA_VERSION',      '3.5.3-dev' );
+define ( 'FRIENDICA_VERSION',      '3.5.4-rc' );
 define ( 'DFRN_PROTOCOL_VERSION',  '2.23'    );
-define ( 'DB_UPDATE_VERSION',      1229      );
+define ( 'DB_UPDATE_VERSION',      1234      );
 
 /**
  * @brief Constant with a HTML line break.
@@ -227,8 +229,9 @@ define('PROTOCOL_UNKNOWN',         0);
 define('PROTOCOL_DFRN',            1);
 define('PROTOCOL_DIASPORA',        2);
 define('PROTOCOL_OSTATUS_SALMON',  3);
-define('PROTOCOL_OSTATUS_FEED',    4);
-define('PROTOCOL_GS_CONVERSATION', 5);
+define('PROTOCOL_OSTATUS_FEED',    4); // Deprecated
+define('PROTOCOL_GS_CONVERSATION', 5); // Deprecated
+define('PROTOCOL_SPLITTED_CONV',   6);
 /** @}*/
 
 /**
@@ -318,6 +321,8 @@ define ( 'NOTIFY_TAGSHARE', 0x0100 );
 define ( 'NOTIFY_POKE',     0x0200 );
 define ( 'NOTIFY_SHARE',    0x0400 );
 
+define ( 'SYSTEM_EMAIL',    0x4000 );
+
 define ( 'NOTIFY_SYSTEM',   0x8000 );
 /* @}*/
 
@@ -389,6 +394,7 @@ define ( 'ACTIVITY_POST',        NAMESPACE_ACTIVITY_SCHEMA . 'post' );
 define ( 'ACTIVITY_UPDATE',      NAMESPACE_ACTIVITY_SCHEMA . 'update' );
 define ( 'ACTIVITY_TAG',         NAMESPACE_ACTIVITY_SCHEMA . 'tag' );
 define ( 'ACTIVITY_FAVORITE',    NAMESPACE_ACTIVITY_SCHEMA . 'favorite' );
+define ( 'ACTIVITY_UNFAVORITE',  NAMESPACE_ACTIVITY_SCHEMA . 'unfavorite' );
 define ( 'ACTIVITY_SHARE',       NAMESPACE_ACTIVITY_SCHEMA . 'share' );
 define ( 'ACTIVITY_DELETE',      NAMESPACE_ACTIVITY_SCHEMA . 'delete' );
 
@@ -504,6 +510,11 @@ function startup() {
  */
 function get_app() {
        global $a;
+
+       if (empty($a)) {
+               $a = new App(dirname(__DIR__));
+       }
+
        return $a;
 }
 
@@ -550,31 +561,16 @@ function system_unavailable() {
        killme();
 }
 
-function clean_urls() {
-       $a = get_app();
-       return true;
-}
-
-function z_path() {
-       $base = App::get_baseurl();
-
-       if (!clean_urls()) {
-               $base .= '/?q=';
-       }
-
-       return $base;
-}
-
 /**
  * @brief Returns the baseurl.
  *
- * @see App::get_baseurl()
+ * @see System::baseUrl()
  *
  * @return string
- * @TODO Maybe super-flous and deprecated? Seems to only wrap App::get_baseurl()
+ * @TODO Function is deprecated and only used in some addons
  */
 function z_root() {
-       return App::get_baseurl();
+       return System::baseUrl();
 }
 
 /**
@@ -600,7 +596,12 @@ function is_ajax() {
        return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
 }
 
-function check_db() {
+/**
+ * @brief Function to check if request was an AJAX (xmlhttprequest) request.
+ *
+ * @param $via_worker boolean Is the check run via the poller?
+ */
+function check_db($via_worker) {
 
        $build = get_config('system', 'build');
        if (!x($build)) {
@@ -608,7 +609,10 @@ function check_db() {
                $build = DB_UPDATE_VERSION;
        }
        if ($build != DB_UPDATE_VERSION) {
-               proc_run(PRIORITY_CRITICAL, 'include/dbupdate.php');
+               // When we cannot execute the database update via the worker, we will do it directly
+               if (!proc_run(PRIORITY_CRITICAL, 'include/dbupdate.php') && $via_worker) {
+                       update_db(get_app());
+               }
        }
 }
 
@@ -627,10 +631,10 @@ function check_url(App $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', App::get_baseurl());
+               $url = set_config('system', 'url', System::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());
+       if ((!link_compare($url, System::baseUrl())) && (!preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/", $a->get_hostname))) {
+               $url = set_config('system', 'url', System::baseUrl());
        }
 
        return;
@@ -912,7 +916,7 @@ function killme() {
  */
 function goaway($s) {
        if (!strstr(normalise_link($s), "http://")) {
-               $s = App::get_baseurl() . "/" . $s;
+               $s = System::baseUrl() . "/" . $s;
        }
 
        header("Location: $s");
@@ -960,6 +964,10 @@ function public_contact() {
  * @return int|bool visitor_id or false
  */
 function remote_user() {
+       // You cannot be both local and remote
+       if (local_user()) {
+               return false;
+       }
        if ((x($_SESSION, 'authenticated')) && (x($_SESSION, 'visitor_id'))) {
                return intval($_SESSION['visitor_id']);
        }
@@ -993,7 +1001,7 @@ function notice($s) {
 function info($s) {
        $a = get_app();
 
-       if (local_user() AND get_pconfig(local_user(), 'system', 'ignore_info')) {
+       if (local_user() && get_pconfig(local_user(), 'system', 'ignore_info')) {
                return;
        }
 
@@ -1029,6 +1037,8 @@ function get_max_import_size() {
  *
  * @hooks 'proc_run'
  *     array $arr
+ *
+ * @return boolean "false" if proc_run couldn't be executed
  */
 function proc_run($cmd) {
 
@@ -1038,7 +1048,7 @@ function proc_run($cmd) {
 
        $args = array();
        if (!count($proc_args)) {
-               return;
+               return false;
        }
 
        // Preserve the first parameter
@@ -1063,12 +1073,13 @@ function proc_run($cmd) {
        $arr = array('args' => $args, 'run_cmd' => true);
 
        call_hooks("proc_run", $arr);
-       if (!$arr['run_cmd'] OR ! count($args)) {
-               return;
+       if (!$arr['run_cmd'] || ! count($args)) {
+               return true;
        }
 
        $priority = PRIORITY_MEDIUM;
        $dont_fork = get_config("system", "worker_dont_fork");
+       $created = datetime_convert();
 
        if (is_int($run_parameter)) {
                $priority = $run_parameter;
@@ -1076,6 +1087,9 @@ function proc_run($cmd) {
                if (isset($run_parameter['priority'])) {
                        $priority = $run_parameter['priority'];
                }
+               if (isset($run_parameter['created'])) {
+                       $created = $run_parameter['created'];
+               }
                if (isset($run_parameter['dont_fork'])) {
                        $dont_fork = $run_parameter['dont_fork'];
                }
@@ -1085,26 +1099,41 @@ function proc_run($cmd) {
        array_shift($argv);
 
        $parameters = json_encode($argv);
-       $found = dba::select('workerqueue', array('id'), array('parameter' => $parameters), array('limit' => 1));
+       $found = dba::exists('workerqueue', array('parameter' => $parameters, 'done' => false));
 
-       if (!dbm::is_result($found)) {
-               dba::insert('workerqueue', array('parameter' => $parameters, 'created' => datetime_convert(), 'priority' => $priority));
+       // Quit if there was a database error - a precaution for the update process to 3.5.3
+       if (dba::errorNo() != 0) {
+               return false;
+       }
+
+       if (!$found) {
+               dba::insert('workerqueue', array('parameter' => $parameters, 'created' => $created, 'priority' => $priority));
        }
 
        // Should we quit and wait for the poller to be called as a cronjob?
        if ($dont_fork) {
-               return;
+               return true;
+       }
+
+       // If there is a lock then we don't have to check for too much worker
+       if (!Lock::set('poller_worker', 0)) {
+               return true;
        }
 
        // If there are already enough workers running, don't fork another one
-       if (poller_too_much_workers()) {
-               return;
+       $quit = poller_too_much_workers();
+       Lock::remove('poller_worker');
+
+       if ($quit) {
+               return true;
        }
 
        // Now call the poller to execute the jobs that we just added to the queue
        $args = array("include/poller.php", "no_cron");
 
        $a->proc_run($args);
+
+       return true;
 }
 
 function current_theme() {
@@ -1379,6 +1408,46 @@ function get_server() {
        return($server);
 }
 
+function get_temppath() {
+       $a = get_app();
+
+       $temppath = get_config("system", "temppath");
+
+       if (($temppath != "") && App::directory_usable($temppath)) {
+               // We have a temp path and it is usable
+               return App::realpath($temppath);
+       }
+
+       // We don't have a working preconfigured temp path, so we take the system path.
+       $temppath = sys_get_temp_dir();
+
+       // Check if it is usable
+       if (($temppath != "") && App::directory_usable($temppath)) {
+               // Always store the real path, not the path through symlinks
+               $temppath = App::realpath($temppath);
+
+               // To avoid any interferences with other systems we create our own directory
+               $new_temppath = $temppath . "/" . $a->get_hostname();
+               if (!is_dir($new_temppath)) {
+                       /// @TODO There is a mkdir()+chmod() upwards, maybe generalize this (+ configurable) into a function/method?
+                       mkdir($new_temppath);
+               }
+
+               if (App::directory_usable($new_temppath)) {
+                       // The new path is usable, we are happy
+                       set_config("system", "temppath", $new_temppath);
+                       return $new_temppath;
+               } else {
+                       // We can't create a subdirectory, strange.
+                       // But the directory seems to work, so we use it but don't store it.
+                       return $temppath;
+               }
+       }
+
+       // Reaching this point means that the operating system is configured badly.
+       return '';
+}
+
 function get_cachefile($file, $writemode = true) {
        $cache = get_itemcachepath();
 
@@ -1407,7 +1476,7 @@ function clear_cache($basepath = "", $path = "") {
                $path = $basepath;
        }
 
-       if (($path == "") OR (!is_dir($path))) {
+       if (($path == "") || (!is_dir($path))) {
                return;
        }
 
@@ -1424,10 +1493,10 @@ function clear_cache($basepath = "", $path = "") {
                if ($dh = opendir($path)) {
                        while (($file = readdir($dh)) !== false) {
                                $fullpath = $path . "/" . $file;
-                               if ((filetype($fullpath) == "dir") and ($file != ".") and ($file != "..")) {
+                               if ((filetype($fullpath) == "dir") && ($file != ".") && ($file != "..")) {
                                        clear_cache($basepath, $fullpath);
                                }
-                               if ((filetype($fullpath) == "file") and (filectime($fullpath) < (time() - $cachetime))) {
+                               if ((filetype($fullpath) == "file") && (filectime($fullpath) < (time() - $cachetime))) {
                                        unlink($fullpath);
                                }
                        }
@@ -1444,8 +1513,8 @@ function get_itemcachepath() {
        }
 
        $itemcache = get_config('system', 'itemcache');
-       if (($itemcache != "") AND App::directory_usable($itemcache)) {
-               return $itemcache;
+       if (($itemcache != "") && App::directory_usable($itemcache)) {
+               return App::realpath($itemcache);
        }
 
        $temppath = get_temppath();
@@ -1471,7 +1540,7 @@ function get_itemcachepath() {
  */
 function get_spoolpath() {
        $spoolpath = get_config('system', 'spoolpath');
-       if (($spoolpath != "") AND App::directory_usable($spoolpath)) {
+       if (($spoolpath != "") && App::directory_usable($spoolpath)) {
                // We have a spool path and it is usable
                return $spoolpath;
        }
@@ -1501,43 +1570,6 @@ function get_spoolpath() {
        return "";
 }
 
-function get_temppath() {
-       $a = get_app();
-
-       $temppath = get_config("system", "temppath");
-
-       if (($temppath != "") AND App::directory_usable($temppath)) {
-               // We have a temp path and it is usable
-               return $temppath;
-       }
-
-       // We don't have a working preconfigured temp path, so we take the system path.
-       $temppath = sys_get_temp_dir();
-
-       // Check if it is usable
-       if (($temppath != "") AND App::directory_usable($temppath)) {
-               // To avoid any interferences with other systems we create our own directory
-               $new_temppath = $temppath . "/" . $a->get_hostname();
-               if (!is_dir($new_temppath)) {
-                       /// @TODO There is a mkdir()+chmod() upwards, maybe generalize this (+ configurable) into a function/method?
-                       mkdir($new_temppath);
-               }
-
-               if (App::directory_usable($new_temppath)) {
-                       // The new path is usable, we are happy
-                       set_config("system", "temppath", $new_temppath);
-                       return $new_temppath;
-               } else {
-                       // We can't create a subdirectory, strange.
-                       // But the directory seems to work, so we use it but don't store it.
-                       return $temppath;
-               }
-       }
-
-       // Reaching this point means that the operating system is configured badly.
-       return '';
-}
-
 /// @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
@@ -1638,7 +1670,7 @@ function argv($x) {
 function infinite_scroll_data($module) {
 
        if (get_pconfig(local_user(), 'system', 'infinite_scroll')
-               AND ($module == "network") AND ($_GET["mode"] != "minimal")) {
+               && ($module == "network") && ($_GET["mode"] != "minimal")) {
 
                // get the page number
                if (is_string($_GET["page"])) {
@@ -1651,12 +1683,12 @@ function infinite_scroll_data($module) {
 
                // try to get the uri from which we load the content
                foreach ($_GET AS $param => $value) {
-                       if (($param != "page") AND ($param != "q")) {
+                       if (($param != "page") && ($param != "q")) {
                                $reload_uri .= "&" . $param . "=" . urlencode($value);
                        }
                }
 
-               if (($a->page_offset != "") AND ! strstr($reload_uri, "&offset=")) {
+               if (($a->page_offset != "") && ! strstr($reload_uri, "&offset=")) {
                        $reload_uri .= "&offset=" . urlencode($a->page_offset);
                }