summaryrefslogtreecommitdiff
path: root/account/login/index.php
blob: 54a668165cd04521a6ed4c1bfbf1b688e1947d2a (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
98
99
100
101
102
103
104
105
106
<?php
include_once "{$_SERVER['DOCUMENT_ROOT']}/lib/accounts.php";

if (authorize_user() && !CLIENT_REQUIRES_JSON) {
    header("Location: /account");
    exit;
}

include_once "{$_SERVER['DOCUMENT_ROOT']}/lib/partials.php";
include_once "{$_SERVER['DOCUMENT_ROOT']}/lib/config.php";
include_once "{$_SERVER['DOCUMENT_ROOT']}/lib/alert.php";
include_once "{$_SERVER['DOCUMENT_ROOT']}/lib/utils.php";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (!isset($_POST["username"], $_POST["password"])) {
        generate_alert("/account/login", "Not enough POST fields");
        exit;
    }

    $username = $_POST["username"];
    $password = $_POST["password"];
    $remember = intval($_POST["remember"] ?? "0") != 0;

    $db = new PDO(CONFIG['database']['url'], CONFIG['database']['user'], CONFIG['database']['pass']);
    $stmt = $db->prepare("SELECT secret_key, password FROM users WHERE username = ? AND password IS NOT NULL");
    $stmt->execute([$username]);

    $row = $stmt->fetch(PDO::FETCH_ASSOC) ?: null;
    if (!$row || !password_verify($password, $row["password"])) {
        generate_alert("/account/login", "User not found or is not accessable", 404);
        exit;
    }

    if (CLIENT_REQUIRES_JSON) {
        json_response([
            "status_code" => 200,
            "message" => null,
            "data" => [
                'secret_key' => $row["secret_key"]
            ]
        ]);
    } else {
        setcookie("secret_key", $row["secret_key"], $remember ? (time() + CONFIG['account']['maxcookielifetime']) : 0, "/");
        header("Location: /account");
    }

    exit();
}
?>

<html>

<head>
    <title>Login - <?= CONFIG['instance']['name'] ?></title>
    <link rel="stylesheet" href="/static/style.css">
    <link rel="shortcut icon" href="/static/favicon.ico" type="image/x-icon">
</head>

<body>
    <div class="container">
        <div class="wrapper">
            <?php html_navigation_bar(); ?>
            <section class="content" style="width: 400px;">
                <?php display_alert() ?>
                <section class="box">
                    <div class="box navtab">
                        <p>Log in to <?= CONFIG['instance']['name'] ?></p>
                    </div>
                    <div class="box content">
                        <form action="/account/login/" method="post">
                            <div>
                                <label for="username">Username</label>
                                <input type="text" name="username" id="form-username" required>
                            </div>
                            <div>
                                <label for="password">Password</label>
                                <input type="password" name="password" id="form-password" required>
                            </div>
                            <div>
                                <input type="checkbox" name="remember" value="1" id="form-remember">
                                <label for="remember" class="inline">Remember me</label>
                            </div>
                            <div>
                                <button type="submit">Log in</button>
                                <?php if (CONFIG['account']['registration']): ?>
                                    <a href="/account/register.php">Register</a>
                                <?php endif; ?>
                            </div>
                        </form>
                    </div>
                </section>

                <?php if (CONFIG['twitch']['registration']): ?>
                    <section class="box column">
                        <a href="/account/login/twitch.php" class="button purple big">Login with Twitch</a>
                        <p style="font-size: 12px;">Logging in via Twitch gives you the ability to use
                            <?= CONFIG['instance']['name'] ?> emotes in your Twitch chat.
                        </p>
                    </section>
                <?php endif; ?>
            </section>
        </div>
    </div>
</body>

</html>