<?php
// tracking.php

// Get the recipient's UUID or email and post ID from the query string
$uuid = isset($_GET['uuid']) ? $_GET['uuid'] : null;
$email = isset($_GET['email']) ? $_GET['email'] : null;
$post_id = isset($_GET['post_id']) ? $_GET['post_id'] : 'unknown';

// Determine which identifier to use
if ($uuid) {
    $identifier_type = 'uuid';
    $identifier_value = $uuid;
} elseif ($email) {
    $identifier_type = 'email';
    $identifier_value = $email;
} else {
    $identifier_type = 'unknown';
    $identifier_value = 'unknown';
}

// Log tracking data
$log = [
    'identifier_type' => $identifier_type,
    'identifier_value' => $identifier_value,
    'post_id' => $post_id,
    'ip' => $_SERVER['REMOTE_ADDR'],
    'user_agent' => $_SERVER['HTTP_USER_AGENT'],
    'referer' => $_SERVER['HTTP_REFERER'] ?? 'direct',
    'timestamp' => date('Y-m-d H:i:s'),
];

// Create a log file for the current day
$log_date = date('Y-m-d');
$log_dir = '/var/www/dea.strategicinsightsreport.com/logs';
$log_file = $log_dir . '/pixel_log_' . $log_date . '.txt';

// Ensure the logs directory exists
if (!file_exists($log_dir)) {
    mkdir($log_dir, 0777, true);
}

// Write the log data to the daily log file
$file = fopen($log_file, 'a');
fwrite($file, json_encode($log) . PHP_EOL);
fclose($file);

// Output a 1x1 transparent GIF pixel image
header('Content-Type: image/gif');
echo base64_decode('R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==');
?>
