The feed links, the notification email, the webmaster address and the cookie domain now use it.
96 lines
2.9 KiB
PHP
96 lines
2.9 KiB
PHP
<?php
|
|
/*
|
|
* Return a configuration value or secret.
|
|
*
|
|
* Values come from the environment first (for example Apache SetEnv),
|
|
* then from the .env file, then from $default. The .env file is the one
|
|
* named by the XXX_ENV_FILE environment variable, or else the .env file
|
|
* next to this one. Git does not track .env; see .env.example.
|
|
*
|
|
* Each line of .env is NAME=value. Blank lines and lines that start
|
|
* with # are skipped, and quotes around a value are removed.
|
|
*/
|
|
function config($key, $default = NULL) {
|
|
static $values = NULL;
|
|
|
|
$env = getenv($key);
|
|
if ($env !== FALSE)
|
|
return $env;
|
|
|
|
if ($values === NULL) {
|
|
$values = array();
|
|
$file = getenv('XXX_ENV_FILE') ?: __DIR__ . '/.env';
|
|
if (is_readable($file)) {
|
|
foreach (file($file, FILE_IGNORE_NEW_LINES) as $line) {
|
|
$line = trim($line);
|
|
if ($line === '' || $line[0] === '#' || strpos($line, '=') === FALSE)
|
|
continue;
|
|
list($name, $value) = explode('=', $line, 2);
|
|
$name = trim(preg_replace('/^export\s+/', '', $name));
|
|
$value = trim($value);
|
|
if (strlen($value) >= 2 &&
|
|
($value[0] === '"' || $value[0] === "'") &&
|
|
substr($value, -1) === $value[0])
|
|
$value = substr($value, 1, -1);
|
|
$values[$name] = $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
return array_key_exists($key, $values) ? $values[$key] : $default;
|
|
}
|
|
|
|
/*
|
|
* 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;
|
|
if (site_url() === '') {
|
|
http_response_code(403);
|
|
echo "This page needs HTTPS.\n";
|
|
} else {
|
|
header('Location: ' . site_url('https') . $_SERVER['REQUEST_URI']);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
// Always log PHP errors; show them only if DISPLAY_ERRORS is set (for development)
|
|
ini_set('log_errors', '1');
|
|
ini_set('display_errors', (string)config('DISPLAY_ERRORS', '0'));
|
|
?>
|