]> git.mxchange.org Git - friendica.git/commitdiff
Moved BaseURL to App namespace (because similar type as Arguments/Modules/Modes)
authorPhilipp Holzer <admin+github@philipp.info>
Thu, 15 Aug 2019 15:23:00 +0000 (17:23 +0200)
committerPhilipp Holzer <admin+github@philipp.info>
Thu, 15 Aug 2019 15:23:00 +0000 (17:23 +0200)
13 files changed:
src/App.php
src/App/BaseURL.php [new file with mode: 0644]
src/Core/Session.php
src/Core/System.php
src/Model/Contact.php
src/Module/Admin/Site.php
src/Module/Install.php
src/Protocol/DFRN.php
src/Util/BaseURL.php [deleted file]
static/dependencies.config.php
tests/src/Console/AutomaticInstallationConsoleTest.php
tests/src/Content/Text/BBCodeTest.php
tests/src/Util/BaseURLTest.php

index c9a66c523de9961cf5a7791e8b6ba3f553311cd5..0a7bab0e16c0fdcf33db028ec107985dca06aa3d 100644 (file)
@@ -9,6 +9,7 @@ use DOMDocument;
 use DOMXPath;
 use Exception;
 use Friendica\App\Arguments;
+use Friendica\App\BaseURL;
 use Friendica\Core\Config\Cache\ConfigCache;
 use Friendica\Core\Config\Configuration;
 use Friendica\Core\Config\PConfiguration;
@@ -20,7 +21,6 @@ use Friendica\Model\Profile;
 use Friendica\Module\Login;
 use Friendica\Module\Special\HTTPException as ModuleHTTPException;
 use Friendica\Network\HTTPException;
-use Friendica\Util\BaseURL;
 use Friendica\Util\ConfigFileLoader;
 use Friendica\Util\HTTPSignature;
 use Friendica\Util\Profiler;
@@ -536,21 +536,13 @@ class App
         * @param string $origURL
         *
         * @return string The cleaned url
-        * @throws HTTPException\InternalServerErrorException
+        *
+        * @deprecated 2019.09 - Use BaseURL->remove() instead
+        * @see BaseURL::remove()
         */
        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;
