1
0
Fork 0

Remove BaseURL Auto-Check

This commit is contained in:
Philipp Holzer 2023-01-21 20:20:29 +01:00
parent 9ff3aca3b2
commit 3de78fc60b
Signed by: nupplaPhil
GPG key ID: 24A7501396EB5432
3 changed files with 5 additions and 143 deletions

View file

@ -69,8 +69,6 @@ if (!DI::mode()->has(App\Mode::MAINTENANCEDISABLED)) {
return;
}
DI::baseUrl()->saveByURL(DI::config()->get('system', 'url'));
$spawn = array_key_exists('s', $options) || array_key_exists('spawn', $options);
if ($spawn) {

View file

@ -257,109 +257,14 @@ class BaseURL
*/
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 (!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();
}
/**

View file

@ -200,47 +200,6 @@ class BaseURLTest extends MockedTest
];
}
/**
* Test the default config determination
* @dataProvider dataDefault
*/
public function testCheck($server, $input, $assert)
{
$configMock = \Mockery::mock(IManageConfigValues::class);
$configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
$configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
$configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
$configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
// If we don't have an urlPath as an input, we assert it, we will save it to the DB for the next time
if (!isset($input['urlPath']) && isset($assert['urlPath'])) {
$configMock->shouldReceive('set')->with('system', 'urlpath', $assert['urlPath'])->once();
}
// If we don't have the ssl_policy as an input, we assert it, we will save it to the DB for the next time
if (!isset($input['sslPolicy']) && isset($assert['sslPolicy'])) {
$configMock->shouldReceive('set')->with('system', 'ssl_policy', $assert['sslPolicy'])->once();
}
// If we don't have the hostname as an input, we assert it, we will save it to the DB for the next time
if (empty($input['hostname']) && !empty($assert['hostname'])) {
$configMock->shouldReceive('set')->with('config', 'hostname', $assert['hostname'])->once();
}
// If we don't have an URL at first, but we assert it, we will save it to the DB for the next time
if (empty($input['url']) && !empty($assert['url'])) {
$configMock->shouldReceive('set')->with('system', 'url', $assert['url'])->once();
}
$baseUrl = new BaseURL($configMock, $server);
self::assertEquals($assert['hostname'], $baseUrl->getHostname());
self::assertEquals($assert['urlPath'], $baseUrl->getUrlPath());
self::assertEquals($assert['sslPolicy'], $baseUrl->getSSLPolicy());
self::assertEquals($assert['scheme'], $baseUrl->getScheme());
self::assertEquals($assert['url'], $baseUrl->get());
}
public function dataSave()
{
return [