Take the site address from SITE_URL.

The feed links, the notification email, the webmaster address and the cookie domain now use it.
This commit is contained in:
tim
2026-09-26 15:27:51 +02:00
parent 52f4855850
commit 5d657d7e45
11 changed files with 75 additions and 30 deletions
+36 -6
View File
@@ -41,20 +41,50 @@ function config($key, $default = NULL) {
}
/*
* Send a plain HTTP request to the same page over HTTPS, and stop. The
* host comes from SITE_URL, because the browser controls the Host header.
* The address of the site comes from SITE_URL, never from the Host
* header, which the browser controls. site_url() gives the scheme, host
* and port (for example "https://xxx.org.za"), and site_host() gives the
* host. Both are empty if SITE_URL has no host.
*/
function site_url_parts() {
$site = parse_url(trim((string)config('SITE_URL', '')));
return is_array($site) && !empty($site['host']) ? $site : array();
}
function site_host() {
return site_url_parts()['host'] ?? '';
}
function site_url($scheme = NULL) {
$site = site_url_parts();
if (!$site)
return '';
$port = isset($site['port']) ? ':' . $site['port'] : '';
return ($scheme ?? $site['scheme'] ?? 'https') . '://' . $site['host'] . $port;
}
// An address at the site's host, such as "editor@xxx.org.za"
function site_email($name) {
return $name . '@' . site_host();
}
// The cookie domain: the site's host and its subdomains
function cookie_domain() {
return site_host() === '' ? '' : '.' . site_host();
}
/*
* Send a plain HTTP request to the same page over HTTPS, and stop.
* Without SITE_URL, the page refuses plain HTTP.
*/
function require_https() {
if (isset($_SERVER['HTTPS']))
return;
$site = parse_url((string)config('SITE_URL', ''));
if (empty($site['host'])) {
if (site_url() === '') {
http_response_code(403);
echo "This page needs HTTPS.\n";
} else {
$port = isset($site['port']) ? ':' . $site['port'] : '';
header('Location: https://' . $site['host'] . $port . $_SERVER['REQUEST_URI']);
header('Location: ' . site_url('https') . $_SERVER['REQUEST_URI']);
}
exit;
}