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

191 lines
9.5 KiB
PHP
Executable File

<?php
header('Content-Type: application/json');
$db = pg_connect("host=localhost dbname=adx_system user=admin password=admin123");
pg_query($db, "SET search_path TO admin,public");
$action = $_GET['action'] ?? $_POST['action'] ?? '';
switch($action) {
// ═══════ DASHBOARD STATS ═══════
case 'stats':
$stats = [];
$stats['creatives_active'] = pg_fetch_result(pg_query($db,"SELECT COUNT(*) FROM offer_creatives WHERE status='active'"),0,0);
$stats['creatives_total'] = pg_fetch_result(pg_query($db,"SELECT COUNT(*) FROM offer_creatives"),0,0);
$stats['offers'] = pg_fetch_result(pg_query($db,"SELECT COUNT(*) FROM affiliate_offers WHERE network='CX3' AND status='active'"),0,0);
$stats['o365_graph_send'] = pg_fetch_result(pg_query($db,"SELECT COUNT(*) FROM office_accounts WHERE password_status='graph_send'"),0,0);
$stats['o365_graph_ok'] = pg_fetch_result(pg_query($db,"SELECT COUNT(*) FROM office_accounts WHERE password_status='graph_ok'"),0,0);
$stats['o365_total'] = pg_fetch_result(pg_query($db,"SELECT COUNT(*) FROM office_accounts"),0,0);
$stats['send_methods'] = pg_fetch_result(pg_query($db,"SELECT COUNT(*) FROM send_methods WHERE is_active=true"),0,0);
$stats['brain_configs'] = pg_fetch_result(pg_query($db,"SELECT COUNT(*) FROM brain_configs"),0,0);
$stats['domains_verified'] = pg_fetch_result(pg_query($db,"SELECT COUNT(*) FROM verified_sender_domains"),0,0);
$stats['sends_today'] = pg_fetch_result(pg_query($db,"SELECT COUNT(*) FROM unified_send_log WHERE created_at::date=CURRENT_DATE"),0,0);
// Contacts breakdown by ISP
$cr = pg_query($db, "SELECT * FROM admin.total_clients()");
$contacts_isps = [];
$contacts_total = 0;
while ($row = pg_fetch_assoc($cr)) {
$contacts_isps[] = ['isp'=>$row['isp'], 'tables'=>(int)$row['table_count'], 'contacts'=>(int)$row['total_rows']];
$contacts_total += (int)$row['total_rows'];
}
$stats['contacts'] = number_format($contacts_total);
$stats['contacts_raw'] = $contacts_total;
$stats['contacts_isps'] = $contacts_isps;
// Brand pool
$r = pg_query($db,"SELECT brand, COUNT(*) as cnt FROM office_accounts WHERE password_status IN ('graph_ok','graph_send') AND brand IS NOT NULL GROUP BY brand ORDER BY brand");
$stats['brand_pool'] = [];
while($row = pg_fetch_assoc($r)) $stats['brand_pool'][] = $row;
echo json_encode($stats);
break;
// ═══════ CREATIVES ═══════
case 'creatives_list':
$r = pg_query($db,"SELECT c.id, c.from_name, c.subject_line, c.s3_image_url, c.status, c.from_domain,
SUBSTRING(c.html_body FROM 'o=([0-9]+)') as offer_id,
ao.name as offer_name, ao.payout
FROM offer_creatives c
LEFT JOIN affiliate_offers ao ON ao.id = CAST(NULLIF(SUBSTRING(c.html_body FROM 'o=([0-9]+)'),'') AS INTEGER)
ORDER BY c.id");
$rows = [];
while($row = pg_fetch_assoc($r)) $rows[] = $row;
echo json_encode($rows);
break;
case 'creative_save':
$data = json_decode(file_get_contents('php://input'), true);
$id = intval($data['id'] ?? 0);
if($id > 0) {
// Rebuild HTML from params
$oid = intval($data['offer_id']);
$img = $data['image_url'];
$unsub = $data['unsub_text'] ?? 'Abmelden';
$t = 'https://track.wevup.app';
$html = "<html><body style=\"margin:0;padding:0;background:#f9f9f9;\"><center>" .
"<a href=\"$t/api/click.php?t={tid}&o=$oid\">" .
"<img src=\"$img\" width=\"600\" style=\"display:block;border:0;max-width:100%;\" alt=\"\">" .
"</a>" .
"<p style=\"color:#aaa;font-size:9px;font-family:Arial;margin:10px 0;\">" .
"<a href=\"$t/api/unsub.php?t={tid}\" style=\"color:#aaa;text-decoration:none;\">$unsub</a>" .
"</p></center>" .
"<img src=\"$t/api/open.php?t={tid}\" width=\"1\" height=\"1\" style=\"display:none\" />" .
"</body></html>";
pg_query_params($db,"UPDATE offer_creatives SET subject_line=$1, from_name=$2, s3_image_url=$3, html_body=$4, status=$5 WHERE id=$6",
[$data['subject_line'], $data['from_name'], $img, $html, $data['status'], $id]);
echo json_encode(['ok'=>true,'id'=>$id]);
} else {
// Create new
$oid = intval($data['offer_id']);
$img = $data['image_url'];
$unsub = $data['unsub_text'] ?? 'Abmelden';
$t = 'https://track.wevup.app';
$html = "<html><body style=\"margin:0;padding:0;background:#f9f9f9;\"><center>" .
"<a href=\"$t/api/click.php?t={tid}&o=$oid\">" .
"<img src=\"$img\" width=\"600\" style=\"display:block;border:0;max-width:100%;\" alt=\"\">" .
"</a>" .
"<p style=\"color:#aaa;font-size:9px;font-family:Arial;margin:10px 0;\">" .
"<a href=\"$t/api/unsub.php?t={tid}\" style=\"color:#aaa;text-decoration:none;\">$unsub</a>" .
"</p></center>" .
"<img src=\"$t/api/open.php?t={tid}\" width=\"1\" height=\"1\" style=\"display:none\" />" .
"</body></html>";
$r = pg_query_params($db,"INSERT INTO offer_creatives (offer_id, creative_name, subject_line, from_name, s3_image_url, html_body, status) VALUES (1, $1, $2, $3, $4, $5, $6) RETURNING id",
['CX3 Creative', $data['subject_line'], $data['from_name'], $img, $html, $data['status'] ?? 'active']);
$new = pg_fetch_assoc($r);
echo json_encode(['ok'=>true,'id'=>$new['id']]);
}
break;
case 'creative_delete':
$id = intval($_GET['id'] ?? 0);
pg_query_params($db,"UPDATE offer_creatives SET status='disabled' WHERE id=$1",[$id]);
echo json_encode(['ok'=>true]);
break;
// ═══════ OFFERS ═══════
case 'offers_list':
$r = pg_query($db,"SELECT id, name, offer_name, payout, country_code, vertical, tracking_url, preview_url, status FROM affiliate_offers WHERE network='CX3' ORDER BY id");
$rows = [];
while($row = pg_fetch_assoc($r)) $rows[] = $row;
echo json_encode($rows);
break;
case 'offer_save':
$data = json_decode(file_get_contents('php://input'), true);
$id = intval($data['id'] ?? 0);
if($id > 0) {
pg_query_params($db,"UPDATE affiliate_offers SET name=$1, payout=$2, country_code=$3, tracking_url=$4, preview_url=$5, status=$6, vertical=$7 WHERE id=$8",
[$data['name'], $data['payout'], $data['country_code'], $data['tracking_url'], $data['preview_url'], $data['status'], $data['vertical'], $id]);
} else {
pg_query_params($db,"INSERT INTO affiliate_offers (name, offer_name, payout, country_code, tracking_url, preview_url, status, vertical, network) VALUES ($1,$1,$2,$3,$4,$5,$6,$7,'CX3')",
[$data['name'], $data['payout'], $data['country_code'], $data['tracking_url'], $data['preview_url'], $data['status'] ?? 'active', $data['vertical']]);
}
echo json_encode(['ok'=>true]);
break;
// ═══════ O365 ACCOUNTS ═══════
case 'accounts_list':
$r = pg_query($db,"SELECT id, admin_email, brand, password_status, tenant_domain FROM office_accounts WHERE password_status IN ('graph_send','graph_ok') ORDER BY brand, id");
$rows = [];
while($row = pg_fetch_assoc($r)) $rows[] = $row;
echo json_encode($rows);
break;
case 'account_rebrand':
$data = json_decode(file_get_contents('php://input'), true);
pg_query_params($db,"UPDATE office_accounts SET brand=$1 WHERE id=$2",[$data['brand'],$data['id']]);
echo json_encode(['ok'=>true]);
break;
// ═══════ BRAIN CONFIGS ═══════
case 'brain_list':
$r = pg_query($db,"SELECT id, isp, send_method, inbox_rate, is_winner, from_name, domain_used, tests_count FROM brain_configs ORDER BY isp, inbox_rate DESC");
$rows = [];
while($row = pg_fetch_assoc($r)) $rows[] = $row;
echo json_encode($rows);
break;
// ═══════ ISP METHODS ═══════
case 'isp_methods_list':
$r = pg_query($db,"SELECT * FROM isp_methods ORDER BY isp");
$rows = [];
while($row = pg_fetch_assoc($r)) $rows[] = $row;
echo json_encode($rows);
break;
// ═══════ SEND LOG ═══════
case 'send_log':
$limit = intval($_GET['limit'] ?? 50);
$r = pg_query($db,"SELECT id, recipient, subject, send_method, status, tracking_id, created_at FROM unified_send_log ORDER BY id DESC LIMIT $limit");
$rows = [];
while($row = pg_fetch_assoc($r)) $rows[] = $row;
echo json_encode($rows);
break;
// ═══════ TEST SEND ═══════
case 'test_send':
$data = json_decode(file_get_contents('php://input'), true);
$cid = intval($data['creative_id']);
$to = $data['to'] ?? 'yacineutt@gmail.com';
require_once("/opt/wevads/config/mta_helper.php");
$r = pg_query_params($db,"SELECT subject_line, html_body, from_name FROM offer_creatives WHERE id=$1",[$cid]);
$c = pg_fetch_assoc($r);
$tid = "ADM_" . uniqid();
$html = str_replace("{tid}", $tid, $c["html_body"]);
$result = o365_graph_send($to, $c["subject_line"], $html, $c["from_name"]);
echo json_encode(['ok'=>strpos($result,'OK')===0, 'result'=>$result, 'tracking_id'=>$tid]);
break;
// ═══════ SEND METHODS ═══════
case 'methods_list':
$r = pg_query($db,"SELECT id, method_name, method_type, is_active, priority FROM send_methods WHERE is_active=true ORDER BY priority DESC, method_name");
$rows = [];
while($row = pg_fetch_assoc($r)) $rows[] = $row;
echo json_encode($rows);
break;
default:
echo json_encode(['error'=>'unknown action']);
}