This commit is contained in:
ssbg04 committed 2026-06-06 09:17:54 +08:00
1 parent 508cf6a748
commit c8f3e7059d
2 files changed
+63 -4

No files matched your search

+28 -4
View File
@@ -33,7 +33,7 @@ $contactEmail = $_ENV['CONTACT_EMAIL'] ?? getenv('CONTACT_EMAIL')
<title>Cris Charles Garcia | Software Developer</title> <title>Cris Charles Garcia | Software Developer</title>
<link rel="icon" type="image/svg+xml" href="assets/img/favicon.svg"> <link rel="icon" type="image/svg+xml" href="assets/img/favicon.svg">
<link rel="stylesheet" href="assets/css/style.css?v=2.0.1"> <link rel="stylesheet" href="assets/css/style.css?v=2.0.1">
<script src="https://www.google.com/recaptcha/api.js" async defer></script> <script src="https://www.google.com/recaptcha/enterprise.js?render=<?php echo htmlspecialchars($recaptchaSiteKey); ?>"></script>
</head> </head>
<body> <body>
@@ -208,14 +208,38 @@ $contactEmail = $_ENV['CONTACT_EMAIL'] ?? getenv('CONTACT_EMAIL')
<form action="send_contact.php" method="POST" class="email-form" id="contact-form"> <form action="send_contact.php" method="POST" class="email-form" id="contact-form">
<input type="hidden" name="_captcha" value="false"> <input type="hidden" name="_captcha" value="false">
<input type="hidden" name="g-recaptcha-response" id="g-recaptcha-response">
<input type="text" name="name" id="form-name" placeholder="Name" required> <input type="text" name="name" id="form-name" placeholder="Name" required>
<input type="text" name="subject" id="form-subject" placeholder="Subject" required> <input type="text" name="subject" id="form-subject" placeholder="Subject" required>
<textarea name="message" id="form-message" rows="5" placeholder="Message" required></textarea> <textarea name="message" id="form-message" rows="5" placeholder="Message" required></textarea>
<?php if ($recaptchaSiteKey): ?>
<div class="g-recaptcha" data-sitekey="<?php echo htmlspecialchars($recaptchaSiteKey); ?>"></div>
<?php endif; ?>
<button type="submit" class="btn primary full-width" id="btn-send-message">Send Message</button> <button type="submit" class="btn primary full-width" id="btn-send-message">Send Message</button>
</form> </form>
<?php if ($recaptchaSiteKey): ?>
<script>
const contactForm = document.getElementById('contact-form');
contactForm.addEventListener('submit', function(e) {
e.preventDefault();
const btn = document.getElementById('btn-send-message');
btn.textContent = 'Verifying...';
btn.disabled = true;
grecaptcha.enterprise.ready(async () => {
try {
const token = await grecaptcha.enterprise.execute('<?php echo htmlspecialchars($recaptchaSiteKey); ?>', {action: 'LOGIN'});
document.getElementById('g-recaptcha-response').value = token;
contactForm.submit();
} catch (err) {
console.error('reCAPTCHA error:', err);
alert('reCAPTCHA failed to load. Please try again.');
btn.textContent = 'Send Message';
btn.disabled = false;
}
});
});
</script>
<?php endif; ?>
</div> </div>
</section> </section>
+35
View File
@@ -19,6 +19,41 @@ if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
exit(); exit();
} }
// -------------------------------------------------------------
// Verify Google reCAPTCHA
// -------------------------------------------------------------
$recaptchaSecret = $_ENV['RECAPTCHA_SECRET_KEY'] ?? getenv('RECAPTCHA_SECRET_KEY');
$recaptchaResponse = $_POST['g-recaptcha-response'] ?? '';
if (empty($recaptchaResponse)) {
header("Location: index.php?status=error&msg=Please complete the reCAPTCHA verification.#contact");
exit();
}
// Use native PHP to verify the token (No Composer required!)
$verifyUrl = 'https://www.google.com/recaptcha/api/siteverify';
$verifyData = [
'secret' => $recaptchaSecret,
'response' => $recaptchaResponse,
'remoteip' => getClientIp()
];
$options = [
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($verifyData)
]
];
$context = stream_context_create($options);
$verifyResult = file_get_contents($verifyUrl, false, $context);
$responseData = json_decode($verifyResult);
if (!$responseData || !$responseData->success) {
header("Location: index.php?status=error&msg=reCAPTCHA verification failed. Please try again.#contact");
exit();
}
$clientIp = getClientIp(); $clientIp = getClientIp();
$rateLimit = intval($_ENV['RATE_LIMIT'] ?? getenv('RATE_LIMIT') ?? 10); $rateLimit = intval($_ENV['RATE_LIMIT'] ?? getenv('RATE_LIMIT') ?? 10);