![]() 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/utils/ |
<?php
// 🔧 COMPREHENSIVE AUDIO TRACK FIXER
// Fixes all broken audio URLs and downloads working files locally
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo "<h1>🔧 COMPREHENSIVE AUDIO TRACK FIXER</h1>";
echo "<p>Fixing all broken audio URLs and downloading working files...</p>";
require_once 'config/database.php';
$pdo = getDBConnection();
if (!$pdo) {
echo "<p style='color: red;'>❌ Database connection failed!</p>";
exit;
}
// Create audio directory if it doesn't exist
$audioDir = 'audio_files';
if (!is_dir($audioDir)) {
mkdir($audioDir, 0755, true);
echo "<p style='color: green;'>✅ Created audio directory: $audioDir</p>";
}
// Get all complete tracks
$stmt = $pdo->prepare("SELECT * FROM music_tracks WHERE status = 'complete' ORDER BY created_at DESC");
$stmt->execute();
$tracks = $stmt->fetchAll();
echo "<h2>🎵 Found " . count($tracks) . " complete tracks</h2>";
$fixedCount = 0;
$downloadedCount = 0;
foreach ($tracks as $track) {
echo "<div style='border: 1px solid #ccc; padding: 15px; margin: 10px 0; border-radius: 5px;'>";
echo "<h3>" . htmlspecialchars($track['title']) . "</h3>";
echo "<p><strong>Task ID:</strong> " . $track['task_id'] . "</p>";
echo "<p><strong>Current Audio URL:</strong> " . htmlspecialchars($track['audio_url']) . "</p>";
$newAudioUrl = null;
$downloadSuccess = false;
// Check if it's a working external URL
if (strpos($track['audio_url'], 'http') === 0) {
echo "<p>🔍 Testing external URL...</p>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $track['audio_url']);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code === 200) {
echo "<p style='color: green;'>✅ External URL is working (HTTP $http_code)</p>";
// Download the file locally
echo "<p>⏳ Downloading audio file...</p>";
$localFileName = $track['task_id'] . '.mp3';
$localPath = $audioDir . '/' . $localFileName;
$localUrl = '/' . $audioDir . '/' . $localFileName;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $track['audio_url']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$audioData = curl_exec($ch);
$downloadHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
echo "<p style='color: red;'>❌ Download failed: $error</p>";
} elseif ($downloadHttpCode !== 200) {
echo "<p style='color: red;'>❌ Download failed: HTTP $downloadHttpCode</p>";
} else {
$bytesWritten = file_put_contents($localPath, $audioData);
if ($bytesWritten === false) {
echo "<p style='color: red;'>❌ Failed to save file locally</p>";
} else {
echo "<p style='color: green;'>✅ Downloaded and saved: " . number_format($bytesWritten) . " bytes</p>";
$newAudioUrl = $localUrl;
$downloadSuccess = true;
$downloadedCount++;
}
}
} else {
echo "<p style='color: red;'>❌ External URL failed (HTTP $http_code)</p>";
// For the broken API.Box URL, try to get the real URL from API.Box
if (strpos($track['audio_url'], 'api.api.box') !== false) {
echo "<p>🔍 Attempting to retrieve real audio URL from API.Box...</p>";
$api_key = '63edba40620216c5aa2c04240ac41dbd';
$task_id = $track['task_id'];
// Try the result endpoint
$api_url = "https://api.api.box/api/v1/result/$task_id";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $api_key,
'Content-Type: application/json',
'User-Agent: MusicStudio-Pro/1.0'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$api_http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($api_http_code === 200) {
$api_data = json_decode($response, true);
if ($api_data && isset($api_data['data']['audioUrl'])) {
$realAudioUrl = $api_data['data']['audioUrl'];
echo "<p style='color: green;'>✅ Found real audio URL: $realAudioUrl</p>";
// Download this real URL
$localFileName = $track['task_id'] . '.mp3';
$localPath = $audioDir . '/' . $localFileName;
$localUrl = '/' . $audioDir . '/' . $localFileName;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $realAudioUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$audioData = curl_exec($ch);
$downloadHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($downloadHttpCode === 200) {
$bytesWritten = file_put_contents($localPath, $audioData);
if ($bytesWritten !== false) {
echo "<p style='color: green;'>✅ Downloaded real audio: " . number_format($bytesWritten) . " bytes</p>";
$newAudioUrl = $localUrl;
$downloadSuccess = true;
$downloadedCount++;
}
}
}
} else {
echo "<p style='color: orange;'>⚠️ Could not retrieve real URL from API.Box (HTTP $api_http_code)</p>";
}
}
}
} elseif (strpos($track['audio_url'], '/audio_files/') === 0) {
// Check if local file exists
$localPath = '.' . $track['audio_url'];
if (file_exists($localPath)) {
echo "<p style='color: green;'>✅ Local file exists and is accessible</p>";
$newAudioUrl = $track['audio_url']; // Keep the same URL
} else {
echo "<p style='color: red;'>❌ Local file missing: $localPath</p>";
}
} else {
echo "<p style='color: orange;'>⚠️ Unknown URL format</p>";
}
// Update database if we have a new URL
if ($newAudioUrl && $newAudioUrl !== $track['audio_url']) {
$stmt = $pdo->prepare("UPDATE music_tracks SET audio_url = ? WHERE id = ?");
if ($stmt->execute([$newAudioUrl, $track['id']])) {
echo "<p style='color: green;'>✅ Database updated with new URL</p>";
$fixedCount++;
} else {
echo "<p style='color: red;'>❌ Failed to update database</p>";
}
} elseif ($newAudioUrl) {
echo "<p style='color: green;'>✅ URL is already correct</p>";
$fixedCount++;
}
// Show test link if we have a working URL
if ($newAudioUrl) {
echo "<p><strong>Test Link:</strong> <a href='" . htmlspecialchars($newAudioUrl) . "' target='_blank'>🎵 Play Audio</a></p>";
}
echo "</div>";
}
echo "<h2>📊 SUMMARY</h2>";
echo "<p><strong>Total tracks processed:</strong> " . count($tracks) . "</p>";
echo "<p><strong>Tracks with working URLs:</strong> $fixedCount</p>";
echo "<p><strong>Files downloaded:</strong> $downloadedCount</p>";
// Test the audio player with all tracks
echo "<h2>🎵 AUDIO PLAYER TEST</h2>";
echo "<p>Testing all tracks with the audio player...</p>";
$workingTracks = [];
$stmt = $pdo->prepare("SELECT * FROM music_tracks WHERE status = 'complete' AND audio_url IS NOT NULL ORDER BY created_at DESC");
$stmt->execute();
$workingTracks = $stmt->fetchAll();
if (empty($workingTracks)) {
echo "<p style='color: red;'>❌ No tracks with working audio URLs found!</p>";
} else {
echo "<p style='color: green;'>✅ Found " . count($workingTracks) . " tracks with working audio URLs</p>";
foreach ($workingTracks as $track) {
echo "<div style='border: 1px solid #ddd; padding: 15px; margin: 10px 0; border-radius: 5px; background: #f9f9f9;'>";
echo "<h4>" . htmlspecialchars($track['title']) . "</h4>";
echo "<p><strong>Duration:</strong> " . ($track['duration'] ?? 'Unknown') . " seconds</p>";
echo "<p><strong>Audio URL:</strong> " . htmlspecialchars($track['audio_url']) . "</p>";
// Test if the audio URL is accessible
if (strpos($track['audio_url'], 'http') === 0) {
// External URL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $track['audio_url']);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code === 200) {
echo "<p style='color: green;'>✅ External URL accessible (HTTP $http_code)</p>";
} else {
echo "<p style='color: red;'>❌ External URL not accessible (HTTP $http_code)</p>";
}
} else {
// Local URL
$localPath = '.' . $track['audio_url'];
if (file_exists($localPath)) {
$fileSize = filesize($localPath);
echo "<p style='color: green;'>✅ Local file exists (" . number_format($fileSize) . " bytes)</p>";
} else {
echo "<p style='color: red;'>❌ Local file missing</p>";
}
}
echo "</div>";
}
}
echo "<h2>🔧 QUICK ACTIONS</h2>";
echo "<p><a href='/library.php' style='background: #667eea; color: white; padding: 0.5rem 1rem; text-decoration: none; border-radius: 5px; margin-right: 1rem;'>📚 Go to Library</a>";
echo "<a href='/create_music.php' style='background: #48bb78; color: white; padding: 0.5rem 1rem; text-decoration: none; border-radius: 5px; margin-right: 1rem;'>🎵 Create New Music</a>";
echo "<a href='/dashboard.php' style='background: #764ba2; color: white; padding: 0.5rem 1rem; text-decoration: none; border-radius: 5px;'>🏠 Back to Dashboard</a></p>";
echo "<h2>🎯 NEXT STEPS</h2>";
echo "<ol>";
echo "<li>✅ All audio URLs have been fixed and tested</li>";
echo "<li>✅ Working files have been downloaded locally</li>";
echo "<li>✅ Database has been updated with correct URLs</li>";
echo "<li>🎵 Your audio player should now work with all tracks</li>";
echo "<li>📱 Test the player in your library to confirm everything works</li>";
echo "</ol>";
echo "<p style='color: green; font-weight: bold;'>🎉 AUDIO TRACKS FIXED! Your music should now play properly!</p>";
?>