Refactor API structure: update internal handling, enhance input retrieval, and improve template loading
This commit is contained in:
1 parent
a33de15f0c
commit
9cb50d7d28
13 files changed
+181
-95
No files matched your search
+64
-13
@@ -6,13 +6,17 @@ if ($debug) {
|
||||
ini_set("display_startup_errors", 1);
|
||||
error_reporting(E_ALL);
|
||||
}
|
||||
|
||||
debug_print_backtrace();
|
||||
// API Main Library
|
||||
class gbAPI {
|
||||
public $username;
|
||||
public $input;
|
||||
|
||||
private $db;
|
||||
private $config;
|
||||
private $internal;
|
||||
private $response;
|
||||
|
||||
private function b64url_encode($data) {
|
||||
return rtrim(strtr(base64_encode($data), "+/", "-_"), "=");
|
||||
@@ -23,10 +27,13 @@ class gbAPI {
|
||||
}
|
||||
|
||||
function __construct($authRequired, $internal) {
|
||||
define("APP_PATH", __DIR__ . "/../");
|
||||
if (!defined("APP_PATH")) {
|
||||
define("APP_PATH", __DIR__ . "/../");
|
||||
}
|
||||
|
||||
$this->internal = $internal;
|
||||
|
||||
if ($authRequired) {
|
||||
if ($authRequired && !$internal) {
|
||||
if (!$this->checkAuth()) {
|
||||
$status = 401;
|
||||
$data = [
|
||||
@@ -39,6 +46,27 @@ class gbAPI {
|
||||
}
|
||||
}
|
||||
|
||||
function getInput() {
|
||||
if (isset($this->input)) {
|
||||
return $this->input;
|
||||
}elseif ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
try {
|
||||
$input = json_decode(file_get_contents("php://input"), true);
|
||||
} catch (JsonException $error) {
|
||||
$this->answer(400, [
|
||||
"error" => "invalid_body",
|
||||
"error_text" => "Can not read request body: " . $error
|
||||
]);
|
||||
}
|
||||
return $input;
|
||||
}else{
|
||||
$this->answer(400, [
|
||||
"error" => "false_request_method",
|
||||
"error_text" => "Only POST allowed"
|
||||
], true);
|
||||
}
|
||||
}
|
||||
|
||||
function getConfig() {
|
||||
if (isset($this->config)) {
|
||||
return $config;
|
||||
@@ -136,27 +164,49 @@ class gbAPI {
|
||||
}
|
||||
|
||||
function answer($status, $data = [], $error = false) {
|
||||
http_response_code($status);
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
|
||||
|
||||
if ($error) {
|
||||
$json = [];
|
||||
$return = [];
|
||||
|
||||
$json["status"] = $status;
|
||||
$return["status"] = $status;
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
$json[$key] = $value;
|
||||
$return[$key] = $value;
|
||||
}
|
||||
}else{
|
||||
$json = [
|
||||
$return = [
|
||||
"status" => $status,
|
||||
"data" => $data
|
||||
];
|
||||
}
|
||||
|
||||
// Check if request is internal or external
|
||||
if ($this->internal) {
|
||||
$this->response = $return;
|
||||
}else{
|
||||
http_response_code($status);
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
|
||||
$return = json_encode($json);
|
||||
die($return);
|
||||
$return = json_encode($return);
|
||||
die($return);
|
||||
}
|
||||
}
|
||||
|
||||
function getAnswer() {
|
||||
if (!isset($this->response)) {
|
||||
$error = [
|
||||
"status" => 500,
|
||||
"error" => "empty_return",
|
||||
"error_text" => "API Returned nothing."
|
||||
];
|
||||
|
||||
return $error;
|
||||
}
|
||||
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
function getResponse() {
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
function dbCommand($command, $expectResult, $stmtArgsRequired = false, $stmtArgs = []) {
|
||||
@@ -192,6 +242,7 @@ class gbAPI {
|
||||
$command = "SELECT name, value FROM settings WHERE name = :name";
|
||||
|
||||
$value = $this->dbCommand($command, true, true, [":name" => $key]);
|
||||
return $value;
|
||||
|
||||
return $value[0]["value"];
|
||||
}
|
||||
}
|
||||
+3
-2
@@ -2,11 +2,12 @@
|
||||
// Set internal to false so that the answer goes to the client
|
||||
if (!isset($internal)) {
|
||||
$internal = false;
|
||||
define("API_DIR", "../");
|
||||
}
|
||||
|
||||
// Load Main Library
|
||||
require "../api.php";
|
||||
$api = new gbAPI(true);
|
||||
require API_DIR . "api.php";
|
||||
$api = new gbAPI(true, $internal);
|
||||
|
||||
$api->answer(200, [
|
||||
"login" => true,
|
||||
|
||||
+4
-10
@@ -2,22 +2,16 @@
|
||||
// Set internal to false so that the answer goes to the client
|
||||
if (!isset($internal)) {
|
||||
$internal = false;
|
||||
define("API_DIR", "../");
|
||||
}
|
||||
|
||||
// Load API Library
|
||||
require "../api.php";
|
||||
require API_DIR . "api.php";
|
||||
|
||||
$api = new gbAPI(false);
|
||||
$api = new gbAPI(false, $internal);
|
||||
|
||||
// Check Request Body
|
||||
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
|
||||
$api->answer(400, [
|
||||
"error" => "false_request_method",
|
||||
"error_text" => "Only POST allowed"
|
||||
], true);
|
||||
}
|
||||
|
||||
$input = json_decode(file_get_contents("php://input"), true);
|
||||
$input = $api->getInput();
|
||||
|
||||
if (!isset($input["username"]) || !isset($input["password"])) {
|
||||
$api->answer(400, [
|
||||
|
||||
+4
-10
@@ -2,21 +2,15 @@
|
||||
// Set internal to false so that the answer goes to the client
|
||||
if (!isset($internal)) {
|
||||
$internal = false;
|
||||
define("API_DIR", "../");
|
||||
}
|
||||
|
||||
// Load Main Library
|
||||
require "../api.php";
|
||||
require API_DIR . "api.php";
|
||||
|
||||
$api = new gbAPI(false);
|
||||
$api = new gbAPI(false, $internal);
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
|
||||
$api->answer(400, [
|
||||
"error" => "false_request_method",
|
||||
"error_text" => "Only POST allowed"
|
||||
]);
|
||||
}
|
||||
|
||||
$input = json_decode(file_get_contents("php://input"), true);
|
||||
$input = $api->getInput();
|
||||
|
||||
if (!isset($input["name"]) || !isset($input["text"])) {
|
||||
$api->answer(400, [
|
||||
|
||||
+3
-2
@@ -2,12 +2,13 @@
|
||||
// Set internal to false so that the answer goes to the client
|
||||
if (!isset($internal)) {
|
||||
$internal = false;
|
||||
define("API_DIR", "../");
|
||||
}
|
||||
|
||||
// Load Main Library
|
||||
require "../api.php";
|
||||
require API_DIR . "api.php";
|
||||
|
||||
$api = new gbAPI(false);
|
||||
$api = new gbAPI(false, $internal);
|
||||
|
||||
// Get Entrys
|
||||
$command = "SELECT * FROM entrys WHERE status = :status ORDER BY date DESC";
|
||||
|
||||
@@ -2,12 +2,13 @@
|
||||
// Set internal to false so that the answer goes to the client
|
||||
if (!isset($internal)) {
|
||||
$internal = false;
|
||||
define("API_DIR", "../");
|
||||
}
|
||||
|
||||
// Load Main Library
|
||||
require "../api.php";
|
||||
require API_DIR . "api.php";
|
||||
|
||||
$api = new gbAPI(false);
|
||||
$api = new gbAPI(false, $internal);
|
||||
|
||||
// Get App Name
|
||||
$command = "SELECT name, value FROM settings WHERE name = :name";
|
||||
|
||||
+12
-13
@@ -1,18 +1,17 @@
|
||||
<?php
|
||||
// Load Main Libary
|
||||
require "../api.php";
|
||||
|
||||
$api = new gbAPI(false);
|
||||
|
||||
// Check Request body
|
||||
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
|
||||
$api->answer(400, [
|
||||
"error" => "false_request_method",
|
||||
"error_text" => "Only POST allowed"
|
||||
]);
|
||||
// Set internal to false so that the answer goes to the client
|
||||
if (!isset($internal)) {
|
||||
$internal = false;
|
||||
define("API_DIR", "../");
|
||||
}
|
||||
|
||||
$input = json_decode(file_get_contents("php://input"), true);
|
||||
// Load Main Libary
|
||||
require API_DIR . "api.php";
|
||||
|
||||
$api = new gbAPI(false, $internal);
|
||||
|
||||
// Check Request body
|
||||
$input = $api->getInput();
|
||||
|
||||
if (!isset($input["username"]) || !isset($input["password"]) || !isset($input["password_repeat"])) {
|
||||
$api->answer(400, [
|
||||
@@ -94,12 +93,12 @@ $env = "";
|
||||
|
||||
$cookie_token = bin2hex(random_bytes(32));
|
||||
$env .= "COOKIE_TOKEN_SIGNATURE=" . $cookie_token . "\n";
|
||||
$env .= "COOKIE_TOKEN_LIFETIME=86400\n";
|
||||
|
||||
$api_token = bin2hex(random_bytes(32));
|
||||
$env .= "API_TOKEN_SIGNATURE=" . $api_token . "\n";
|
||||
|
||||
$env .= "DB_FILE=database.db";
|
||||
$env .= "COOKIE_TOKEN_LIFETIME=86400";
|
||||
|
||||
file_put_contents(APP_PATH . ".env", $env);
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
// Set internal to false so that the answer goes to the client
|
||||
if (!isset($internal)) {
|
||||
$internal = false;
|
||||
define("API_DIR", "../");
|
||||
}
|
||||
// Load Main Library
|
||||
require API_DIR . "api.php";
|
||||
$api = new gbAPI(false, $internal);
|
||||
|
||||
// Get Template
|
||||
$template = $api->getSetting("template");
|
||||
|
||||
$api->answer(200, [
|
||||
"template_name" => $template
|
||||
]);
|
||||
Reference in new issue
Block a user