Files
wevads-platform/scripts/webhook.php
2026-02-26 04:53:11 +01:00

70 lines
3.6 KiB
PHP
Executable File

<?php
header('Content-Type: application/json');
$pdo = new PDO("pgsql:host=localhost;dbname=adx_system", "admin", "admin123");
$input = json_decode(file_get_contents('php://input'), true);
$action = $_GET['action'] ?? $input['action'] ?? '';
switch ($action) {
case 'trigger_warmup':
// Trigger warmup for specific account or all
$accountId = $input['account_id'] ?? null;
$sql = "SELECT id FROM admin.warmup_accounts WHERE status = 'active'";
if ($accountId) $sql .= " AND id = " . intval($accountId);
$accounts = $pdo->query($sql)->fetchAll(PDO::FETCH_COLUMN);
echo json_encode(['success' => true, 'triggered' => count($accounts)]);
break;
case 'create_campaign':
$stmt = $pdo->prepare("INSERT INTO admin.campaigns (name, isp, sponsor, offer, servers_count, region, status) VALUES (?, ?, ?, ?, ?, ?, 'pending') RETURNING id");
$stmt->execute([$input['name'], $input['isp'] ?? 'videotron', $input['sponsor'] ?? '', $input['offer'] ?? '', $input['servers_count'] ?? 5, $input['region'] ?? 'af-south-1']);
echo json_encode(['success' => true, 'campaign_id' => $stmt->fetchColumn()]);
break;
case 'start_campaign':
$pdo->exec("UPDATE admin.campaigns SET status = 'running', started_at = NOW() WHERE id = " . intval($input['campaign_id']));
echo json_encode(['success' => true]);
break;
case 'send_alert':
$config = $pdo->query("SELECT * FROM admin.telegram_config WHERE is_active = true LIMIT 1")->fetch(PDO::FETCH_ASSOC);
if ($config) {
$msg = "🔔 <b>{$input['title']}</b>\n\n{$input['message']}";
$ch = curl_init("https://api.telegram.org/bot{$config['bot_token']}/sendMessage");
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => ['chat_id' => $config['chat_id'], 'text' => $msg, 'parse_mode' => 'HTML']]);
curl_exec($ch); curl_close($ch);
}
echo json_encode(['success' => true]);
break;
case 'check_inbox':
$stmt = $pdo->prepare("SELECT * FROM admin.imap_checkers WHERE id = ?");
$stmt->execute([$input['checker_id']]);
$acc = $stmt->fetch(PDO::FETCH_ASSOC);
if ($acc) {
$mailbox = "{" . $acc['imap_host'] . ":" . $acc['imap_port'] . "/imap/ssl/novalidate-cert}INBOX";
$imap = @imap_open($mailbox, $acc['email'], $acc['password'], 0, 1);
if ($imap) {
$info = imap_check($imap);
$inbox = $info->Nmsgs ?? 0;
imap_close($imap);
$pdo->exec("UPDATE admin.imap_checkers SET inbox_count = $inbox, last_check = NOW() WHERE id = {$acc['id']}");
echo json_encode(['success' => true, 'inbox' => $inbox]);
} else {
echo json_encode(['success' => false, 'error' => imap_last_error()]);
}
}
break;
case 'get_stats':
echo json_encode(['success' => true, 'stats' => [
'campaigns_running' => $pdo->query("SELECT COUNT(*) FROM admin.campaigns WHERE status = 'running'")->fetchColumn(),
'warmup_active' => $pdo->query("SELECT COUNT(*) FROM admin.warmup_accounts WHERE status = 'active'")->fetchColumn(),
'pmta_queued' => $pdo->query("SELECT COALESCE(SUM(queued_emails), 0) FROM admin.pmta_servers")->fetchColumn(),
]]);
break;
default:
echo json_encode(['success' => false, 'error' => 'Unknown action', 'available' => ['trigger_warmup', 'create_campaign', 'start_campaign', 'send_alert', 'check_inbox', 'get_stats']]);
}