summaryrefslogtreecommitdiff
path: root/src/application/mystic/forum/utils/RequestUtils.php
blob: 796a476ae39dbb11cdfc146d788ed4e06fe2687b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
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;

    private static ?string $formErrorDestination = null;

    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 {
        if (!self::isRequestMethod($method)) {
            http_response_code(415);
            Messaging::error("Invalid request method " . self::getRequestMethod());
            exit;
        }
    }

    public static function getRequiredField(string $field, string $formId): string {
        $fieldValue = $_POST[$field] ?? null;
        if ($fieldValue === null) {
            //http_response_code(400);
            //Messaging::error("Missing required field $field");
            RequestUtils::triggerFormError("Missing required field '$field'", $formId);
            //exit;
        }
        return $fieldValue;
    }

    public static function storeForm(): void {
        $_SESSION["lastForm"] = $_POST ?? [];
        $_SESSION["lastForm_uri"] = $_SERVER["REQUEST_URI"];
    }

    public static function setFormErrorDestination(?string $dest): ?string {
        $prev = self::$formErrorDestination;
        self::$formErrorDestination = $dest;
        return $prev;
    }

    public static function triggerFormError(string $message, string $formId, ?string $next = null): never {
        $next ??= self::$formErrorDestination ?? $_SERVER["REQUEST_URI"];
        $_SESSION["formError/$formId"] = $message;
        // store last form submission
        self::storeForm();
        header("Location: $next");
        exit;
    }

    public static function getAndClearFormError(string $formId): ?string {
        $err = $_SESSION["formError/$formId"] ?? null;
        unset($_SESSION["formError/$formId"]);
        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"]);
    }
}