fipamo/brain/controller/APIControl.inc.php

83 lines
2.3 KiB
PHP
Raw Normal View History

<?php
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
2021-04-17 05:50:03 +02:00
include "../brain/api/v1/AuthAPI.inc.php";
include "../brain/api/v1/ImagesAPI.inc.php";
include "../brain/api/v1/PagesAPI.inc.php";
include "../brain/api/v1/SettingsAPI.inc.php";
class APIControl
{
public static function get(
ServerRequestInterface $request,
ResponseInterface $response,
array $args
): ResponseInterface {
switch (isset($args["third"]) ? $args["third"] : "none") {
case "status":
2021-04-17 05:50:03 +02:00
$result = AuthAPI::status();
break;
default:
break;
}
$response->getBody()->write(json_encode($result));
return $response->withHeader("Content-Type", "application/json");
}
public static function post(
ServerRequestInterface $request,
ResponseInterface $response,
array $args
): ResponseInterface {
$contentType = $request->getHeader("Content-Type");
switch ($contentType[0]) {
case "application/json":
$body = json_decode(file_get_contents("php://input"), true);
break;
default:
break;
}
switch (isset($args["third"]) ? $args["third"] : "none") {
case "login":
2021-04-17 05:50:03 +02:00
$result = AuthAPI::login($body);
break;
case "logout":
2021-04-17 05:50:03 +02:00
$result = AuthAPI::logout($body);
break;
case "page":
$token = $request->getHeader("fipamo-access-token");
//Verify token for admin tasks
if (Session::verifyToken($token[0])) {
$result = PagesAPI::handlePageTask($request, $args);
} else {
$result = [
"message" => "API access denied, homie",
"type" => "API_ERROR",
];
}
break;
case "settings":
$token = $request->getHeader("fipamo-access-token");
if (Session::verifyToken($token[0])) {
$result = SettingsAPI::handleSettingsTask($request, $args, $body);
} else {
$result = [
"message" => "API access denied, homie",
"type" => "API_ERROR",
];
}
break;
default:
$result = [
"message" => "Oh, nothing to do. That's unfortunate",
"type" => "TASK_NONE",
];
break;
}
$response->getBody()->write(json_encode($result));
return $response->withHeader("Content-Type", "application/json");
}
}