]> git.mxchange.org Git - friendica.git/blobdiff - boot.php
Merge pull request #4165 from MrPetovan/bug/4163-null-profile
[friendica.git] / boot.php
index 52026a7fb31cc52dc6c8e9710b8063b4eb42a567..8c3cd786f54dd5de79a074e143c528e77959aa0c 100644 (file)
--- a/boot.php
+++ b/boot.php
@@ -27,7 +27,8 @@ use Friendica\Core\Config;
 use Friendica\Core\PConfig;
 use Friendica\Core\Worker;
 use Friendica\Database\DBM;
-use Friendica\Util\Lock;
+use Friendica\Model\Contact;
+use Friendica\Database\DBStructure;
 
 require_once 'include/network.php';
 require_once 'include/plugin.php';
@@ -35,17 +36,21 @@ require_once 'include/text.php';
 require_once 'include/datetime.php';
 require_once 'include/pgettext.php';
 require_once 'include/nav.php';
-require_once 'include/features.php';
 require_once 'include/identity.php';
-require_once 'update.php';
-require_once 'include/dbstructure.php';
-require_once 'include/poller.php';
 
 define('FRIENDICA_PLATFORM',     'Friendica');
 define('FRIENDICA_CODENAME',     'Asparagus');
 define('FRIENDICA_VERSION',      '3.6-dev');
 define('DFRN_PROTOCOL_VERSION',  '2.23');
-define('DB_UPDATE_VERSION',      1235);
+define('DB_UPDATE_VERSION',      1238);
+define('NEW_UPDATE_ROUTINE_VERSION', 1170);
+
+/**
+ * @brief Constants for the database update check
+ */
+const DB_UPDATE_NOT_CHECKED = 0; // Database check wasn't executed before
+const DB_UPDATE_SUCCESSFUL = 1;  // Database check was successful
+const DB_UPDATE_FAILED = 2;      // Database check failed
 
 /**
  * @brief Constant with a HTML line break.
@@ -254,25 +259,24 @@ define('PROTOCOL_SPLITTED_CONV',   6);
  * @{
  */
 define('NETWORK_DFRN',             'dfrn');    // Friendica, Mistpark, other DFRN implementations
-define('NETWORK_ZOT',              'zot!');    // Zot!
-define('NETWORK_OSTATUS',          'stat');    // status.net, identi.ca, GNU-social, other OStatus implementations
+define('NETWORK_ZOT',              'zot!');    // Zot! - Currently unsupported
+define('NETWORK_OSTATUS',          'stat');    // GNU-social, Pleroma, Mastodon, other OStatus implementations
 define('NETWORK_FEED',             'feed');    // RSS/Atom feeds with no known "post/notify" protocol
 define('NETWORK_DIASPORA',         'dspr');    // Diaspora
 define('NETWORK_MAIL',             'mail');    // IMAP/POP
-define('NETWORK_MAIL2',            'mai2');    // extended IMAP/POP
 define('NETWORK_FACEBOOK',         'face');    // Facebook API
 define('NETWORK_LINKEDIN',         'lnkd');    // LinkedIn
-define('NETWORK_XMPP',             'xmpp');    // XMPP
-define('NETWORK_MYSPACE',          'mysp');    // MySpace
+define('NETWORK_XMPP',             'xmpp');    // XMPP - Currently unsupported
+define('NETWORK_MYSPACE',          'mysp');    // MySpace - Currently unsupported
 define('NETWORK_GPLUS',            'goog');    // Google+
 define('NETWORK_PUMPIO',           'pump');    // pump.io
 define('NETWORK_TWITTER',          'twit');    // Twitter
 define('NETWORK_DIASPORA2',        'dspc');    // Diaspora connector
 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_APPNET',           'apdn');    // app.net - Dead protocol
