summaryrefslogtreecommitdiff
path: root/src/index.php
blob: 8e2ce4e403a2f13c2f5666389df199103ec7e56e (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
<?php

use mystic\forum\Database;
use mystic\forum\exceptions\DatabaseConnectionException;
use mystic\forum\Messaging;
use mystic\forum\orm\Post;
use mystic\forum\orm\Topic;
use mystic\forum\orm\User;
use mystic\forum\utils\RequestUtils;

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException(html_entity_decode($errstr), $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");

session_name("fsid");
session_start();

$_rq_method = $_SERVER["REQUEST_METHOD"] ?? "GET";
$_action = $_GET["_action"] ?? null;

require_once __DIR__ . "/vendor/autoload.php";

$db = null;
try {
    $db = new Database(Database::getConnectionString("db", "postgres", "postgres", "postgres"));
} catch (DatabaseConnectionException $ex) {
    Messaging::error([
        Messaging::bold("Failed to connect to database!"),
        Messaging::italic($ex->getMessage()),
    ]);
    exit;
}

$db->ensureTable(User::class);
$db->ensureTable(Topic::class);
$db->ensureTable(Post::class);

$superuser = new User();
$superuser->id = "SUPERUSER";
if (!$db->fetch($superuser)) {
    $superUserPassword = base64_encode(random_bytes(12));

    $superuser->name = "superuser";
    $superuser->passwordHash = password_hash($superUserPassword, PASSWORD_DEFAULT);
    $superuser->displayName = "SuperUser";
    $superuser->created = new \DateTimeImmutable();

    $db->insert($superuser);

    Messaging::info([
        Messaging::bold("Superuser account created"),
        [
            "Username" => $superuser->name,
            "Password" => $superUserPassword,
        ],
        "Please note that the password can only be shown this time, so please note it down!",
    ]);
    exit;
}

// initialization finished

if ($_action === "auth") {
    RequestUtils::ensureRequestMethod("POST");
    // TODO Login logic
} elseif ($_action === null) {
    echo "Hello";
} else {
    http_response_code(404);
    Messaging::error("Invalid or unknown action $_action");
}