]> git.mxchange.org Git - friendica.git/blobdiff - boot.php
Use short form array syntax everywhere
[friendica.git] / boot.php
index 0dd8e03f254bbb96a02c1422509987c256a5b16a..4bfa5c09858acdf0e7d1e9e26e1ccf283bbb6f64 100644 (file)
--- a/boot.php
+++ b/boot.php
@@ -29,6 +29,7 @@ use Friendica\Core\Worker;
 use Friendica\Database\DBM;
 use Friendica\Model\Contact;
 use Friendica\Database\DBStructure;
+use Friendica\Module\Login;
 
 require_once 'include/network.php';
 require_once 'include/plugin.php';
@@ -36,14 +37,12 @@ require_once 'include/text.php';
 require_once 'include/datetime.php';
 require_once 'include/pgettext.php';
 require_once 'include/nav.php';
-require_once 'include/identity.php';
-require_once 'update.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',      1237);
+define('DB_UPDATE_VERSION',      1242);
 define('NEW_UPDATE_ROUTINE_VERSION', 1170);
 
 /**
@@ -229,9 +228,10 @@ define('ACCOUNT_TYPE_COMMUNITY',   3);
  * Type of the community page
  * @{
  */
-define('CP_NO_COMMUNITY_PAGE',   -1);
+define('CP_NO_COMMUNITY_PAGE',  -1);
 define('CP_USERS_ON_SERVER',     0);
 define('CP_GLOBAL_COMMUNITY',    1);
+define('CP_USERS_AND_GLOBAL',    2);
 /**
  * @}
  */
@@ -288,7 +288,7 @@ define('NETWORK_PHANTOM',          'unkn');    // Place holder
  * and existing allocations MUST NEVER BE CHANGED
  * OR RE-ASSIGNED! You may only add to them.
  */
-$netgroup_ids = array(
+$netgroup_ids = [
        NETWORK_DFRN     => (-1),
        NETWORK_ZOT      => (-2),
        NETWORK_OSTATUS  => (-3),
@@ -310,7 +310,7 @@ $netgroup_ids = array(
        NETWORK_PNUT      => (-20),
 
        NETWORK_PHANTOM  => (-127),
-);
+];
 
 /**
  * Maximum number of "people who like (or don't like) this"  that we will list by name
@@ -504,7 +504,7 @@ function startup()
        ini_set('pcre.backtrack_limit', 500000);
 
        if (get_magic_quotes_gpc()) {
-               $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
+               $process = [&$_GET, &$_POST, &$_COOKIE, &$_REQUEST];
                while (list($key, $val) = each($process)) {
                        foreach ($val as $k => $v) {
                                unset($process[$key][$k]);
@@ -573,6 +573,48 @@ function x($s, $k = null)
        }
 }
 
+/**
+ * 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 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;
+}
+
 /**
  * @brief Returns the baseurl.
  *
@@ -619,14 +661,21 @@ function is_ajax()
 function check_db($via_worker)
 {
        $build = Config::get('system', 'build');
-       if (!x($build)) {
-               Config::set('system', 'build', DB_UPDATE_VERSION);
+
+       if (empty($build)) {
+               Config::set('system', 'build', DB_UPDATE_VERSION - 1);
                $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) {
-                       update_db(get_app());
+                       update_db();
                }
        }
 }
@@ -647,11 +696,8 @@ 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)) {
-               $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))) {
-               $url = Config::set('system', 'url', System::baseUrl());
+       if (empty($url) || (!link_compare($url, System::baseUrl())) && (!preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/", $a->get_hostname))) {
+               Config::set('system', 'url', System::baseUrl());
        }
 
        return;
@@ -661,66 +707,49 @@ function check_url(App $a)
  * @brief Automatic database updates
  * @param object $a App
  */
-function update_db(App $a)
+function update_db()
 {
        $build = Config::get('system', 'build');
-       if (!x($build)) {
-               $build = Config::set('system', 'build', DB_UPDATE_VERSION);
+
+       if (empty($build) || ($build > DB_UPDATE_VERSION)) {
+               $build = DB_UPDATE_VERSION - 1;
+               Config::set('system', 'build', $build);
        }
 
        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 (!is_null($t)) {
-                                       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 = DBStructure::update(false, true);
-                               if ($retval) {
-                                       DBStructure::updateFail(
-                                               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;
                                }
                        }
                }
@@ -759,15 +788,14 @@ function run_update_function($x)
                        return false;
                } else {
                        Config::set('database', 'update_' . $x, 'success');
-                       Config::set('system', 'build', $x + 1);
+                       Config::set('system', 'build', $x);
                        return true;
                }
        } else {
                Config::set('database', 'update_' . $x, 'success');
-               Config::set('system', 'build', $x + 1);
+               Config::set('system', 'build', $x);
                return true;
        }
-       return true;
 }
 
 /**
@@ -789,11 +817,11 @@ function check_plugins(App $a)
        if (DBM::is_result($r)) {
                $installed = $r;
        } else {
-               $installed = array();
+               $installed = [];
        }
 
        $plugins = Config::get('system', 'addon');
-       $plugins_arr = array();
+       $plugins_arr = [];
 
        if ($plugins) {
                $plugins_arr = explode(',', str_replace(' ', '', $plugins));
@@ -801,7 +829,7 @@ function check_plugins(App $a)
 
        $a->plugins = $plugins_arr;
 
-       $installed_arr = array();
+       $installed_arr = [];
 
        if (count($installed)) {
                foreach ($installed as $i) {
@@ -846,83 +874,6 @@ function get_guid($size = 16, $prefix = "")
        }
 }
 
-/**
- * @brief Wrapper for adding a login box.
- *
- * @param bool $register If $register == true provide a registration link.
- *                                              This will most always depend on the value of $a->config['register_policy'].
- * @param bool $hiddens  optional
- *
- * @return string Returns the complete html for inserting into the page
- *
- * @hooks 'login_hook'
- *     string $o
- */
-function login($register = false, $hiddens = false)
-{
-       $a = get_app();
-       $o = "";
-       $reg = false;
-       if ($register) {
-               $reg = array(
-                       'title' => t('Create a New Account'),
-                       'desc' => t('Register')
-               );
-       }
-
-       $noid = Config::get('system', 'no_openid');
-
-       $dest_url = $a->query_string;
-
-       if (local_user()) {
-               $tpl = get_markup_template("logout.tpl");
-       } else {
-               $a->page['htmlhead'] .= replace_macros(
-                       get_markup_template("login_head.tpl"),
-                       array(
-                       '$baseurl' => $a->get_baseurl(true)
-                       )
-               );
-
-               $tpl = get_markup_template("login.tpl");
-               $_SESSION['return_url'] = $a->query_string;
-               $a->module = 'login';
-       }
-
-       $o .= replace_macros(
-               $tpl,
-               array(
-               '$dest_url'     => $dest_url,
-               '$logout'       => t('Logout'),
-               '$login'        => t('Login'),
-
-               '$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: '),'',''),
-
-               '$hiddens'      => $hiddens,
-
-               '$register'     => $reg,
-
-               '$lostpass'     => t('Forgot your password?'),
-               '$lostlink'     => t('Password Reset'),
-
-               '$tostitle'     => t('Website Terms of Service'),
-               '$toslink'      => t('terms of service'),
-
-               '$privacytitle' => t('Website Privacy Policy'),
-               '$privacylink'  => t('privacy policy'),
-               )
-       );
-
-       call_hooks('login_hook', $o);
-
-       return $o;
-}
-
 /**
  * @brief Used to end the current process, after saving session state.
  */
