| Server IP : 65.1.209.187 / Your IP : 216.73.216.144 Web Server : nginx/1.24.0 System : Linux ip-172-31-5-206 6.14.0-1015-aws #15~24.04.1-Ubuntu SMP Tue Sep 23 22:44:48 UTC 2025 x86_64 User : ( 1001) PHP Version : 8.3.6 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/drjunior_wordpress/wp-content/themes/drjunior/ |
Upload File : |
<?php
/**
* AJAX Form Handler for Dr. Junior Theme
* With Enhanced Debugging
*/
// Enable error logging
ini_set('display_errors', 0); // Don't display errors to users
ini_set('log_errors', 1);
$debug_log_file = __DIR__ . '/ajax-form-debug.log';
ini_set('error_log', $debug_log_file);
// Debug mode - set to true to get detailed logs
define('DEBUG_MODE', true);
// Custom debug logging function
function debug_log($message, $data = null) {
if (DEBUG_MODE) {
$log_entry = date('[Y-m-d H:i:s] ') . $message;
if ($data !== null) {
$log_entry .= ' | Data: ' . print_r($data, true);
}
$log_entry .= "\n";
error_log($log_entry, 3, $GLOBALS['debug_log_file']);
}
}
debug_log('=== AJAX Form Handler Started ===');
debug_log('Request Method', $_SERVER['REQUEST_METHOD']);
debug_log('POST Data Received', $_POST);
// Load WordPress
try {
debug_log('Attempting to load WordPress...');
require_once('../../../wp-load.php');
debug_log('WordPress loaded successfully');
} catch (Exception $e) {
debug_log('ERROR: Failed to load WordPress', $e->getMessage());
header('Content-Type: application/json');
echo json_encode([
'success' => false,
'message' => 'Server configuration error.',
'debug' => DEBUG_MODE ? 'Failed to load WordPress: ' . $e->getMessage() : null
]);
exit;
}
header('Content-Type: application/json');
// Get POST data
debug_log('Sanitizing POST data...');
$name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
$email = isset($_POST['email']) ? sanitize_email($_POST['email']) : '';
$phone = isset($_POST['phone']) ? sanitize_text_field($_POST['phone']) : '';
$location = isset($_POST['location']) ? sanitize_text_field($_POST['location']) : '';
$comment = isset($_POST['comment']) ? sanitize_textarea_field($_POST['comment']) : '';
$token = isset($_POST['token']) ? sanitize_text_field($_POST['token']) : '';
$action = isset($_POST['action']) ? sanitize_text_field($_POST['action']) : '';
debug_log('Sanitized Data', [
'name' => $name,
'email' => $email,
'phone' => $phone,
'location' => $location,
'comment' => substr($comment, 0, 50) . '...',
'token' => substr($token, 0, 20) . '...',
'action' => $action
]);
// Email configuration
$email_from = "mailer@drjunior.in";
$common_mail = "support@drjunior.in";
define("RECAPTCHA_V3_SECRET_KEY", '6LcHEKwqAAAAAGTIBEewYOvJznYBFz35kv2RMUYJ');
debug_log('Email Configuration', [
'from' => $email_from,
'to' => $common_mail
]);
// Validate required fields
debug_log('Validating required fields...');
$missing_fields = [];
if (empty($name)) $missing_fields[] = 'name';
if (empty($email)) $missing_fields[] = 'email';
if (empty($phone)) $missing_fields[] = 'phone';
if (empty($location)) $missing_fields[] = 'location';
if (!empty($missing_fields)) {
debug_log('Validation failed - Missing fields', $missing_fields);
echo json_encode([
'success' => false,
'message' => 'Please fill in all required fields.',
'debug' => DEBUG_MODE ? 'Missing fields: ' . implode(', ', $missing_fields) : null
]);
exit;
}
debug_log('All required fields validated successfully');
// Verify reCAPTCHA
debug_log('Starting reCAPTCHA verification...');
// Check if cURL is available
if (!function_exists('curl_init')) {
debug_log('ERROR: cURL is not available on this server');
echo json_encode([
'success' => false,
'message' => 'Server configuration error.',
'debug' => DEBUG_MODE ? 'cURL extension is not installed' : null
]);
exit;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'secret' => RECAPTCHA_V3_SECRET_KEY,
'response' => $token
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
$curl_error = curl_error($ch);
$curl_errno = curl_errno($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
debug_log('cURL Request completed', [
'http_code' => $http_code,
'curl_errno' => $curl_errno,
'curl_error' => $curl_error,
'response' => $response
]);
if ($curl_errno) {
debug_log('ERROR: cURL failed', [
'errno' => $curl_errno,
'error' => $curl_error
]);
echo json_encode([
'success' => false,
'message' => 'Network error occurred.',
'debug' => DEBUG_MODE ? "cURL Error: $curl_error" : null
]);
exit;
}
$arrResponse = json_decode($response, true);
debug_log('reCAPTCHA Response decoded', $arrResponse);
// Check reCAPTCHA validation
debug_log('Validating reCAPTCHA response...');
if (!isset($arrResponse["success"])) {
debug_log('ERROR: Invalid reCAPTCHA response format', $arrResponse);
echo json_encode([
'success' => false,
'message' => 'Security verification error.',
'debug' => DEBUG_MODE ? 'Invalid reCAPTCHA response format' : null
]);
exit;
}
$recaptcha_score = isset($arrResponse["score"]) ? $arrResponse["score"] : 0;
debug_log('reCAPTCHA Validation Result', [
'success' => $arrResponse["success"],
'score' => $recaptcha_score,
'action' => isset($arrResponse["action"]) ? $arrResponse["action"] : 'N/A',
'challenge_ts' => isset($arrResponse["challenge_ts"]) ? $arrResponse["challenge_ts"] : 'N/A',
'hostname' => isset($arrResponse["hostname"]) ? $arrResponse["hostname"] : 'N/A',
'error-codes' => isset($arrResponse["error-codes"]) ? $arrResponse["error-codes"] : []
]);
if ($arrResponse["success"] == '1' && $recaptcha_score >= 0.5) {
debug_log('reCAPTCHA validation passed - Proceeding to send email');
// Log server mail configuration
debug_log('Server Mail Configuration', [
'mail_function_exists' => function_exists('mail'),
'wp_mail_function_exists' => function_exists('wp_mail'),
'sendmail_path' => ini_get('sendmail_path'),
'SMTP' => ini_get('SMTP'),
'smtp_port' => ini_get('smtp_port'),
'sendmail_from' => ini_get('sendmail_from')
]);
// Valid submission - send email
$subject = "Enquiry from $name - Dr. Junior";
$mailto = $common_mail;
debug_log('Preparing email', [
'to' => $mailto,
'subject' => $subject,
'from' => $email_from
]);
// Prepare email headers for both wp_mail and mail()
$headers_array = [
'Content-Type: text/html; charset=UTF-8',
'From: ' . $name . ' <' . $email_from . '>',
'Reply-To: ' . $name . ' <' . $email . '>'
];
debug_log('Email headers prepared', $headers_array);
// Email message
$message = '<table style="width:60%;padding:1%; height:auto; margin:0 auto;background:#008da6;">
<tr>
<td colspan="2" style="width:50%;height:63px; line-height:63px; text-align:center; font-family:Arial, Helvetica,sans-serif; font-size:18px; font-weight:bold; color:#fff;text-transform:uppercase">Enquiry - Dr. Junior</td>
</tr>
<tr>
<td style="background-color:#fff;width:50%;height:63px; line-height:63px; text-align:center; font-family:Arial, Helvetica,sans-serif; font-size:14px; font-weight:bold; color:#474747">Name:</td>
<td style="background-color:#fff;width:50%;height:63px; line-height:63px; text-align:center; font-family:Arial, Helvetica,sans-serif; font-size:14px; font-weight:bold; color:#474747">' . $name . '</td>
</tr>
<tr>
<td style="background-color:#fff;width:50%;height:63px; line-height:63px; text-align:center; font-family:Arial, Helvetica,sans-serif; font-size:14px; font-weight:bold; color:#474747">Email:</td>
<td style="background-color:#fff;width:50%;height:63px; line-height:63px; text-align:center; font-family:Arial, Helvetica,sans-serif; font-size:14px; font-weight:bold; color:#474747">' . $email . '</td>
</tr>
<tr>
<td style="background-color:#fff;width:50%;height:63px; line-height:63px; text-align:center; font-family:Arial, Helvetica,sans-serif; font-size:14px; font-weight:bold; color:#474747">Phone:</td>
<td style="background-color:#fff;width:50%;height:63px; line-height:63px; text-align:center; font-family:Arial, Helvetica,sans-serif; font-size:14px; font-weight:bold; color:#474747">' . $phone . '</td>
</tr>
<tr>
<td style="background-color:#fff;width:50%;height:63px; line-height:63px; text-align:center; font-family:Arial, Helvetica,sans-serif; font-size:14px; font-weight:bold; color:#474747">Location:</td>
<td style="background-color:#fff;width:50%;height:63px; line-height:63px; text-align:center; font-family:Arial, Helvetica,sans-serif; font-size:14px; font-weight:bold; color:#474747">' . $location . '</td>
</tr>
<tr>
<td style="background-color:#fff;width:50%;height:63px;text-align:center; font-family:Arial, Helvetica,sans-serif; font-size:14px; font-weight:bold; color:#474747">Comment:</td>
<td style="background-color:#fff;width:50%;height:63px; text-align:center; font-family:Arial, Helvetica,sans-serif; font-size:14px; font-weight:bold; color:#474747">' . $comment . '</td>
</tr>
</table>';
debug_log('Email message prepared', [
'message_length' => strlen($message)
]);
// Try sending email with WordPress wp_mail first (more reliable)
$mail_sent = false;
$mail_method = '';
$mail_errors = [];
if (function_exists('wp_mail')) {
debug_log('Attempting to send email using wp_mail()...');
// Add filter to capture wp_mail errors
add_action('wp_mail_failed', function($wp_error) use (&$mail_errors) {
$mail_errors = [
'error_code' => $wp_error->get_error_code(),
'error_message' => $wp_error->get_error_message(),
'error_data' => $wp_error->get_error_data()
];
debug_log('wp_mail failed', $mail_errors);
});
// Set error handler to catch any PHP errors
set_error_handler(function($errno, $errstr, $errfile, $errline) {
debug_log('PHP Error during wp_mail', [
'errno' => $errno,
'errstr' => $errstr,
'file' => $errfile,
'line' => $errline
]);
});
try {
$mail_sent = wp_mail($mailto, $subject, $message, $headers_array);
$mail_method = 'wp_mail';
debug_log('wp_mail attempt completed', [
'result' => $mail_sent ? 'SUCCESS' : 'FAILED',
'method' => 'wp_mail'
]);
} catch (Exception $e) {
debug_log('wp_mail threw exception', [
'message' => $e->getMessage(),
'trace' => $e->getTraceAsString()
]);
}
restore_error_handler();
}
// Fallback to PHP mail() if wp_mail failed or doesn't exist
if (!$mail_sent && function_exists('mail')) {
debug_log('wp_mail failed or unavailable, trying PHP mail()...');
// Convert headers array to string for mail()
$headers_string = implode("\r\n", $headers_array);
// Set error handler to catch mail errors
set_error_handler(function($errno, $errstr, $errfile, $errline) use (&$mail_errors) {
$mail_errors = [
'errno' => $errno,
'errstr' => $errstr,
'file' => $errfile,
'line' => $errline
];
debug_log('PHP Error during mail()', $mail_errors);
});
try {
$mail_sent = mail($mailto, $subject, $message, $headers_string);
$mail_method = 'mail';
debug_log('mail() attempt completed', [
'result' => $mail_sent ? 'SUCCESS' : 'FAILED',
'method' => 'mail',
'last_error' => error_get_last()
]);
} catch (Exception $e) {
debug_log('mail() threw exception', [
'message' => $e->getMessage(),
'trace' => $e->getTraceAsString()
]);
}
restore_error_handler();
}
// Check result
if ($mail_sent) {
debug_log('=== Email sent successfully ===', [
'method' => $mail_method,
'to' => $mailto,
'subject' => $subject
]);
echo json_encode([
'success' => true,
'message' => 'Thank you for your enquiry! We will get back to you soon.',
'debug' => DEBUG_MODE ? [
'recaptcha_score' => $recaptcha_score,
'mail_sent' => true,
'mail_method' => $mail_method
] : null
]);
} else {
debug_log('ERROR: All email sending methods failed', [
'wp_mail_available' => function_exists('wp_mail'),
'mail_available' => function_exists('mail'),
'errors' => $mail_errors,
'sendmail_path' => ini_get('sendmail_path'),
'last_php_error' => error_get_last()
]);
// Build detailed error message for debugging
$debug_message = 'Email sending failed. ';
if (!empty($mail_errors)) {
if (isset($mail_errors['error_message'])) {
$debug_message .= 'Error: ' . $mail_errors['error_message'];
} elseif (isset($mail_errors['errstr'])) {
$debug_message .= 'Error: ' . $mail_errors['errstr'];
}
}
// Check for common issues
$sendmail_path = ini_get('sendmail_path');
if (empty($sendmail_path) || $sendmail_path === 'null') {
$debug_message .= ' | sendmail_path not configured in php.ini';
}
echo json_encode([
'success' => false,
'message' => 'Sorry, there was an error sending your message. Please try again.',
'debug' => DEBUG_MODE ? $debug_message : null
]);
}
} else {
// reCAPTCHA validation failed
debug_log('reCAPTCHA validation FAILED', [
'success' => $arrResponse["success"],
'score' => $recaptcha_score,
'required_score' => 0.5,
'error-codes' => isset($arrResponse["error-codes"]) ? $arrResponse["error-codes"] : []
]);
$debug_info = null;
if (DEBUG_MODE) {
$debug_info = 'reCAPTCHA failed: ';
if ($arrResponse["success"] != '1') {
$debug_info .= 'Verification failed. ';
}
if ($recaptcha_score < 0.5) {
$debug_info .= "Score too low ($recaptcha_score < 0.5). ";
}
if (isset($arrResponse["error-codes"])) {
$debug_info .= 'Errors: ' . implode(', ', $arrResponse["error-codes"]);
}
}
echo json_encode([
'success' => false,
'message' => 'Security verification failed. Please try again.',
'debug' => $debug_info
]);
}
debug_log('=== AJAX Form Handler Completed ===');
debug_log('');
exit;
?>