diff options
Diffstat (limited to 'src/index.php')
-rw-r--r-- | src/index.php | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/src/index.php b/src/index.php index 62a300a..8e2ce4e 100644 --- a/src/index.php +++ b/src/index.php @@ -1,13 +1,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 = new Database(Database::getConnectionString("db", "postgres", "postgres", "postgres")); +$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"); +} |