setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } return $pdo; } // Ensure tables exist function ensureTables($pdo) { $pdo->exec("CREATE TABLE IF NOT EXISTS admin.alert_config (key VARCHAR(100) PRIMARY KEY, value TEXT, updated_at TIMESTAMP DEFAULT NOW())"); $pdo->exec("CREATE TABLE IF NOT EXISTS admin.alert_history (id SERIAL PRIMARY KEY, type VARCHAR(50), severity VARCHAR(20), title VARCHAR(255), message TEXT, data JSONB, sent_email BOOLEAN DEFAULT FALSE, sent_telegram BOOLEAN DEFAULT FALSE, acknowledged BOOLEAN DEFAULT FALSE, created_at TIMESTAMP DEFAULT NOW())"); } if (isset($_GET['action'])) { header('Content-Type: application/json'); $pdo = getDb(); ensureTables($pdo); switch ($_GET['action']) { case 'run_checks': $alerts = []; // Check blocked accounts $stats = $pdo->query("SELECT COUNT(*) FILTER (WHERE status = 'Blocked') as blocked, COUNT(*) as total FROM admin.office_accounts")->fetch(PDO::FETCH_ASSOC); $blockedPercent = $stats['total'] > 0 ? ($stats['blocked'] / $stats['total']) * 100 : 0; if ($blockedPercent > 30) { $alerts[] = ['type' => 'blocked_high', 'severity' => $blockedPercent > 50 ? 'critical' : 'warning', 'title' => "โ ๏ธ High Blocked Rate: " . round($blockedPercent, 1) . "%", 'message' => "{$stats['blocked']} of {$stats['total']} accounts blocked"]; } // Save alerts foreach ($alerts as $alert) { $stmt = $pdo->prepare("INSERT INTO admin.alert_history (type, severity, title, message, data) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$alert['type'], $alert['severity'], $alert['title'], $alert['message'], json_encode($alert)]); // Send Telegram if configured $tgToken = $pdo->query("SELECT value FROM admin.alert_config WHERE key = 'telegram_bot_token'")->fetchColumn(); $tgChat = $pdo->query("SELECT value FROM admin.alert_config WHERE key = 'telegram_chat_id'")->fetchColumn(); if ($tgToken && $tgChat) { $text = "๐จ *WEVAL SEND ALERT*\n\n*{$alert['title']}*\n\n{$alert['message']}"; @file_get_contents("https://api.telegram.org/bot{$tgToken}/sendMessage?" . http_build_query(['chat_id' => $tgChat, 'text' => $text, 'parse_mode' => 'Markdown'])); } } echo json_encode(['alerts' => $alerts, 'checked_at' => date('Y-m-d H:i:s')]); break; case 'history': $history = $pdo->query("SELECT * FROM admin.alert_history ORDER BY created_at DESC LIMIT 50")->fetchAll(PDO::FETCH_ASSOC); echo json_encode($history); break; case 'config': if ($_SERVER['REQUEST_METHOD'] === 'POST') { foreach ($_POST as $key => $value) { $stmt = $pdo->prepare("INSERT INTO admin.alert_config (key, value, updated_at) VALUES (?, ?, NOW()) ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = NOW()"); $stmt->execute([$key, $value]); } echo json_encode(['success' => true]); } else { $config = $pdo->query("SELECT key, value FROM admin.alert_config")->fetchAll(PDO::FETCH_KEY_PAIR); echo json_encode($config); } break; case 'test': $tgToken = $pdo->query("SELECT value FROM admin.alert_config WHERE key = 'telegram_bot_token'")->fetchColumn(); $tgChat = $pdo->query("SELECT value FROM admin.alert_config WHERE key = 'telegram_chat_id'")->fetchColumn(); $result = ['telegram' => false, 'email' => false]; if ($tgToken && $tgChat) { $text = "๐งช *Test Alert from WEVAL SEND*\n\nYour alert system is working!"; $response = @file_get_contents("https://api.telegram.org/bot{$tgToken}/sendMessage?" . http_build_query(['chat_id' => $tgChat, 'text' => $text, 'parse_mode' => 'Markdown'])); $result['telegram'] = $response !== false; } echo json_encode($result); break; default: echo json_encode(['error' => 'Unknown action']); } exit; } ?>
Loading...
Add this cron job to run checks every 15 minutes:
*/15 * * * * curl -s "http://localhost:5821/alert-system.php?action=run_checks" > /dev/null