X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FApp.php;h=652b048260bf835e0006e076c96ead8e17e8f27b;hb=e952d6677bf01ab23f80c4ffbe0e4e3d0c1ef43b;hp=2330bc118586026f119e80c0c3a5596d171a58f4;hpb=5e5fde0a63c57d2873bfa29404110269ca9abeee;p=friendica.git diff --git a/src/App.php b/src/App.php index 2330bc1185..652b048260 100644 --- a/src/App.php +++ b/src/App.php @@ -4,17 +4,19 @@ */ namespace Friendica; -use Friendica\Core\Cache; use Friendica\Core\Config; use Friendica\Core\L10n; use Friendica\Core\PConfig; use Friendica\Core\System; +use Friendica\Database\DBM; +use dba; use Detection\MobileDetect; use Exception; require_once 'boot.php'; +require_once 'include/dba.php'; require_once 'include/text.php'; /** @@ -32,11 +34,16 @@ require_once 'include/text.php'; */ class App { + const MODE_NORMAL = 0; + const MODE_INSTALL = 1; + const MODE_MAINTENANCE = 2; + public $module_loaded = false; public $module_class = null; - public $query_string; - public $config; - public $page; + public $query_string = ''; + public $config = []; + public $page = []; + public $pager = []; public $page_offset; public $profile; public $profile_uid; @@ -48,15 +55,15 @@ class App public $content; public $data = []; public $error = false; - public $cmd; + public $cmd = ''; public $argv; public $argc; public $module; - public $pager; + public $mode = App::MODE_NORMAL; public $strings; public $basepath; - public $path; - public $hooks; + public $urlpath; + public $hooks = []; public $timezone; public $interactive = true; public $addons; @@ -120,7 +127,6 @@ class App private $curl_code; private $curl_content_type; private $curl_headers; - private static $a; /** * @brief App constructor. @@ -129,21 +135,21 @@ class App */ public function __construct($basepath) { - global $default_timezone; - if (!static::directory_usable($basepath, false)) { throw new Exception('Basepath ' . $basepath . ' isn\'t usable.'); } $this->basepath = rtrim($basepath, DIRECTORY_SEPARATOR); - if (file_exists($this->basepath . DIRECTORY_SEPARATOR . '.htpreconfig.php')) { - include $this->basepath . DIRECTORY_SEPARATOR . '.htpreconfig.php'; - } + $this->determineUrlPath(); - $this->timezone = ((x($default_timezone)) ? $default_timezone : 'UTC'); + $this->loadConfigFiles(); - date_default_timezone_set($this->timezone); + $this->loadDatabase(); + + $this->determineMode(); + + $this->loadDefaultTimezone(); $this->performance['start'] = microtime(true); $this->performance['database'] = 0; @@ -166,15 +172,26 @@ class App $this->callstack['rendering'] = []; $this->callstack['parser'] = []; - $this->config = []; - $this->page = []; - $this->pager = []; + $this->page = [ + 'aside' => '', + 'bottom' => '', + 'content' => '', + 'end' => '', + 'footer' => '', + 'htmlhead' => '', + 'nav' => '', + 'page_title' => '', + 'right_aside' => '', + 'template' => '', + 'title' => '' + ]; - $this->query_string = ''; + $this->process_id = System::processID('log'); - $this->process_id = uniqid('log', true); + set_time_limit(0); - startup(); + // This has to be quite large to deal with embedded private photos + ini_set('pcre.backtrack_limit', 500000); $this->scheme = 'http'; @@ -194,16 +211,6 @@ class App if (x($_SERVER, 'SERVER_PORT') && $_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) { $this->hostname .= ':' . $_SERVER['SERVER_PORT']; } - /* - * Figure out if we are running at the top of a domain - * or in a sub-directory and adjust accordingly - */ - - /// @TODO This kind of escaping breaks syntax-highlightning on CoolEdit (Midnight Commander) - $path = trim(dirname($_SERVER['SCRIPT_NAME']), '/\\'); - if (isset($path) && strlen($path) && ($path != $this->path)) { - $this->path = $path; - } } set_include_path( @@ -212,27 +219,18 @@ class App . $this->basepath . DIRECTORY_SEPARATOR . 'library' . PATH_SEPARATOR . $this->basepath); - - if (is_array($_SERVER['argv']) && $_SERVER['argc'] > 1 && substr(end($_SERVER['argv']), 0, 4) == 'http') { - $this->set_baseurl(array_pop($_SERVER['argv'])); - $_SERVER['argc'] --; - } - if ((x($_SERVER, 'QUERY_STRING')) && substr($_SERVER['QUERY_STRING'], 0, 9) === 'pagename=') { $this->query_string = substr($_SERVER['QUERY_STRING'], 9); - - // removing trailing / - maybe a nginx problem - $this->query_string = ltrim($this->query_string, '/'); } elseif ((x($_SERVER, 'QUERY_STRING')) && substr($_SERVER['QUERY_STRING'], 0, 2) === 'q=') { $this->query_string = substr($_SERVER['QUERY_STRING'], 2); - - // removing trailing / - maybe a nginx problem - $this->query_string = ltrim($this->query_string, '/'); } - if (x($_GET, 'pagename')) { + // removing trailing / - maybe a nginx problem + $this->query_string = ltrim($this->query_string, '/'); + + if (!empty($_GET['pagename'])) { $this->cmd = trim($_GET['pagename'], '/\\'); - } elseif (x($_GET, 'q')) { + } elseif (!empty($_GET['q'])) { $this->cmd = trim($_GET['q'], '/\\'); } @@ -289,12 +287,178 @@ class App $this->is_tablet = $mobile_detect->isTablet(); // Friendica-Client - $this->is_friendica_app = ($_SERVER['HTTP_USER_AGENT'] == 'Apache-HttpClient/UNAVAILABLE (java 1.4)'); + $this->is_friendica_app = isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT'] == 'Apache-HttpClient/UNAVAILABLE (java 1.4)'; // Register template engines $this->register_template_engine('Friendica\Render\FriendicaSmartyEngine'); + } + + private function loadConfigFiles() + { + $this->loadConfigFile($this->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'defaults.ini.php'); + + // Legacy .htconfig.php support + if (file_exists($this->basepath . DIRECTORY_SEPARATOR . '.htpreconfig.php')) { + $a = $this; + include $this->basepath . DIRECTORY_SEPARATOR . '.htpreconfig.php'; + } + + // Legacy .htconfig.php support + if (file_exists($this->basepath . DIRECTORY_SEPARATOR . '.htconfig.php')) { + $a = $this; + + include $this->basepath . DIRECTORY_SEPARATOR . '.htconfig.php'; + unset($db_host, $db_user, $db_pass, $db_data); + + if (isset($default_timezone)) { + $this->setConfigValue('system', 'default_timezone', $default_timezone); + unset($default_timezone); + } + + if (isset($pidfile)) { + $this->setConfigValue('system', 'pidfile', $pidfile); + unset($pidfile); + } + } + + if (file_exists($this->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php')) { + $this->loadConfigFile($this->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php'); + } + } + + public function loadConfigFile($filepath) + { + if (!file_exists($filepath)) { + throw new Exception('Error parsing non-existent config file ' . $filepath); + } + + $contents = include($filepath); + + $config = parse_ini_string($contents, true, INI_SCANNER_TYPED); + + if ($config === false) { + throw new Exception('Error parsing config file ' . $filepath); + } + + foreach($config as $category => $values) { + foreach($values as $key => $value) { + $this->setConfigValue($category, $key, $value); + } + } + } + + private function loadAddonConfig() + { + // Loads addons default config + Core\Addon::callHooks('load_config'); + + // Load the local config file again in case there are overwritten addon config + if (file_exists($this->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php')) { + $this->loadConfigFile($this->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php'); + } + } + + private function loadDefaultTimezone() + { + if ($this->getConfigValue('system', 'default_timezone')) { + $this->timezone = $this->getConfigValue('system', 'default_timezone'); + } else { + global $default_timezone; + $this->timezone = !empty($default_timezone) ? $default_timezone : 'UTC'; + } - self::$a = $this; + if ($this->timezone) { + date_default_timezone_set($this->timezone); + } + } + + /** + * Figure out if we are running at the top of a domain + * or in a sub-directory and adjust accordingly + */ + private function determineUrlPath() + { + $this->urlpath = $this->getConfigValue('system', 'urlpath'); + + /* SCRIPT_URL gives /path/to/friendica/module/parameter + * QUERY_STRING gives pagename=module/parameter + * + * To get /path/to/friendica we perform dirname() for as many levels as there are slashes in the QUERY_STRING + */ + if (!empty($_SERVER['SCRIPT_URL']) && !empty($_SERVER['QUERY_STRING'])) { + $path = trim(dirname($_SERVER['SCRIPT_URL'], substr_count(trim($_SERVER['QUERY_STRING'], '/'), '/') + 1), '/'); + + if ($path && $path != $this->urlpath) { + $this->urlpath = $path; + } + } + } + + private function determineMode() + { + $this->mode = App::MODE_INSTALL; + + // Missing DB connection + if (!\dba::connected()) { + return; + } + + // Working DB connection, missing tables + if (\dba::fetch_first("SHOW TABLES LIKE 'config'") === false) { + return; + } + + // Maintenance mode check + if (Config::get('system', 'maintenance')) { + $this->mode = App::MODE_MAINTENANCE; + } else { + $this->mode = App::MODE_NORMAL; + } + } + + public function loadDatabase() + { + if (\dba::connected()) { + return; + } + + $db_host = $this->getConfigValue('database', 'hostname'); + $db_user = $this->getConfigValue('database', 'username'); + $db_pass = $this->getConfigValue('database', 'password'); + $db_data = $this->getConfigValue('database', 'database'); + $charset = $this->getConfigValue('database', 'charset'); + + // Use environment variables for mysql if they are set beforehand + if (!empty(getenv('MYSQL_HOST')) + && !empty(getenv('MYSQL_PORT')) + && (!empty(getenv('MYSQL_USERNAME')) || !empty(getenv('MYSQL_USER'))) + && !empty(getenv('MYSQL_PASSWORD')) + && !empty(getenv('MYSQL_DATABASE'))) + { + $db_host = getenv('MYSQL_HOST') . ':' . getenv('MYSQL_PORT'); + + if (!empty(getenv('MYSQL_USERNAME'))) { + $db_user = getenv('MYSQL_USERNAME'); + } elseif (!empty(getenv('MYSQL_USER'))) { + $db_user = getenv('MYSQL_USER'); + } + + $db_pass = getenv('MYSQL_PASSWORD'); + $db_data = getenv('MYSQL_DATABASE'); + }elseif (file_exists($this->basepath . DIRECTORY_SEPARATOR . '.htconfig.php')) { + $a = new \stdClass(); + include $this->basepath . DIRECTORY_SEPARATOR . '.htconfig.php'; + $charset = isset($a->config["system"]["db_charset"]) ? $a->config["system"]["db_charset"] : $charset; + + unset($a); + } + + $stamp1 = microtime(true); + + \dba::connect($db_host, $db_user, $db_pass, $db_data, $charset); + unset($db_host, $db_user, $db_pass, $db_data, $charset); + + $this->save_timestamp($stamp1, "network"); } /** @@ -387,7 +551,7 @@ class App $this->hostname = Config::get('config', 'hostname'); } - return $scheme . '://' . $this->hostname . ((isset($this->path) && strlen($this->path)) ? '/' . $this->path : '' ); + return $scheme . '://' . $this->hostname . (!empty($this->urlpath) ? '/' . $this->urlpath : '' ); } /** @@ -400,16 +564,22 @@ class App public function set_baseurl($url) { $parsed = @parse_url($url); + $hostname = ''; if (x($parsed)) { - $this->scheme = $parsed['scheme']; + if (!empty($parsed['scheme'])) { + $this->scheme = $parsed['scheme']; + } + + if (!empty($parsed['host'])) { + $hostname = $parsed['host']; + } - $hostname = $parsed['host']; if (x($parsed, 'port')) { $hostname .= ':' . $parsed['port']; } if (x($parsed, 'path')) { - $this->path = trim($parsed['path'], '\\/'); + $this->urlpath = trim($parsed['path'], '\\/'); } if (file_exists($this->basepath . DIRECTORY_SEPARATOR . '.htpreconfig.php')) { @@ -420,7 +590,7 @@ class App $this->hostname = Config::get('config', 'hostname'); } - if (!isset($this->hostname) || ( $this->hostname == '')) { + if (!isset($this->hostname) || ($this->hostname == '')) { $this->hostname = $hostname; } } @@ -437,7 +607,7 @@ class App public function get_path() { - return $this->path; + return $this->urlpath; } public function set_pager_total($n) @@ -522,6 +692,7 @@ class App '$touch_icon' => $touch_icon, '$stylesheet' => $stylesheet, '$infinite_scroll' => $invinite_scroll, + '$block_public' => intval(Config::get('system', 'block_public')), ]) . $this->page['htmlhead']; } @@ -854,22 +1025,7 @@ class App return; } - // If the last worker fork was less than 2 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) && ( time() - $result) < 2) { - return; - } - - // 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(); + array_unshift($args, $this->getConfigValue('config', 'php_path', 'php')); for ($x = 0; $x < count($args); $x ++) { $args[$x] = escapeshellarg($args[$x]); @@ -881,7 +1037,7 @@ class App return; } - if (Config::get('system', 'proc_windows')) { + if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { $resource = proc_open('cmd /c start /b ' . $cmdline, [], $foo, $this->get_basepath()); } else { $resource = proc_open($cmdline . ' &', [], $foo, $this->get_basepath()); @@ -982,6 +1138,10 @@ class App if ($cat === 'config') { $this->config[$k] = $value; } else { + if (!isset($this->config[$cat])) { + $this->config[$cat] = []; + } + $this->config[$cat][$k] = $value; } } @@ -1040,6 +1200,14 @@ class App // Only arrays are serialized in database, so we have to unserialize sparingly $value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v; + if (!isset($this->config[$uid])) { + $this->config[$uid] = []; + } + + if (!isset($this->config[$uid][$cat])) { + $this->config[$uid][$cat] = []; + } + $this->config[$uid][$cat][$k] = $value; } @@ -1056,4 +1224,115 @@ class App unset($this->config[$uid][$cat][$k]); } } + + /** + * Generates the site's default sender email address + * + * @return string + */ + public function getSenderEmailAddress() + { + $sender_email = Config::get('config', 'sender_email'); + if (empty($sender_email)) { + $hostname = $this->get_hostname(); + if (strpos($hostname, ':')) { + $hostname = substr($hostname, 0, strpos($hostname, ':')); + } + + $sender_email = 'noreply@' . $hostname; + } + + return $sender_email; + } + + /** + * Returns the current theme name. + * + * @return string + */ + public function getCurrentTheme() + { + if ($this->mode == App::MODE_INSTALL) { + return ''; + } + + //// @TODO Compute the current theme only once (this behavior has + /// already been implemented, but it didn't work well - + /// https://github.com/friendica/friendica/issues/5092) + $this->computeCurrentTheme(); + + return $this->current_theme; + } + + /** + * Computes the current theme name based on the node settings, the user settings and the device type + * + * @throws Exception + */ + private function computeCurrentTheme() + { + $system_theme = Config::get('system', 'theme'); + if (!$system_theme) { + throw new Exception(L10n::t('No system theme config value set.')); + } + + // Sane default + $this->current_theme = $system_theme; + + $allowed_themes = explode(',', Config::get('system', 'allowed_themes', $system_theme)); + + $page_theme = null; + // Find the theme that belongs to the user whose stuff we are looking at + if ($this->profile_uid && ($this->profile_uid != local_user())) { + // Allow folks to override user themes and always use their own on their own site. + // This works only if the user is on the same server + $user = dba::selectFirst('user', ['theme'], ['uid' => $this->profile_uid]); + if (DBM::is_result($user) && !PConfig::get(local_user(), 'system', 'always_my_theme')) { + $page_theme = $user['theme']; + } + } + + if (!empty($_SESSION)) { + $user_theme = defaults($_SESSION, 'theme', $system_theme); + } else { + $user_theme = $system_theme; + } + + // Specific mobile theme override + if (($this->is_mobile || $this->is_tablet) && defaults($_SESSION, 'show-mobile', true)) { + $system_mobile_theme = Config::get('system', 'mobile-theme'); + $user_mobile_theme = defaults($_SESSION, 'mobile-theme', $system_mobile_theme); + + // --- means same mobile theme as desktop + if (!empty($user_mobile_theme) && $user_mobile_theme !== '---') { + $user_theme = $user_mobile_theme; + } + } + + if ($page_theme) { + $theme_name = $page_theme; + } else { + $theme_name = $user_theme; + } + + if ($theme_name + && in_array($theme_name, $allowed_themes) + && (file_exists('view/theme/' . $theme_name . '/style.css') + || file_exists('view/theme/' . $theme_name . '/style.php')) + ) { + $this->current_theme = $theme_name; + } + } + + /** + * @brief Return full URL to theme which is currently in effect. + * + * Provide a sane default if nothing is chosen or the specified theme does not exist. + * + * @return string + */ + public function getCurrentThemeStylesheetPath() + { + return Core\Theme::getStylesheetPath($this->getCurrentTheme()); + } }