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

82 lines
3.2 KiB
PHP
Executable File

<?php
/**
* Brain Config Inject API - returns properly formatted WEVADS headers for a brain config
* Used by the send-process brain inject JS
*/
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
$db = new PDO('pgsql:host=127.0.0.1;dbname=adx_system','postgres','');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec("SET search_path TO admin");
$action = $_GET['action'] ?? 'get_config';
$config_id = intval($_GET['config_id'] ?? 0);
switch($action) {
case 'list':
$rows = $db->query("SELECT id, isp_target, send_method, from_name, from_email, subject_template, inbox_rate, is_winner, is_active FROM brain_configs ORDER BY is_winner DESC, inbox_rate DESC")->fetchAll(PDO::FETCH_ASSOC);
echo json_encode(['configs' => $rows]);
break;
case 'get_config':
if (!$config_id) { echo json_encode(['error' => 'config_id required']); break; }
$cfg = $db->prepare("SELECT * FROM brain_configs WHERE id=?");
$cfg->execute([$config_id]);
$c = $cfg->fetch(PDO::FETCH_ASSOC);
if (!$c) { echo json_encode(['error' => 'Config not found']); break; }
// Build the COMPLETE header in proper WEVADS [bracket] format
$headers = "MIME-Version: 1.0\n";
$headers .= "Message-Id: <[a_7][n_5][n_3][a_3][email_b64]@[domain]>\n";
$headers .= "From: " . ($c['from_name'] ?: '"[Name]"') . " <" . ($c['from_email'] ?: 'info@[domain]') . ">\n";
$headers .= "Subject: " . ($c['subject_template'] ?: 'Your notification') . "\n";
$headers .= "To: [email]\n";
// Content-Transfer-Encoding
$enc = $c['encoding'] ?: 'quoted-printable';
$headers .= "Content-Transfer-Encoding: $enc\n";
// Content-Type
$ct = $c['content_type'] ?: 'multipart/alternative';
$charset = $c['charset'] ?: 'utf-8';
if (strpos($ct, 'charset') === false && strpos($ct, 'multipart') === false) {
$ct .= "; charset=$charset";
}
$headers .= "Content-Type: $ct\n";
$headers .= "Date: [mail_date]\n";
// Custom headers (X-Mailer, X-Priority, etc.)
for ($i = 1; $i <= 5; $i++) {
$name = $c["header_{$i}_name"] ?? '';
$value = $c["header_{$i}_value"] ?? '';
if ($name && $value) {
$headers .= "$name: $value\n";
}
}
// Return all fields in proper WEVADS format
echo json_encode([
'config_id' => $c['id'],
'isp_target' => $c['isp_target'],
'send_method' => $c['send_method'],
'return_path' => 'bounce@[domain]',
'from_name' => $c['from_name'] ?: '"[Name]"',
'from_email' => $c['from_email'] ?: 'info@[domain]',
'subject' => $c['subject_template'] ?: 'Your notification',
'static_domain' => $c['domain_used'] ?: '',
'content_type' => $c['content_type'] ?: 'multipart/alternative',
'charset' => $c['charset'] ?: 'utf-8',
'encoding' => $c['encoding'] ?: 'quoted-printable',
'headers' => $headers,
'body' => $c['body_template'] ?: '',
'inbox_rate' => $c['inbox_rate'],
'is_winner' => $c['is_winner']
]);
break;
default:
echo json_encode(['actions' => ['list', 'get_config'], 'usage' => '?action=get_config&config_id=8']);
}