<?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
*
*
* @return string
*/
- public function getHostname()
+ public function getHostname(): string
{
return $this->hostname;
}
*
* @return string
*/
- public function getScheme()
+ public function getScheme(): string
{
return $this->scheme;
}
*
* @return int
*/
- public function getSSLPolicy()
+ public function getSSLPolicy(): int
{
return $this->sslPolicy;
}
*
* @return string
*/
- public function getUrlPath()
+ public function getUrlPath(): string
{
return $this->urlPath;
}
*
* @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);
* @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);
*/
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->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();
}
/**
*
* @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);
* @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, '/');
/**
* Returns the base url as string
*/
- public function __toString()
+ public function __toString(): string
{
- return $this->get();
+ return (string) $this->get();
}
}