You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
2.2 KiB
61 lines
2.2 KiB
<?php
|
|
|
|
require "../../config.php";
|
|
require "../../vendor/autoload.php";
|
|
require "../../sql/database.php";
|
|
require "../../src/index-utils.php";
|
|
|
|
use IQBall\Api\API;
|
|
use IQBall\Api\Controller\APIAuthController;
|
|
use IQBall\Api\Controller\APITacticController;
|
|
use IQBall\App\Session\PhpSessionHandle;
|
|
use IQBall\Core\Action;
|
|
use IQBall\Core\Connection;
|
|
use IQBall\Core\Data\Account;
|
|
use IQBall\Core\Gateway\AccountGateway;
|
|
use IQBall\Core\Gateway\TacticInfoGateway;
|
|
use IQBall\Core\Model\AuthModel;
|
|
use IQBall\Core\Model\TacticModel;
|
|
|
|
function getTacticController(): APITacticController {
|
|
return new APITacticController(new TacticModel(new TacticInfoGateway(new Connection(get_database()))));
|
|
}
|
|
|
|
function getAuthController(): APIAuthController {
|
|
return new APIAuthController(new AuthModel(new AccountGateway(new Connection(get_database()))));
|
|
}
|
|
|
|
function getRoutes(): AltoRouter {
|
|
$router = new AltoRouter();
|
|
$router->setBasePath(get_public_path() . "/api");
|
|
|
|
$router->map("POST", "/auth", Action::noAuth(fn() => getAuthController()->authorize()));
|
|
$router->map("POST", "/tactic/[i:id]/edit/name", Action::auth(fn(int $id, Account $acc) => getTacticController()->updateName($id, $acc)));
|
|
$router->map("POST", "/tactic/[i:id]/save", Action::auth(fn(int $id) => getTacticController()->saveContent($id)));
|
|
|
|
return $router;
|
|
}
|
|
|
|
/**
|
|
* Defines the way of being authorised through the API
|
|
* By checking if an Authorisation header is set, and by expecting its value to be a valid token of an account.
|
|
* If the header is not set, fallback to the App's PHP session system, and try to extract the account from it.
|
|
* @return Account|null
|
|
* @throws Exception
|
|
*/
|
|
function tryGetAuthorization(): ?Account {
|
|
$headers = getallheaders();
|
|
|
|
// If no authorization header is set, try fallback to php session.
|
|
if (!isset($headers['Authorization'])) {
|
|
$session = PhpSessionHandle::init();
|
|
return $session->getAccount();
|
|
}
|
|
|
|
$token = $headers['Authorization'];
|
|
$gateway = new AccountGateway(new Connection(get_database()));
|
|
return $gateway->getAccountFromToken($token);
|
|
}
|
|
|
|
Api::render(API::handleMatch(getRoutes()->match(), fn() => tryGetAuthorization()));
|