diff options
author | Jonas Kohl | 2024-09-12 19:49:17 +0200 |
---|---|---|
committer | Jonas Kohl | 2024-09-12 19:49:17 +0200 |
commit | 086e2d2668784469ec114f6e6fd2b3dace3d7c3b (patch) | |
tree | b9bacedb713501d88d24085940267a7c94e69b29 /src/application/mystic/forum/utils | |
parent | 34b1b391d4b03659a96f868857c230002b351514 (diff) |
Way more progress on forum
Diffstat (limited to 'src/application/mystic/forum/utils')
-rw-r--r-- | src/application/mystic/forum/utils/RequestUtils.php | 77 | ||||
-rw-r--r-- | src/application/mystic/forum/utils/ValidationUtils.php | 22 |
2 files changed, 95 insertions, 4 deletions
diff --git a/src/application/mystic/forum/utils/RequestUtils.php b/src/application/mystic/forum/utils/RequestUtils.php index 2f40013..f6ce3a3 100644 --- a/src/application/mystic/forum/utils/RequestUtils.php +++ b/src/application/mystic/forum/utils/RequestUtils.php @@ -3,17 +3,86 @@ declare(strict_types=1); namespace mystic\forum\utils; +use mystic\forum\Database; use mystic\forum\Messaging; +use mystic\forum\orm\User; final class RequestUtils { use StaticClass; + public static function getRequestMethod(): string { + return strtoupper($_SERVER["REQUEST_METHOD"] ?? "GET"); + } + + public static function isRequestMethod(string $method): bool { + $rMethod = self::getRequestMethod(); + return strcasecmp($rMethod, $method) === 0; + } + public static function ensureRequestMethod(string $method): void { - $rMethod = $_SERVER["REQUEST_METHOD"]; - if (strcasecmp($rMethod, $method) !== 0) { - http_response_code(500); - Messaging::error("Invalid request method $rMethod"); + if (!self::isRequestMethod($method)) { + http_response_code(415); + Messaging::error("Invalid request method " . self::getRequestMethod()); + exit; + } + } + + public static function getRequiredField(string $field): string { + $fieldValue = $_POST[$field] ?? null; + if ($fieldValue === null) { + http_response_code(400); + Messaging::error("Missing required field $field"); exit; } + return $fieldValue; + } + + public static function storeForm(): void { + $_SESSION["lastForm"] = $_POST ?? []; + $_SESSION["lastForm_uri"] = $_SERVER["REQUEST_URI"]; + } + + public static function triggerFormError(string $message, ?string $next = null): never { + $next ??= $_SERVER["REQUEST_URI"]; + $_SESSION["formError"] = $message; + // store last form submission + self::storeForm(); + header("Location: $next"); + exit; + } + + public static function getAndClearFormError(): ?string { + $err = $_SESSION["formError"] ?? null; + unset($_SESSION["formError"]); + return $err; + } + + public static function getLastForm(string &$lastFormUri): ?array { + $lastFormUri = $_SESSION["lastForm_uri"] ?? ""; + return $_SESSION["lastForm"] ?? null; + } + + public static function clearLastForm(): void { + unset($_SESSION["lastForm"]); + unset($_SESSION["lastForm_uri"]); + } + + public static function getAuthorizedUser(Database &$db): ?User { + $userId = $_SESSION["authedUser"] ?? null; + if ($userId === null) + return null; + $user = new User(); + $user->id = $userId; + if (!$db->fetch($user)) + return null; + return $user; + } + + public static function setAuthorizedUser(User &$user): void { + $_SESSION["authedUser"] = $user->id; + } + + public static function unsetAuthorizedUser(): void { + unset($_SESSION["authedUser"]); } } diff --git a/src/application/mystic/forum/utils/ValidationUtils.php b/src/application/mystic/forum/utils/ValidationUtils.php new file mode 100644 index 0000000..df97914 --- /dev/null +++ b/src/application/mystic/forum/utils/ValidationUtils.php @@ -0,0 +1,22 @@ +<?php +declare(strict_types=1); + +namespace mystic\forum\utils; + +use mystic\forum\Database; +use mystic\forum\orm\User; + +final class ValidationUtils { + use StaticClass; + + public static function isUsernameValid(string $name): bool { + return !!preg_match('/^[a-z0-9]([._](?![._])|[a-z0-9]){2,30}[a-z0-9]$/', $name); + } + + public static function isUsernameAvailable(Database &$db, string $name): bool { + $user = new User(); + $user->name = $name; + return !$db->fetchWhere($user, "name"); + } +} + |