From 0ab7835c3b718d449ad8d7df5fea8adf03b02e6e Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Fri, 14 Jan 2022 03:59:38 +0100 Subject: [PATCH] Modernize code from krk-litvinov.cz --- 2012-prosinec/index.php | 18 +++++++++++------- 2014/index.php | 18 +++++++++++------- 2015-prosinec/index.php | 18 +++++++++++------- 2015/index.php | 18 +++++++++++------- 4 files changed, 44 insertions(+), 28 deletions(-) diff --git a/2012-prosinec/index.php b/2012-prosinec/index.php index dddbdcb..4b1bfa7 100644 --- a/2012-prosinec/index.php +++ b/2012-prosinec/index.php @@ -8,12 +8,12 @@ function stringEndsWith($string, $end) { } session_start(); $included = true; -$locale = 'cs'; -$url = $_SERVER['REQUEST_URI']; +$lang = 'cs'; +$url = $_SERVER['REQUEST_URI'] ?? ''; $root = '/2012-prosinec/'; -$uri = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], $root) + strlen($root)); +$uri = substr($url, strpos($url, $root) + strlen($root)); if ($uri == '') { - header('Location: http://' . $_SERVER['HTTP_HOST'] . $root . $locale . '/'); + header('Location: http://' . $_SERVER['HTTP_HOST'] . $root . $lang . '/'); exit; } elseif (preg_match('/^([a-z]{2})\\/(.+)(\\?.*)?$/', $uri, $matches)) { $lang = htmlspecialchars(substr($matches[1], 0, 2)); @@ -26,19 +26,23 @@ if ($uri == '') { $lang = htmlspecialchars(substr($matches[1], 0, 2)); $_GET['page'] = (isset($matches[2]) ? $matches[2] . '/' : '') . 'main'; } else { - $_GET['error'] = 404; + $_GET['page'] = 'error/404'; +} +if (!in_array($lang, ['cs', 'de'], true)) { + header('Location: http://' . $_SERVER['HTTP_HOST'] . '/cs/' . ($_GET['page'] ?? '')); + exit; } $page = htmlspecialchars($_GET['page']); $pretitle = '3. Česko-německý skirogaining'; $mainMail = 'tojnar@gmail.com'; -include $lang . '.php'; +require $lang . '.php'; $CMS['%url%'] = 'http://' . $_SERVER['HTTP_HOST'] . htmlspecialchars($url); $CMS['%root%'] = rtrim($root, '/'); $CMS['%lang%'] = $lang; $CMS['%unknownAuthor%'] = 'Jan Tojnar'; $CMS['%headerLinkHref%'] = rplc('%root%/') . $lang; $CMS['%pretitle%'] = $pretitle; -$CMS['%comefrom%'] = $_SERVER['HTTP_REFERER']; +$CMS['%comefrom%'] = $_SERVER['HTTP_REFERER'] ?? 'n/a'; $CMS['%dateFormat%'] = 'd.m.Y H:i'; function rplc($string) { global $CMS; diff --git a/2014/index.php b/2014/index.php index ed766fd..01fef6a 100644 --- a/2014/index.php +++ b/2014/index.php @@ -8,12 +8,12 @@ function stringEndsWith($string, $end) { } session_start(); $included = true; -$locale = 'cs'; -$url = $_SERVER['REQUEST_URI']; +$lang = 'cs'; +$url = $_SERVER['REQUEST_URI'] ?? ''; $root = '/2014/'; -$uri = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], $root) + strlen($root)); +$uri = substr($url, strpos($url, $root) + strlen($root)); if ($uri == '') { - header('Location: http://' . $_SERVER['HTTP_HOST'] . $root . $locale . '/'); + header('Location: http://' . $_SERVER['HTTP_HOST'] . $root . $lang . '/'); exit; } elseif (preg_match('/^([a-z]{2})(?:\\/(.+)(\\?.*)?)?$/', $uri, $matches)) { $lang = htmlspecialchars(substr($matches[1], 0, 2)); @@ -26,19 +26,23 @@ if ($uri == '') { $lang = htmlspecialchars(substr($matches[1], 0, 2)); $_GET['page'] = (isset($matches[2]) ? $matches[2] . '/' : '') . 'main'; } else { - $_GET['error'] = 404; + $_GET['page'] = 'error/404'; +} +if (!in_array($lang, ['cs', 'en'], true)) { + header('Location: http://' . $_SERVER['HTTP_HOST'] . '/en/' . ($_GET['page'] ?? '')); + exit; } $page = htmlspecialchars($_GET['page']); $pretitle = '4. přeshraniční skirogaining'; $mainMail = 'tojnar@gmail.com'; -include $lang . '.php'; +require $lang . '.php'; $CMS['%url%'] = 'http://' . $_SERVER['HTTP_HOST'] . htmlspecialchars($url); $CMS['%root%'] = rtrim($root, '/'); $CMS['%lang%'] = $lang; $CMS['%unknownAuthor%'] = 'Jan Tojnar'; $CMS['%headerLinkHref%'] = rplc('%root%/') . $lang; $CMS['%pretitle%'] = $pretitle; -$CMS['%comefrom%'] = $_SERVER['HTTP_REFERER']; +$CMS['%comefrom%'] = $_SERVER['HTTP_REFERER'] ?? 'n/a'; $CMS['%dateFormat%'] = 'd.m.Y H:i'; function rplc($string) { global $CMS; diff --git a/2015-prosinec/index.php b/2015-prosinec/index.php index abb4f2c..7aeea43 100644 --- a/2015-prosinec/index.php +++ b/2015-prosinec/index.php @@ -8,12 +8,12 @@ function stringEndsWith($string, $end) { } session_start(); $included = true; -$locale = 'cs'; -$url = $_SERVER['REQUEST_URI']; +$lang = 'cs'; +$url = $_SERVER['REQUEST_URI'] ?? ''; $root = '/2015-prosinec/'; -$uri = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], $root) + strlen($root)); +$uri = substr($url, strpos($url, $root) + strlen($root)); if ($uri == '') { - header('Location: http://' . $_SERVER['HTTP_HOST'] . $root . $locale . '/'); + header('Location: http://' . $_SERVER['HTTP_HOST'] . $root . $lang . '/'); exit; } elseif (preg_match('/^([a-z]{2})(?:\\/(.+)(\\?.*)?)?$/', $uri, $matches)) { $lang = htmlspecialchars(substr($matches[1], 0, 2)); @@ -26,19 +26,23 @@ if ($uri == '') { $lang = htmlspecialchars(substr($matches[1], 0, 2)); $_GET['page'] = (isset($matches[2]) ? $matches[2] . '/' : '') . 'main'; } else { - $_GET['error'] = 404; + $_GET['page'] = 'error/404'; +} +if (!in_array($lang, ['cs', 'en'], true)) { + header('Location: http://' . $_SERVER['HTTP_HOST'] . '/en/' . ($_GET['page'] ?? '')); + exit; } $page = htmlspecialchars($_GET['page']); $pretitle = '6. přeshraniční skirogaining'; $mainMail = 'tojnar@gmail.com'; -include $lang . '.php'; +require $lang . '.php'; $CMS['%url%'] = 'http://' . $_SERVER['HTTP_HOST'] . htmlspecialchars($url); $CMS['%root%'] = rtrim($root, '/'); $CMS['%lang%'] = $lang; $CMS['%unknownAuthor%'] = 'Jan Tojnar'; $CMS['%headerLinkHref%'] = rplc('%root%/') . $lang; $CMS['%pretitle%'] = $pretitle; -$CMS['%comefrom%'] = $_SERVER['HTTP_REFERER']; +$CMS['%comefrom%'] = $_SERVER['HTTP_REFERER'] ?? 'n/a'; $CMS['%dateFormat%'] = 'd.m.Y H:i'; function rplc($string) { global $CMS; diff --git a/2015/index.php b/2015/index.php index 5e98420..343082d 100644 --- a/2015/index.php +++ b/2015/index.php @@ -8,12 +8,12 @@ function stringEndsWith($string, $end) { } session_start(); $included = true; -$locale = 'cs'; -$url = $_SERVER['REQUEST_URI']; +$lang = 'cs'; +$url = $_SERVER['REQUEST_URI'] ?? ''; $root = '/2015/'; -$uri = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], $root) + strlen($root)); +$uri = substr($url, strpos($url, $root) + strlen($root)); if ($uri == '') { - header('Location: http://' . $_SERVER['HTTP_HOST'] . $root . $locale . '/'); + header('Location: http://' . $_SERVER['HTTP_HOST'] . $root . $lang . '/'); exit; } elseif (preg_match('/^([a-z]{2})(?:\\/(.+)(\\?.*)?)?$/', $uri, $matches)) { $lang = htmlspecialchars(substr($matches[1], 0, 2)); @@ -26,19 +26,23 @@ if ($uri == '') { $lang = htmlspecialchars(substr($matches[1], 0, 2)); $_GET['page'] = (isset($matches[2]) ? $matches[2] . '/' : '') . 'main'; } else { - $_GET['error'] = 404; + $_GET['page'] = 'error/404'; +} +if (!in_array($lang, ['cs', 'de', 'en'], true)) { + header('Location: http://' . $_SERVER['HTTP_HOST'] . '/en/' . ($_GET['page'] ?? '')); + exit; } $page = htmlspecialchars($_GET['page']); $pretitle = '5. přeshraniční skirogaining'; $mainMail = 'tojnar@gmail.com'; -include $lang . '.php'; +require $lang . '.php'; $CMS['%url%'] = 'http://' . $_SERVER['HTTP_HOST'] . htmlspecialchars($url); $CMS['%root%'] = rtrim($root, '/'); $CMS['%lang%'] = $lang; $CMS['%unknownAuthor%'] = 'Jan Tojnar'; $CMS['%headerLinkHref%'] = rplc('%root%/') . $lang; $CMS['%pretitle%'] = $pretitle; -$CMS['%comefrom%'] = $_SERVER['HTTP_REFERER']; +$CMS['%comefrom%'] = $_SERVER['HTTP_REFERER'] ?? 'n/a'; $CMS['%dateFormat%'] = 'd.m.Y H:i'; function rplc($string) { global $CMS;