diff --git a/index.php b/index.php index 64882f9..452ae66 100644 --- a/index.php +++ b/index.php @@ -1,4 +1,15 @@ format('T'); @@ -11,25 +22,26 @@ function toTZ($dt, $tz, $format='Y-m-d H:i') { } function setUserCookie($name, $value) { - setcookie($name, $value, time() + 60 * 60 * 24 * 365, '/'); + setcookie($name, $value, time() + COOKIE_LIFETIME, '/'); $_COOKIE[$name] = $value; } $popTZ = [ - 'America/Los_Angeles', - 'America/New_York', - 'Europe/London', - 'Europe/Paris', - 'Asia/Dubai', - 'Asia/Jakarta', - 'Asia/Tokyo', - 'Australia/Sydney', + 'America/Los_Angeles', // PST/PDT + 'America/New_York', // EST/EDT + 'Europe/London', // BST/GMT + 'Europe/Paris', // CEST/CET + 'Asia/Dubai', // UTC+04 "UAE" + 'Asia/Jakarta', // WIB + 'Asia/Tokyo', // JST + 'Australia/Sydney', // AEST/AEDT 'UTC' ]; +$allTZ = timezone_identifiers_list(DateTimeZone::ALL); + // User sets timezone cookie -if (isset($_POST['user-tz']) && - in_array($_POST['user-tz'], timezone_identifiers_list(DateTimeZone::ALL))) { +if (isset($_POST['user-tz']) && in_array($_POST['user-tz'], $allTZ)) { setUserCookie('user-tz', $_POST['user-tz']); } @@ -44,38 +56,43 @@ if (isset($_POST['no-js'])) { } // Micro-routing -if (isset($_POST['datetime']) && isset($_POST['timezone'])) { - // -- Redirect to submitted date - $postTZ = $_POST['timezone']; - - // Handle if timezone is an offset - if (is_numeric($_POST['timezone'])) { - $isNeg = str_starts_with($postTZ, '-'); - $h = str_pad(abs(intdiv($postTZ, 60)), 2, '0', STR_PAD_LEFT); - $m = str_pad($postTZ % 60, 2, '0', STR_PAD_LEFT); - $postTZ = ($isNeg ? '-' : '+') . $h . $m; - } +try { + if (isset($_POST['datetime']) && isset($_POST['timezone'])) { + // -- Redirect to submitted date + $postTZ = $_POST['timezone']; + + // Handle if timezone is an offset + if (is_numeric($_POST['timezone'])) { + $isNeg = str_starts_with($postTZ, '-'); + $h = str_pad(abs(intdiv($postTZ, 60)), 2, '0', STR_PAD_LEFT); + $m = str_pad($postTZ % 60, 2, '0', STR_PAD_LEFT); + $postTZ = ($isNeg ? '-' : '+') . $h . $m; + } - // Make our date object - $dateObj = new DateTime($_POST['datetime'], new DateTimeZone($postTZ)); - - // Redirect, with the "+" replaced by "_" - header('Location: /' . str_replace('+', '_', $dateObj->format('c'))); - exit; -} elseif ($_SERVER['REQUEST_URI'] !== '/') { - // Remove leading "/" - $req = substr($_SERVER['REQUEST_URI'], 1); - - // -- Show date infos - // First check if date is following the format - $re = '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[\_|-]\d{2}:\d{2}$/'; - if (!preg_match($re, $req)) { - http_response_code(404); - $error = 'Date is not valid, wrong format, or file not found.'; - } else { - // Make our date object, convert back "_" to "+" - $dt = new DateTime(str_replace('_', '+', $req)); + // Make our date object + $dateObj = new DateTime($_POST['datetime'], new DateTimeZone($postTZ)); + + // Redirect, with the "+" replaced by "_" + header('Location: /' . str_replace('+', '_', $dateObj->format('c'))); + exit; + } elseif ($_SERVER['REQUEST_URI'] !== '/') { + // Remove leading "/" + $req = substr($_SERVER['REQUEST_URI'], 1); + + // -- Show date infos + // First check if date is following the format + $re = '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[\_|-]\d{2}:\d{2}$/'; + if (!preg_match($re, $req)) { + http_response_code(404); + $error = 'Date is not valid, wrong format, or file not found.'; + } else { + // Make our date object, convert back "_" to "+" + $dt = new DateTime(str_replace('_', '+', $req)); + } } +} catch (Exception $e) { + http_response_code(400); + $error = 'Invalid date format or date out of range.'; } ?> @@ -87,13 +104,13 @@ if (isset($_POST['datetime']) && isset($_POST['timezone'])) { - Time Zones Are Hard<?= isset($req) ? ' - ' . str_replace('_', '+', $req) : '' ?> + Time Zones Are Hard<?= isset($req) ? ' - ' . htmlspecialchars(str_replace('_', '+', $req), ENT_QUOTES, 'UTF-8') : '' ?> - @@ -128,7 +145,7 @@ if (isset($_POST['datetime']) && isset($_POST['timezone'])) { ?> Please select your local time zone (or enable JavaScript):
- @@ -138,7 +155,7 @@ if (isset($_POST['datetime']) && isset($_POST['timezone'])) { - + @@ -146,7 +163,7 @@ if (isset($_POST['datetime']) && isset($_POST['timezone'])) {
- +
@@ -175,10 +192,10 @@ if (isset($_POST['datetime']) && isset($_POST['timezone'])) {
- +
- @@ -266,7 +283,7 @@ if (isset($_POST['datetime']) && isset($_POST['timezone'])) { - + @@ -279,7 +296,7 @@ if (isset($_POST['datetime']) && isset($_POST['timezone'])) {
-