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

@ -7,7 +7,7 @@ Once your Shaarli instance is installed, the file `data/config.json.php` is gene
- its values override those defined in `index.php` - its values override those defined in `index.php`
- it is wrapped in a PHP comment so that its contents are never served by the web server, regardless of configuration - it is wrapped in a PHP comment so that its contents are never served by the web server, regardless of configuration
**Do not edit configuration options in index.php! Your changes would be lost.** **Do not edit configuration options in index.php! Your changes would be lost.**
## Tools menu ## Tools menu
@ -135,71 +135,72 @@ Some settings can be configured directly from a web browser by accesing the `Too
## Settings ## Settings
### Credentials ### Credentials
_These settings should not be edited_ _These settings should not be edited_
- **login**: Login username. - **login**: Login username.
- **hash**: Generated password hash. - **hash**: Generated password hash.
- **salt**: Password salt. - **salt**: Password salt.
### General ### General
- **title**: Shaarli's instance title. - **title**: Shaarli's instance title.
- **header_link**: Link to the homepage. - **header_link**: Link to the homepage.
- **links_per_page**: Number of Shaares displayed per page. - **links_per_page**: Number of Shaares displayed per page.
- **timezone**: See [the list of supported timezones](http://php.net/manual/en/timezones.php). - **timezone**: See [the list of supported timezones](http://php.net/manual/en/timezones.php).
- **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
- **session_protection_disabled**: Disable session cookie hijacking protection (not recommended). - **session_protection_disabled**: Disable session cookie hijacking protection (not recommended).
It might be useful if your IP adress often changes. It might be useful if your IP adress often changes.
- **ban_after**: Failed login attempts before being IP banned. - **ban_after**: Failed login attempts before being IP banned.
- **ban_duration**: IP ban duration in seconds. - **ban_duration**: IP ban duration in seconds.
- **open_shaarli**: Anyone can add a new Shaare while logged out if enabled. - **open_shaarli**: Anyone can add a new Shaare while logged out if enabled.
- **trusted_proxies**: List of trusted IP which won't be banned after failed login attemps. Useful if Shaarli is behind a reverse proxy. - **trusted_proxies**: List of trusted IP which won't be banned after failed login attemps. Useful if Shaarli is behind a reverse proxy.
- **allowed_protocols**: List of allowed protocols in shaare URLs or markdown-rendered descriptions. Useful if you want to store `javascript:` links (bookmarklets) in Shaarli (default: `["ftp", "ftps", "magnet"]`). - **allowed_protocols**: List of allowed protocols in shaare URLs or markdown-rendered descriptions. Useful if you want to store `javascript:` links (bookmarklets) in Shaarli (default: `["ftp", "ftps", "magnet"]`).
### Resources ### Resources
- **data_dir**: Data directory. - **data_dir**: Data directory.
- **datastore**: Shaarli's Shaares database file path. - **datastore**: Shaarli's Shaares database file path.
- **history**: Shaarli's operation history file path. - **history**: Shaarli's operation history file path.
- **updates**: File path for the ran updates file. - **updates**: File path for the ran updates file.
- **log**: Log file path. - **log**: Log file path.
- **update_check**: Last update check file path. - **update_check**: Last update check file path.
- **raintpl_tpl**: Templates directory. - **raintpl_tpl**: Templates directory.
- **raintpl_tmp**: Template engine cache directory. - **raintpl_tmp**: Template engine cache directory.
- **thumbnails_cache**: Thumbnails cache directory. - **thumbnails_cache**: Thumbnails cache directory.
- **page_cache**: Shaarli's internal cache directory. - **page_cache**: Shaarli's internal cache directory.
- **ban_file**: Banned IP file path. - **ban_file**: Banned IP file path.
### Translation ### Translation
- **language**: translation language (also see [Translations](Translations)) - **language**: translation language (also see [Translations](Translations))
- **auto** (default): The translation language is chosen from the browser locale. - **auto** (default): The translation language is chosen from the browser locale.
It means that the language can be different for 2 different visitors depending on their locale. It means that the language can be different for 2 different visitors depending on their locale.
- **en**: Use the English translation. - **en**: Use the English translation.
- **fr**: Use the French translation. - **fr**: Use the French translation.
- **mode**: - **mode**:
- **auto** or **php** (default): Use the PHP implementation of gettext (slower) - **auto** or **php** (default): Use the PHP implementation of gettext (slower)
- **gettext**: Use PHP builtin gettext extension - **gettext**: Use PHP builtin gettext extension
(faster, but requires `php-gettext` to be installed and to reload the web server on update) (faster, but requires `php-gettext` to be installed and to reload the web server on update)
- **extension**: Translation extensions for custom themes or plugins. - **extension**: Translation extensions for custom themes or plugins.
Must be an associative array: `translation domain => translation path`. Must be an associative array: `translation domain => translation path`.
### Updates ### Updates
- **check_updates**: Enable or disable update check to the git repository. - **check_updates**: Enable or disable update check to the git repository.
- **check_updates_branch**: Git branch used to check updates (e.g. `stable` or `master`). - **check_updates_branch**: Git branch used to check updates (e.g. `stable` or `master`).
- **check_updates_interval**: Look for new version every N seconds (default: every day). - **check_updates_interval**: Look for new version every N seconds (default: every day).
### Privacy ### Privacy
- **default_private_links**: Check the private checkbox by default for every new Shaare. - **default_private_links**: Check the private checkbox by default for every new Shaare.
- **hide_public_links**: All Shaares are hidden while logged out. - **hide_public_links**: All Shaares are hidden while logged out.
- **force_login**: if **hide_public_links** and this are set to `true`, all anonymous users are redirected to the login page. - **force_login**: if **hide_public_links** and this are set to `true`, all anonymous users are redirected to the login page.
- **hide_timestamps**: Timestamps are hidden. - **hide_timestamps**: Timestamps are hidden.
- **remember_user_default**: Default state of the login page's *remember me* checkbox - **remember_user_default**: Default state of the login page's *remember me* checkbox
@ -207,14 +208,14 @@ Must be an associative array: `translation domain => translation path`.
### Feed ### Feed
- **rss_permalinks**: Enable this to redirect RSS links to Shaarli's permalinks instead of shaared URL. - **rss_permalinks**: Enable this to redirect RSS links to Shaarli's permalinks instead of shaared URL.
- **show_atom**: Display ATOM feed button. - **show_atom**: Display ATOM feed button.
### Thumbnail ### Thumbnail
- **enable_thumbnails**: Enable or disable thumbnail display. - **enable_thumbnails**: Enable or disable thumbnail display.
- **enable_localcache**: Enable or disable local cache. - **enable_localcache**: Enable or disable local cache.
## Plugins configuration ## Plugins configuration
See [Plugins](Plugins.md) See [Plugins](Plugins.md)

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',
)
)
);
}
}