]> git.mxchange.org Git - friendica.git/blobdiff - boot.php
Improve display feedback on like actions
[friendica.git] / boot.php
index 92dd051671f95513436470c642de46177509d154..6f036d846e90887faa62cc21baf2256bb7224cfc 100644 (file)
--- a/boot.php
+++ b/boot.php
@@ -32,15 +32,14 @@ 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');
 
 define ( 'FRIENDICA_PLATFORM',     'Friendica');
 define ( 'FRIENDICA_CODENAME',     'Asparagus');
-define ( 'FRIENDICA_VERSION',      '3.5.1-dev' );
+define ( 'FRIENDICA_VERSION',      '3.5.2-dev' );
 define ( 'DFRN_PROTOCOL_VERSION',  '2.23'    );
-define ( 'DB_UPDATE_VERSION',      1213      );
+define ( 'DB_UPDATE_VERSION',      1216      );
 
 /**
  * @brief Constant with a HTML line break.
@@ -245,6 +244,7 @@ define ( 'NETWORK_STATUSNET',        'stac');    // Statusnet connector
 define ( 'NETWORK_APPNET',           'apdn');    // app.net
 define ( 'NETWORK_NEWS',             'nntp');    // Network News Transfer Protocol
 define ( 'NETWORK_ICALENDAR',        'ical');    // iCalendar
+define ( 'NETWORK_PNUT',             'pnut');    // pnut.io
 define ( 'NETWORK_PHANTOM',          'unkn');    // Place holder
 /** @}*/
 
@@ -274,6 +274,7 @@ $netgroup_ids = array(
        NETWORK_APPNET    => (-17),
        NETWORK_NEWS      => (-18),
        NETWORK_ICALENDAR => (-19),
+       NETWORK_PNUT      => (-20),
 
        NETWORK_PHANTOM  => (-127),
 );
@@ -428,11 +429,29 @@ define('PRIORITY_LOW',       40);
 define('PRIORITY_NEGLIGIBLE',50);
 /* @}*/
 
+/**
+ * @name Social Relay settings
+ *
+ * See here: https://github.com/jaywink/social-relay
+ * and here: https://wiki.diasporafoundation.org/Relay_servers_for_public_posts
+ * @{
+ */
+define('SR_SCOPE_NONE', '');
+define('SR_SCOPE_ALL',  'all');
+define('SR_SCOPE_TAGS', 'tags');
+/* @}*/
+
+/**
+ * Lowest possible date time value
+ */
+
+define ('NULL_DATE', '0001-01-01 00:00:00');
+
 
 // Normally this constant is defined - but not if "pcntl" isn't installed
