<?php
/**
 * track.php — Email tracking (open pixel + click redirect)
 * 
 * Open tracking: <img src="https://eu261compensation.com/track.php?t=o&id=CUSTOMER_ID&src=ej">
 * Click tracking: <a href="https://eu261compensation.com/track.php?t=c&id=CUSTOMER_ID&src=ej&url=ENCODED_URL">
 * 
 * src=ej  → easyJet (customers table)
 * src=wz  → Wizz Air (wz_passengers table)
 */

$DB_HOST='localhost';$DB_NAME='deeldabr_eu261';$DB_USER='deeldabr_eu261';$DB_PASS='ASDjaS(DCWEFHjewke3we4f@#';
try{
  $pdo=new PDO("mysql:host=$DB_HOST;dbname=$DB_NAME;charset=utf8mb4",$DB_USER,$DB_PASS,[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);
}catch(PDOException $e){
  if(isset($_GET['t'])&&$_GET['t']==='o'){header('Content-Type:image/gif');echo base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7');exit;}
  header('Location:https://eu261compensation.com');exit;
}

// Ensure table exists with source column
$pdo->exec("CREATE TABLE IF NOT EXISTS email_tracking (
  id INT AUTO_INCREMENT PRIMARY KEY,
  customer_id INT NOT NULL,
  source VARCHAR(20) NOT NULL DEFAULT 'customers',
  event_type ENUM('open','click') NOT NULL,
  ip VARCHAR(45) DEFAULT '',
  user_agent VARCHAR(500) DEFAULT '',
  tracked_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_customer (customer_id),
  INDEX idx_source (source),
  INDEX idx_type (event_type),
  INDEX idx_time (tracked_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
// Add source column if missing (existing installs)
try{$pdo->exec("ALTER TABLE email_tracking ADD COLUMN source VARCHAR(20) NOT NULL DEFAULT 'customers' AFTER customer_id, ADD INDEX idx_source (source)");}catch(Exception $e){}

$type=$_GET['t']??'';
$customerId=intval($_GET['id']??0);
$source=($_GET['src']??'ej')==='wz'?'wz_passengers':'customers';

if(!$customerId){
  if($type==='o'){header('Content-Type:image/gif');echo base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7');exit;}
  header('Location:https://eu261compensation.com');exit;
}

$ip=$_SERVER['REMOTE_ADDR']??'';
$ua=substr($_SERVER['HTTP_USER_AGENT']??'',0,500);

if($type==='o'){
  try{
    $pdo->prepare("INSERT INTO email_tracking (customer_id,source,event_type,ip,user_agent) VALUES (?,?,'open',?,?)")
      ->execute([$customerId,$source,$ip,$ua]);
  }catch(Exception $e){}
  
  header('Content-Type:image/gif');
  header('Cache-Control:no-store,no-cache,must-revalidate,max-age=0');
  header('Pragma:no-cache');
  echo base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7');
  exit;
}

if($type==='c'){
  $url=$_GET['url']??'';
  
  // Safety: if URL got double-encoded, decode it once more
  if($url && strpos($url,'%')!==false && !preg_match('#^https?://#',$url)){
    $url=urldecode($url);
  }
  
  try{
    $pdo->prepare("INSERT INTO email_tracking (customer_id,source,event_type,ip,user_agent) VALUES (?,?,'click',?,?)")
      ->execute([$customerId,$source,$ip,$ua]);
  }catch(Exception $e){}
  
  if($url && preg_match('#^https?://#',$url)){
    header('Location:'.$url);
  }else{
    header('Location:https://eu261compensation.com');
  }
  exit;
}

header('Location:https://eu261compensation.com');
exit;