Add manual configuration for root URL

This new setting under 'general.root_url' allows to override automatic discovery of Shaarli instance's URL.

Fixes #1339
This commit is contained in:
ArthurHoaro 2020-09-03 14:51:41 +02:00
parent e809908f9e
commit 650a5f09cb
6 changed files with 134 additions and 41 deletions

View file

@ -122,9 +122,9 @@ public function buildData(string $feedType, ?array $userInput)
$data['language'] = $this->getTypeLanguage($feedType); $data['language'] = $this->getTypeLanguage($feedType);
$data['last_update'] = $this->getLatestDateFormatted($feedType); $data['last_update'] = $this->getLatestDateFormatted($feedType);
$data['show_dates'] = !$this->hideDates || $this->isLoggedIn; $data['show_dates'] = !$this->hideDates || $this->isLoggedIn;
// Remove leading slash from REQUEST_URI. // Remove leading path from REQUEST_URI (already contained in $pageaddr).
$data['self_link'] = escape(server_url($this->serverInfo)) $requestUri = preg_replace('#(.*?/)(feed.*)#', '$2', escape($this->serverInfo['REQUEST_URI']));
. escape($this->serverInfo['REQUEST_URI']); $data['self_link'] = $pageaddr . $requestUri;
$data['index_url'] = $pageaddr; $data['index_url'] = $pageaddr;
$data['usepermalinks'] = $this->usePermalinks === true; $data['usepermalinks'] = $this->usePermalinks === true;
$data['links'] = $linkDisplayed; $data['links'] = $linkDisplayed;

View file

@ -369,7 +369,11 @@ function server_url($server)
*/ */
function index_url($server) function index_url($server)
{ {
$scriptname = $server['SCRIPT_NAME'] ?? ''; if (defined('SHAARLI_ROOT_URL') && null !== SHAARLI_ROOT_URL) {
return rtrim(SHAARLI_ROOT_URL, '/') . '/';
}
$scriptname = !empty($server['SCRIPT_NAME']) ? $server['SCRIPT_NAME'] : '/';
if (endsWith($scriptname, 'index.php')) { if (endsWith($scriptname, 'index.php')) {
$scriptname = substr($scriptname, 0, -9); $scriptname = substr($scriptname, 0, -9);
} }
@ -392,7 +396,7 @@ function page_url($server)
$scriptname = substr($scriptname, 0, -9); $scriptname = substr($scriptname, 0, -9);
} }
$route = ltrim($server['REQUEST_URI'] ?? '', $scriptname); $route = preg_replace('@^' . $scriptname . '@', '', $server['REQUEST_URI'] ?? '');
if (! empty($server['QUERY_STRING'])) { if (! empty($server['QUERY_STRING'])) {
return index_url($server) . $route . '?' . $server['QUERY_STRING']; return index_url($server) . $route . '?' . $server['QUERY_STRING'];
} }

View file

@ -151,6 +151,7 @@ _These settings should not be edited_
- **enabled_plugins**: List of enabled plugins. - **enabled_plugins**: List of enabled plugins.
- **default_note_title**: Default title of a new note. - **default_note_title**: Default title of a new note.
- **retrieve_description** (boolean): If set to true, for every new Shaare Shaarli will try to retrieve the description and keywords from the HTML meta tags. - **retrieve_description** (boolean): If set to true, for every new Shaare Shaarli will try to retrieve the description and keywords from the HTML meta tags.
- **root_url**: Overrides automatic discovery of Shaarli instance's URL (e.g.) `https://sub.domain.tld/shaarli-folder/`.
### Security ### Security

View file

@ -35,6 +35,9 @@
$conf = new ConfigManager(); $conf = new ConfigManager();
// Manually override root URL for complex server configurations
define('SHAARLI_ROOT_URL', $conf->get('general.root_url', null));
// In dev mode, throw exception on any warning // In dev mode, throw exception on any warning
if ($conf->get('dev.debug', false)) { if ($conf->get('dev.debug', false)) {
// See all errors (for debugging only) // See all errors (for debugging only)

View file

@ -5,12 +5,14 @@
namespace Shaarli\Http; namespace Shaarli\Http;
use PHPUnit\Framework\TestCase;
require_once 'application/http/HttpUtils.php'; require_once 'application/http/HttpUtils.php';
/** /**
* Unitary tests for index_url() * Unitary tests for index_url()
*/ */
class IndexUrlTest extends \PHPUnit\Framework\TestCase class IndexUrlTest extends TestCase
{ {
/** /**
* If on the main page, remove "index.php" from the URL resource * If on the main page, remove "index.php" from the URL resource
@ -103,4 +105,36 @@ public function testPageUrlWithRoute()
) )
); );
} }
/**
* The route is stored in REQUEST_URI and subfolder
*/
public function testPageUrlWithRouteUnderSubfolder()
{
$this->assertEquals(
'http://host.tld/subfolder/picture-wall',
page_url(
array(
'HTTPS' => 'Off',
'SERVER_NAME' => 'host.tld',
'SERVER_PORT' => '80',
'SCRIPT_NAME' => '/subfolder/index.php',
'REQUEST_URI' => '/subfolder/picture-wall',
)
)
);
$this->assertEquals(
'http://host.tld/subfolder/admin/picture-wall',
page_url(
array(
'HTTPS' => 'Off',
'SERVER_NAME' => 'host.tld',
'SERVER_PORT' => '80',
'SCRIPT_NAME' => '/subfolder/admin/index.php',
'REQUEST_URI' => '/subfolder/admin/picture-wall',
)
)
);
}
} }

View file

@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace Shaarli\Http;
use PHPUnit\Framework\TestCase;
/**
* Test index_url with SHAARLI_ROOT_URL defined to override automatic retrieval.
* This should stay in its dedicated class to make sure to not alter other tests of the suite.
*/
class IndexUrlTestWithConstant extends TestCase
{
public static function setUpBeforeClass(): void
{
define('SHAARLI_ROOT_URL', 'http://other-host.tld/subfolder/');
}
/**
* The route is stored in REQUEST_URI and subfolder
*/
public function testIndexUrlWithConstantDefined()
{
$this->assertEquals(
'http://other-host.tld/subfolder/',
index_url(
array(
'HTTPS' => 'Off',
'SERVER_NAME' => 'host.tld',
'SERVER_PORT' => '80',
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/picture-wall',
)
)
);
$this->assertEquals(
'http://other-host.tld/subfolder/',
index_url(
array(
'HTTPS' => 'Off',
'SERVER_NAME' => 'host.tld',
'SERVER_PORT' => '80',
'SCRIPT_NAME' => '/admin/index.php',
'REQUEST_URI' => '/admin/picture-wall',
)
)
);
}
}