]> git.mxchange.org Git - friendica.git/blobdiff - src/App/BaseURL.php
Create specific module to display HTML message when a conversation isn't found in...
[friendica.git] / src / App / BaseURL.php
index 7e1ab1c2b9a3418cf37917d1f89d3a3f9345c405..564527a652287f0327fc513c1df50c2d2b28ad4f 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -21,7 +21,7 @@
 
 namespace Friendica\App;
 
-use Friendica\Core\Config\IConfig;
+use Friendica\Core\Config\Capability\IManageConfigValues;
 use Friendica\Core\System;
 use Friendica\Util\Network;
 use Friendica\Util\Strings;
@@ -56,7 +56,7 @@ class BaseURL
        /**
         * The Friendica Config
         *
-        * @var IConfig
+        * @var IManageConfigValues
         */
        private $config;
 
@@ -107,7 +107,7 @@ class BaseURL
         *
         * @return string
         */
-       public function getHostname()
+       public function getHostname(): string
        {
                return $this->hostname;
        }
@@ -117,7 +117,7 @@ class BaseURL
         *
         * @return string
         */
-       public function getScheme()
+       public function getScheme(): string
        {
                return $this->scheme;
        }
@@ -127,7 +127,7 @@ class BaseURL
         *
         * @return int
         */
-       public function getSSLPolicy()
+       public function getSSLPolicy(): int
        {
                return $this->sslPolicy;
        }
@@ -137,7 +137,7 @@ class BaseURL
         *
         * @return string
         */
-       public function getUrlPath()
+       public function getUrlPath(): string
        {
                return $this->urlPath;
        }
@@ -151,7 +151,7 @@ class BaseURL
         *
         * @return string
         */
