Skip to content

Commit

Permalink
PUSH
Browse files Browse the repository at this point in the history
-> Started a api rewrite
-> Now using headers for api
  • Loading branch information
NaysKutzu committed Oct 19, 2023
1 parent a6e9a9c commit ad78108
Show file tree
Hide file tree
Showing 21 changed files with 268 additions and 115 deletions.
62 changes: 28 additions & 34 deletions api/admin/base.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
<?php
if (isset($_GET['api_key'])) {
if (!$_GET['api_key'] == "") {
$api_key = mysqli_real_escape_string($conn,$_GET['api_key']);
$query = "SELECT * FROM mythicaldash_apikeys WHERE `skey` = '$api_key'";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
//CONTINUE CODE HERE

} else {
$rsp = array(
"code" => 403,
"error" => "The server understood the request, but it refuses to authorize it.",
"message" => "Im sorry but the api key is wrong"
);
http_response_code(403);
die(json_encode($rsp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
<?php
include(__DIR__ . "/../base.php");
$headers = getallheaders();

if (isset($headers['Authorization']) && !$headers['Authorization'] == "") {
$authorizationHeader = $headers['Authorization'];
$api_key = mysqli_real_escape_string($conn, $authorizationHeader);
$query = "SELECT * FROM mythicaldash_apikeys WHERE `skey` = '$api_key'";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
//CONTINUE CODE HERE

} else {
$rsp = array(
"code" => 400,
"error" => "The server cannot understand the request due to a client error.",
"message" => "Please provide an api key"
);
http_response_code(400);
die(json_encode($rsp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
$response = [
"code" => 403,
"error" => "Unauthorized",
"message" => "Please make sure your API key is valid."
];
http_response_code(403);
die(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
} else {
$response = [
"code" => 401,
"error" => "Authentication required",
"message" => "Please provide your API key."
];
http_response_code(401);
die(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
else {
$rsp = array(
"code" => 400,
"error" => "The server cannot understand the request due to a client error.",
"message" => "Please provide an api key"
);
http_response_code(400);
die(json_encode($rsp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}

?>
76 changes: 56 additions & 20 deletions api/admin/statistics.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,61 @@
<?php
include(__DIR__ . '/../base.php');
include(__DIR__ . '/base.php');
include("base.php");
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
try {
$userCountQuery = "SELECT COUNT(*) AS user_count FROM mythicaldash_users";
$userCountResult = $conn->query($userCountQuery);
$userCount = $userCountResult->fetch_assoc()['user_count'];

$userCountQuery = "SELECT COUNT(*) AS user_count FROM mythicaldash_users";
$userCountResult = $conn->query($userCountQuery);
$userCount = $userCountResult->fetch_assoc()['user_count'];
$ticketCountQuery = "SELECT COUNT(*) AS ticket_count FROM mythicaldash_tickets";
$ticketCountResult = $conn->query($ticketCountQuery);
$ticketCount = $ticketCountResult->fetch_assoc()['ticket_count'];

$ticketCountQuery = "SELECT COUNT(*) AS ticket_count FROM mythicaldash_tickets";
$ticketCountResult = $conn->query($ticketCountQuery);
$ticketCount = $ticketCountResult->fetch_assoc()['ticket_count'];
$serverCountQuery = "SELECT COUNT(*) AS servers FROM mythicaldash_servers";
$serverCountResult = $conn->query($serverCountQuery);
$serverCount = $serverCountResult->fetch_assoc()['servers'];

$rsp = array(
"code" => 200,
"error" => null,
"message" => "Sure here you go:",
"statistics" => array(
"users" => $userCount,
"tickets" => $ticketCount
)
);
http_response_code(200);
$conn->close();
die(json_encode($rsp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
$serverQueueQuery = "SELECT COUNT(*) AS serversq FROM mythicaldash_servers_queue";
$serverQueueCountResult = $conn->query($serverQueueQuery);
$serverQueueCount = $serverQueueCountResult->fetch_assoc()['serversq'];

$locationsQuery = "SELECT COUNT(*) AS locations FROM mythicaldash_locations";
$locationsCountResult = $conn->query($locationsQuery);
$locationsCount = $locationsCountResult->fetch_assoc()['locations'];

$eggsQuery = "SELECT COUNT(*) AS eggs FROM mythicaldash_eggs";
$eggsCountResult = $conn->query($eggsQuery);
$eggCount = $eggsCountResult->fetch_assoc()['eggs'];
$rsp = array(
"code" => 200,
"error" => null,
"message" => null,
"data" => array(
"users" => $userCount,
"tickets" => $ticketCount,
"servers" => $serverCount,
"servers_queue" => $serverQueueCount,
"locations" => $locationsCount,
"eggs" => $eggCount,
)
);
http_response_code(200);
die(json_encode($rsp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
} catch (Exception $e) {
$rsp = array(
"code" => 500,
"error" => "The server encountered a situation it doesn't know how to handle.",
"message" => "We are sorry, but our server can't handle this request. Please do not try again!"
);
http_response_code(500);
die(json_encode($rsp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
} else {
$rsp = array(
"code" => 405,
"error" => "A request was made of a page using a request method not supported by that page",
"message" => "Please use a get request"
);
http_response_code(405);
die(json_encode($rsp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
?>
11 changes: 10 additions & 1 deletion api/base.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<?php
header('Content-type: application/json');
ini_set("display_errors", 0);

ini_set("display_startup_errors", 0);
if (!is_writable(__DIR__)) {
http_response_code(500);
$rsp = array(
"code" => 500,
"error" => "The server is not ready to handle the request.",
"message" => "We have no write permission for our home directory. Please update the permission by executing this in the server shell: chown -R www-data:www-data /var/www/client/ && chown -R www-data:www-data /var/www/client/*"
);
die(json_encode($rsp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}

?>
38 changes: 2 additions & 36 deletions api/client/base.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,3 @@
<?php
if (isset($_GET['api_key'])) {
if (!$_GET['api_key'] == "") {
$api_key = mysqli_real_escape_string($conn, $_GET['api_key']);
$query = "SELECT * FROM mythicaldash_users WHERE `api_key` = '$api_key'";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
//CONTINUE CODE HERE

} else {
$rsp = array(
"code" => 403,
"error" => "The server understood the request, but it refuses to authorize it.",
"message" => "Im sorry but the api key is wrong"
);
http_response_code(403);
die(json_encode($rsp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
} else {
$rsp = array(
"code" => 400,
"error" => "The server cannot understand the request due to a client error.",
"message" => "Please provide an api key"
);
http_response_code(400);
die(json_encode($rsp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
} else {
$rsp = array(
"code" => 400,
"error" => "The server cannot understand the request due to a client error.",
"message" => "Please provide an api key"
);
http_response_code(400);
die(json_encode($rsp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
<?php
include(__DIR__."/../base.php");
?>
5 changes: 0 additions & 5 deletions api/info.php

This file was deleted.

20 changes: 20 additions & 0 deletions api/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
include('base.php');
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$rsp = array(
"code" => 200,
"error" => null,
"message" => "Hi, and welcome to MythicalDash main api this is the main path of our API. Make sure to check our docs for the requests you can make!"
);
http_response_code(200);
die(json_encode($rsp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
} else {
$rsp = array(
"code" => 405,
"error" => "A request was made of a page using a request method not supported by that page",
"message" => "Please use a get request"
);
http_response_code(405);
die(json_encode($rsp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
?>
38 changes: 38 additions & 0 deletions oldapi/admin/base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
if (isset($_GET['api_key'])) {
if (!$_GET['api_key'] == "") {
$api_key = mysqli_real_escape_string($conn,$_GET['api_key']);
$query = "SELECT * FROM mythicaldash_apikeys WHERE `skey` = '$api_key'";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
//CONTINUE CODE HERE

} else {
$rsp = array(
"code" => 403,
"error" => "The server understood the request, but it refuses to authorize it.",
"message" => "Im sorry but the api key is wrong"
);
http_response_code(403);
die(json_encode($rsp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
} else {
$rsp = array(
"code" => 400,
"error" => "The server cannot understand the request due to a client error.",
"message" => "Please provide an api key"
);
http_response_code(400);
die(json_encode($rsp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
}
else {
$rsp = array(
"code" => 400,
"error" => "The server cannot understand the request due to a client error.",
"message" => "Please provide an api key"
);
http_response_code(400);
die(json_encode($rsp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
?>
45 changes: 45 additions & 0 deletions oldapi/admin/statistics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
include(__DIR__ . '/../base.php');
include(__DIR__ . '/base.php');

$userCountQuery = "SELECT COUNT(*) AS user_count FROM mythicaldash_users";
$userCountResult = $conn->query($userCountQuery);
$userCount = $userCountResult->fetch_assoc()['user_count'];

$ticketCountQuery = "SELECT COUNT(*) AS ticket_count FROM mythicaldash_tickets";
$ticketCountResult = $conn->query($ticketCountQuery);
$ticketCount = $ticketCountResult->fetch_assoc()['ticket_count'];

$Servers = "SELECT COUNT(*) AS servers FROM mythicaldash_servers";
$serverCountResult = $conn->query($Servers);
$serverCount = $serverCountResult->fetch_assoc()['servers'];

$servers_queue = "SELECT COUNT(*) AS serversq FROM mythicaldash_servers_queue";
$serverqCountResult = $conn->query($servers_queue);
$servers_queueCount = $serverqCountResult->fetch_assoc()['serversq'];

$locations = "SELECT COUNT(*) AS locations FROM mythicaldash_locations";
$locationsCountResult = $conn->query($locations);
$locationsCount = $locationsCountResult->fetch_assoc()['locations'];

$eggs = "SELECT COUNT(*) AS eggs FROM mythicaldash_eggs";
$eggsCountResult = $conn->query($eggs);
$eggCount = $eggsCountResult->fetch_assoc()['eggs'];

$rsp = array(
"code" => 200,
"error" => null,
"message" => null,
"statistics" => array(
"servers" => $serverCount,
"servers_queue" => $servers_queueCount,
"eggs" => $eggCount,
"locations" => $locationsCount,
"users" => $userCount,
"tickets" => $ticketCount,
)
);
http_response_code(200);
$conn->close();
die(json_encode($rsp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
?>
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions oldapi/base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
header('Content-type: application/json');
ini_set("display_errors", 0);


?>
37 changes: 37 additions & 0 deletions oldapi/client/base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
if (isset($_GET['api_key'])) {
if (!$_GET['api_key'] == "") {
$api_key = mysqli_real_escape_string($conn, $_GET['api_key']);
$query = "SELECT * FROM mythicaldash_users WHERE `api_key` = '$api_key'";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
//CONTINUE CODE HERE

} else {
$rsp = array(
"code" => 403,
"error" => "The server understood the request, but it refuses to authorize it.",
"message" => "Im sorry but the api key is wrong"
);
http_response_code(403);
die(json_encode($rsp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
} else {
$rsp = array(
"code" => 400,
"error" => "The server cannot understand the request due to a client error.",
"message" => "Please provide an api key"
);
http_response_code(400);
die(json_encode($rsp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
} else {
$rsp = array(
"code" => 400,
"error" => "The server cannot understand the request due to a client error.",
"message" => "Please provide an api key"
);
http_response_code(400);
die(json_encode($rsp, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
?>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions routes/api/admin/index.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

$router->add("/api/admin/statistics", function () {
require("../include/main.php");
require("../api/admin/statistics.php");
});
//$router->add("/api/admin/statistics", function () {
// require("../include/main.php");
// require("../api/admin/statistics.php");
//});
?>
Loading

0 comments on commit ad78108

Please sign in to comment.