146 lines
4.2 KiB
PHP
146 lines
4.2 KiB
PHP
<?php
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
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";
|
|
include "../brain/api/v1/InitAPI.inc.php";
|
|
|
|
class APIControl
|
|
{
|
|
public static function get(
|
|
ServerRequestInterface $request,
|
|
ResponseInterface $response,
|
|
array $args
|
|
): ResponseInterface {
|
|
$filename = "";
|
|
switch (isset($args["third"]) ? $args["third"] : "none") {
|
|
case "status":
|
|
$result = AuthAPI::status();
|
|
break;
|
|
case "files":
|
|
if (Session::active()) {
|
|
if ($args["third"] == "backup") {
|
|
$filename = "../config/backups/latest_backup.zip";
|
|
if (file_exists($filename)) {
|
|
header("Content-Type: application/zip");
|
|
header(
|
|
'Content-Disposition: attachment; filename="' .
|
|
basename($filename) .
|
|
'"'
|
|
);
|
|
header("Content-Length: " . filesize($filename));
|
|
|
|
flush();
|
|
// return readfile($filename);
|
|
//readfile($filename);
|
|
// delete file
|
|
//unlink($filename);
|
|
}
|
|
}
|
|
} else {
|
|
$result = [
|
|
"message" => "API access denied, homie",
|
|
"type" => "API_ERROR",
|
|
];
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
|
|
$freshResponse = $response;
|
|
|
|
if ($args["third"] == "files") {
|
|
$freshResponse
|
|
->getBody()
|
|
->write(file_get_contents("../config/backups/latest_back.zip"));
|
|
|
|
$freshResponse->withHeader("Content-Type", "application/zip");
|
|
return $freshResponse->withAddedHeader(
|
|
"Content-Disposition",
|
|
"attachment; filename=latest_backup.zip"
|
|
);
|
|
} else {
|
|
$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 "restore":
|
|
case "init":
|
|
$task = $args["third"];
|
|
$result = InitApi::handleInitTasks(
|
|
$task,
|
|
$task == "init" ? $body : $request
|
|
);
|
|
break;
|
|
case "backup":
|
|
$token = $request->getHeader("fipamo-access-token");
|
|
//Verify token for admin tasks
|
|
if (Session::verifyToken($token[0])) {
|
|
$result = SettingsAPI::createBackup();
|
|
} else {
|
|
$result = [
|
|
"message" => "API access denied, homie",
|
|
"type" => "API_ERROR",
|
|
];
|
|
}
|
|
break;
|
|
case "login":
|
|
$result = AuthAPI::login($body);
|
|
break;
|
|
case "logout":
|
|
$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");
|
|
}
|
|
}
|