death-notifier/src/main/api.php

113 lines
4.8 KiB
PHP

<?php
use com\fwdekker\deathnotifier\ActionDispatcher;
use com\fwdekker\deathnotifier\ActionMethod;
use com\fwdekker\deathnotifier\Database;
use com\fwdekker\deathnotifier\EmulateCronCliAction;
use com\fwdekker\deathnotifier\LoggerUtil;
use com\fwdekker\deathnotifier\mailer\MailManager;
use com\fwdekker\deathnotifier\mailer\ProcessEmailQueueCliAction;
use com\fwdekker\deathnotifier\mediawiki\MediaWiki;
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\TrackingManager;
use com\fwdekker\deathnotifier\tracking\UpdateTrackingsCliAction;
use com\fwdekker\deathnotifier\user\GetUserDataAction;
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\UpdateEmailAction;
use com\fwdekker\deathnotifier\user\UpdatePasswordAction;
use com\fwdekker\deathnotifier\user\UserDeleteAction;
use com\fwdekker\deathnotifier\user\UserManager;
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";
// Preamble
$config = Util::read_config();
LoggerUtil::configure($config["logger"]);
$logger = LoggerUtil::with_name();
$db = new Database($config["database"]["filename"]);
$mediawiki = new MediaWiki();
$mail_manager = new MailManager($db->conn);
$user_manager = new UserManager($db->conn, $mail_manager);
$tracking_manager = new TrackingManager($db->conn);
// Handle request
try {
session_start();
$_SESSION["token"] = $_SESSION["token"] ?? Util::generate_csrf_token();
$_POST = Util::parse_post();
// Update database
$db->auto_install($mail_manager, $user_manager, $tracking_manager);
$db->auto_migrate();
// Dispatch request
$dispatcher = new ActionDispatcher();
// GET actions
$dispatcher->register_action(new StartSessionAction($config, $user_manager));
$dispatcher->register_action(new GetUserDataAction($user_manager));
$dispatcher->register_action(new ListTrackingsAction($tracking_manager));
$dispatcher->register_action(new ValidatePasswordResetTokenAction($user_manager));
// POST actions
$dispatcher->register_action(new RegisterAction($user_manager));
$dispatcher->register_action(new LoginAction($user_manager));
$dispatcher->register_action(new LogoutAction());
$dispatcher->register_action(new ResendVerifyEmailAction($user_manager));
$dispatcher->register_action(new VerifyEmailAction($user_manager));
$dispatcher->register_action(new UpdateEmailAction($user_manager));
$dispatcher->register_action(new ToggleNotificationsAction($user_manager));
$dispatcher->register_action(new UpdatePasswordAction($user_manager));
$dispatcher->register_action(new SendPasswordResetAction($user_manager));
$dispatcher->register_action(new ResetPasswordAction($user_manager));
$dispatcher->register_action(new UserDeleteAction($user_manager));
$dispatcher->register_action(new AddTrackingAction($tracking_manager, $mediawiki));
$dispatcher->register_action(new RemoveTrackingAction($tracking_manager));
// CLI actions
$cli_actions = [
new UpdateTrackingsCliAction($config, $db->conn, $tracking_manager, $mediawiki, $mail_manager),
new ProcessEmailQueueCliAction($config, $mail_manager),
];
$dispatcher->register_action($cli_actions[0]);
$dispatcher->register_action($cli_actions[1]);
$dispatcher->register_action(new EmulateCronCliAction($cli_actions));
// Dispatch
if (isset($_GET["action"]))
$response = $dispatcher->handle(ActionMethod::GET);
else if (isset($_POST["action"]))
$response = $dispatcher->handle(ActionMethod::POST);
else if ($argc > 1)
$response = $dispatcher->handle(ActionMethod::CLI);
else
$response = Response::satisfied();
} catch (Exception $exception) {
$response = Response::unsatisfied("An unexpected error occurred. Please try again later.");
$logger->error("An unexpected error occurred. Please try again later.", ["cause" => $exception]);
}
// Respond
header("Content-type:application/json;charset=utf-8");
exit(json_encode([
"payload" => $response->payload,
"satisfied" => $response->satisfied,
"token" => $_SESSION["token"]
]));