SiteBaseUrl
in package
Single source of the absolute website address, used for links that leave the request: e-mails (activation, password reset, adverts, orders, watchdog), sitemaps, feeds, `og:url`.
Without a setting an absolute link is built from the Host header of the request that produced
it (link('//…'), getBaseUrl()). A forged Host would put a foreign domain into an e-mail sent
to someone else, and e-mails sent from cron would depend on how cron calls the website.
Rules:
- when the
site.baseUrlsetting (System module, website settings, e-mail sending) is filled in, it always wins, whatever the requestHostsays; - when empty, the request address is used (scheme + host + port + base path);
- without a request (CLI, cron started from the command line) and without the setting the result
is
'': no link is built andabsolutize()returns its input unchanged. Nothing throws.
This class neither redirects nor checks Host for pages; it only changes links meant to be sent
or published. Presenter links are still built by the router (language, slug and persistent
parameters stay); absolutize() only swaps the request address prefix for the configured one.
rewriteRequestBase() is the safety net for every e-mail body
(SendEmailTrait).
Table of Contents
Constants
- SETTING_KEY : string = 'site.baseUrl'
- Setting key (module 1 = System). An empty value means the request address is used.
- SETTING_MODULE : int = 1
Properties
- $httpRequest : IRequest|null
- $settingContext : SettingContext
Methods
- __construct() : mixed
- absolutize() : string
- Converts an absolute link from a presenter or `LinkGenerator` (`http://<request host>/<base path>/…`) to the configured address and completes a relative path (`/advert/…`) to an absolute one.
- get() : string
- Returns the absolute website address without a trailing slash (`https://example.cz/eshop`), or `''` when it cannot be determined (CLI without the setting).
- getConfigured() : string|null
- Returns the configured address (normalized), `null` when unset or the stored value is invalid.
- normalize() : string|null
- Validates and normalizes a value from the form. Returns: - `''` for empty input (the request address is used), - `scheme://host[:port][/path]` with a lowercase scheme and host, no trailing slash and no default port (80 for http, 443 for https), - `null` for invalid input: a scheme other than http(s), missing host, credentials, query (`?`), fragment (`#`), backslash, spaces or control characters, `.` or `..` in the path.
- rewriteRequestBase() : string
- Safety net over a finished text (a rendered e-mail body): replaces every occurrence of the request address (`http://host[:port]/base`) with the configured address. It also covers links the caller built by hand (feed, watchdog advert list, invoice), not via `absolutize()`.
- getRequestBase() : string
- Returns the request address without a trailing slash (`http://127.0.0.1:8000`), exactly the one the presenter builds `link('//…')` from. Outside HTTP (CLI) the host is empty, so the result is `''`.
- getRequestBasePath() : string
- isValidHost() : bool
- Accepts a domain name (labels of letters, digits and hyphens, international names as punycode) or an IPv4 address.
Constants
SETTING_KEY
Setting key (module 1 = System). An empty value means the request address is used.
public
string
SETTING_KEY
= 'site.baseUrl'
SETTING_MODULE
public
int
SETTING_MODULE
= 1
Properties
$httpRequest read-only
private
IRequest|null
$httpRequest
= null
$settingContext read-only
private
SettingContext
$settingContext
Methods
__construct()
public
__construct(SettingContext $settingContext[, IRequest|null $httpRequest = null ]) : mixed
Parameters
- $settingContext : SettingContext
- $httpRequest : IRequest|null = null
absolutize()
Converts an absolute link from a presenter or `LinkGenerator` (`http://<request host>/<base path>/…`) to the configured address and completes a relative path (`/advert/…`) to an absolute one.
public
absolutize(string $url) : string
A foreign absolute address, and everything when no address is configured, is returned unchanged.
Parameters
- $url : string
Return values
stringget()
Returns the absolute website address without a trailing slash (`https://example.cz/eshop`), or `''` when it cannot be determined (CLI without the setting).
public
get() : string
Return values
stringgetConfigured()
Returns the configured address (normalized), `null` when unset or the stored value is invalid.
public
getConfigured() : string|null
Return values
string|nullnormalize()
Validates and normalizes a value from the form. Returns: - `''` for empty input (the request address is used), - `scheme://host[:port][/path]` with a lowercase scheme and host, no trailing slash and no default port (80 for http, 443 for https), - `null` for invalid input: a scheme other than http(s), missing host, credentials, query (`?`), fragment (`#`), backslash, spaces or control characters, `.` or `..` in the path.
public
static normalize(string $raw) : string|null
Parameters
- $raw : string
Return values
string|nullrewriteRequestBase()
Safety net over a finished text (a rendered e-mail body): replaces every occurrence of the request address (`http://host[:port]/base`) with the configured address. It also covers links the caller built by hand (feed, watchdog advert list, invoice), not via `absolutize()`.
public
rewriteRequestBase(string $text) : string
The address must be followed by an address end (/, ?, #, quote, <, space, end of text),
so http://evil.test does not touch http://evil.test.example.
Parameters
- $text : string
Return values
stringgetRequestBase()
Returns the request address without a trailing slash (`http://127.0.0.1:8000`), exactly the one the presenter builds `link('//…')` from. Outside HTTP (CLI) the host is empty, so the result is `''`.
private
getRequestBase() : string
Return values
stringgetRequestBasePath()
private
getRequestBasePath() : string
Return values
stringisValidHost()
Accepts a domain name (labels of letters, digits and hyphens, international names as punycode) or an IPv4 address.
private
static isValidHost(string $host) : bool
Parameters
- $host : string