![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/soundstudiopro.com/private_html/ |
<?php
// fix_external_audio_urls.php - Find and fix all tracks with external URLs
require_once 'config/database.php';
$pdo = getDBConnection();
if (!$pdo) {
die("❌ Database connection failed\n");
}
// Include the download function from callback.php
function downloadAndStoreAudio($audioUrl, $taskId, $type = 'main', $variationIndex = null) {
if (empty($audioUrl) || !filter_var($audioUrl, FILTER_VALIDATE_URL)) {
return null;
}
// Create audio storage directory
$audioDir = 'audio_files/';
if (!is_dir($audioDir)) {
mkdir($audioDir, 0755, true);
}
// Generate filename
$extension = pathinfo(parse_url($audioUrl, PHP_URL_PATH), PATHINFO_EXTENSION) ?: 'mp3';
if ($type === 'variation' && $variationIndex !== null) {
$filename = "{$taskId}_variation_{$variationIndex}.{$extension}";
} else {
$filename = "{$taskId}.{$extension}";
}
$localPath = $audioDir . $filename;
$webPath = '/audio_files/' . $filename;
// Skip if file already exists
if (file_exists($localPath)) {
return $webPath;
}
// Download the file with progress tracking
$context = stream_context_create([
'http' => [
'timeout' => 120, // Reduced to 2 minutes per file
'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'method' => 'GET',
'header' => 'Accept: audio/*'
]
]);
// Use cURL for better progress tracking and error handling
if (function_exists('curl_init')) {
$ch = curl_init($audioUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$audioContent = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($audioContent === false || $httpCode !== 200 || !empty($error)) {
error_log("Failed to download $audioUrl: HTTP $httpCode, Error: $error");
return null;
}
} else {
// Fallback to file_get_contents
$audioContent = @file_get_contents($audioUrl, false, $context);
if ($audioContent === false) {
return null;
}
}
if (empty($audioContent)) {
return null;
}
// Save the file
if (file_put_contents($localPath, $audioContent, LOCK_EX)) {
chmod($localPath, 0644);
return $webPath;
}
return null;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Fix External Audio URLs</title>
<style>
body { font-family: Arial; padding: 20px; background: #1a1a1a; color: #fff; }
.container { max-width: 1200px; margin: 0 auto; }
h1 { color: #667eea; }
h2 { color: #764ba2; margin-top: 30px; }
table { width: 100%; border-collapse: collapse; margin: 20px 0; background: rgba(255,255,255,0.05); }
th, td { padding: 12px; text-align: left; border: 1px solid rgba(255,255,255,0.1); }
th { background: rgba(102, 126, 234, 0.3); color: #fff; }
tr:hover { background: rgba(255,255,255,0.05); }
.external { color: #ffa500; }
.local { color: #10b981; }
.error { color: #ef4444; }
.success { color: #10b981; }
.btn {
background: linear-gradient(135deg, #667eea, #764ba2);
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
margin: 5px;
text-decoration: none;
display: inline-block;
}
.btn:hover { opacity: 0.9; }
.btn-danger { background: linear-gradient(135deg, #ef4444, #dc2626); }
.stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
margin: 20px 0;
}
.stat-card {
background: rgba(255,255,255,0.05);
padding: 20px;
border-radius: 10px;
border: 1px solid rgba(255,255,255,0.1);
}
.stat-number { font-size: 2rem; font-weight: bold; color: #667eea; }
.stat-label { color: #a0aec0; margin-top: 5px; }
code { background: rgba(0,0,0,0.3); padding: 2px 6px; border-radius: 3px; font-size: 0.9em; }
</style>
</head>
<body>
<div class="container">
<h1>🎵 Fix External Audio URLs</h1>
<?php
$action = $_GET['action'] ?? 'list';
if ($action === 'fix_all') {
// Fix all external URLs
echo "<h2>🔄 Downloading All External Audio Files...</h2>";
echo "<p><strong>Note:</strong> This may take several minutes depending on file sizes and number of tracks.</p>";
echo "<p>Processing tracks... (this page will update as each track is processed)</p>";
// Flush output buffer so user can see progress
if (ob_get_level()) {
ob_end_flush();
}
ob_implicit_flush(true);
$stmt = $pdo->query("
SELECT id, task_id, title, audio_url, user_id
FROM music_tracks
WHERE status = 'complete'
AND audio_url IS NOT NULL
AND audio_url != ''
AND (audio_url LIKE 'http://%' OR audio_url LIKE 'https://%')
AND audio_url NOT LIKE '/audio_files/%'
ORDER BY created_at DESC
");
$tracks = $stmt->fetchAll(PDO::FETCH_ASSOC);
$total = count($tracks);
$success = 0;
$failed = 0;
$skipped = 0;
$current = 0;
echo "<div style='background: rgba(255,255,255,0.05); padding: 20px; border-radius: 10px; margin: 20px 0;'>";
echo "<p><strong>Progress:</strong> <span id='progress'>0</span> / $total tracks</p>";
echo "<div style='width: 100%; background: rgba(255,255,255,0.1); border-radius: 5px; height: 30px; margin: 10px 0;'>";
echo "<div id='progressBar' style='width: 0%; background: linear-gradient(135deg, #667eea, #764ba2); height: 100%; border-radius: 5px; transition: width 0.3s;'></div>";
echo "</div>";
echo "</div>";
echo "<table>";
echo "<tr><th>#</th><th>Title</th><th>Task ID</th><th>Status</th><th>New URL</th></tr>";
foreach ($tracks as $track) {
$current++;
$progress = round(($current / $total) * 100);
echo "<tr>";
echo "<td><strong>$current / $total</strong></td>";
echo "<td>" . htmlspecialchars($track['title']) . "</td>";
echo "<td><code>" . htmlspecialchars($track['task_id']) . "</code></td>";
// Update progress
echo "<script>document.getElementById('progress').textContent = '$current'; document.getElementById('progressBar').style.width = '$progress%';</script>";
flush();
echo "<td>⏳ Downloading...</td>";
echo "<td>-</td>";
echo "</tr>";
flush();
$localUrl = downloadAndStoreAudio($track['audio_url'], $track['task_id'], 'main');
// Update the row
echo "<script>
var row = document.querySelector('table tr:nth-child(" . ($current + 1) . ")');
if (row) {
var statusCell = row.cells[3];
var urlCell = row.cells[4];
";
if ($localUrl) {
// Update database
$updateStmt = $pdo->prepare("UPDATE music_tracks SET audio_url = ? WHERE id = ?");
if ($updateStmt->execute([$localUrl, $track['id']])) {
echo "statusCell.innerHTML = '<span class=\"success\">✅ Downloaded & Updated</span>';";
echo "statusCell.className = 'success';";
echo "urlCell.innerHTML = '<span class=\"local\">" . htmlspecialchars($localUrl, ENT_QUOTES) . "</span>';";
$success++;
} else {
echo "statusCell.innerHTML = '<span class=\"error\">❌ Database update failed</span>';";
echo "statusCell.className = 'error';";
echo "urlCell.innerHTML = '-';";
$failed++;
}
} else {
echo "statusCell.innerHTML = '<span class=\"error\">❌ Download failed</span>';";
echo "statusCell.className = 'error';";
echo "urlCell.innerHTML = '-';";
$failed++;
}
echo "}</script>";
flush();
}
echo "</table>";
echo "<div class='stats'>";
echo "<div class='stat-card'><div class='stat-number'>$total</div><div class='stat-label'>Total Tracks</div></div>";
echo "<div class='stat-card'><div class='stat-number' style='color: #10b981;'>$success</div><div class='stat-label'>Successfully Fixed</div></div>";
echo "<div class='stat-card'><div class='stat-number' style='color: #ef4444;'>$failed</div><div class='stat-label'>Failed</div></div>";
echo "</div>";
echo "<p><a href='?action=list' class='btn'>← Back to List</a></p>";
} elseif ($action === 'fix_one') {
// Fix a single track
$trackId = $_GET['id'] ?? null;
if ($trackId) {
$stmt = $pdo->prepare("SELECT id, task_id, title, audio_url FROM music_tracks WHERE id = ?");
$stmt->execute([$trackId]);
$track = $stmt->fetch(PDO::FETCH_ASSOC);
if ($track) {
echo "<h2>Fixing: " . htmlspecialchars($track['title']) . "</h2>";
$localUrl = downloadAndStoreAudio($track['audio_url'], $track['task_id'], 'main');
if ($localUrl) {
$updateStmt = $pdo->prepare("UPDATE music_tracks SET audio_url = ? WHERE id = ?");
if ($updateStmt->execute([$localUrl, $track['id']])) {
echo "<p class='success'>✅ Successfully downloaded and updated!</p>";
echo "<p><strong>New URL:</strong> <span class='local'>$localUrl</span></p>";
} else {
echo "<p class='error'>❌ Failed to update database</p>";
}
} else {
echo "<p class='error'>❌ Failed to download audio file</p>";
}
}
}
echo "<p><a href='?action=list' class='btn'>← Back to List</a></p>";
} else {
// List all tracks with external URLs
$stmt = $pdo->query("
SELECT
mt.id,
mt.task_id,
mt.title,
mt.audio_url,
mt.created_at,
u.name as artist_name
FROM music_tracks mt
LEFT JOIN users u ON mt.user_id = u.id
WHERE mt.status = 'complete'
AND mt.audio_url IS NOT NULL
AND mt.audio_url != ''
AND (mt.audio_url LIKE 'http://%' OR mt.audio_url LIKE 'https://%')
AND mt.audio_url NOT LIKE '/audio_files/%'
ORDER BY mt.created_at DESC
");
$tracks = $stmt->fetchAll(PDO::FETCH_ASSOC);
$total = count($tracks);
echo "<div class='stats'>";
echo "<div class='stat-card'><div class='stat-number'>$total</div><div class='stat-label'>Tracks with External URLs</div></div>";
echo "</div>";
if ($total > 0) {
echo "<p>";
echo "<a href='?action=fix_all' class='btn' onclick='return confirm(\"This will download all external audio files. This may take a while. Continue?\");'>🔧 Fix All Tracks</a>";
echo "</p>";
echo "<h2>📋 Tracks with External URLs</h2>";
echo "<table>";
echo "<tr>";
echo "<th>Title</th>";
echo "<th>Artist</th>";
echo "<th>Task ID</th>";
echo "<th>External URL</th>";
echo "<th>Created</th>";
echo "<th>Action</th>";
echo "</tr>";
foreach ($tracks as $track) {
echo "<tr>";
echo "<td><strong>" . htmlspecialchars($track['title']) . "</strong></td>";
echo "<td>" . htmlspecialchars($track['artist_name'] ?? 'Unknown') . "</td>";
echo "<td><code>" . htmlspecialchars($track['task_id']) . "</code></td>";
echo "<td class='external'>" . htmlspecialchars(substr($track['audio_url'], 0, 60)) . "...</td>";
echo "<td>" . date('M j, Y', strtotime($track['created_at'])) . "</td>";
echo "<td><a href='?action=fix_one&id=" . $track['id'] . "' class='btn' style='padding: 5px 10px; font-size: 0.9rem;'>Fix This One</a></td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "<p class='success'>✅ Great! No tracks with external URLs found. All audio files are hosted locally.</p>";
}
}
?>
</div>
</body>
</html>