Add API keys and server list api

This commit is contained in:
marc-go
2026-02-18 15:05:34 +01:00
parent 75899064c5
commit 4b4a46225f
5 changed files with 118 additions and 2 deletions

View File

@@ -15,6 +15,8 @@ ini_set("display_startup_errors", 1);
error_reporting(E_ALL);
class mcServ {
private $api_keys;
public function checkConf() {
if (!file_exists("../mcServ.db")) {
return false;
@@ -64,4 +66,53 @@ class mcServ {
return $hash;
}
public function checkApiAuth() {
if ($this->checkLogin()) {
return true;
}elseif (isset($_SERVER["HTTP_X_API_KEY"])) {
$key = $_SERVER["HTTP_X_API_KEY"];
$db = $this->getDB();
$sql = "SELECT * FROM api_keys WHERE key = :key";
$stmt = $db->prepare($sql);
$stmt->execute([":key" => $key]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if ($result) {
$user_id = $result["user_id"];
$sql = "SELECT username FROM users WHERE id = :id";
$stmt = $db->prepare($sql);
$stmt->execute([":id" => $user_id]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$username = $result["username"];
$this->api_keys[$key] = $username;
return true;
}else{
return false;
}
}else{
return false;
}
}
public function getUser() {
if ($this->checkLogin()) {
return $_COOKIE["username"];
}elseif (isset($_SERVER["HTTP_X_API_KEY"])) {
$key = $_SERVER["HTTP_X_API_KEY"];
if (isset($this->api_keys[$key])) {
return $this->api_keys[$key];
}
}
}
}