- /**
- * 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 IManageConfigValues
- */
- 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(): string
- {
- return $this->hostname;
- }
-
- /**
- * Returns the current scheme of this call
- *
- * @return string
- */
- public function getScheme(): string
- {
- return $this->scheme;
- }
-
- /**
- * Returns the SSL policy of this node
- *
- * @return int
- */
- public function getSSLPolicy(): int
- {
- return $this->sslPolicy;
- }
-
- /**
- * Returns the sub-path of this URL
- *
- * @return string
- */
- public function getUrlPath(): string
- {
- 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(bool $ssl = false): string
- {
- 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
- * @TODO Find proper types
- */
- public function save($hostname = null, $sslPolicy = null, $urlPath = null): bool
- {
- $currUrl = $this->url;
-
- $configTransaction = $this->config->beginTransaction();
-
- if (!empty($hostname) && $hostname !== $this->hostname) {
- $configTransaction->set('config', 'hostname', $hostname);
- $this->hostname = $hostname;
- }
-
- if (isset($sslPolicy) && $sslPolicy !== $this->sslPolicy) {
- $configTransaction->set('system', 'ssl_policy', $sslPolicy);
- $this->sslPolicy = $sslPolicy;
- }
-
- if (isset($urlPath) && $urlPath !== $this->urlPath) {
- $configTransaction->set('system', 'urlpath', $urlPath);
- $this->urlPath = $urlPath;
- }
-
- $this->determineBaseUrl();
- if ($this->url !== $currUrl) {
- $configTransaction->set('system', 'url', $this->url);
- }
-
- $configTransaction->commit();
-
- 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()