Files
wevads-platform/public/s3_upload_direct.php

201 lines
7.1 KiB
PHP

<?php
header('Content-Type: application/json');
error_reporting(0);
// Load AWS SDK
$autoloadPaths = [
'/opt/wevads/vendor/autoload.php',
__DIR__ . '/../vendor/autoload.php'
];
foreach ($autoloadPaths as $p) { if (file_exists($p)) { require_once $p; break; } }
// Load config
$cfgPath = '/opt/wevads/app/config/amazoncloud.crd.json';
if (!file_exists($cfgPath)) { echo json_encode(['success'=>false,'error'=>'AWS config not found']); exit; }
$cfg = json_decode(file_get_contents($cfgPath), true);
function getS3() {
global $cfg;
return new Aws\S3\S3Client([
'region' => $cfg['region'],
'version' => 'latest',
'credentials' => ['key' => $cfg['AWSAccessKeyId'], 'secret' => $cfg['AWSSecretKey']],
'suppress_php_deprecation_warning' => true
]);
}
$action = $_POST['action'] ?? '';
// ===== ACTION: uploadToS3 =====
if ($action === 'uploadToS3') {
if (!isset($_FILES['file']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK) {
echo json_encode(['success'=>false,'error'=>'No file or upload error']); exit;
}
$file = $_FILES['file'];
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$folder = $_POST['folder'] ?? 'creatives';
// Use wevads-tracking bucket (same as native Production.php flow)
$bucket = $cfg['BucketName'] ?? 'wevads-tracking';
$customBucket = $_POST['bucket'] ?? '';
if ($customBucket && $customBucket !== 'auto') $bucket = $customBucket;
$key = $folder . '/' . uniqid() . '_' . preg_replace('/[^a-zA-Z0-9._-]/', '', $file['name']);
try {
$s3 = getS3();
$result = $s3->putObject([
'Bucket' => $bucket,
'Key' => $key,
'SourceFile' => $file['tmp_name'],
'ContentType' => $file['type']
]);
$url = sprintf('https://%s.s3.%s.amazonaws.com/%s', $bucket, $cfg['region'], $key);
$response = [
'success' => true,
'url' => $url,
'image_url' => $url,
'bucket' => $bucket,
'key' => $key,
'size' => $file['size'],
'type' => $ext
];
$redirectUrl = $_POST['redirect-url'] ?? '';
if ($redirectUrl) {
$htmlKey = $folder . '/' . uniqid() . '.html';
$htmlBody = "<script>document.location.href = '" . $redirectUrl . "/'+window.location.href.split('?')[1];</script>";
$s3->putObject(['Bucket' => $bucket, 'Key' => $htmlKey, 'Body' => $htmlBody, 'ContentType' => 'text/html']);
$response['html_url'] = sprintf('https://%s.s3.%s.amazonaws.com/%s', $bucket, $cfg['region'], $htmlKey);
}
echo json_encode($response);
} catch (Exception $e) {
echo json_encode(['success'=>false,'error'=>'S3 upload failed: '.$e->getMessage()]);
}
exit;
}
// ===== ACTION: generateRedirect =====
if ($action === 'generateRedirect') {
$imageUrl = $_POST['image_url'] ?? '';
$redirectUrl = $_POST['redirect_url'] ?? 'http://151.80.235.110';
if (!$imageUrl) {
echo json_encode(['success'=>false,'error'=>'No image_url provided']); exit;
}
$bucket = $cfg['BucketName'] ?? 'wevads-tracking';
$htmlKey = 'redirects/' . uniqid() . '.html';
// Native WEVADS S3 redirect HTML
// When clicked: S3 HTML -> JS redirect -> tracking server (151.80.235.110) -> click.php -> sponsor
// The ?[url] part is appended by the email template at send time
$htmlContent = "<script>document.location.href = '" . $redirectUrl . "/'+window.location.href.split('?')[1];</script>";
try {
$s3 = getS3();
$result = $s3->putObject([
'Bucket' => $bucket,
'Key' => $htmlKey,
'Body' => $htmlContent,
'ContentType' => 'text/html'
]);
$pageUrl = sprintf('https://%s.s3.%s.amazonaws.com/%s', $bucket, $cfg['region'], $htmlKey);
echo json_encode([
'success' => true,
'page_url' => $pageUrl,
'redirect_url' => $redirectUrl,
'html_key' => $htmlKey
]);
} catch (Exception $e) {
echo json_encode(['success'=>false,'error'=>'S3 redirect failed: '.$e->getMessage()]);
}
exit;
}
// ===== ACTION: createBucketUpload (native Production.php style - unique bucket per creative) =====
if ($action === 'createBucketUpload') {
if (!isset($_FILES['file']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK) {
echo json_encode(['success'=>false,'error'=>'No file']); exit;
}
$file = $_FILES['file'];
$redirectUrl = $_POST['redirect_url'] ?? 'http://151.80.235.110';
// Create unique bucket name (native WEVADS pattern: ff + hex)
$uniqueBucket = 'ff' . bin2hex(random_bytes(10));
try {
$s3 = getS3();
// Create bucket
$s3->createBucket([
'Bucket' => $uniqueBucket,
'CreateBucketConfiguration' => ['LocationConstraint' => $cfg['region']]
]);
// Disable block public access
$s3->putPublicAccessBlock([
'Bucket' => $uniqueBucket,
'PublicAccessBlockConfiguration' => [
'BlockPublicAcls' => false,
'IgnorePublicAcls' => false,
'BlockPublicPolicy' => false,
'RestrictPublicBuckets' => false
]
]);
// Set bucket policy for public read
$policy = json_encode([
'Version' => '2012-10-17',
'Statement' => [[
'Sid' => 'PublicRead',
'Effect' => 'Allow',
'Principal' => '*',
'Action' => 's3:GetObject',
'Resource' => "arn:aws:s3:::$uniqueBucket/*"
]]
]);
$s3->putBucketPolicy(['Bucket' => $uniqueBucket, 'Policy' => $policy]);
// Upload image
$imgKey = uniqid() . '_' . preg_replace('/[^a-zA-Z0-9._-]/', '', basename($file['name']));
$s3->putObject([
'Bucket' => $uniqueBucket,
'Key' => $imgKey,
'SourceFile' => $file['tmp_name'],
'ContentType' => $file['type']
]);
// Upload HTML redirect
$htmlKey = uniqid() . '.html';
$htmlContent = "<script>document.location.href = '" . $redirectUrl . "/'+window.location.href.split('?')[1];</script>";
$s3->putObject([
'Bucket' => $uniqueBucket,
'Key' => $htmlKey,
'Body' => $htmlContent,
'ContentType' => 'text/html'
]);
$baseUrl = sprintf('https://%s.s3.%s.amazonaws.com', $uniqueBucket, $cfg['region']);
echo json_encode([
'success' => true,
'bucket' => $uniqueBucket,
'image_url' => "$baseUrl/$imgKey",
'page_url' => "$baseUrl/$htmlKey",
'redirect_url' => $redirectUrl
]);
} catch (Exception $e) {
echo json_encode(['success'=>false,'error'=>'Bucket creation failed: '.$e->getMessage()]);
}
exit;
}
// Unknown action
echo json_encode(['success'=>false,'error'=>'Unknown action: '.$action]);