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

185 lines
6.1 KiB
PHP
Executable File

<?php
// Redirect GET to HTML UI
if ($_SERVER["REQUEST_METHOD"] === "GET" && !isset($_GET["action"]) && !isset($_GET["api"])) { header("Location: semi-auto-send.html"); exit; }
/**
* send-engine.php - API Semi-Auto Send Engine
* Interface pour le frontend d'envoi semi-automatique
*/
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
// DB Connection
try {
$pdo = new PDO('pgsql:host=localhost;dbname=adx_system', 'admin', 'admin123');
$pdo->exec('SET search_path TO admin, public');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo json_encode(['error' => 'DB connection failed: ' . $e->getMessage()]);
exit;
}
// Get input
$input = json_decode(file_get_contents('php://input'), true) ?: [];
$action = $input['action'] ?? $_GET['action'] ?? 'detect_isp';
switch ($action) {
case 'detect_isp':
$email = $input['email'] ?? $_GET['email'] ?? '';
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo json_encode(['error' => 'Valid email address required']);
exit;
}
$domain = strtolower(explode('@', $email)[1]);
// ISP mapping
$isp_map = [
't-online.de' => 'T-Online',
'gmx.de' => 'GMX',
'gmx.net' => 'GMX',
'web.de' => 'Web.de',
'outlook.com' => 'Outlook',
'hotmail.com' => 'Outlook',
'live.com' => 'Outlook',
'gmail.com' => 'Gmail',
'googlemail.com' => 'Gmail',
'yahoo.com' => 'Yahoo',
'yahoo.fr' => 'Yahoo',
'yahoo.de' => 'Yahoo',
'ziggo.nl' => 'Ziggo',
'alice.it' => 'Alice',
'orange.fr' => 'Orange',
'sfr.fr' => 'SFR',
'free.fr' => 'Free',
'laposte.net' => 'LaPoste',
'wanadoo.fr' => 'Orange',
'aol.com' => 'AOL',
'icloud.com' => 'iCloud',
'me.com' => 'iCloud',
'mail.ru' => 'Mail.ru',
'yandex.ru' => 'Yandex',
'seznam.cz' => 'Seznam',
'wp.pl' => 'WP.PL',
'o2.pl' => 'O2.PL',
'interia.pl' => 'Interia'
];
$isp = $isp_map[$domain] ?? ucfirst(explode('.', $domain)[0]);
// Get winning configs for this domain
$stmt = $pdo->prepare("
SELECT id, send_method, inbox_rate, x_mailer, priority, from_domain, smtp_host, smtp_port
FROM brain_send_configs
WHERE isp_target ILIKE :domain
AND is_winner = true
AND status = 'winner'
ORDER BY inbox_rate DESC
LIMIT 5
");
$stmt->execute(['domain' => "%$domain%"]);
$winners = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode([
'email' => $email,
'domain' => $domain,
'isp' => $isp,
'winners' => $winners,
'count' => count($winners),
'timestamp' => date('c')
]);
break;
case 'get_winners':
$isp = $input['isp'] ?? $_GET['isp'] ?? '';
if (empty($isp)) {
echo json_encode(['error' => 'ISP parameter required']);
exit;
}
$stmt = $pdo->prepare("
SELECT * FROM brain_send_configs
WHERE isp_target ILIKE :isp
AND is_winner = true
ORDER BY inbox_rate DESC
");
$stmt->execute(['isp' => "%$isp%"]);
$configs = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode([
'isp' => $isp,
'configs' => $configs,
'count' => count($configs)
]);
break;
case 'send':
// Forward to brain-unified-send.php
$ch = curl_init('http://127.0.0.1:5821/deliverads/brain-unified-send.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($input));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$result = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($result) {
echo $result;
} else {
echo json_encode([
'success' => false,
'error' => 'Send engine unreachable',
'http_code' => $http_code
]);
}
break;
case 'stats':
$stmt = $pdo->query("
SELECT
COUNT(*) as total_sends,
COUNT(*) FILTER (WHERE created_at >= CURRENT_DATE) as sends_today,
COUNT(DISTINCT isp_target) as isps_covered,
COUNT(DISTINCT send_method) as methods_used,
ROUND(AVG(CASE WHEN error_message IS NULL THEN 1 ELSE 0 END) * 100, 1) as success_rate
FROM unified_send_log_new
");
$stats = $stmt->fetch(PDO::FETCH_ASSOC);
// Top 5 ISPs today
$stmt = $pdo->query("
SELECT isp_target, COUNT(*) as send_count
FROM unified_send_log_new
WHERE created_at >= CURRENT_DATE
GROUP BY isp_target
ORDER BY send_count DESC
LIMIT 5
");
$top_isps = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode([
'stats' => $stats,
'top_isps_today' => $top_isps,
'timestamp' => date('c')
]);
break;
case 'test':
echo json_encode([
'status' => 'online',
'name' => 'Send Engine API',
'version' => '2.0',
'available_actions' => ['detect_isp', 'get_winners', 'send', 'stats', 'test']
]);
break;
default:
echo json_encode([
'error' => 'Unknown action',
'available_actions' => ['detect_isp', 'get_winners', 'send', 'stats', 'test']
]);
}