death-notifier/src/main/api.php

125 lines
5.3 KiB
PHP

<?php
use com\fwdekker\deathnotifier\ActionDispatcher;
use com\fwdekker\deathnotifier\ActionMethod;
use com\fwdekker\deathnotifier\Config;
use com\fwdekker\deathnotifier\Database;
use com\fwdekker\deathnotifier\EmulateCronAction;
use com\fwdekker\deathnotifier\LoggerUtil;
use com\fwdekker\deathnotifier\mailer\EmailQueue;
use com\fwdekker\deathnotifier\mailer\ProcessEmailQueueAction;
use com\fwdekker\deathnotifier\wikipedia\Wikipedia;
use com\fwdekker\deathnotifier\Response;
use com\fwdekker\deathnotifier\StartSessionAction;
use com\fwdekker\deathnotifier\tracking\AddTrackingAction;
use com\fwdekker\deathnotifier\tracking\ListTrackingsAction;
use com\fwdekker\deathnotifier\tracking\RemoveTrackingAction;
use com\fwdekker\deathnotifier\tracking\TrackingList;
use com\fwdekker\deathnotifier\tracking\UpdateTrackingsAction;
use com\fwdekker\deathnotifier\user\GetPublicUserDataAction;
use com\fwdekker\deathnotifier\user\LoginAction;
use com\fwdekker\deathnotifier\user\LogoutAction;
use com\fwdekker\deathnotifier\user\RegisterAction;
use com\fwdekker\deathnotifier\user\ResendVerifyEmailAction;
use com\fwdekker\deathnotifier\user\ResetPasswordAction;
use com\fwdekker\deathnotifier\user\SendPasswordResetAction;
use com\fwdekker\deathnotifier\user\ToggleNotificationsAction;
use com\fwdekker\deathnotifier\user\ChangeEmailAction;
use com\fwdekker\deathnotifier\user\ChangePasswordAction;
use com\fwdekker\deathnotifier\user\UserDeleteAction;
use com\fwdekker\deathnotifier\user\UserList;
use com\fwdekker\deathnotifier\user\ValidatePasswordResetTokenAction;
use com\fwdekker\deathnotifier\user\VerifyEmailAction;
use com\fwdekker\deathnotifier\Util;
/** @noinspection PhpIncludeInspection Exists after `npm run deploy` */
require_once __DIR__ . "/.vendor/autoload.php";
$logger = LoggerUtil::with_name(); // This also registers error handler
// Wrap everything in try-catch to always return *something* to user
try {
// Preamble
session_start();
$_SESSION["token"] = $_SESSION["token"] ?? Util::generate_csrf_token();
$db = new Database(Config::get("database.filename"));
$wikipedia = new Wikipedia();
$email_queue = new EmailQueue($db);
$user_list = new UserList($db);
$tracking_list = new TrackingList($db);
$db->auto_install($email_queue, $user_list, $tracking_list);
$db->auto_migrate();
// Set up request handlers
$dispatcher = new ActionDispatcher();
// GET actions
$dispatcher->register_actions([
[ActionMethod::GET, "start-session", new StartSessionAction($user_list)],
[ActionMethod::GET, "get-user-data", new GetPublicUserDataAction($user_list)],
[ActionMethod::GET, "list-trackings", new ListTrackingsAction($tracking_list)],
[ActionMethod::GET, "validate-password-reset-token", new ValidatePasswordResetTokenAction($user_list)],
]);
// POST actions
$dispatcher->register_actions([
[ActionMethod::POST, "register", new RegisterAction($user_list, $email_queue)],
[ActionMethod::POST, "login", new LoginAction($user_list)],
[ActionMethod::POST, "logout", new LogoutAction()],
[ActionMethod::POST, "user-delete", new UserDeleteAction($user_list)],
[ActionMethod::POST, "update-email", new ChangeEmailAction($user_list, $email_queue)],
[ActionMethod::POST, "verify-email", new VerifyEmailAction($user_list)],
[ActionMethod::POST, "resend-verify-email", new ResendVerifyEmailAction($user_list, $email_queue)],
[ActionMethod::POST, "toggle-notifications", new ToggleNotificationsAction($user_list)],
[ActionMethod::POST, "update-password", new ChangePasswordAction($user_list, $email_queue)],
[ActionMethod::POST, "send-password-reset", new SendPasswordResetAction($user_list, $email_queue)],
[ActionMethod::POST, "reset-password", new ResetPasswordAction($user_list, $email_queue)],
[ActionMethod::POST, "add-tracking", new AddTrackingAction($tracking_list, $wikipedia)],
[ActionMethod::POST, "remove-tracking", new RemoveTrackingAction($tracking_list)],
]);
// CLI actions
$cli_actions = [
new UpdateTrackingsAction($tracking_list, $wikipedia, $email_queue),
new ProcessEmailQueueAction($email_queue),
];
$dispatcher->register_actions([
[ActionMethod::CLI, "update-trackings", $cli_actions[0]],
[ActionMethod::CLI, "process-email-queue", $cli_actions[1]],
[ActionMethod::CLI, "emulate-cron", new EmulateCronAction($cli_actions)],
]);
// Handle request
if (!isset($_SERVER["REQUEST_METHOD"]))
$response = $dispatcher->handle(ActionMethod::CLI, Util::parse_cli());
else if ($_SERVER["REQUEST_METHOD"] === "GET")
$response = $dispatcher->handle(ActionMethod::GET, $_GET);
else if ($_SERVER["REQUEST_METHOD"] === "POST")
$response = $dispatcher->handle(ActionMethod::POST, Util::parse_post());
else
$response = new Response(satisfied: true, payload: null);
} catch (Throwable $exception) {
$logger->error("An unhandled error occurred.", ["cause" => $exception]);
$response = Response::unsatisfied("An unhandled error occurred. Please try again later.");
}
// Respond
header("Content-type:application/json;charset=utf-8");
exit(json_encode([
"payload" => $response->payload,
"satisfied" => $response->satisfied,
"token" => $_SESSION["token"],
]));