+define('NETWORK_NEWS',             'nntp');    // Network News Transfer Protocol - Currently unsupported
+define('NETWORK_ICALENDAR',        'ical');    // iCalendar - Currently unsupported
+define('NETWORK_PNUT',             'pnut');    // pnut.io - Currently unsupported
 define('NETWORK_PHANTOM',          'unkn');    // Place holder
 /**
  * @}
@@ -290,7 +294,6 @@ $netgroup_ids = array(
        NETWORK_FEED     => (-4),
        NETWORK_DIASPORA => (-5),
        NETWORK_MAIL     => (-6),
-       NETWORK_MAIL2    => (-7),
        NETWORK_FACEBOOK => (-8),
        NETWORK_LINKEDIN => (-9),
        NETWORK_XMPP     => (-10),
@@ -414,7 +417,6 @@ define('ACTIVITY_SHARE',       NAMESPACE_ACTIVITY_SCHEMA . 'share');
 define('ACTIVITY_DELETE',      NAMESPACE_ACTIVITY_SCHEMA . 'delete');
 
 define('ACTIVITY_POKE',        NAMESPACE_ZOT . '/activity/poke');
-define('ACTIVITY_MOOD',        NAMESPACE_ZOT . '/activity/mood');
 
 define('ACTIVITY_OBJ_BOOKMARK', NAMESPACE_ACTIVITY_SCHEMA . 'bookmark');
 define('ACTIVITY_OBJ_COMMENT', NAMESPACE_ACTIVITY_SCHEMA . 'comment');
@@ -521,6 +523,8 @@ function startup()
  * @brief Retrieve the App structure
  *
  * Useful in functions which require it but don't get it passed to them
+ *
+ * @return App
  */
 function get_app()
 {
@@ -569,13 +573,45 @@ function x($s, $k = null)
 }
 
 /**
- * @brief Called from db initialisation if db is dead.
+ * Return the provided variable value if it exists and is truthy or the provided
+ * default value instead.
+ *
+ * Works with initialized variables and potentially uninitialized array keys
+ *
+ * Usages:
+ * - defaults($var, $default)
+ * - defaults($array, 'key', $default)
+ *
+ * @brief Returns a defaut value if the provided variable or array key is falsy
+ * @see x()
+ * @return mixed
  */
