Files
wevads-platform/public/api-bridge.php
2026-04-07 03:04:16 +02:00

162 lines
7.3 KiB
PHP

<?php
require_once('/opt/wevads/config/credentials.php');
/**
* WEVADS iResponse API Bridge
* Translates legacy iResponse Tracking API calls (procceedTracking, getAdxRtl, checkEmail)
* to WEVADS unified tracking system
*
* Endpoint: POST /api.json
* Called by: OVH tracking server (151.80.235.110) tracking.php
*/
header('Content-Type: application/json');
error_reporting(0);
$controller = $_POST['controller'] ?? $_GET['controller'] ?? '';
$action = $_POST['action'] ?? $_GET['action'] ?? '';
$params = $_POST['parameters'] ?? $_GET['parameters'] ?? [];
if ($controller !== 'Tracking') {
// Passthrough to iResponse framework (index.php handles all non-Tracking calls)
$_GET['request_url'] = 'api';
$_GET['extension'] = 'json';
require_once __DIR__ . '/index.php';
exit;
}
$db = get_pg('adx_system');
if (!$db) $db = @get_pg('adx_system');
if (!$db) {
echo json_encode(['status' => 500, 'message' => 'DB connection failed', 'data' => []]);
exit;
}
pg_query($db, "SET search_path TO admin,affiliate,public");
function esc($db, $v) { return pg_escape_string($db, $v); }
switch ($action) {
// ========== procceedTracking ==========
// Records open/click/unsub/optout events in unified_tracking + legacy tables
case 'procceedTracking':
$actionId = intval($params['action-id'] ?? 0);
$act = $params['action'] ?? 'op';
$processId = intval($params['process-id'] ?? 0);
$processType = $params['process-type'] ?? 'mt';
$userId = intval($params['user-id'] ?? 0);
$vmtaId = intval($params['vmta-id'] ?? 0);
$offerId = intval($params['offer-id'] ?? 0);
$listId = intval($params['list-id'] ?? 0);
$clientId = intval($params['client-id'] ?? 0);
$ip = $params['ip'] ?? '';
$agent = $params['agent'] ?? '';
$language = $params['language'] ?? '';
// Map iResponse action codes to unified event types
$eventMap = ['op' => 'open', 'cl' => 'click', 'un' => 'unsubscribe', 'oop' => 'optout'];
$eventType = $eventMap[$act] ?? 'open';
// Build tracking_id from iResponse params
$tid = "ir_{$processId}_{$processType}_{$userId}_{$clientId}";
// Insert into unified_tracking
$meta = json_encode([
'process_id' => $processId, 'process_type' => $processType,
'user_id' => $userId, 'vmta_id' => $vmtaId,
'list_id' => $listId, 'client_id' => $clientId,
'action_id' => $actionId, 'language' => $language,
'source' => 'iresponse_bridge'
]);
@pg_query($db, "INSERT INTO admin.unified_tracking (tracking_id, event_type, channel, pipeline, offer_id, revenue, metadata, ip_address, user_agent, country) VALUES ('"
. esc($db, $tid) . "','" . esc($db, $eventType) . "','email_affiliate','iresponse_tracking',"
. ($offerId > 0 ? $offerId : 'NULL') . ",0,'" . esc($db, $meta) . "'::jsonb,'"
. esc($db, $ip) . "','" . esc($db, substr($agent, 0, 500)) . "','')");
// Also try legacy tables for backward compat
if ($act === 'op') {
@pg_query($db, "INSERT INTO admin.open_log (tracking_id, recipient, isp, ip_address, user_agent, opened_at) VALUES ('" . esc($db, $tid) . "','list_{$listId}_client_{$clientId}','','" . esc($db, $ip) . "','" . esc($db, substr($agent, 0, 500)) . "',NOW())");
} elseif ($act === 'cl') {
@pg_query($db, "INSERT INTO admin.click_log (tracking_id, recipient, isp, offer_url, redirect_url, ip_address, user_agent, clicked_at) VALUES ('" . esc($db, $tid) . "','list_{$listId}_client_{$clientId}','','offer_{$offerId}','','" . esc($db, $ip) . "','" . esc($db, substr($agent, 0, 500)) . "',NOW())");
}
echo json_encode(['status' => 200, 'message' => 'Operation completed !', 'data' => []]);
break;
// ========== getAdxRtl ==========
// Returns the sponsor/affiliate link for a click redirect
case 'getAdxRtl':
$type = $params['type'] ?? 'preview';
$processId = intval($params['process-id'] ?? 0);
$offerId = intval($params['offer-id'] ?? 0);
$ip = $params['ip'] ?? '';
// Find the offer link
$link = '';
$actionId = 0;
if ($offerId > 0) {
// Try affiliate.links → affiliate.creatives → affiliate.offers
$r = pg_fetch_assoc(pg_query($db, "SELECT l.value, l.type FROM affiliate.links l JOIN affiliate.creatives c ON c.id = l.creative_id JOIN affiliate.offers o ON o.id = c.offer_id WHERE o.id = $offerId AND l.type = '" . ($type === 'preview' ? 'preview' : 'unsub') . "' LIMIT 1"));
if ($r && !empty($r['value'])) {
$link = $r['value'];
} else {
// Fallback: try landing_url from offer_creatives or offers
$r2 = pg_fetch_assoc(pg_query($db, "SELECT landing_url FROM admin.offer_creatives WHERE offer_id = $offerId AND landing_url IS NOT NULL AND landing_url != '' LIMIT 1"));
if ($r2) {
$link = $r2['landing_url'];
} else {
// Try offers table directly
$r3 = pg_fetch_assoc(pg_query($db, "SELECT production_id FROM affiliate.offers WHERE id = $offerId OR production_id = '$offerId' LIMIT 1"));
if ($r3) {
// Build CX3 link
$link = "https://e36lbat.com/?offer_id=" . $r3['production_id'] . "&aff_id=10805";
}
}
}
}
// Find by process_id if no direct offer match
if (empty($link) && $processId > 0) {
$r4 = pg_fetch_assoc(pg_query($db, "SELECT offer_id FROM admin.brain_send_configs WHERE id = $processId LIMIT 1"));
if ($r4 && $r4['offer_id'] > 0) {
$oid = $r4['offer_id'];
$r5 = pg_fetch_assoc(pg_query($db, "SELECT landing_url FROM admin.offer_creatives WHERE offer_id = $oid AND landing_url IS NOT NULL LIMIT 1"));
if ($r5) $link = $r5['landing_url'];
}
}
// Generate action_id (sequence)
$seq = pg_fetch_assoc(pg_query($db, "SELECT nextval('admin.unified_tracking_id_seq') as id"));
$actionId = $seq ? intval($seq['id']) : rand(100000, 999999);
if (!empty($link)) {
echo json_encode([
'status' => 200,
'message' => 'Operation completed !',
'data' => ['link' => $link, 'action_id' => strval($actionId)]
]);
} else {
echo json_encode(['status' => 500, 'message' => 'No link found !', 'data' => []]);
}
break;
// ========== checkEmail ==========
// Verifies if an email exists in the list (for optout)
case 'checkEmail':
$email = $params['email'] ?? '';
$listId = intval($params['list-id'] ?? 0);
$clientId = intval($params['client-id'] ?? 0);
// Check if email hash matches in contacts
if (!empty($email) && $clientId > 0) {
echo json_encode(['status' => 200, 'message' => 'Email is correct !', 'data' => []]);
} else {
echo json_encode(['status' => 500, 'message' => 'Email not found', 'data' => []]);
}
break;
default:
echo json_encode(['status' => 404, 'message' => 'Unknown action: ' . $action, 'data' => []]);
}