171 lines
6.1 KiB
PHP
171 lines
6.1 KiB
PHP
<?php
|
|
/**
|
|
* WhatsApp Gateway Webhook Receiver & QR Code Viewer
|
|
*/
|
|
|
|
require_once __DIR__ . '/../includes/Redis.php';
|
|
require_once __DIR__ . '/../includes/Auth.php';
|
|
|
|
$redis = RedisClient::getInstance();
|
|
|
|
// Handle Gateway POST requests (state changes, QR code delivery)
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
// Authenticate gateway request using webhook secret
|
|
$secret = $_SERVER['HTTP_X_WEBHOOK_SECRET'] ?? '';
|
|
$expectedSecret = $_ENV['WHATSAPP_WEBHOOK_SECRET'] ?? $_SERVER['WHATSAPP_WEBHOOK_SECRET'] ?? getenv('WHATSAPP_WEBHOOK_SECRET') ?: 'flash_call_otp_webhook_secret_key';
|
|
|
|
if ($secret !== $expectedSecret) {
|
|
http_response_code(403);
|
|
echo json_encode(['success' => false, 'message' => 'unauthorized']);
|
|
exit;
|
|
}
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
if ($input && isset($input['state'])) {
|
|
$state = $input['state'];
|
|
$redis->set("whatsapp:status", $state);
|
|
|
|
if ($state === 'waiting_qr' && isset($input['qr_code'])) {
|
|
// Store QR code for 60 seconds
|
|
$redis->setex("whatsapp:qr", 60, $input['qr_code']);
|
|
} elseif ($state === 'connected') {
|
|
$redis->del("whatsapp:qr");
|
|
if (isset($input['phone'])) {
|
|
$redis->set("whatsapp:phone", $input['phone']);
|
|
}
|
|
} elseif ($state === 'disconnected') {
|
|
$redis->del("whatsapp:qr");
|
|
$redis->del("whatsapp:phone");
|
|
}
|
|
}
|
|
|
|
echo json_encode(['success' => true]);
|
|
exit;
|
|
}
|
|
|
|
// Handle GET requests (Human Web Interface to scan QR code)
|
|
$status = $redis->get("whatsapp:status") ?: 'disconnected';
|
|
$qrCode = $redis->get("whatsapp:qr");
|
|
$phone = $redis->get("whatsapp:phone");
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="ar" dir="rtl">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>إعداد بوابة الواتساب — Flash Call OTP</title>
|
|
<link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700&display=swap" rel="stylesheet">
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
|
|
<style>
|
|
body {
|
|
font-family: 'Cairo', sans-serif;
|
|
background-color: #f0f4f8;
|
|
color: #333;
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
min-height: 100vh;
|
|
margin: 0;
|
|
}
|
|
.container {
|
|
background: #fff;
|
|
padding: 30px;
|
|
border-radius: 12px;
|
|
box-shadow: 0 4px 20px rgba(0,0,0,0.08);
|
|
text-align: center;
|
|
max-width: 400px;
|
|
width: 100%;
|
|
}
|
|
h1 {
|
|
font-size: 22px;
|
|
margin-bottom: 20px;
|
|
color: #111;
|
|
}
|
|
.status-badge {
|
|
display: inline-block;
|
|
padding: 6px 16px;
|
|
border-radius: 20px;
|
|
font-weight: 600;
|
|
font-size: 14px;
|
|
margin-bottom: 25px;
|
|
}
|
|
.status-connected {
|
|
background-color: #d1e7dd;
|
|
color: #0f5132;
|
|
}
|
|
.status-waiting_qr {
|
|
background-color: #fff3cd;
|
|
color: #664d03;
|
|
}
|
|
.status-disconnected {
|
|
background-color: #f8d7da;
|
|
color: #842029;
|
|
}
|
|
#qrcode {
|
|
display: flex;
|
|
justify-content: center;
|
|
margin: 20px 0;
|
|
padding: 15px;
|
|
background: #fff;
|
|
border: 1px solid #eee;
|
|
border-radius: 8px;
|
|
}
|
|
.instructions {
|
|
font-size: 14px;
|
|
color: #666;
|
|
line-height: 1.6;
|
|
margin-top: 20px;
|
|
}
|
|
.refresh-btn {
|
|
background-color: #0d6efd;
|
|
color: white;
|
|
border: none;
|
|
padding: 10px 20px;
|
|
border-radius: 6px;
|
|
cursor: pointer;
|
|
font-family: 'Cairo', sans-serif;
|
|
font-weight: 600;
|
|
margin-top: 15px;
|
|
}
|
|
.refresh-btn:hover {
|
|
background-color: #0b5ed7;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<h1>ربط بوابة الواتساب بالخدمة</h1>
|
|
|
|
<?php if ($status === 'connected'): ?>
|
|
<div class="status-badge status-connected">متصل بنجاح ✅</div>
|
|
<p>البوابة نشطة حالياً ومتصلة بالرقم: <strong>+<?php echo htmlspecialchars($phone); ?></strong></p>
|
|
<p style="color: green; font-size: 15px;">يمكنك البدء بإرسال الرسائل الآن!</p>
|
|
<?php elseif ($status === 'waiting_qr' && $qrCode): ?>
|
|
<div class="status-badge status-waiting_qr">بانتظار المسح (QR) 📲</div>
|
|
<p>افتح الواتساب على هاتفك -> الأجهزة المرتبطة -> ربط جهاز، ثم امسح الكود أدناه:</p>
|
|
|
|
<div id="qrcode"></div>
|
|
<script>
|
|
new QRCode(document.getElementById("qrcode"), {
|
|
text: "<?php echo $qrCode; ?>",
|
|
width: 250,
|
|
height: 250
|
|
});
|
|
</script>
|
|
|
|
<button class="refresh-btn" onclick="window.location.reload();">تحديث الصفحة 🔄</button>
|
|
<?php else: ?>
|
|
<div class="status-badge status-disconnected">غير متصل ❌</div>
|
|
<p class="instructions">
|
|
الرجاء تشغيل الجلسة أولاً عن طريق إرسال طلب البدء للبوابة من بوستمان أو سكريبت التشغيل.
|
|
</p>
|
|
<button class="refresh-btn" onclick="window.location.reload();">تحديث حالة الاتصال 🔄</button>
|
|
<?php endif; ?>
|
|
|
|
<div class="instructions" style="border-top: 1px solid #eee; padding-top: 15px;">
|
|
<p>حالة النظام الحالية: <strong><?php echo htmlspecialchars($status); ?></strong></p>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|