death-notifier/src/main/api.php

113 lines
4.8 KiB
PHP
Raw Normal View History

2022-08-09 17:03:49 +02:00
<?php
2022-08-09 19:20:02 +02:00
use com\fwdekker\deathnotifier\ActionDispatcher;
use com\fwdekker\deathnotifier\ActionMethod;
use com\fwdekker\deathnotifier\Database;
use com\fwdekker\deathnotifier\EmulateCronCliAction;
2022-12-01 23:05:12 +01:00
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;
2022-12-01 20:52:16 +01:00
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;
2022-08-09 17:03:49 +02:00
2022-08-09 19:20:02 +02:00
/** @noinspection PhpIncludeInspection Exists after `npm run deploy` */
2022-08-29 01:09:33 +02:00
require_once __DIR__ . "/.vendor/autoload.php";
2022-08-09 17:03:49 +02:00
2022-08-29 01:09:33 +02:00
// Preamble
$config = Util::read_config();
2022-12-01 23:05:12 +01:00
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);
2022-11-15 22:26:34 +01:00
2022-08-14 15:52:47 +02:00
// Handle request
try {
session_start();
$_SESSION["token"] = $_SESSION["token"] ?? Util::generate_csrf_token();
$_POST = Util::parse_post();
2022-08-12 20:46:43 +02:00
// Update database
2022-12-01 23:05:12 +01:00
$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 = [
2022-12-01 23:05:12 +01:00
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]);
}
2022-11-12 14:18:00 +01:00
// Respond
header("Content-type:application/json;charset=utf-8");
exit(json_encode([
"payload" => $response->payload,
2022-08-12 20:46:43 +02:00
"satisfied" => $response->satisfied,
"token" => $_SESSION["token"]
]));