- $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 string $url
- *
- * @return bool true, if the save was successful
- */
- public function saveByURL(string $url): bool
- {
- $parsed = @parse_url($url);
-
- if (empty($parsed) || empty($parsed['host'])) {
- 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 IManageConfigValues $config The Friendica IConfiguration
- * @param array $server The $_SERVER array
- */
- public function __construct(IManageConfigValues $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.
- */
-