-if (!defined("SIGTERM"))
+if (!defined("SIGTERM")) {
        define("SIGTERM", 15);
-
+}
 /**
  *
  * Reverse the effect of magic_quotes_gpc if it is enabled.
@@ -572,7 +591,6 @@ class App {
 
        private $scheme;
        private $hostname;
-       private $baseurl;
        private $db;
 
        private $curl_code;
@@ -800,8 +818,6 @@ class App {
         * - 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
@@ -814,17 +830,9 @@ class App {
                        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 (Config::get('system', 'ssl_policy') === SSL_POLICY_FULL) {
+               if (Config::get('system', 'ssl_policy') == SSL_POLICY_FULL) {
                        $scheme = 'https';
                }
 
@@ -840,12 +848,10 @@ class App {
                }
 
                if (Config::get('config', 'hostname') != '') {
-                       $this->hostname = get_config('config', 'hostname');
+                       $this->hostname = Config::get('config', 'hostname');
                }
 
-               $this->baseurl[$cache_index] = $scheme . "://" . $this->hostname . ((isset($this->path) && strlen($this->path)) ? '/' . $this->path : '' );
-
-               return $this->baseurl[$cache_index];
+               return $scheme . "://" . $this->hostname . ((isset($this->path) && strlen($this->path)) ? '/' . $this->path : '' );
        }
 
        /**
@@ -858,8 +864,6 @@ class App {
        function set_baseurl($url) {
                $parsed = @parse_url($url);
 
-               $this->baseurl = [];
-
                if($parsed) {
                        $this->scheme = $parsed['scheme'];
 
@@ -970,7 +974,6 @@ class App {
                        '$local_user' => local_user(),
                        '$generator' => 'Friendica' . ' ' . FRIENDICA_VERSION,
                        '$delitem' => t('Delete this item?'),
-                       '$comment' => t('Comment'),
                        '$showmore' => t('show more'),
                        '$showfewer' => t('show fewer'),
                        '$update_interval' => $interval,
@@ -1349,60 +1352,25 @@ class App {
                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(PRIORITY_MEDIUM, $task);
-                               }
-                               return true;
-                       }
-               }
-               return false;
-       }
-
        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_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.
-                       if (get_config("system", "worker")) {
-                               $cachekey = "app:proc_run:started";
-                               $result = Cache::get($cachekey);
-                               if (!is_null($result)) {
-                                       if ((time() - $result) < 10) {
-                                               return;
-                                       }
-                               }
-                               // Set the timestamp of the last proc_run
-                               Cache::set($cachekey, time(), CACHE_MINUTE);
+               // 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.
+               $cachekey = "app:proc_run:started";
+               $result = Cache::get($cachekey);
+               if (!is_null($result)) {
+                       if ((time() - $result) < 10) {
+                               return;
                        }
-
-                       $args[0] = ((x($this->config,'php_path')) && (strlen($this->config['php_path'])) ? $this->config['php_path'] : 'php');
                }
+               // Set the timestamp of the last proc_run
+               Cache::set($cachekey, time(), CACHE_MINUTE);
+
+               array_unshift($args, ((x($this->config,'php_path')) && (strlen($this->config['php_path'])) ? $this->config['php_path'] : 'php'));
 
                // add baseurl to args. cli scripts can't construct it
                $args[] = $this->get_baseurl();
@@ -1418,6 +1386,53 @@ class App {
                        proc_close(proc_open($cmdline." &",array(),$foo,dirname(__FILE__)));
 
        }
+
+       /**
+        * @brief Returns the system user that is executing the script
+        *
+        * This mostly returns something like "www-data".
+        *
+        * @return string system username
+        */
+       static function systemuser() {
+               if (!function_exists('posix_getpwuid') OR !function_exists('posix_geteuid')) {
+                       return '';
+               }
+
+               $processUser = posix_getpwuid(posix_geteuid());
+               return $processUser['name'];
+       }
+
+       /**
+        * @brief Checks if a given directory is usable for the system
+        *
+        * @return boolean the directory is usable
+        */
+       static function directory_usable($directory) {
+
+               if ($directory == '') {
+                       logger("Directory is empty. This shouldn't happen.", LOGGER_DEBUG);
+                       return false;
+               }
+
+               if (!file_exists($directory)) {
+                       logger('Path "'.$directory.'" does not exist for user '.self::systemuser(), LOGGER_DEBUG);
+                       return false;
+               }
+               if (is_file($directory)) {
+                       logger('Path "'.$directory.'" is a file for user '.self::systemuser(), LOGGER_DEBUG);
+                       return false;
+               }
+               if (!is_dir($directory)) {
+                       logger('Path "'.$directory.'" is not a directory for user '.self::systemuser(), LOGGER_DEBUG);
+                       return false;
+               }
+               if (!is_writable($directory)) {
+                       logger('Path "'.$directory.'" is not writable for user '.self::systemuser(), LOGGER_DEBUG);
+                       return false;
+               }
+               return true;
+       }
 }
 
 /**
@@ -1845,11 +1860,35 @@ function goaway($s) {
  * @return int|bool user id or false
  */
 function local_user() {
-       if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
+       if (x($_SESSION, 'authenticated') && x($_SESSION, 'uid')) {
                return intval($_SESSION['uid']);
+       }
        return false;
 }
 
