105 lines
5.8 KiB
PHP
Executable File
105 lines
5.8 KiB
PHP
Executable File
<?php
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
|
|
header('Access-Control-Allow-Headers: Content-Type');
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(200); exit; }
|
|
|
|
$db = pg_connect("host=localhost dbname=adx_system user=admin password=admin123");
|
|
if (!$db) { echo json_encode(['error'=>'DB failed']); exit; }
|
|
pg_query($db, "SET search_path TO admin, public");
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true) ?: $_REQUEST;
|
|
$action = $input['action'] ?? $_GET['action'] ?? 'status';
|
|
|
|
switch ($action) {
|
|
|
|
// === BRAIN WINNERS ===
|
|
case 'winners':
|
|
$isp = $_GET['isp'] ?? $input['isp'] ?? null;
|
|
$sql = "SELECT id, isp_target, send_method, inbox_rate, return_path, from_name, from_email,
|
|
reply_to, subject_template, header_1_name, header_1_value, header_2_name, header_2_value,
|
|
header_3_name, header_3_value, header_4_name, header_4_value, header_5_name, header_5_value,
|
|
content_type, encoding, charset, domain_used, ip_used, total_sent, body_template,
|
|
tracking_domain, redirect_domain, unsubscribe_url, confidence_score, stability_score
|
|
FROM admin.brain_configs WHERE status='winner'";
|
|
$params = [];
|
|
if ($isp) { $sql .= " AND isp_target ILIKE $1"; $params[] = '%'.$isp.'%'; }
|
|
$sql .= " ORDER BY inbox_rate DESC LIMIT 20";
|
|
$r = $params ? pg_query_params($db, $sql, $params) : pg_query($db, $sql);
|
|
$rows = []; if($r) while ($row = pg_fetch_assoc($r)) $rows[] = $row;
|
|
echo json_encode(['success'=>true,'count'=>count($rows),'winners'=>$rows]);
|
|
break;
|
|
|
|
case 'stats':
|
|
$r = pg_query($db, "SELECT COUNT(*) FILTER(WHERE status='winner') as winners, COUNT(*) as total,
|
|
ROUND(AVG(inbox_rate)::numeric,1) as avg_rate FROM admin.brain_configs");
|
|
$isps = pg_query($db, "SELECT DISTINCT isp_target FROM admin.brain_configs WHERE status='winner' ORDER BY isp_target");
|
|
$ispList = [];
|
|
while ($row = pg_fetch_assoc($isps)) $ispList[] = $row['isp_target'];
|
|
echo json_encode(['success'=>true,'stats'=>pg_fetch_assoc($r),'isps'=>$ispList]);
|
|
break;
|
|
|
|
case 'inject':
|
|
$id = intval($_GET['id'] ?? $input['id'] ?? 0);
|
|
if (!$id) { echo json_encode(['error'=>'Missing id']); exit; }
|
|
$r = pg_query_params($db, "SELECT * FROM admin.brain_configs WHERE id=$1", [$id]);
|
|
echo json_encode(['success'=>true,'config'=>pg_fetch_assoc($r)]);
|
|
break;
|
|
|
|
// === PERSONAS ===
|
|
case 'personas':
|
|
$limit = intval($_GET['limit'] ?? 20);
|
|
$r = pg_query($db, "SELECT id, first_name, last_name, email, country, gender FROM admin.personas WHERE is_active=true ORDER BY RANDOM() LIMIT $limit");
|
|
$rows = []; if($r) while ($row = pg_fetch_assoc($r)) $rows[] = $row;
|
|
echo json_encode(['success'=>true,'personas'=>$rows,'count'=>count($rows)]);
|
|
break;
|
|
|
|
case 'persona_generate':
|
|
$count = min(intval($input['count'] ?? 5), 50);
|
|
$fn = ['Jean','Marie','Pierre','Sophie','Lucas','Emma','Thomas','Léa','Nicolas','Julie','Hans','Klaus','Anna','Fritz','Heike','Stefan','Sabine','Markus','Petra','Werner'];
|
|
$ln = ['Martin','Bernard','Dubois','Müller','Schmidt','Fischer','Weber','Wagner','Becker','Hoffmann','Schäfer','Koch','Meyer','Richter','Klein','Wolf'];
|
|
$personas = [];
|
|
for ($i = 0; $i < $count; $i++) {
|
|
$f = $fn[array_rand($fn)]; $l = $ln[array_rand($ln)];
|
|
$personas[] = ['first_name'=>$f,'last_name'=>$l,'display_name'=>"$f $l",'email'=>strtolower($f).'.'.strtolower($l).rand(10,99).'@'.['gmail.com','outlook.com','yahoo.com','web.de','gmx.de'][array_rand([0,1,2,3,4])]];
|
|
}
|
|
echo json_encode(['success'=>true,'personas'=>$personas]);
|
|
break;
|
|
|
|
// === AI CONTENT GENERATION (HAMID Multi-Provider) ===
|
|
case 'ai_generate':
|
|
require_once('/opt/wevads/hamid-providers-config.php');
|
|
$type = $input['type'] ?? 'subject'; // subject, body, headers
|
|
$isp = $input['isp'] ?? 'OUTLOOK';
|
|
$lang = $input['lang'] ?? 'de';
|
|
$style = $input['style'] ?? 'commercial';
|
|
$provider_name = $input['provider'] ?? null;
|
|
|
|
$langMap = ['de'=>'German','fr'=>'French','en'=>'English','nl'=>'Dutch','it'=>'Italian','es'=>'Spanish'];
|
|
$langFull = $langMap[$lang] ?? 'German';
|
|
|
|
$prompts = [
|
|
'subject' => "Generate 5 email subject lines in $langFull for $isp inbox. Style: $style. High inbox placement. Return ONLY a JSON array of strings, no explanation.",
|
|
'body' => "Generate a professional HTML email body in $langFull targeting $isp. Style: $style. Use ONLY ADX iResponse square bracket tags: [first_name] for name, [from_name] for company, http://[domain]/[url] for click link, http://[domain]/[unsub] for unsubscribe, http://[domain]/[open] as 1x1 tracking pixel. NEVER use curly braces. Clean HTML, no external images. Return ONLY the HTML code.",
|
|
'full' => "Generate a complete email config for $isp inbox delivery in $langFull, style: $style. Return JSON with: subject, from_name_template, return_path_pattern, recommended_headers (array of {name,value}), content_type, encoding, charset. Optimize for inbox placement."
|
|
];
|
|
|
|
$prompt = $prompts[$type] ?? $prompts['subject'];
|
|
$result = callWithFailover($prompt, $provider_name);
|
|
echo json_encode($result);
|
|
break;
|
|
|
|
// === HAMID PROVIDERS STATUS ===
|
|
case 'providers':
|
|
require_once('/opt/wevads/hamid-providers-config.php');
|
|
$providers = getProviders();
|
|
echo json_encode(['success'=>true,'count'=>count($providers),'providers'=>array_map(function($p){
|
|
return ['name'=>$p['provider_name'],'model'=>$p['model'],'priority'=>$p['priority'],'has_key'=>!empty($p['api_key'])];
|
|
}, $providers)]);
|
|
break;
|
|
|
|
default:
|
|
echo json_encode(['success'=>true,'service'=>'brain-send-api','actions'=>['winners','stats','inject','personas','persona_generate','ai_generate','providers']]);
|
|
}
|