-function system_unavailable()
-{
-       include 'system_unavailable.php';
-       system_down();
-       killme();
+function defaults() {
+       $args = func_get_args();
+
+       if (count($args) < 2) {
+               throw new BadFunctionCallException('defaults() requires at least 2 parameters');
+       }
+       if (count($args) > 3) {
+               throw new BadFunctionCallException('defaults() cannot use more than 3 parameters');
+       }
+       if (count($args) === 3 && is_null($args[1])) {
+               throw new BadFunctionCallException('defaults($arr, $key, $def) $key is null');
+       }
+
+       $default = array_pop($args);
+
+       if (call_user_func_array('x', $args)) {
+               if (count($args) === 1) {
+                       $return = $args[0];
+               } else {
+                       $return = $args[0][$args[1]];
+               }
+       } else {
+               $return = $default;
+       }
+
+       return $return;
 }
 
 /**
@@ -619,15 +655,22 @@ function is_ajax()
 /**
  * @brief Function to check if request was an AJAX (xmlhttprequest) request.
  *
- * @param boolean $via_worker boolean Is the check run via the poller?
+ * @param boolean $via_worker boolean Is the check run via the worker?
  */
 function check_db($via_worker)
 {
        $build = Config::get('system', 'build');
-       if (!x($build)) {
+
+       if (empty($build)) {
                Config::set('system', 'build', DB_UPDATE_VERSION);
                $build = DB_UPDATE_VERSION;
        }
+
+       // We don't support upgrading from very old versions anymore
+       if ($build < NEW_UPDATE_ROUTINE_VERSION) {
+               die('You try to update from a version prior to database version 1170. The direct upgrade path is not supported. Please update to version 3.5.4 before updating to this version.');
+       }
+
        if ($build != DB_UPDATE_VERSION) {
                // When we cannot execute the database update via the worker, we will do it directly
                if (!Worker::add(PRIORITY_CRITICAL, 'DBUpdate') && $via_worker) {
@@ -652,7 +695,7 @@ function check_url(App $a)
        // and www.example.com vs example.com.
        // We will only change the url to an ip address if there is no existing setting
 
-       if (!x($url)) {
+       if (empty($url)) {
                $url = Config::set('system', 'url', System::baseUrl());
        }
        if ((!link_compare($url, System::baseUrl())) && (!preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/", $a->get_hostname))) {
@@ -669,63 +712,46 @@ function check_url(App $a)
 function update_db(App $a)
 {
        $build = Config::get('system', 'build');
-       if (!x($build)) {
-               $build = Config::set('system', 'build', DB_UPDATE_VERSION);
+
+       if (empty($build)) {
+               Config::set('system', 'build', DB_UPDATE_VERSION);
+               $build = DB_UPDATE_VERSION;
        }
 
        if ($build != DB_UPDATE_VERSION) {
+               require_once 'update.php';
+
                $stored = intval($build);
                $current = intval(DB_UPDATE_VERSION);
                if ($stored < $current) {
                        Config::load('database');
 
-                       // We're reporting a different version than what is currently installed.
-                       // Run any existing update scripts to bring the database up to current.
-                       // make sure that boot.php and update.php are the same release, we might be
-                       // updating right this very second and the correct version of the update.php
-                       // file may not be here yet. This can happen on a very busy site.
-
-                       if (DB_UPDATE_VERSION == UPDATE_VERSION) {
-                               // Compare the current structure with the defined structure
-
-                               $t = Config::get('database', 'dbupdate_' . DB_UPDATE_VERSION);
-                               if ($t !== false) {
-                                       return;
-                               }
-
-                               Config::set('database', 'dbupdate_' . DB_UPDATE_VERSION, time());
-
-                               // run old update routine (wich could modify the schema and
-                               // conflits with new routine)
-                               for ($x = $stored; $x < NEW_UPDATE_ROUTINE_VERSION; $x++) {
-                                       $r = run_update_function($x);
-                                       if (!$r) {
-                                               break;
-                                       }
-                               }
-                               if ($stored < NEW_UPDATE_ROUTINE_VERSION) {
-                                       $stored = NEW_UPDATE_ROUTINE_VERSION;
-                               }
+                       // Compare the current structure with the defined structure
+                       $t = Config::get('database', 'dbupdate_' . DB_UPDATE_VERSION);
+                       if (!is_null($t)) {
+                               return;
+                       }
 
-                               // run new update routine
-                               // it update the structure in one call
-                               $retval = update_structure(false, true);
-                               if ($retval) {
-                                       update_fail(
-                                               DB_UPDATE_VERSION,
-                                               $retval
-                                       );
-                                       return;
-                               } else {
-                                       Config::set('database', 'dbupdate_' . DB_UPDATE_VERSION, 'success');
-                               }
+                       Config::set('database', 'dbupdate_' . DB_UPDATE_VERSION, time());
+
+                       // run update routine
+                       // it update the structure in one call
+                       $retval = DBStructure::update(false, true);
+                       if ($retval) {
+                               DBStructure::updateFail(
+                                       DB_UPDATE_VERSION,
+                                       $retval
+                               );
+                               return;
+                       } else {
+                               Config::set('database', 'dbupdate_' . DB_UPDATE_VERSION, 'success');
+                       }
 
-                               // run any left update_nnnn functions in update.php
-                               for ($x = $stored; $x < $current; $x ++) {
-                                       $r = run_update_function($x);
-                                       if (!$r) {
-                                               break;
-                                       }
+                       // run any left update_nnnn functions in update.php
+                       for ($x = $stored + 1; $x <= $current; $x++) {
+                               $r = run_update_function($x);
+                               if (!$r) {
+                                       break;
                                }
                        }
                }
@@ -745,7 +771,7 @@ function run_update_function($x)
                // delete the config entry to try again.
 
                $t = Config::get('database', 'update_' . $x);
-               if ($t !== false) {
+               if (!is_null($t)) {
                        return false;
                }
                Config::set('database', 'update_' . $x, time());
@@ -757,7 +783,7 @@ function run_update_function($x)
 
                if ($retval) {
                        //send the administrator an e-mail
-                       update_fail(
+                       DBStructure::updateFail(
                                $x,
                                sprintf(t('Update %s failed. See error logs.'), $x)
                        );
@@ -933,14 +959,8 @@ function login($register = false, $hiddens = false)
  */
 function killme()
 {
-       global $session_exists;
-
        if (!get_app()->is_backend()) {
-               if (!$session_exists) {
-                       session_abort();
-               } else {
-                       session_write_close();
-               }
+               session_write_close();
        }
 
        exit();
@@ -984,10 +1004,10 @@ function public_contact()
        if (!$public_contact_id && x($_SESSION, 'authenticated')) {
                if (x($_SESSION, 'my_address')) {
                        // Local user
-                       $public_contact_id = intval(get_contact($_SESSION['my_address'], 0));
+                       $public_contact_id = intval(Contact::getIdForURL($_SESSION['my_address'], 0));
                } elseif (x($_SESSION, 'visitor_home')) {
                        // Remote user
-                       $public_contact_id = intval(get_contact($_SESSION['visitor_home'], 0));
+                       $public_contact_id = intval(Contact::getIdForURL($_SESSION['visitor_home'], 0));
                }
        } elseif (!x($_SESSION, 'authenticated')) {
                $public_contact_id = false;
@@ -1007,7 +1027,7 @@ function remote_user()
        if (local_user()) {
                return false;
        }
-       if ((x($_SESSION, 'authenticated')) && (x($_SESSION, 'visitor_id'))) {
+       if (x($_SESSION, 'authenticated') && x($_SESSION, 'visitor_id')) {
                return intval($_SESSION['visitor_id']);
        }
        return false;
@@ -1062,21 +1082,9 @@ function info($s)
 function get_max_import_size()
 {
        $a = get_app();
-       return ((x($a->config, 'max_import_size')) ? $a->config['max_import_size'] : 0 );
+       return (x($a->config, 'max_import_size') ? $a->config['max_import_size'] : 0);
 }
 
-/**
- * @brief compatibilty wrapper for Worker::add function
- *
- * @param (integer|array) priority or parameter array, strings are deprecated and are ignored
- *
- * @return boolean "false" if proc_run couldn't be executed
- */
-function proc_run()
-{
-       $proc_args = func_get_args();
-       call_user_func_array('Friendica\Core\Worker::add', $proc_args);
-}
 
 function current_theme()
 {
@@ -1624,14 +1632,11 @@ function argv($x)
 function infinite_scroll_data($module)
 {
        if (PConfig::get(local_user(), 'system', 'infinite_scroll')
-               && ($module == "network") && ($_GET["mode"] != "minimal")
+               && $module == 'network'
+               && defaults($_GET, 'mode', '') != 'minimal'
        ) {
                // get the page number
-               if (is_string($_GET["page"])) {
-                       $pageno = $_GET["page"];
-               } else {
-                       $pageno = 1;
-               }
+               $pageno = defaults($_GET, 'page', 1);
 
                $reload_uri = "";
 
@@ -1642,7 +1647,8 @@ function infinite_scroll_data($module)
                        }
                }
 
-               if (($a->page_offset != "") && ! strstr($reload_uri, "&offset=")) {
+               $a = get_app();
+               if ($a->page_offset != "" && !strstr($reload_uri, "&offset=")) {
                        $reload_uri .= "&offset=" . urlencode($a->page_offset);
                }