File: //home/maintenance.dakarash.co.id/public_html/chat.php
<?php
// Simple mail gateway for maintenance chat (rate-limited, naive)
header('Content-Type: application/json');
// ===== CONFIG =====
$TO_EMAIL = 'info@dakarash.co.id';
$FROM_EMAIL = 'noreply@dakarash.co.id';
$SUBJECT = 'New maintenance chat message';
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['ok'=>false,'error'=>'Method not allowed']);
exit;
}
// Basic rate limit by IP (very naive, 1 message / 10 sec)
$ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
$lock = sys_get_temp_dir() . '/chat_lock_' . preg_replace('/[^a-z0-9\.]/i','_', $ip);
$now = time();
if (file_exists($lock)) {
$last = intval(file_get_contents($lock));
if (($now - $last) < 10) {
http_response_code(429);
echo json_encode(['ok'=>false,'error'=>'Slow down']);
exit;
}
}
file_put_contents($lock, (string)$now);
$message = trim($_POST['message'] ?? '');
$from = trim($_POST['from'] ?? '');
$ua = $_SERVER['HTTP_USER_AGENT'] ?? '';
if ($message === '') {
http_response_code(400);
echo json_encode(['ok'=>false,'error'=>'Empty message']);
exit;
}
$body = "From: {$from}\nIP: {$ip}\nUA: {$ua}\n\nMessage:\n{$message}\n";
$headers = "From: {$FROM_EMAIL}\r\n";
$headers .= "Reply-To: {$FROM_EMAIL}\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
$ok = @mail($TO_EMAIL, $SUBJECT, $body, $headers);
echo json_encode(['ok'=>$ok ? true : false]);