<?php
header("Content-Type: application/json; charset=UTF-8");
require_once "config.php";

$key        = $_GET['key'] ?? '';
$PersonalKey = $_GET['PersonalKey'] ?? '';
$Email      = $_GET['Email'] ?? '';
$Username   = $_GET['Username'] ?? '';
$Password   = $_GET['Password'] ?? '';

// Basic Access Check
if (empty($key) && empty($PersonalKey) && empty($Email) && empty($Username)) {
    echo "Access Denied";
    exit;
}

// Query Conditions
if ($key === 'zabbar') {
    $stmt = $conn->prepare("SELECT * FROM ZabbarUser");
} 
elseif (!empty($PersonalKey)) {
    $stmt = $conn->prepare("SELECT * FROM ZabbarUser WHERE PersonalKey = ?");
    $stmt->bind_param("s", $PersonalKey);
} 
elseif (!empty($Email) && !empty($Password)) {
    $stmt = $conn->prepare("SELECT * FROM ZabbarUser WHERE Email = ? AND Password = ?");
    $stmt->bind_param("ss", $Email, $Password);
} 
elseif (!empty($Username) && !empty($Password)) {
    $stmt = $conn->prepare("SELECT * FROM ZabbarUser WHERE Username = ? AND Password = ?");
    $stmt->bind_param("ss", $Username, $Password);
} 
else {
    echo "Access Denied";
    exit;
}

// Execute
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_all(MYSQLI_ASSOC);

// Output
if (empty($data)) {
    echo "invalid connection";
} elseif (count($data) == 1) {
    echo json_encode($data[0], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
} else {
    echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}

$stmt->close();
$conn->close();
?>