-               }
+               return $this->baseURL->remove($origURL);
        }
 
        /**
diff --git a/src/App/BaseURL.php b/src/App/BaseURL.php
new file mode 100644 (file)
index 0000000..ad5fd0d
--- /dev/null
@@ -0,0 +1,417 @@
+<?php
+
+namespace Friendica\App;
+
+use Friendica\Core\Config\Configuration;
+use Friendica\Util\Network;
+use Friendica\Util\Strings;
+
+/**
+ * A class which checks and contains the basic
+ * environment for the BaseURL (url, urlpath, ssl_policy, hostname, scheme)
+ */
+class BaseURL
+{
+       /**
+        * No SSL necessary
+        */
+       const SSL_POLICY_NONE = 0;
+
+       /**
+        * SSL is necessary
+        */
+       const SSL_POLICY_FULL = 1;
+
+       /**
+        * SSL is optional, but preferred
+        */
+       const SSL_POLICY_SELFSIGN = 2;
+
+       /**
+        * Define the Default SSL scheme
+        */
+       const DEFAULT_SSL_SCHEME = self::SSL_POLICY_SELFSIGN;
+
+       /**
+        * The Friendica Config
+        *
+        * @var Configuration
+        */
+       private $config;
+
+       /**
+        * The server side variables
+        *
+        * @var array
+        */
+       private $server;
+
+       /**
+        * The hostname of the Base URL
+        *
+        * @var string
+        */
+       private $hostname;
+
+       /**
+        * The SSL_POLICY of the Base URL
+        *
+        * @var int
+        */
+       private $sslPolicy;
+
+       /**
+        * The URL sub-path of the Base URL
+        *
+        * @var string
+        */
+       private $urlPath;
+
+       /**
+        * The full URL
+        *
+        * @var string
+        */
+       private $url;
+
+       /**
+        * The current scheme of this call
+        *
+        * @var string
+        */
+       private $scheme;
+
+       /**
+        * Returns the hostname of this node
+        *
+        * @return string
+        */
+       public function getHostname()
+       {
+               return $this->hostname;
+       }
+
+       /**
+        * Returns the current scheme of this call
+        *
+        * @return string
+        */
+       public function getScheme()
+       {
+               return $this->scheme;
+       }
+
+       /**
+        * Returns the SSL policy of this node
+        *
+        * @return int
+        */
+       public function getSSLPolicy()
+       {
+               return $this->sslPolicy;
+       }
+
+       /**
+        * Returns the sub-path of this URL
+        *
+        * @return string
+        */
+       public function getUrlPath()
+       {
+               return $this->urlPath;
+       }
+
+       /**
+        * Returns the full URL of this call
+        *
+        * Note: $ssl parameter value doesn't directly correlate with the resulting protocol
+        *
+        * @param bool $ssl True, if ssl should get used
+        *
+        * @return string
+        */
+       public function get($ssl = false)
+       {
+               if ($this->sslPolicy === self::SSL_POLICY_SELFSIGN && $ssl) {
+                       return Network::switchScheme($this->url);
+               }
+
+               return $this->url;
+       }
+
+       /**
+        * Save current parts of the base Url
+        *
+        * @param string? $hostname
+        * @param int?    $sslPolicy
+        * @param string? $urlPath
+        *
+        * @return bool true, if successful
+        */
+       public function save($hostname = null, $sslPolicy = null, $urlPath = null)
+       {
+               $currHostname  = $this->hostname;
+               $currSSLPolicy = $this->sslPolicy;
+               $currURLPath   = $this->urlPath;
+
+               if (!empty($hostname) && $hostname !== $this->hostname) {
+                       if ($this->config->set('config', 'hostname', $hostname)) {
+                               $this->hostname = $hostname;
+                       } else {
+                               return false;
+                       }
+               }
+
+               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;
+                       }
+               }
+
+               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;
+                       }
+               }
+
+               $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;
+               }
+
+               return true;
+       }
+
+       /**
+        * Save the current url as base URL
+        *
+        * @param $url
+        *
+        * @return bool true, if the save was successful
+        */
+       public function saveByURL($url)
+       {
+               $parsed = @parse_url($url);
+
+               if (empty($parsed)) {
+                       return false;
+               }
+
+               $hostname = $parsed['host'];
+               if (!empty($hostname) && !empty($parsed['port'])) {
+                       $hostname .= ':' . $parsed['port'];
+               }
+
+               $urlPath = null;
+               if (!empty($parsed['path'])) {
+                       $urlPath = trim($parsed['path'], '\\/');
+               }
+
+               $sslPolicy = null;
+               if (!empty($parsed['scheme'])) {
+                       if ($parsed['scheme'] == 'https') {
+                               $sslPolicy = BaseURL::SSL_POLICY_FULL;
+                       }
+               }
+
+               return $this->save($hostname, $sslPolicy, $urlPath);
+       }
+
+       /**
+        * Checks, if a redirect to the HTTPS site would be necessary
+        *
+        * @return bool
+        */
+       public function checkRedirectHttps()
+       {
+               return $this->config->get('system', 'force_ssl') &&
+                      ($this->getScheme() == "http") &&
+                      intval($this->getSSLPolicy()) == BaseURL::SSL_POLICY_FULL &&
+                      strpos($this->get(), 'https://') === 0 &&
+                      !empty($this->server['REQUEST_METHOD']) &&
+                      $this->server['REQUEST_METHOD'] === 'GET';
+       }
+
+       /**
+        * @param Configuration $config The Friendica configuration
+        * @param array         $server The $_SERVER array
+        */
+       public function __construct(Configuration $config, array $server)
+       {
+               $this->config = $config;
+               $this->server = $server;
+
+               $this->determineSchema();
+               $this->checkConfig();
+       }
+
+       /**
+        * Check the current config during loading
+        */
+       public function checkConfig()
+       {
+               $this->hostname  = $this->config->get('config', 'hostname');
+               $this->urlPath   = $this->config->get('system', 'urlpath');
+               $this->sslPolicy = $this->config->get('system', 'ssl_policy');
+               $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 (!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 = '';
+               $relative_script_path = defaults($this->server, 'REDIRECT_URL', $relative_script_path);
+               $relative_script_path = defaults($this->server, 'REDIRECT_URI', $relative_script_path);
+               $relative_script_path = defaults($this->server, 'REDIRECT_SCRIPT_URL', $relative_script_path);
+               $relative_script_path = defaults($this->server, 'SCRIPT_URL', $relative_script_path);
+               $relative_script_path = defaults($this->server, 'REQUEST_URI', $relative_script_path);
+
+               /* $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(rdirname($relative_script_path, substr_count(trim($this->server['QUERY_STRING'], '/'), '/') + 1), '/');
+                       } else {
+                               // Root page
+                               $this->urlPath = trim($relative_script_path, '/');
+                       }
+               }
+       }
+
+       /**
+        * Determine the full URL based on all parts
+        */
+       private function determineBaseUrl()
+       {
+               $scheme = 'http';
+
+               if ($this->sslPolicy == self::SSL_POLICY_FULL) {
+                       $scheme = 'https';
+               }
+
+               $this->url = $scheme . '://' . $this->hostname . (!empty($this->urlPath) ? '/' . $this->urlPath : '');
+       }
+
+       /**
+        * Determine the scheme of the current used link
+        */
+       private function determineSchema()
+       {
+               $this->scheme = 'http';
+
+               if (!empty($this->server['HTTPS']) ||
+                   !empty($this->server['HTTP_FORWARDED']) && preg_match('/proto=https/', $this->server['HTTP_FORWARDED']) ||
+                   !empty($this->server['HTTP_X_FORWARDED_PROTO']) && $this->server['HTTP_X_FORWARDED_PROTO'] == 'https' ||
+                   !empty($this->server['HTTP_X_FORWARDED_SSL']) && $this->server['HTTP_X_FORWARDED_SSL'] == 'on' ||
+                   !empty($this->server['FRONT_END_HTTPS']) && $this->server['FRONT_END_HTTPS'] == 'on' ||
+                   !empty($this->server['SERVER_PORT']) && (intval($this->server['SERVER_PORT']) == 443) // XXX: reasonable assumption, but isn't this hardcoding too much?
+               ) {
+                       $this->scheme = 'https';
+               }
+       }
+
+       /**
+        * Removes the base url from an url. This avoids some mixed content problems.
+        *
+        * @param string $origURL
+        *
+        * @return string The cleaned url
+        */
+       public function remove(string $origURL)
+       {
+               // Remove the hostname from the url if it is an internal link
+               $nurl = Strings::normaliseLink($origURL);
+               $base = Strings::normaliseLink($this->get());
+               $url  = str_replace($base . '/', '', $nurl);
+
+               // if it is an external link return the orignal value
+               if ($url == Strings::normaliseLink($origURL)) {
+                       return $origURL;
+               } else {
+                       return $url;
+               }
+       }
+}
index e54c0e49b910f413b67e3190f1f17991a25f1499..22909a6e6e039720d3d43b6310e6a762304ca331 100644 (file)
@@ -9,9 +9,7 @@ use Friendica\App;
 use Friendica\Core\Session\CacheSessionHandler;
 use Friendica\Core\Session\DatabaseSessionHandler;
 use Friendica\Database\DBA;
-use Friendica\Model\Contact;
 use Friendica\Model\User;
-use Friendica\Util\BaseURL;
 use Friendica\Util\DateTimeFormat;
 
 /**
@@ -30,7 +28,7 @@ class Session
                ini_set('session.use_only_cookies', 1);
                ini_set('session.cookie_httponly', 1);
 
-               if (Config::get('system', 'ssl_policy') == BaseURL::SSL_POLICY_FULL) {
+               if (Config::get('system', 'ssl_policy') == App\BaseURL::SSL_POLICY_FULL) {
                        ini_set('session.cookie_secure', 1);
                }
 
index 89526bdb4e0dd51d869b32a690a5514da95156ab..0888f390a694c8d59769a80e5a876e01ce9e8e37 100644 (file)
@@ -4,9 +4,9 @@
  */
 namespace Friendica\Core;
 
+use Friendica\App\BaseURL;
 use Friendica\BaseObject;
 use Friendica\Network\HTTPException\InternalServerErrorException;
-use Friendica\Util\BaseURL;
 use Friendica\Util\XML;
 
 /**
index f01cb736042372f68e17f4829ac9f37b9910862f..df3efa0c57036567144ddf536b707e0a9ed0a6c7 100644 (file)
@@ -4,6 +4,7 @@
  */
 namespace Friendica\Model;
 
+use Friendica\App\BaseURL;
 use Friendica\BaseObject;
 use Friendica\Content\Pager;
 use Friendica\Core\Config;
@@ -22,7 +23,6 @@ use Friendica\Protocol\Diaspora;
 use Friendica\Protocol\OStatus;
 use Friendica\Protocol\PortableContact;
 use Friendica\Protocol\Salmon;
-use Friendica\Util\BaseURL;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
 use Friendica\Util\Strings;
index c899729f8e7a7b684d5978d3cbd757e965914dd5..dff869df20ae2a7912c25ea611bc60a64fe2cd44 100644 (file)
@@ -14,7 +14,6 @@ use Friendica\Module\BaseAdminModule;
 use Friendica\Module\Register;
 use Friendica\Protocol\PortableContact;
 use Friendica\Util\BasePath;
-use Friendica\Util\BaseURL;
 use Friendica\Util\Strings;
 use Friendica\Worker\Delivery;
 
@@ -246,7 +245,7 @@ class Site extends BaseAdminModule
                        $diaspora_enabled = false;
                }
                if ($ssl_policy != intval(Config::get('system', 'ssl_policy'))) {
-                       if ($ssl_policy == BaseURL::SSL_POLICY_FULL) {
+                       if ($ssl_policy == App\BaseURL::SSL_POLICY_FULL) {
                                DBA::e("UPDATE `contact` SET
                                `url`     = REPLACE(`url`    , 'http:' , 'https:'),
                                `photo`   = REPLACE(`photo`  , 'http:' , 'https:'),
@@ -264,7 +263,7 @@ class Site extends BaseAdminModule
                                `thumb`   = REPLACE(`thumb`  , 'http:' , 'https:')
                                WHERE 1 "
                                );
-                       } elseif ($ssl_policy == BaseURL::SSL_POLICY_SELFSIGN) {
+                       } elseif ($ssl_policy == App\BaseURL::SSL_POLICY_SELFSIGN) {
                                DBA::e("UPDATE `contact` SET
                                `url`     = REPLACE(`url`    , 'https:' , 'http:'),
                                `photo`   = REPLACE(`photo`  , 'https:' , 'http:'),
@@ -512,9 +511,9 @@ class Site extends BaseAdminModule
                ];
 
                $ssl_choices = [
-                       BaseURL::SSL_POLICY_NONE => L10n::t('No SSL policy, links will track page SSL state'),
-                       BaseURL::SSL_POLICY_FULL => L10n::t('Force all links to use SSL'),
-                       BaseURL::SSL_POLICY_SELFSIGN => L10n::t('Self-signed certificate, use SSL for local links only (discouraged)')
+                       App\BaseURL::SSL_POLICY_NONE => L10n::t('No SSL policy, links will track page SSL state'),
+                       App\BaseURL::SSL_POLICY_FULL => L10n::t('Force all links to use SSL'),
+                       App\BaseURL::SSL_POLICY_SELFSIGN => L10n::t('Self-signed certificate, use SSL for local links only (discouraged)')
                ];
 
                $check_git_version_choices = [
index 3b8ebb471c5e6cccd202b57fe6af861d829bfe44..7ba40396659927f15df9f6b28b3759c33c2d4e13 100644 (file)
@@ -10,7 +10,6 @@ use Friendica\Core\L10n;
 use Friendica\Core\Renderer;
 use Friendica\Network\HTTPException;
 use Friendica\Util\BasePath;
-use Friendica\Util\BaseURL;
 use Friendica\Util\Strings;
 use Friendica\Util\Temporal;
 
@@ -180,9 +179,9 @@ class Install extends BaseModule
 
                        case self::BASE_CONFIG:
                                $ssl_choices = [
-                                       BaseURL::SSL_POLICY_NONE     => L10n::t("No SSL policy, links will track page SSL state"),
-                                       BaseURL::SSL_POLICY_FULL     => L10n::t("Force all links to use SSL"),
-                                       BaseURL::SSL_POLICY_SELFSIGN => L10n::t("Self-signed certificate, use SSL for local links only \x28discouraged\x29")
+                                       App\BaseURL::SSL_POLICY_NONE     => L10n::t("No SSL policy, links will track page SSL state"),
+                                       App\BaseURL::SSL_POLICY_FULL     => L10n::t("Force all links to use SSL"),
+                                       App\BaseURL::SSL_POLICY_SELFSIGN => L10n::t("Self-signed certificate, use SSL for local links only \x28discouraged\x29")
                                ];
 
                                $tpl    = Renderer::getMarkupTemplate('install_base.tpl');
index 2abdfe14869fc128a0911ecb8234f963b4f81ba4..273a7c248cc2651762ed1c6fa542818328a38491 100644 (file)
@@ -11,6 +11,7 @@ namespace Friendica\Protocol;
 use DOMDocument;
 use DOMXPath;
 use Friendica\App;
+use Friendica\App\BaseURL;
 use Friendica\Content\OEmbed;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
@@ -31,7 +32,6 @@ use Friendica\Model\Profile;
 use Friendica\Model\User;
 use Friendica\Network\Probe;
 use Friendica\Object\Image;
-use Friendica\Util\BaseURL;
 use Friendica\Util\Crypto;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
diff --git a/src/Util/BaseURL.php b/src/Util/BaseURL.php
deleted file mode 100644 (file)
index be34de3..0000000
+++ /dev/null
@@ -1,382 +0,0 @@
-<?php
-
-namespace Friendica\Util;
-
-use Friendica\Core\Config\Configuration;
-
-/**
- * A class which checks and contains the basic
- * environment for the BaseURL (url, urlpath, ssl_policy, hostname, scheme)
- */
-class BaseURL
-{
-       /**
-        * No SSL necessary
-        */
-       const SSL_POLICY_NONE = 0;
-
-       /**
-        * SSL is necessary
-        */
-       const SSL_POLICY_FULL = 1;
-
-       /**
-        * SSL is optional, but preferred
-        */
-       const SSL_POLICY_SELFSIGN = 2;
-
-       /**
-        * Define the Default SSL scheme
-        */
-       const DEFAULT_SSL_SCHEME = self::SSL_POLICY_SELFSIGN;
-
-       /**
-        * The Friendica Config
-        * @var Configuration
-        */
-       private $config;
-
-       /**
-        * The server side variables
-        * @var array
-        */
-       private $server;
-
-       /**
-        * The hostname of the Base URL
-        * @var string
-        */
-       private $hostname;
-
-       /**
-        * The SSL_POLICY of the Base URL
-        * @var int
-        */
-       private $sslPolicy;
-
-       /**
-        * The URL sub-path of the Base URL
-        * @var string
-        */
-       private $urlPath;
-
-       /**
-        * The full URL
-        * @var string
-        */
-       private $url;
-
-       /**
-        * The current scheme of this call
-        * @var string
-        */
-       private $scheme;
-
-       /**
-        * Returns the hostname of this node
-        * @return string
-        */
-       public function getHostname()
-       {
-               return $this->hostname;
-       }
-
-       /**
-        * Returns the current scheme of this call
-        * @return string
-        */
-       public function getScheme()
-       {
-               return $this->scheme;
-       }
-
-       /**
-        * Returns the SSL policy of this node
-        * @return int
-        */
-       public function getSSLPolicy()
-       {
-               return $this->sslPolicy;
-       }
-
-       /**
-        * Returns the sub-path of this URL
-        * @return string
-        */
-       public function getUrlPath()
-       {
-               return $this->urlPath;
-       }
-
-       /**
-        * Returns the full URL of this call
-        *
-        * Note: $ssl parameter value doesn't directly correlate with the resulting protocol
-        *
-        * @param bool $ssl True, if ssl should get used
-        *
-        * @return string
-        */
-       public function get($ssl = false)
-       {
-               if ($this->sslPolicy === self::SSL_POLICY_SELFSIGN && $ssl) {
-                       return Network::switchScheme($this->url);
-               }
-
-               return $this->url;
-       }
-
-       /**
-        * Save current parts of the base Url
-        *
-        * @param string? $hostname
-        * @param int?    $sslPolicy
-        * @param string? $urlPath
-        *
-        * @return bool true, if successful
-        */
-       public function save($hostname = null, $sslPolicy = null, $urlPath = null)
-       {
-               $currHostname  = $this->hostname;
-               $currSSLPolicy = $this->sslPolicy;
-               $currURLPath   = $this->urlPath;
-
-               if (!empty($hostname) && $hostname !== $this->hostname) {
-                       if ($this->config->set('config', 'hostname', $hostname)) {
-                               $this->hostname  = $hostname;
-                       } else {
-                               return false;
-                       }
-               }
-
-               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;
-                       }
-               }
-
-               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;
-                       }
-               }
-
-               $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;
-               }
-
-               return true;
-       }
-
-       /**
-        * Save the current url as base URL
-        *
-        * @param $url
-        *
-        * @return bool true, if the save was successful
-        */
-       public function saveByURL($url)
-       {
-               $parsed = @parse_url($url);
-
-               if (empty($parsed)) {
-                       return false;
-               }
-
-               $hostname = $parsed['host'];
-               if (!empty($hostname) && !empty($parsed['port'])) {
-                       $hostname .= ':' . $parsed['port'];
-               }
-
-               $urlPath = null;
-               if (!empty($parsed['path'])) {
-                       $urlPath = trim($parsed['path'], '\\/');
-               }
-
-               $sslPolicy = null;
-               if (!empty($parsed['scheme'])) {
-                       if ($parsed['scheme'] == 'https') {
-                               $sslPolicy = BaseURL::SSL_POLICY_FULL;
-                       }
-               }
-
-               return $this->save($hostname, $sslPolicy, $urlPath);
-       }
-
-       /**
-        * Checks, if a redirect to the HTTPS site would be necessary
-        *
-        * @return bool
-        */
-       public function checkRedirectHttps()
-       {
-               return $this->config->get('system', 'force_ssl')
-                       && ($this->getScheme() == "http")
-                       && intval($this->getSSLPolicy()) == BaseURL::SSL_POLICY_FULL
-                       && strpos($this->get(), 'https://') === 0
-                       && !empty($this->server['REQUEST_METHOD'])
-                       && $this->server['REQUEST_METHOD'] === 'GET';
-       }
-
-       /**
-        * @param Configuration $config The Friendica configuration
-        * @param array         $server The $_SERVER array
-        */
-       public function __construct(Configuration $config, array $server)
-       {
-               $this->config = $config;
-               $this->server = $server;
-
-               $this->determineSchema();
-               $this->checkConfig();
-       }
-
-       /**
-        * Check the current config during loading
-        */
-       public function checkConfig()
-       {
-               $this->hostname  = $this->config->get('config', 'hostname');
-               $this->urlPath   = $this->config->get('system', 'urlpath');
-               $this->sslPolicy = $this->config->get('system', 'ssl_policy');
-               $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 (!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 = '';
-               $relative_script_path = defaults($this->server, 'REDIRECT_URL', $relative_script_path);
-               $relative_script_path = defaults($this->server, 'REDIRECT_URI', $relative_script_path);
-               $relative_script_path = defaults($this->server, 'REDIRECT_SCRIPT_URL', $relative_script_path);
-               $relative_script_path = defaults($this->server, 'SCRIPT_URL', $relative_script_path);
-               $relative_script_path = defaults($this->server, 'REQUEST_URI', $relative_script_path);
-
-               /* $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(rdirname($relative_script_path, substr_count(trim($this->server['QUERY_STRING'], '/'), '/') + 1), '/');
-                       } else {
-                               // Root page
-                               $this->urlPath = trim($relative_script_path, '/');
-                       }
-               }
-       }
-
-       /**
-        * Determine the full URL based on all parts
-        */
-       private function determineBaseUrl()
-       {
-               $scheme = 'http';
-
-               if ($this->sslPolicy == self::SSL_POLICY_FULL) {
-                       $scheme = 'https';
-               }
-
-               $this->url = $scheme . '://' . $this->hostname . (!empty($this->urlPath) ? '/' . $this->urlPath : '' );
-       }
-
-       /**
-        * Determine the scheme of the current used link
-        */
-       private function determineSchema()
-       {
-               $this->scheme = 'http';
-
-               if (!empty($this->server['HTTPS']) ||
-                       !empty($this->server['HTTP_FORWARDED']) && preg_match('/proto=https/', $this->server['HTTP_FORWARDED']) ||
-                       !empty($this->server['HTTP_X_FORWARDED_PROTO']) && $this->server['HTTP_X_FORWARDED_PROTO'] == 'https' ||
-                       !empty($this->server['HTTP_X_FORWARDED_SSL']) && $this->server['HTTP_X_FORWARDED_SSL'] == 'on' ||
-                       !empty($this->server['FRONT_END_HTTPS']) && $this->server['FRONT_END_HTTPS'] == 'on' ||
-                       !empty($this->server['SERVER_PORT']) && (intval($this->server['SERVER_PORT']) == 443) // XXX: reasonable assumption, but isn't this hardcoding too much?
-               ) {
-                       $this->scheme = 'https';
-               }
-       }
-}
index 7a4adb5ea194929a062f51629794e06289be053d..e32f99a60ca9a0ee3f7ce5ce54f19f332b49969a 100644 (file)
@@ -85,12 +85,12 @@ return [
                ],
        ],
        /**
-        * Creates the Util\BaseURL
+        * Creates the App\BaseURL
         *
         * Same as:
-        *   $baseURL = new Util\BaseURL($configuration, $_SERVER);
+        *   $baseURL = new App\BaseURL($configuration, $_SERVER);
         */
