Files
wevads-platform/scripts/s3-creative-gen.php
2026-02-26 04:53:11 +01:00

124 lines
5.1 KiB
PHP

<?php
/**
* S3 CREATIVE GENERATOR
* Generates S3-hosted HTML redirectors + image placeholders for each active offer
* Pattern: PH1=S3 HTML (JS redirect), PH2=S3 image
* Usage: php s3-creative-gen.php [generate|status|test]
*/
require_once '/opt/wevads/vendor/autoload.php';
use Aws\S3\S3Client;
$pdo = new PDO("pgsql:host=localhost;dbname=adx_system","admin","admin123");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$action = $argv[1] ?? 'status';
// S3 Config
$aws = $pdo->query("SELECT access_key, secret_key FROM admin.aws_accounts WHERE name='AWS-S3'")->fetch(PDO::FETCH_ASSOC);
$s3cfg = $pdo->query("SELECT bucket_name, region, base_url FROM admin.s3_config WHERE is_active=true LIMIT 1")->fetch(PDO::FETCH_ASSOC);
$s3 = new S3Client([
'version' => 'latest',
'region' => $s3cfg['region'] ?? 'eu-north-1',
'credentials' => [
'key' => $aws['access_key'],
'secret' => $aws['secret_key'],
]
]);
$BUCKET = $s3cfg['bucket_name'] ?? 'wevads-tracking';
$BASE_URL = $s3cfg['base_url'] ?? "https://$BUCKET.s3.eu-north-1.amazonaws.com";
$TRACK_SERVER = 'http://151.80.235.110'; // OVH tracking server
switch($action) {
case 'generate':
// Get active offers with landing_url
$offers = $pdo->query("SELECT DISTINCT oc.offer_id, ao.name as offer_name, oc.landing_url, oc.id as creative_id
FROM admin.offer_creatives oc
LEFT JOIN affiliate.offers ao ON ao.id = oc.offer_id
WHERE oc.status='active' AND oc.landing_url IS NOT NULL AND oc.landing_url != ''
AND (oc.s3_image_url IS NULL OR oc.s3_image_url LIKE '%pixel.gif')
ORDER BY oc.offer_id LIMIT 50")->fetchAll(PDO::FETCH_ASSOC);
$generated = 0;
foreach ($offers as $o) {
$uid = substr(md5($o['creative_id'] . time()), 0, 12);
// 1. Generate S3 HTML redirector
$htmlKey = "creatives/{$uid}.html";
$htmlContent = '<script>document.location.href="' . $TRACK_SERVER . '/track.php?e=click&ch=s3_redir&"+window.location.href.split("#")[1];</script>';
try {
$s3->putObject([
'Bucket' => $BUCKET,
'Key' => $htmlKey,
'Body' => $htmlContent,
'ContentType' => 'text/html',
]);
$s3HtmlUrl = "$BASE_URL/$htmlKey";
// 2. Generate placeholder image (1x1 pixel for now, will be replaced with real creatives)
$imgKey = "creatives/{$uid}_img.png";
// Upload the blank.png as placeholder
$blankPng = @file_get_contents("$BASE_URL/blank.png");
if ($blankPng) {
$s3->putObject([
'Bucket' => $BUCKET,
'Key' => $imgKey,
'Body' => $blankPng,
'ContentType' => 'image/png',
]);
}
$s3ImgUrl = "$BASE_URL/$imgKey";
// 3. Update offer_creative with S3 URLs
$pdo->prepare("UPDATE admin.offer_creatives SET s3_image_url = ? WHERE id = ?")
->execute([$s3ImgUrl, $o['creative_id']]);
// 4. Log to s3_assets
$pdo->prepare("INSERT INTO admin.s3_assets (sponsor_id, asset_type, filename, s3_key, s3_url, cdn_url, mime_type, bucket, uploaded_at)
VALUES (?, 'html', ?, ?, ?, ?, 'text/html', ?, NOW())
ON CONFLICT DO NOTHING")
->execute([$o['offer_id'], basename($htmlKey), $htmlKey, $s3HtmlUrl, $s3HtmlUrl, $BUCKET]);
$pdo->prepare("INSERT INTO admin.s3_assets (sponsor_id, asset_type, filename, s3_key, s3_url, cdn_url, mime_type, bucket, uploaded_at)
VALUES (?, 'image', ?, ?, ?, ?, 'image/png', ?, NOW())
ON CONFLICT DO NOTHING")
->execute([$o['offer_id'], basename($imgKey), $imgKey, $s3ImgUrl, $s3ImgUrl, $BUCKET]);
$generated++;
echo " OK offer #{$o['offer_id']}: HTML={$s3HtmlUrl} IMG={$s3ImgUrl}\n";
} catch (Exception $e) {
echo " FAIL offer #{$o['offer_id']}: " . $e->getMessage() . "\n";
}
}
echo "\nGenerated: $generated/" . count($offers) . "\n";
break;
case 'test':
// Test S3 access
echo "Bucket: $BUCKET\n";
echo "Base URL: $BASE_URL\n";
$result = $s3->listObjectsV2(['Bucket' => $BUCKET, 'MaxKeys' => 5]);
echo "Objects: " . count($result['Contents']) . "\n";
foreach ($result['Contents'] as $obj) {
echo " {$obj['Key']} ({$obj['Size']} bytes)\n";
}
break;
case 'status':
default:
$stats = [
'total_creatives' => $pdo->query("SELECT COUNT(*) FROM admin.offer_creatives WHERE status='active'")->fetchColumn(),
'with_s3' => $pdo->query("SELECT COUNT(*) FROM admin.offer_creatives WHERE s3_image_url IS NOT NULL AND s3_image_url NOT LIKE '%pixel.gif'")->fetchColumn(),
's3_assets' => $pdo->query("SELECT COUNT(*) FROM admin.s3_assets")->fetchColumn(),
'offers_active' => $pdo->query("SELECT COUNT(*) FROM affiliate.offers WHERE status='active'")->fetchColumn(),
];
echo json_encode($stats, JSON_PRETTY_PRINT) . "\n";
break;
}