- /**
- * Figure out if we are running at the top of a domain or in a sub-directory and adjust accordingly
- */
- private function determineURLPath()
- {
- /*
- * The automatic path detection in this function is currently deactivated,
- * see issue https://github.com/friendica/friendica/issues/6679
- *
- * The problem is that the function seems to be confused with some url.
- * These then confuses the detection which changes the url path.
- */
-
- /* Relative script path to the web server root
- * Not all of those $_SERVER properties can be present, so we do by inverse priority order
- */
-/*
- $relative_script_path = '';
- $relative_script_path = defaults($_SERVER, 'REDIRECT_URL' , $relative_script_path);
- $relative_script_path = defaults($_SERVER, 'REDIRECT_URI' , $relative_script_path);
- $relative_script_path = defaults($_SERVER, 'REDIRECT_SCRIPT_URL', $relative_script_path);
- $relative_script_path = defaults($_SERVER, 'SCRIPT_URL' , $relative_script_path);
- $relative_script_path = defaults($_SERVER, 'REQUEST_URI' , $relative_script_path);
-*/
- $this->urlPath = $this->config->get('system', 'urlpath');
-
- /* $relative_script_path gives /relative/path/to/friendica/module/parameter
- * QUERY_STRING gives pagename=module/parameter
- *
- * To get /relative/path/to/friendica we perform dirname() for as many levels as there are slashes in the QUERY_STRING
- */
-/*
- if (!empty($relative_script_path)) {
- // Module
- if (!empty($_SERVER['QUERY_STRING'])) {
- $path = trim(rdirname($relative_script_path, substr_count(trim($_SERVER['QUERY_STRING'], '/'), '/') + 1), '/');
- } else {
- // Root page
- $path = trim($relative_script_path, '/');
- }
-
- if ($path && $path != $this->urlPath) {
- $this->urlPath = $path;
- }
- }
-*/
- }
-
- public function getScheme()
- {
- return $this->scheme;
- }
-
- /**
- * @brief Retrieves the Friendica instance base URL
- *
- * This function assembles the base URL from multiple parts:
- * - Protocol is determined either by the request or a combination of
- * system.ssl_policy and the $ssl parameter.
- * - Host name is determined either by system.hostname or inferred from request
- * - Path is inferred from SCRIPT_NAME
- *
- * 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
- * @return string Friendica server base URL
- * @throws InternalServerErrorException
- */
- public function getBaseURL($ssl = false)
- {
- $scheme = $this->scheme;
-
- if ($this->config->get('system', 'ssl_policy') == SSL_POLICY_FULL) {
- $scheme = 'https';
- }
-
- // Basically, we have $ssl = true on any links which can only be seen by a logged in user
- // (and also the login link). Anything seen by an outsider will have it turned off.
-
- if ($this->config->get('system', 'ssl_policy') == SSL_POLICY_SELFSIGN) {
- if ($ssl) {
- $scheme = 'https';
- } else {
- $scheme = 'http';
- }
- }
-
- if ($this->config->get('config', 'hostname') != '') {
- $this->hostname = $this->config->get('config', 'hostname');
- }
-
- return $scheme . '://' . $this->hostname . (!empty($this->getURLPath()) ? '/' . $this->getURLPath() : '' );
- }
-
- /**
- * @brief Initializes the baseurl components
- *
- * Clears the baseurl cache to prevent inconsistencies
- *
- * @param string $url
- * @throws InternalServerErrorException
- */
- public function setBaseURL($url)
- {
- $parsed = @parse_url($url);
- $hostname = '';
-
- if (!empty($parsed)) {
- if (!empty($parsed['scheme'])) {
- $this->scheme = $parsed['scheme'];
- }
-
- if (!empty($parsed['host'])) {
- $hostname = $parsed['host'];
- }
-
- if (!empty($parsed['port'])) {
- $hostname .= ':' . $parsed['port'];
- }
- if (!empty($parsed['path'])) {
- $this->urlPath = trim($parsed['path'], '\\/');
- }
-
- if (file_exists($this->getBasePath() . '/.htpreconfig.php')) {
- include $this->getBasePath() . '/.htpreconfig.php';
- }
-
- if ($this->config->get('config', 'hostname') != '') {
- $this->hostname = $this->config->get('config', 'hostname');
- }
-
- if (!isset($this->hostname) || ($this->hostname == '')) {
- $this->hostname = $hostname;
- }
- }
- }
-
- public function getHostName()
- {
- if ($this->config->get('config', 'hostname') != '') {
- $this->hostname = $this->config->get('config', 'hostname');
- }
-
- return $this->hostname;
- }
-
- public function getURLPath()
- {
- return $this->urlPath;
- }
-
- /**
- * Initializes App->page['htmlhead'].
- *
- * Includes:
- * - Page title
- * - Favicons
- * - Registered stylesheets (through App->registerStylesheet())
- * - Infinite scroll data
- * - head.tpl template
- */
- public function initHead()
- {
- $interval = ((local_user()) ? Core\PConfig::get(local_user(), 'system', 'update_interval') : 40000);
-
- // If the update is 'deactivated' set it to the highest integer number (~24 days)
- if ($interval < 0) {
- $interval = 2147483647;
- }
-
- if ($interval < 10000) {
- $interval = 40000;
- }
-
- // Default title: current module called
- if (empty($this->page['title']) && $this->module) {
- $this->page['title'] = ucfirst($this->module);
- }
-
- // Prepend the sitename to the page title
- $this->page['title'] = $this->config->get('config', 'sitename', '') . (!empty($this->page['title']) ? ' | ' . $this->page['title'] : '');
-
- if (!empty(Core\Renderer::$theme['stylesheet'])) {
- $stylesheet = Core\Renderer::$theme['stylesheet'];
- } else {
- $stylesheet = $this->getCurrentThemeStylesheetPath();
- }
-
- $this->registerStylesheet($stylesheet);
-
- $shortcut_icon = $this->config->get('system', 'shortcut_icon');
- if ($shortcut_icon == '') {
- $shortcut_icon = 'images/friendica-32.png';
- }
-
- $touch_icon = $this->config->get('system', 'touch_icon');
- if ($touch_icon == '') {
- $touch_icon = 'images/friendica-128.png';
- }
-
- Core\Hook::callAll('head', $this->page['htmlhead']);
-
- $tpl = Core\Renderer::getMarkupTemplate('head.tpl');
- /* put the head template at the beginning of page['htmlhead']
- * since the code added by the modules frequently depends on it
- * being first
- */
- $this->page['htmlhead'] = Core\Renderer::replaceMacros($tpl, [
- '$baseurl' => $this->getBaseURL(),
- '$local_user' => local_user(),
- '$generator' => 'Friendica' . ' ' . FRIENDICA_VERSION,
- '$delitem' => Core\L10n::t('Delete this item?'),
- '$update_interval' => $interval,
- '$shortcut_icon' => $shortcut_icon,
- '$touch_icon' => $touch_icon,
- '$block_public' => intval($this->config->get('system', 'block_public')),
- '$stylesheets' => $this->stylesheets,
- ]) . $this->page['htmlhead'];
- }
-
- /**
- * Initializes App->page['footer'].
- *
- * Includes:
- * - Javascript homebase
- * - Mobile toggle link
- * - Registered footer scripts (through App->registerFooterScript())
- * - footer.tpl template
- */
- public function initFooter()
- {
- // If you're just visiting, let javascript take you home
- if (!empty($_SESSION['visitor_home'])) {
- $homebase = $_SESSION['visitor_home'];
- } elseif (local_user()) {
- $homebase = 'profile/' . $this->user['nickname'];
- }
-
- if (isset($homebase)) {
- $this->page['footer'] .= '<script>var homebase="' . $homebase . '";</script>' . "\n";
- }
-
- /*
- * Add a "toggle mobile" link if we're using a mobile device
- */
- if ($this->is_mobile || $this->is_tablet) {
- if (isset($_SESSION['show-mobile']) && !$_SESSION['show-mobile']) {
- $link = 'toggle_mobile?address=' . urlencode(curPageURL());
- } else {
- $link = 'toggle_mobile?off=1&address=' . urlencode(curPageURL());
- }
- $this->page['footer'] .= Core\Renderer::replaceMacros(Core\Renderer::getMarkupTemplate("toggle_mobile_footer.tpl"), [
- '$toggle_link' => $link,
- '$toggle_text' => Core\L10n::t('toggle mobile')
- ]);
- }
-
- Core\Hook::callAll('footer', $this->page['footer']);
-
- $tpl = Core\Renderer::getMarkupTemplate('footer.tpl');
- $this->page['footer'] = Core\Renderer::replaceMacros($tpl, [
- '$baseurl' => $this->getBaseURL(),
- '$footerScripts' => $this->footerScripts,
- ]) . $this->page['footer'];
- }
-
- /**
- * @brief Removes the base url from an url. This avoids some mixed content problems.
- *
- * @param string $origURL
- *
- * @return string The cleaned url
- * @throws InternalServerErrorException
- */
- public function removeBaseURL($origURL)
- {
- // Remove the hostname from the url if it is an internal link
- $nurl = Util\Strings::normaliseLink($origURL);
- $base = Util\Strings::normaliseLink($this->getBaseURL());
- $url = str_replace($base . '/', '', $nurl);
-
- // if it is an external link return the orignal value
- if ($url == Util\Strings::normaliseLink($origURL)) {
- return $origURL;
- } else {
- return $url;
- }
- }
-