142 lines
4.3 KiB
PHP
142 lines
4.3 KiB
PHP
<?php
|
|
/**
|
|
* TRACKING ENDPOINT - FIXED VERSION
|
|
* Handles opens (o), clicks (c), unsubscribes (u), conversions (l)
|
|
* Now properly redirects to Everflow/CX3 tracking URLs
|
|
*/
|
|
$dbHost = 'localhost';
|
|
$dbName = 'adx_system';
|
|
$dbUser = 'admin';
|
|
$dbPass = 'admin123';
|
|
|
|
try {
|
|
$pdo = new PDO("pgsql:host=$dbHost;dbname=$dbName", $dbUser, $dbPass);
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
} catch (PDOException $e) {
|
|
error_log("DB Error: " . $e->getMessage());
|
|
exit;
|
|
}
|
|
|
|
$uri = $_SERVER['REQUEST_URI'];
|
|
$parts = explode('/', trim($uri, '/'));
|
|
|
|
if (count($parts) < 2) {
|
|
http_response_code(400);
|
|
exit;
|
|
}
|
|
|
|
$action = $parts[0];
|
|
$code = $parts[1];
|
|
|
|
// Get tracking record
|
|
$stmt = $pdo->prepare("SELECT * FROM admin.seed_tracking WHERE tracking_code = ?");
|
|
$stmt->execute([$code]);
|
|
$record = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$record) {
|
|
http_response_code(404);
|
|
exit;
|
|
}
|
|
|
|
switch ($action) {
|
|
case 'o': // Open
|
|
$pdo->prepare("
|
|
UPDATE admin.seed_tracking
|
|
SET opened_at = COALESCE(opened_at, NOW()),
|
|
open_count = open_count + 1,
|
|
inbox_placement = 'inbox'
|
|
WHERE tracking_code = ?
|
|
")->execute([$code]);
|
|
// Return 1x1 transparent gif
|
|
header('Content-Type: image/gif');
|
|
echo base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7');
|
|
break;
|
|
|
|
case 'c': // Click
|
|
$pdo->prepare("
|
|
UPDATE admin.seed_tracking
|
|
SET clicked_at = COALESCE(clicked_at, NOW()),
|
|
click_count = click_count + 1,
|
|
inbox_placement = 'inbox'
|
|
WHERE tracking_code = ?
|
|
")->execute([$code]);
|
|
|
|
// Get redirect URL - check multiple sources
|
|
$url = null;
|
|
$offerId = $record['offer_id'];
|
|
|
|
// 1. Try affiliate.links (preview type) - contains Everflow/CX3 URLs
|
|
$linkStmt = $pdo->prepare("
|
|
SELECT l.value
|
|
FROM affiliate.links l
|
|
WHERE l.offer_id = ? AND l.type = 'preview' AND l.value IS NOT NULL AND l.value != ''
|
|
LIMIT 1
|
|
");
|
|
$linkStmt->execute([$offerId]);
|
|
$link = $linkStmt->fetch(PDO::FETCH_ASSOC);
|
|
if ($link && !empty($link['value'])) {
|
|
$url = $link['value'];
|
|
}
|
|
|
|
// 2. Fallback: Try admin.affiliate_offers
|
|
if (!$url) {
|
|
$offerStmt = $pdo->prepare("
|
|
SELECT tracking_url, landing_page
|
|
FROM admin.affiliate_offers
|
|
WHERE id = ? OR offer_id = ?
|
|
");
|
|
$offerStmt->execute([$offerId, $offerId]);
|
|
$offer = $offerStmt->fetch(PDO::FETCH_ASSOC);
|
|
if ($offer) {
|
|
$url = $offer['tracking_url'] ?: $offer['landing_page'];
|
|
}
|
|
}
|
|
|
|
// 3. Fallback: Try affiliate.offers by production_id
|
|
if (!$url) {
|
|
$offerStmt = $pdo->prepare("
|
|
SELECT l.value
|
|
FROM affiliate.offers o
|
|
JOIN affiliate.links l ON l.offer_id = o.id
|
|
WHERE o.production_id = ? AND l.type = 'preview'
|
|
LIMIT 1
|
|
");
|
|
$offerStmt->execute([$offerId]);
|
|
$offer = $offerStmt->fetch(PDO::FETCH_ASSOC);
|
|
if ($offer && !empty($offer['value'])) {
|
|
$url = $offer['value'];
|
|
}
|
|
}
|
|
|
|
// 4. Log if no URL found (for debugging)
|
|
if (!$url) {
|
|
error_log("TRACKING: No redirect URL found for offer_id=$offerId, code=$code");
|
|
$url = 'https://example.com';
|
|
} else {
|
|
error_log("TRACKING: Redirecting to $url for offer_id=$offerId");
|
|
}
|
|
|
|
header("Location: $url", true, 302);
|
|
exit;
|
|
break;
|
|
|
|
case 'l': // Lead/Conversion (postback)
|
|
$pdo->prepare("
|
|
UPDATE admin.seed_tracking
|
|
SET converted_at = COALESCE(converted_at, NOW())
|
|
WHERE tracking_code = ?
|
|
")->execute([$code]);
|
|
echo "OK";
|
|
break;
|
|
|
|
case 'u': // Unsubscribe
|
|
$pdo->prepare("
|
|
UPDATE admin.brain_seeds SET is_active = false WHERE id = ?
|
|
")->execute([$record['seed_id']]);
|
|
echo "<html><body><h2>You have been unsubscribed.</h2></body></html>";
|
|
break;
|
|
|
|
default:
|
|
http_response_code(400);
|
|
}
|