-       public function get($ssl = false)
+       public function get(bool $ssl = false): string
        {
                if ($this->sslPolicy === self::SSL_POLICY_SELFSIGN && $ssl) {
                        return Network::switchScheme($this->url);
@@ -168,71 +168,51 @@ class BaseURL
         * @param string? $urlPath
         *
         * @return bool true, if successful
+        * @TODO Find proper types
         */
-       public function save($hostname = null, $sslPolicy = null, $urlPath = null)
+       public function save($hostname = null, $sslPolicy = null, $urlPath = null): bool
        {
-               $currHostname  = $this->hostname;
-               $currSSLPolicy = $this->sslPolicy;
-               $currURLPath   = $this->urlPath;
+               $currUrl = $this->url;
+
+               $configTransaction = $this->config->beginTransaction();
 
                if (!empty($hostname) && $hostname !== $this->hostname) {
-                       if ($this->config->set('config', 'hostname', $hostname)) {
-                               $this->hostname = $hostname;
-                       } else {
-                               return false;
-                       }
+                       $configTransaction->set('config', 'hostname', $hostname);
+                       $this->hostname = $hostname;
                }
 
                if (isset($sslPolicy) && $sslPolicy !== $this->sslPolicy) {
-                       if ($this->config->set('system', 'ssl_policy', $sslPolicy)) {
-                               $this->sslPolicy = $sslPolicy;
-                       } else {
-                               $this->hostname = $currHostname;
-                               $this->config->set('config', 'hostname', $this->hostname);
-                               return false;
-                       }
+                       $configTransaction->set('system', 'ssl_policy', $sslPolicy);
+                       $this->sslPolicy = $sslPolicy;
                }
 
                if (isset($urlPath) && $urlPath !== $this->urlPath) {
-                       if ($this->config->set('system', 'urlpath', $urlPath)) {
-                               $this->urlPath = $urlPath;
-                       } else {
-                               $this->hostname  = $currHostname;
-                               $this->sslPolicy = $currSSLPolicy;
-                               $this->config->set('config', 'hostname', $this->hostname);
-                               $this->config->set('system', 'ssl_policy', $this->sslPolicy);
-                               return false;
-                       }
+                       $configTransaction->set('system', 'urlpath', $urlPath);
+                       $this->urlPath = $urlPath;
                }
 
                $this->determineBaseUrl();
-               if (!$this->config->set('system', 'url', $this->url)) {
-                       $this->hostname  = $currHostname;
-                       $this->sslPolicy = $currSSLPolicy;
-                       $this->urlPath   = $currURLPath;
-                       $this->determineBaseUrl();
-
-                       $this->config->set('config', 'hostname', $this->hostname);
-                       $this->config->set('system', 'ssl_policy', $this->sslPolicy);
-                       $this->config->set('system', 'urlpath', $this->urlPath);
-                       return false;
+               if ($this->url !== $currUrl) {
+                       $configTransaction->set('system', 'url', $this->url);
                }
 
+               $configTransaction->commit();
+
                return true;
        }
 
        /**
         * Save the current url as base URL
         *
-        * @param $url
+        * @param string $url
         *
         * @return bool true, if the save was successful
         */
-       public function saveByURL($url)
+       public function saveByURL(string $url): bool
        {
                $parsed = @parse_url($url);
 
-               if (empty($parsed)) {
+               if (empty($parsed) || empty($parsed['host'])) {
                        return false;
                }
 
@@ -272,114 +252,23 @@ class BaseURL
        }
 
        /**
-        * @param IConfig $config The Friendica IConfiguration
-        * @param array   $server The $_SERVER array
+        * @param IManageConfigValues $config The Friendica IConfiguration
+        * @param array               $server The $_SERVER array
         */
-       public function __construct(IConfig $config, array $server)
-       {
-               $this->config = $config;
-               $this->server = $server;
-
-               $this->determineSchema();
-               $this->checkConfig();
-       }
-
-       /**
-        * Check the current config during loading
-        */
-       public function checkConfig()
+       public function __construct(IManageConfigValues $config, array $server)
        {
+               $this->config    = $config;
+               $this->server    = $server;
                $this->hostname  = $this->config->get('config', 'hostname');
-               $this->urlPath   = $this->config->get('system', 'urlpath');
-               $this->sslPolicy = $this->config->get('system', 'ssl_policy');
+               $this->urlPath   = $this->config->get('system', 'urlpath') ?? '';
+               $this->sslPolicy = $this->config->get('system', 'ssl_policy') ?? static::DEFAULT_SSL_SCHEME;
                $this->url       = $this->config->get('system', 'url');
 
-               if (empty($this->hostname)) {
-                       $this->determineHostname();
-
-                       if (!empty($this->hostname)) {
-                               $this->config->set('config', 'hostname', $this->hostname);
-                       }
-               }
-
-               if (!isset($this->urlPath)) {
-                       $this->determineURLPath();
-                       $this->config->set('system', 'urlpath', $this->urlPath);
+               if (empty($this->hostname) || empty($this->url)) {
+                       throw new \Exception('Invalid config - Missing system.url or config.hostname');
                }
 
-               if (!isset($this->sslPolicy)) {
-                       if ($this->scheme == 'https') {
-                               $this->sslPolicy = self::SSL_POLICY_FULL;
-                       } else {
-                               $this->sslPolicy = self::DEFAULT_SSL_SCHEME;
-                       }
-                       $this->config->set('system', 'ssl_policy', $this->sslPolicy);
-               }
-
-               if (empty($this->url)) {
-                       $this->determineBaseUrl();
-
-                       if (!empty($this->url)) {
-                               $this->config->set('system', 'url', $this->url);
-                       }
-               }
-       }
-
-       /**
-        * Determines the hostname of this node if not set already
-        */
-       private function determineHostname()
-       {
-               $this->hostname = '';
-
-               if (!empty($this->server['SERVER_NAME'])) {
-                       $this->hostname = $this->server['SERVER_NAME'];
-
-                       if (!empty($this->server['SERVER_PORT']) && $this->server['SERVER_PORT'] != 80 && $this->server['SERVER_PORT'] != 443) {
-                               $this->hostname .= ':' . $this->server['SERVER_PORT'];
-                       }
-               }
-       }
-
-       /**
-        * Figure out if we are running at the top of a domain or in a sub-directory
-        */
-       private function determineURLPath()
-       {
-               $this->urlPath = '';
-
-               /*
-                * 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 =
-                       ($this->server['REDIRECT_URL']        ?? '') ?:
-                       ($this->server['REDIRECT_URI']        ?? '') ?:
-                       ($this->server['REDIRECT_SCRIPT_URL'] ?? '') ?:
-                       ($this->server['SCRIPT_URL']          ?? '') ?:
-                        $this->server['REQUEST_URI']         ?? '';
-
-               /* $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($this->server['QUERY_STRING'])) {
-                               $this->urlPath = trim(dirname($relative_script_path, substr_count(trim($this->server['QUERY_STRING'], '/'), '/') + 1), '/');
-                       } else {
-                               // Root page
-                               $this->urlPath = trim($relative_script_path, '/');
-                       }
-               }
+               $this->determineSchema();
        }
 
        /**
@@ -421,7 +310,7 @@ class BaseURL
         *
         * @return string The cleaned url
         */
-       public function remove(string $origURL)
+       public function remove(string $origURL): string
        {
                // Remove the hostname from the url if it is an internal link
                $nurl = Strings::normaliseLink($origURL);
@@ -443,12 +332,16 @@ class BaseURL
         * @param string $toUrl The destination URL (Default is empty, which is the default page of the Friendica node)
         * @param bool   $ssl   if true, base URL will try to get called with https:// (works just for relative paths)
         *
+        * @throws HTTPException\FoundException
+        * @throws HTTPException\MovedPermanentlyException
+        * @throws HTTPException\TemporaryRedirectException
+        *
         * @throws HTTPException\InternalServerErrorException In Case the given URL is not relative to the Friendica node
         */
-       public function redirect($toUrl = '', $ssl = false)
+       public function redirect(string $toUrl = '', bool $ssl = false)
        {
                if (!empty(parse_url($toUrl, PHP_URL_SCHEME))) {
-                       throw new HTTPException\InternalServerErrorException("'$toUrl is not a relative path, please use System::externalRedirectTo");
+                       throw new HTTPException\InternalServerErrorException("$toUrl is not a relative path, please use System::externalRedirectTo");
                }
 
                $redirectTo = $this->get($ssl) . '/' . ltrim($toUrl, '/');
@@ -458,8 +351,8 @@ class BaseURL
        /**
         * Returns the base url as string
         */
-       public function __toString()
+       public function __toString(): string
        {
-               return $this->get();
+               return (string) $this->get();
        }
 }