@@ -938,13 +889,15 @@ function killme()
 /**
  * @brief Redirect to another URL and terminate this process.
  */
-function goaway($s)
+function goaway($path)
 {
-       if (!strstr(normalise_link($s), "http://")) {
-               $s = System::baseUrl() . "/" . $s;
+       if (strstr(normalise_link($path), 'http://')) {
+               $url = $path;
+       } else {
+               $url = System::baseUrl() . '/' . ltrim($path, '/');
        }
 
-       header("Location: $s");
+       header("Location: $url");
        killme();
 }
 
@@ -996,7 +949,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;
@@ -1013,7 +966,7 @@ function notice($s)
 {
        $a = get_app();
        if (!x($_SESSION, 'sysmsg')) {
-               $_SESSION['sysmsg'] = array();
+               $_SESSION['sysmsg'] = [];
        }
        if ($a->interactive) {
                $_SESSION['sysmsg'][] = $s;
@@ -1036,7 +989,7 @@ function info($s)
        }
 
        if (!x($_SESSION, 'sysmsg_info')) {
-               $_SESSION['sysmsg_info'] = array();
+               $_SESSION['sysmsg_info'] = [];
        }
        if ($a->interactive) {
                $_SESSION['sysmsg_info'][] = $s;
@@ -1051,13 +1004,13 @@ 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);
 }
 
 
 function current_theme()
 {
-       $app_base_themes = array('duepuntozero', 'dispy', 'quattro');
+       $app_base_themes = ['duepuntozero', 'dispy', 'quattro'];
 
        $a = get_app();
 
@@ -1093,7 +1046,6 @@ function current_theme()
 
        if ($is_mobile) {
                if (isset($_SESSION['show-mobile']) && !$_SESSION['show-mobile']) {
-                       $system_theme = $standard_system_theme;
                        $theme_name = $standard_theme_name;
                } else {
                        $system_theme = Config::get('system', 'mobile-theme', '');
@@ -1104,7 +1056,6 @@ function current_theme()
 
                        if ($theme_name === '---') {
                                // user has selected to have the mobile theme be the same as the normal one
-                               $system_theme = $standard_system_theme;
                                $theme_name = $standard_theme_name;
 
                                if ($page_theme) {
@@ -1113,7 +1064,6 @@ function current_theme()
                        }
                }
        } else {
-               $system_theme = $standard_system_theme;
                $theme_name = $standard_theme_name;
 
                if ($page_theme) {
@@ -1288,10 +1238,10 @@ function explode_querystring($query)
                $args = array_values($args);
        }
 
-       return array(
+       return [
                'base' => $base,
                'args' => $args,
-       );
+       ];
 }
 
 /**
@@ -1601,14 +1551,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 = "";
 
@@ -1619,11 +1566,12 @@ 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);
                }
 
-               $arr = array("pageno" => $pageno, "reload_uri" => $reload_uri);
+               $arr = ["pageno" => $pageno, "reload_uri" => $reload_uri];
 
                return $arr;
        }