Time Zones Are Hard - https://tz.rita.moe

Minor changes but it makes more sense

Changed files
+23 -21
+23 -21
index.php
···
<?php
// Configuration
const COOKIE_LIFETIME = 60 * 60 * 24 * 365; // 1 year
-
const SITE_URL = 'https://tz.rita.moe';
+
const SITE_BASE = 'https://tz.rita.moe'; // Base URL of the site, no trailing slash
// Set security headers
$nonce = bin2hex(random_bytes(16));
···
// Micro-routing
try {
if (isset($_POST['datetime']) && isset($_POST['timezone'])) {
-
// -- Redirect to submitted date
-
$postTZ = $_POST['timezone'];
+
// -- Redirect to submitted date
+
// @NOTE(Kody): I'm fine not validating all inputs here.
+
// Either it will fall in the catch block, or fail at the validation after the redirect.
-
// 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;
-
}
+
// Handle if timezone is an offset
+
$postTZ = $_POST['timezone'];
+
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));
+
// 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;
+
// Redirect, with the "+" replaced by "_"
+
header('Location: /' . str_replace('+', '_', $dateObj->format('c')));
+
exit;
} elseif ($_SERVER['REQUEST_URI'] !== '/') {
+
// -- Show date infos
// Remove leading "/"
$req = substr($_SERVER['REQUEST_URI'], 1);
-
// -- Show date infos
-
// First check if date is following the format
+
// 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);
···
}
} catch (Exception $e) {
http_response_code(400);
-
$error = 'Invalid date format or date out of range.';
+
$error = 'Date is not valid, or wrong format.';
}
?>
<!DOCTYPE html>
···
</div>
<div class="share">
-
<input type="hidden" id="url" value="<?= htmlspecialchars(SITE_URL . $_SERVER['REQUEST_URI'], ENT_QUOTES, 'UTF-8') ?>"/>
+
<input type="hidden" id="url" value="<?= htmlspecialchars(SITE_BASE . $_SERVER['REQUEST_URI'], ENT_QUOTES, 'UTF-8') ?>"/>
</div>
<script nonce="<?= $nonce ?>">
···
if (navigator.canShare) {
// Use native navigator share
navigator.share({
-
url: '<?= htmlspecialchars(SITE_URL . $_SERVER['REQUEST_URI'], ENT_QUOTES, 'UTF-8') ?>'
+
url: '<?= htmlspecialchars(SITE_BASE . $_SERVER['REQUEST_URI'], ENT_QUOTES, 'UTF-8') ?>'
})
} else if (navigator.clipboard) {
// Use Clipboard API
-
navigator.clipboard.writeText('<?= htmlspecialchars(SITE_URL . $_SERVER['REQUEST_URI'], ENT_QUOTES, 'UTF-8') ?>')
+
navigator.clipboard.writeText('<?= htmlspecialchars(SITE_BASE . $_SERVER['REQUEST_URI'], ENT_QUOTES, 'UTF-8') ?>')
.then(() => {
alert('URL copied to clipboard!')
})