Add API keys and server list api
This commit is contained in:
51
main.php
51
main.php
@@ -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];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user