+/**
+ * @brief Returns the public contact id of logged in user or false.
+ *
+ * @return int|bool public contact id or false
+ */
+function public_contact() {
+       static $public_contact_id = false;
+
+       if (!$public_contact_id && x($_SESSION, 'authenticated')) {
+               if (x($_SESSION, 'my_address')) {
+                       // Local user
+                       $public_contact_id = intval(get_contact($_SESSION['my_address'], 0));
+               } else if (x($_SESSION, 'visitor_home')) {
+                       // Remote user
+                       $public_contact_id = intval(get_contact($_SESSION['visitor_home'], 0));
+               }
+       } else if (!x($_SESSION, 'authenticated')) {
+               $public_contact_id = false;
+       }
+
+       return $public_contact_id;
+}
+
 /**
  * @brief Returns contact id of authenticated site visitor or false
  *
@@ -1908,10 +1947,9 @@ 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|array) $cmd program to run, priority or parameter array
+ * @param (integer|array) priority or parameter array, $cmd atrings are deprecated and are ignored
  *
  * 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);
  *
@@ -1956,11 +1994,6 @@ function proc_run($cmd){
        if (!$arr['run_cmd'] OR !count($args))
                return;
 
-       if (!get_config("system", "worker") OR (is_string($run_parameter) AND ($run_parameter != 'php'))) {
-               $a->proc_run($args);
-               return;
-       }
-
        $priority = PRIORITY_MEDIUM;
        $dont_fork = get_config("system", "worker_dont_fork");
 
@@ -1995,7 +2028,7 @@ function proc_run($cmd){
        }
 
        // Checking number of workers
-       $workers = q("SELECT COUNT(*) AS `workers` FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
+       $workers = q("SELECT COUNT(*) AS `workers` FROM `workerqueue` WHERE `executed` > '%s'", dbesc(NULL_DATE));
 
        // Get number of allowed number of worker threads
        $queues = intval(get_config("system", "worker_queues"));
@@ -2008,7 +2041,7 @@ function proc_run($cmd){
                return;
 
        // Now call the poller to execute the jobs that we just added to the queue
-       $args = array("php", "include/poller.php", "no_cron");
+       $args = array("include/poller.php", "no_cron");
 
        $a->proc_run($args);
 }
@@ -2322,8 +2355,9 @@ function get_itemcachepath() {
                return "";
 
        $itemcache = get_config('system','itemcache');
-       if (($itemcache != "") AND is_dir($itemcache) AND is_writable($itemcache))
-               return($itemcache);
+       if (($itemcache != "") AND App::directory_usable($itemcache)) {
+               return $itemcache;
+       }
 
        $temppath = get_temppath();
 
@@ -2333,32 +2367,9 @@ function get_itemcachepath() {
                        mkdir($itemcache);
                }
 
-               if (is_dir($itemcache) AND is_writable($itemcache)) {
+               if (App::directory_usable($itemcache)) {
                        set_config("system", "itemcache", $itemcache);
-                       return($itemcache);
-               }
-       }
-       return "";
-}
-
-function get_lockpath() {
-       $lockpath = get_config('system','lockpath');
-       if (($lockpath != "") AND is_dir($lockpath) AND is_writable($lockpath))
-               return($lockpath);
-
-       $temppath = get_temppath();
-
-       if ($temppath != "") {
-               $lockpath = $temppath."/lock";
-
-               if (!is_dir($lockpath))
-                       mkdir($lockpath);
-               elseif (!is_writable($lockpath))
-                       $lockpath = $temppath;
-
-               if (is_dir($lockpath) AND is_writable($lockpath)) {
-                       set_config("system", "lockpath", $lockpath);
-                       return($lockpath);
+                       return $itemcache;
                }
        }
        return "";
@@ -2371,49 +2382,69 @@ function get_lockpath() {
  */
 function get_spoolpath() {
        $spoolpath = get_config('system','spoolpath');
-       if (($spoolpath != "") AND is_dir($spoolpath) AND is_writable($spoolpath)) {
-               return($spoolpath);
+       if (($spoolpath != "") AND App::directory_usable($spoolpath)) {
+               // We have a spool path and it is usable
+               return $spoolpath;
        }
 
+       // We don't have a working preconfigured spool path, so we take the temp path.
        $temppath = get_temppath();
 
        if ($temppath != "") {
+               // To avoid any interferences with other systems we create our own directory
                $spoolpath = $temppath."/spool";
-
                if (!is_dir($spoolpath)) {
                        mkdir($spoolpath);
-               } elseif (!is_writable($spoolpath)) {
-                       $spoolpath = $temppath;
                }
 
-               if (is_dir($spoolpath) AND is_writable($spoolpath)) {
+               if (App::directory_usable($spoolpath)) {
+                       // The new path is usable, we are happy
                        set_config("system", "spoolpath", $spoolpath);
-                       return($spoolpath);
+                       return $spoolpath;
+               } 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_temppath() {
        $a = get_app();
 
-       $temppath = get_config("system","temppath");
-       if (($temppath != "") AND is_dir($temppath) AND is_writable($temppath))
-               return($temppath);
+       $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();
-       if (($temppath != "") AND is_dir($temppath) AND is_writable($temppath)) {
-               $temppath .= "/".$a->get_hostname();
-               if (!is_dir($temppath))
-                       mkdir($temppath);
-
-               if (is_dir($temppath) AND is_writable($temppath)) {
-                       set_config("system", "temppath", $temppath);
-                       return($temppath);
+
+       // 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 .= "/".$a->get_hostname();
+               if (!is_dir($new_temppath))
+                       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;
                }
        }
 
-       return("");
+       // Reaching this point means that the operating system is configured badly.
+       return '';
 }
 
 /// @deprecated