-       Util\BaseURL::class             => [
+       App\BaseURL::class             => [
                'constructParams' => [
                        $_SERVER,
                ],
index a8c4894b9d315706706cae0fa07c73f1db0b761b..774db31746fd5d94f1fa4e629df7c31ea8f24334 100644 (file)
@@ -15,7 +15,6 @@ use Friendica\Test\Util\DBAMockTrait;
 use Friendica\Test\Util\DBStructureMockTrait;
 use Friendica\Test\Util\RendererMockTrait;
 use Friendica\Test\Util\VFSTrait;
-use Friendica\Util\BaseURL;
 use Friendica\Util\Logger\VoidLogger;
 use Mockery\MockInterface;
 use org\bovigo\vfs\vfsStream;
@@ -353,7 +352,7 @@ FIN;
                $this->assertConfigEntry('system', 'language', $assertion, ($default) ? Installer::DEFAULT_LANG : null);
                $this->assertConfigEntry('system', 'url', $assertion);
                $this->assertConfigEntry('system', 'urlpath', $assertion);
-               $this->assertConfigEntry('system', 'ssl_policy', $assertion, ($default) ? BaseURL::DEFAULT_SSL_SCHEME : null);
+               $this->assertConfigEntry('system', 'ssl_policy', $assertion, ($default) ? App\BaseURL::DEFAULT_SSL_SCHEME : null);
                $this->assertConfigEntry('system', 'basepath', ($realBasepath) ? $this->root->url() : $assertion);
        }
 
index 6938f8ed5a05249bfa2e92b544c6943e144a3447..899f32764dc9307546a48dae84f831d629290ce3 100644 (file)
@@ -2,12 +2,12 @@
 
 namespace Friendica\Test\src\Content\Text;
 
+use Friendica\App\BaseURL;
 use Friendica\Content\Text\BBCode;
 use Friendica\Core\L10n\L10n;
 use Friendica\Test\MockedTest;
 use Friendica\Test\Util\AppMockTrait;
 use Friendica\Test\Util\VFSTrait;
-use Friendica\Util\BaseURL;
 
 class BBCodeTest extends MockedTest
 {
index 1e74af39fe3937d96811b7d1bd21736830619500..7f63027fcb7c10f8552f25157ab724a3305b7d8e 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 namespace Friendica\Test\src\Util;
 
+use Friendica\App\BaseURL;
 use Friendica\Core\Config\Configuration;
 use Friendica\Test\MockedTest;
-use Friendica\Util\BaseURL;
 
 class BaseURLTest extends MockedTest
 {