<?php
// web-test.php

// Load .env manually
$envPath = __DIR__ . '/.env';
if (!file_exists($envPath)) {
    die("Error: .env file not found at $envPath");
}

$lines = file($envPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
    if (strpos(trim($line), '#') === 0) continue;
    [$key, $value] = array_map('trim', explode('=', $line, 2));
    $_ENV[$key] = $value;
}

// Token check (basic auth layer)
$providedToken = $_GET['token'] ?? '';
$allowedToken = '12345'; // You can set your own here

if ($providedToken !== $allowedToken) {
    http_response_code(403);
    die("Forbidden: invalid token.");
}

// Database connection
$conn = new mysqli($_ENV['DB_HOST'], $_ENV['DB_USER'], $_ENV['DB_PASS'], $_ENV['DB_NAME']);
if ($conn->connect_error) {
    die("Database connection failed: " . $conn->connect_error);
}

echo "<h3>✅ Database connected successfully!</h3>";
echo "<p>Token received: " . htmlspecialchars($providedToken) . "</p>";

// Optional: log to file
$logPath = $_ENV['LOG_PATH'] . '/test.log';
file_put_contents($logPath, "[" . date('Y-m-d H:i:s') . "] Test successful for token $providedToken\n", FILE_APPEND);

$conn->close();
?>
