fipamo/brain/controller/APIControl.inc.php

85 lines
2.2 KiB
PHP

<?php
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
include "../brain/api/v1/ImagesAPI.inc.php";
class APIControl
{
public static function get(
ServerRequestInterface $request,
ResponseInterface $response,
array $args
): ResponseInterface {
switch (isset($args["third"]) ? $args["third"] : "none") {
case "status":
$result = Auth::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;
}
//there's only one verion of the api for now
switch (isset($args["third"]) ? $args["third"] : "none") {
case "login":
$result = Auth::login($body);
break;
case "logout":
$result = Auth::logout($body);
break;
case "page":
//move methdology to its own API class
$task = $args["fourth"];
$token = $request->getHeader("fipamo-access-token");
if (Session::verifyToken($token[0])) {
switch ($task) {
case "delete":
case "add":
case "edit":
$result = (new Book("../content/pages"))->editPage(
$task,
$request
);
break;
case "add-entry-image":
$result = ImagesAPI::uploadImage($request);
break;
}
} 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");
}
}