fix: HTTP 500 - add missing DB includes to sub-pages, fix env reads, add cURL guards

This commit is contained in:
ssbg04 committed 2026-06-05 23:28:31 +08:00
1 parent 4685a6c3a2
commit b2ceb2f1fb
6 files changed
+22 -7

No files matched your search

+11 -4
View File
@@ -1,9 +1,9 @@
<?php <?php
header('Content-Type: application/json'); header('Content-Type: application/json');
// Start session for simple rate limiting // Start session for simple rate limiting (skip if session already started or headers sent)
if (session_status() === PHP_SESSION_NONE) { if (session_status() === PHP_SESSION_NONE && !headers_sent()) {
session_start(); @session_start();
} }
// Simple rate limiting: max 30 requests per minute // Simple rate limiting: max 30 requests per minute
@@ -25,7 +25,7 @@ $_SESSION['chat_requests'][] = $now;
// Load DB connection which also loads environment variables // Load DB connection which also loads environment variables
require_once __DIR__ . '/../config/db.php'; require_once __DIR__ . '/../config/db.php';
$apiKey = getenv('GEMINI_API_KEY') ?: ($_ENV['GEMINI_API_KEY'] ?? ''); $apiKey = $_ENV['GEMINI_API_KEY'] ?? getenv('GEMINI_API_KEY') ?? '';
if (empty($apiKey)) { if (empty($apiKey)) {
http_response_code(500); http_response_code(500);
echo json_encode(['error' => 'API Key configuration is missing. Please configure GEMINI_API_KEY in your .env file.']); echo json_encode(['error' => 'API Key configuration is missing. Please configure GEMINI_API_KEY in your .env file.']);
@@ -166,6 +166,13 @@ $requestPayload = [
] ]
]; ];
// Guard: cURL must be available
if (!function_exists('curl_init')) {
http_response_code(503);
echo json_encode(['error' => 'cURL is not available on this server. Please use the client-side fallback.']);
exit;
}
// Call Gemini API via cURL // Call Gemini API via cURL
$url = "https://generativelanguage.googleapis.com/v1/models/gemini-3.5-flash:generateContent?key=" . urlencode($apiKey); $url = "https://generativelanguage.googleapis.com/v1/models/gemini-3.5-flash:generateContent?key=" . urlencode($apiKey);
+1
View File
@@ -1,3 +1,4 @@
<?php require_once __DIR__ . '/config/db.php'; ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
+1
View File
@@ -1,3 +1,4 @@
<?php require_once __DIR__ . '/config/db.php'; ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
+1
View File
@@ -1,3 +1,4 @@
<?php require_once __DIR__ . '/config/db.php'; ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
+7 -2
View File
@@ -20,7 +20,7 @@ if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
} }
$clientIp = getClientIp(); $clientIp = getClientIp();
$rateLimit = intval(getenv('RATE_LIMIT') ?: ($_ENV['RATE_LIMIT'] ?? 10)); $rateLimit = intval($_ENV['RATE_LIMIT'] ?? getenv('RATE_LIMIT') ?? 10);
// Query submissions count from this IP address in the last hour // Query submissions count from this IP address in the last hour
$stmt = $mysqli->prepare("SELECT COUNT(*) as count FROM email_submissions WHERE ip_address = ? AND submitted_at >= NOW() - INTERVAL 1 HOUR"); $stmt = $mysqli->prepare("SELECT COUNT(*) as count FROM email_submissions WHERE ip_address = ? AND submitted_at >= NOW() - INTERVAL 1 HOUR");
@@ -48,9 +48,14 @@ if ($logStmt) {
} }
// Forward submission to FormSubmit via cURL // Forward submission to FormSubmit via cURL
$contactEmail = getenv('CONTACT_EMAIL') ?: ($_ENV['CONTACT_EMAIL'] ?? 'crischarlesgarcia345@gmail.com'); $contactEmail = $_ENV['CONTACT_EMAIL'] ?? getenv('CONTACT_EMAIL') ?? 'crischarlesgarcia345@gmail.com';
$url = "https://formsubmit.co/" . $contactEmail; $url = "https://formsubmit.co/" . $contactEmail;
if (!function_exists('curl_init')) {
header("Location: index.php?status=error&msg=cURL is not available on this server.#contact");
exit();
}
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POST, true);
+1 -1
View File
@@ -80,7 +80,7 @@ $systemPrompt .= "3. If asked about contact options or hiring, point them to his
$systemPrompt .= "4. Do not make up facts or external projects not listed in the context. If you don't know the answer, politely invite the user to contact Cris directly.\n"; $systemPrompt .= "4. Do not make up facts or external projects not listed in the context. If you don't know the answer, politely invite the user to contact Cris directly.\n";
$systemPrompt .= "5. Format your output using simple markdown (like bolding or bullet points) to make it readable.\n"; $systemPrompt .= "5. Format your output using simple markdown (like bolding or bullet points) to make it readable.\n";
$apiKey = getenv('GEMINI_API_KEY') ?: ($_ENV['GEMINI_API_KEY'] ?? ''); $apiKey = $_ENV['GEMINI_API_KEY'] ?? getenv('GEMINI_API_KEY') ?? '';
?> ?>
<!-- Pass DB configuration context directly to javascript to allow direct API requests on restricted hosting environments (like InfinityFree) --> <!-- Pass DB configuration context directly to javascript to allow direct API requests on restricted hosting environments (like InfinityFree) -->