PDO::ERRMODE_EXCEPTION]); $pdo->exec("CREATE TABLE IF NOT EXISTS admin.seed_inboxes (id SERIAL PRIMARY KEY, name VARCHAR(255), email VARCHAR(255) UNIQUE, password VARCHAR(255), imap_host VARCHAR(255), imap_port INTEGER DEFAULT 993, provider VARCHAR(100), isp VARCHAR(100), is_active BOOLEAN DEFAULT true)"); $pdo->exec("CREATE TABLE IF NOT EXISTS admin.seed_results (id SERIAL PRIMARY KEY, campaign_id INTEGER, inbox_id INTEGER, email VARCHAR(255), location VARCHAR(50), subject VARCHAR(500), received_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)"); $action = $_POST['action'] ?? $_GET['action'] ?? ''; switch ($action) { case 'add': $stmt = $pdo->prepare("INSERT INTO admin.seed_inboxes (name, email, password, imap_host, imap_port, provider, isp) VALUES (?, ?, ?, ?, ?, ?, ?) ON CONFLICT (email) DO NOTHING"); $stmt->execute([$_POST['name'], $_POST['email'], $_POST['password'], $_POST['imap_host'], $_POST['imap_port'] ?? 993, $_POST['provider'] ?? '', $_POST['isp'] ?? '']); echo json_encode(['success' => true]); break; case 'list': $inboxes = $pdo->query("SELECT id, name, email, imap_host, provider, isp, is_active FROM admin.seed_inboxes ORDER BY isp, email")->fetchAll(PDO::FETCH_ASSOC); echo json_encode(['inboxes' => $inboxes, 'count' => count($inboxes)]); break; case 'check': $stmt = $pdo->prepare("SELECT * FROM admin.seed_inboxes WHERE id = ?"); $stmt->execute([$_POST['id']]); $inbox = $stmt->fetch(PDO::FETCH_ASSOC); if (!$inbox) { echo json_encode(['error' => 'Not found']); break; } $mailbox = "{" . $inbox['imap_host'] . ":" . $inbox['imap_port'] . "/imap/ssl/novalidate-cert}"; $result = ['inbox' => 0, 'spam' => 0, 'recent' => []]; $imap = @imap_open($mailbox . "INBOX", $inbox['email'], $inbox['password'], 0, 1); if ($imap) { $info = imap_check($imap); $result['inbox'] = $info->Nmsgs ?? 0; if ($result['inbox'] > 0) { $start = max(1, $result['inbox'] - 5); $emails = imap_fetch_overview($imap, "$start:{$result['inbox']}"); foreach ($emails as $e) $result['recent'][] = ['subject' => imap_utf8($e->subject ?? ''), 'from' => imap_utf8($e->from ?? '')]; } imap_close($imap); } $imapSpam = @imap_open($mailbox . "Junk", $inbox['email'], $inbox['password'], 0, 1); if (!$imapSpam) $imapSpam = @imap_open($mailbox . "Spam", $inbox['email'], $inbox['password'], 0, 1); if ($imapSpam) { $info = imap_check($imapSpam); $result['spam'] = $info->Nmsgs ?? 0; imap_close($imapSpam); } echo json_encode(['success' => true, 'result' => $result]); break; case 'check_all': $inboxes = $pdo->query("SELECT * FROM admin.seed_inboxes WHERE is_active = true")->fetchAll(PDO::FETCH_ASSOC); $results = []; foreach ($inboxes as $inbox) { $mailbox = "{" . $inbox['imap_host'] . ":" . $inbox['imap_port'] . "/imap/ssl/novalidate-cert}INBOX"; $imap = @imap_open($mailbox, $inbox['email'], $inbox['password'], 0, 1); $count = 0; if ($imap) { $info = imap_check($imap); $count = $info->Nmsgs ?? 0; imap_close($imap); } $results[] = ['email' => $inbox['email'], 'isp' => $inbox['isp'], 'inbox' => $count]; } echo json_encode(['results' => $results]); break; case 'log_result': $stmt = $pdo->prepare("INSERT INTO admin.seed_results (campaign_id, inbox_id, email, location, subject) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$_POST['campaign_id'], $_POST['inbox_id'], $_POST['email'], $_POST['location'], $_POST['subject'] ?? '']); echo json_encode(['success' => true]); break; case 'campaign_results': $cid = intval($_GET['campaign_id']); $results = $pdo->query("SELECT sr.*, si.isp FROM admin.seed_results sr LEFT JOIN admin.seed_inboxes si ON sr.inbox_id = si.id WHERE sr.campaign_id = $cid ORDER BY sr.received_at DESC")->fetchAll(PDO::FETCH_ASSOC); $inbox = count(array_filter($results, fn($r) => $r['location'] == 'inbox')); $spam = count(array_filter($results, fn($r) => $r['location'] == 'spam')); $total = count($results); echo json_encode(['results' => $results, 'inbox' => $inbox, 'spam' => $spam, 'inbox_rate' => $total > 0 ? round(($inbox / $total) * 100, 2) : 0]); break; case 'delete': $pdo->exec("DELETE FROM admin.seed_inboxes WHERE id = " . intval($_POST['id'])); echo json_encode(['success' => true]); break; case 'stats': echo json_encode([ 'total' => $pdo->query("SELECT COUNT(*) FROM admin.seed_inboxes")->fetchColumn(), 'active' => $pdo->query("SELECT COUNT(*) FROM admin.seed_inboxes WHERE is_active = true")->fetchColumn(), 'by_isp' => $pdo->query("SELECT isp, COUNT(*) as count FROM admin.seed_inboxes GROUP BY isp ORDER BY count DESC")->fetchAll(PDO::FETCH_ASSOC) ]); break; default: echo json_encode(['actions' => ['add','list','check','check_all','log_result','campaign_results','delete','stats']]); }