37243-vm/api/config.php
2026-01-02 22:45:22 +00:00

49 lines
1.3 KiB
PHP

<?php
require_once __DIR__ . '/../db/config.php';
function validateApiKey() {
$headers = getallheaders();
if (!isset($headers['Authorization'])) {
return false;
}
$auth_header = $headers['Authorization'];
if (preg_match('/Bearer\s(\S+)/', $auth_header, $matches)) {
$api_key = $matches[1];
$stmt = db()->prepare("SELECT * FROM api_keys WHERE api_key = ? AND is_active = TRUE");
$stmt->execute([$api_key]);
$key_data = $stmt->fetch();
if ($key_data) {
return true;
}
}
return false;
}
function logWebhook($endpoint, $body, $status_code) {
$headers = getallheaders();
$ip_address = $_SERVER['REMOTE_ADDR'] ?? null;
try {
$stmt = db()->prepare("INSERT INTO webhook_logs (endpoint, request_headers, request_body, response_status, ip_address) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([
$endpoint,
json_encode($headers),
$body,
$status_code,
$ip_address
]);
} catch (PDOException $e) {
error_log("Failed to log webhook request: " . $e->getMessage());
}
}
function sendJsonResponse($data, $statusCode = 200) {
header('Content-Type: application/json');
http_response_code($statusCode);
echo json_encode($data);
exit;
}