Skip to content

Commit

Permalink
Merge pull request #123 from anditv21/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
anditv21 authored Aug 14, 2023
2 parents 2b1bce7 + 3f68893 commit 4f8e007
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 14 deletions.
67 changes: 54 additions & 13 deletions src/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

$API = new ApiController();

// Get the server's IP address
$serverIP = $_SERVER['SERVER_ADDR'];

// Check data

if (isset($_GET['stats'])) {
Expand All @@ -15,23 +18,61 @@
return true;
}

if (empty($_GET['user']) || empty($_GET['pass']) || empty($_GET['hwid']) || empty($_GET['key'])) {
$response = array('status' => 'failed', 'error' => 'Missing arguments');
} else {
$username = Util::securevar($_GET['user']);
$passwordHash = Util::securevar($_GET['pass']);
$hwidHash = Util::securevar($_GET['hwid']);
$key = Util::securevar($_GET['key']);
if (isset($_GET['bot']) && $_GET['bot'] === 'true') {
$allowedIP = $serverIP;

if ($_SERVER['REMOTE_ADDR'] !== $allowedIP) {
$response = array('status' => 'failed', 'error' => 'Unauthorized IP');
} else {
if (empty($_GET['key'])) {
$response = array('status' => 'failed', 'error' => 'Missing key');
} else {
$key = Util::securevar($_GET['key']);

if (BOT_KEY === $key) {
if (isset($_GET['function'])) {
$botFunction = Util::securevar($_GET['function']);

if ($botFunction === 'getbydcid') {
if (isset($_GET['dcid']) && !empty($_GET['dcid'])) {
$dcid = Util::securevar($_GET['dcid']);
$response = $API->getbydcid($dcid);
} else {
$response = array('status' => 'failed', 'error' => "Missing or empty 'discord id' parameter");

if (API_KEY === $key) {
}
} elseif ($botFunction === 'test') {

// decode
$password = base64_decode($passwordHash);
$hwid = base64_decode($hwidHash);
} else {
$response = array('status' => 'failed', 'error' => 'Invalid bot function');
}
} else {
$response = array('status' => 'failed', 'error' => 'Missing bot function');
}

$response = $API->getUserAPI($username, $password, $hwid);
} else {
$response = array('status' => 'failed', 'error' => 'Invalid bot key');
}
}
}
} else {
if (empty($_GET['user']) || empty($_GET['pass']) || empty($_GET['hwid']) || empty($_GET['key'])) {
$response = array('status' => 'failed', 'error' => 'Missing arguments');
} else {
$response = array('status' => 'failed', 'error' => 'Invalid API key');
$username = Util::securevar($_GET['user']);
$passwordHash = Util::securevar($_GET['pass']);
$hwidHash = Util::securevar($_GET['hwid']);
$key = Util::securevar($_GET['key']);

if (API_KEY === $key) {
// decode
$password = base64_decode($passwordHash);
$hwid = base64_decode($hwidHash);

$response = $API->getUserAPI($username, $password, $hwid);
} else {
$response = array('status' => 'failed', 'error' => 'Invalid API key');
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/app/controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ public function getStatsAPI()
{
return $this->statsAPI();
}

public function getbydcid($dcid)
{
return $this->getuserbydiscord($dcid);
}
}
3 changes: 3 additions & 0 deletions src/app/core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@

// API key
define('API_KEY', 'yes');

// Bot API key
define('BOT_KEY', 'yes');
39 changes: 38 additions & 1 deletion src/app/models/ApiModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected function statsAPI()
$sub = $this->statement->rowCount();
} catch (Exception $e) {
$response = [
"status" => "error",
"status" => "failed",
"exception" => $e,
];
}
Expand All @@ -110,4 +110,41 @@ protected function statsAPI()
];
return $response;
}

protected function getuserbydiscord($dcid)
{
try {
$this->prepare("SELECT `username`, `displayname`, `banned`, `admin`, `supp` FROM `users` WHERE `dcid` = ?");
$this->statement->execute([$dcid]);
$result = $this->statement->fetch(PDO::FETCH_ASSOC);

if (!$result) {
$response = [
"status" => "failed",
"error" => "No user with the provided discord id was found"
];
} else {
$username = $result['username'] ?? '';
$displayname = $result['displayname'] ?? '';
$banned = $result['banned'] ?? '';
$admin = $result['admin'] ?? '';
$supp = $result['supp'] ?? '';

$response = [
"username" => $username,
"display_name" => $displayname,
"banned" => $banned,
"admin" => $admin,
"supp" => $supp
];
}
} catch (Exception $e) {
$response = [
"status" => "failed",
"error" => $e->getMessage()
];
}
return $response;
}
}

0 comments on commit 4f8e007

Please sign in to comment.