diff --git a/.env.example b/.env.example index 26f060b..714341a 100644 --- a/.env.example +++ b/.env.example @@ -7,6 +7,10 @@ DB_USER=xxx DB_PASS= DB_NAME=xxx +# Address of the site. The pages send plain HTTP requests to this host +# over HTTPS. If it is empty, the pages refuse plain HTTP. +SITE_URL=https://xxx.org.za + # Google reCAPTCHA v2 keys (https://www.google.com/recaptcha/admin). # Leave RECAPTCHA_SITE_KEY empty to turn the captcha off. RECAPTCHA_SITE_KEY= diff --git a/config.lib.php b/config.lib.php index db7d6e8..6b01fe1 100644 --- a/config.lib.php +++ b/config.lib.php @@ -40,6 +40,25 @@ function config($key, $default = NULL) { return array_key_exists($key, $values) ? $values[$key] : $default; } +/* + * 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. + * 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'])) { + 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']); + } + 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')); diff --git a/publish/addeditor.php b/publish/addeditor.php index fe9423c..3520cac 100644 --- a/publish/addeditor.php +++ b/publish/addeditor.php @@ -1,15 +1,13 @@ The editor was not added: the editors.password column is too short for the password hash. Widen it to VARCHAR(255).
-