T.ME/BIBIL_0DAY
CasperSecurity


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/public_html/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/gositeme/domains/soundstudiopro.com/public_html/download_all_variations.php
<?php
/**
 * Download all variation audio files from external URLs to local storage
 * Updates database to use local paths instead of external URLs
 */

session_start();
require_once 'config/database.php';

// Check if user is admin
if (!isset($_SESSION['user_id']) || !isset($_SESSION['is_admin']) || !$_SESSION['is_admin']) {
    die("Unauthorized - Admin access required");
}

$pdo = getDBConnection();

// Get all variations - we'll check each one to see if it needs downloading
$stmt = $pdo->query("
    SELECT 
        av.id,
        av.track_id,
        av.variation_index,
        av.audio_url,
        av.stream_audio_url,
        av.source_audio_url,
        mt.task_id
    FROM audio_variations av
    JOIN music_tracks mt ON av.track_id = mt.id
    WHERE (av.stream_audio_url LIKE 'http%' OR av.source_audio_url LIKE 'http%' OR av.audio_url LIKE 'http%')
    ORDER BY av.track_id, av.variation_index
");

$variations = $stmt->fetchAll();
$total = count($variations);

echo "<h1>Downloading Variation Audio Files</h1>";
echo "<p>Found <strong>$total</strong> variations with external URLs</p>";
echo "<hr>";

$downloaded = 0;
$failed = 0;
$skipped = 0;

// Function to download and store audio
function downloadAndStoreAudio($audioUrl, $taskId, $variationIndex) {
    if (empty($audioUrl) || !filter_var($audioUrl, FILTER_VALIDATE_URL)) {
        return null;
    }
    
    // CRITICAL FIX: musicfile.api.box URLs need .mp3 extension
    // If URL doesn't have an extension, add .mp3
    $parsedUrl = parse_url($audioUrl);
    $path = $parsedUrl['path'] ?? '';
    $extension = pathinfo($path, PATHINFO_EXTENSION);
    
    if (empty($extension) && strpos($audioUrl, 'musicfile.api.box') !== false) {
        $audioUrl .= '.mp3';
    }
    
    $audioDir = 'audio_files/';
    if (!is_dir($audioDir)) {
        mkdir($audioDir, 0755, true);
    }
    
    $fileExtension = $extension ?: 'mp3';
    $filename = "{$taskId}_variation_{$variationIndex}.{$fileExtension}";
    $localPath = $audioDir . $filename;
    $webPath = '/audio_files/' . $filename;
    
    // Check if file already exists and has content (skip if > 1KB to avoid re-downloading)
    if (file_exists($localPath) && filesize($localPath) > 1024) {
        return $webPath; // Already downloaded and has content
    }
    
    // Download using cURL for better error handling
    $ch = curl_init($audioUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 300);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36');
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Accept: audio/mpeg, audio/*, */*',
        'Accept-Language: en-US,en;q=0.9',
        'Connection: keep-alive',
        'Referer: https://musicfile.api.box/'
    ]);
    
    $audioContent = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    $error = curl_error($ch);
    $contentLength = strlen($audioContent);
    curl_close($ch);
    
    // Return error details for debugging
    if ($httpCode !== 200) {
        return ['error' => "HTTP $httpCode", 'http_code' => $httpCode];
    }
    if ($audioContent === false) {
        return ['error' => "Download failed: $error", 'curl_error' => $error];
    }
    if (!empty($error)) {
        return ['error' => "cURL error: $error", 'curl_error' => $error];
    }
    if ($contentLength < 1024) {
        return ['error' => "File too small: $contentLength bytes", 'size' => $contentLength];
    }
    
    // Save the file
    if (file_put_contents($localPath, $audioContent, LOCK_EX)) {
        chmod($localPath, 0644);
        return $webPath;
    }
    
    return null;
}

foreach ($variations as $variation) {
    $trackId = $variation['track_id'];
    $varIndex = $variation['variation_index'];
    $taskId = $variation['task_id'];
    
    echo "<div style='margin: 10px 0; padding: 10px; border: 1px solid #ccc; border-radius: 5px;'>";
    echo "<strong>Track ID: $trackId, Variation: $varIndex</strong><br>";
    
    // Check if local file exists and is valid
    $needsDownload = true;
    if (!empty($variation['audio_url']) && strpos($variation['audio_url'], '/audio_files/') === 0) {
        $localPath = ltrim($variation['audio_url'], '/');
        if (file_exists($localPath) && filesize($localPath) > 1024) {
            echo "<span style='color: blue;'>ℹ️ Already has valid local file: {$variation['audio_url']}</span>";
            $needsDownload = false;
            $skipped++;
        } else {
            echo "<span style='color: orange;'>⚠️ Local file exists but is empty/invalid, will re-download</span><br>";
        }
    }
    
    if ($needsDownload) {
        // Get the best URL to download from
        $downloadUrl = null;
        if (!empty($variation['audio_url']) && strpos($variation['audio_url'], 'http') === 0) {
            $downloadUrl = $variation['audio_url'];
        } elseif (!empty($variation['stream_audio_url']) && strpos($variation['stream_audio_url'], 'http') === 0) {
            $downloadUrl = $variation['stream_audio_url'];
        } elseif (!empty($variation['source_audio_url']) && strpos($variation['source_audio_url'], 'http') === 0) {
            $downloadUrl = $variation['source_audio_url'];
        }
        
        if (!$downloadUrl) {
            echo "<span style='color: orange;'>⚠️ No valid download URL found</span>";
            $skipped++;
        } else {
        echo "Downloading from: <a href='$downloadUrl' target='_blank'>$downloadUrl</a><br>";
        
        $result = downloadAndStoreAudio($downloadUrl, $taskId, 'variation', $varIndex);
        
        // Check if result is an array (error) or string (success)
        if (is_array($result) && isset($result['error'])) {
            echo "<span style='color: red;'>❌ Download failed: {$result['error']}</span>";
            $failed++;
        } elseif (is_string($result) && !empty($result)) {
            $localUrl = $result;
            // Verify file was actually saved and has content
            $localPath = ltrim($localUrl, '/');
            if (file_exists($localPath) && filesize($localPath) > 1024) {
                // Update database
                $updateStmt = $pdo->prepare("UPDATE audio_variations SET audio_url = ? WHERE id = ?");
                $updateResult = $updateStmt->execute([$localUrl, $variation['id']]);
                
                if ($updateResult) {
                    $fileSize = filesize($localPath);
                    echo "<span style='color: green;'>✅ Downloaded and saved: $localUrl (" . number_format($fileSize) . " bytes)</span>";
                    $downloaded++;
                } else {
                    echo "<span style='color: red;'>❌ Failed to update database</span>";
                    $failed++;
                }
            } else {
                echo "<span style='color: red;'>❌ Downloaded file is empty or missing</span>";
                $failed++;
            }
        } else {
            echo "<span style='color: red;'>❌ Failed to download audio file (unknown error)</span>";
            $failed++;
        }
        }
    }
    
    echo "</div>";
    flush();
    ob_flush();
}

echo "<hr>";
echo "<h2>Summary</h2>";
echo "<p><strong>Total variations:</strong> $total</p>";
echo "<p style='color: green;'><strong>Downloaded:</strong> $downloaded</p>";
echo "<p style='color: red;'><strong>Failed:</strong> $failed</p>";
echo "<p style='color: orange;'><strong>Skipped:</strong> $skipped</p>";

echo "<p><a href='/library.php'>Back to Library</a></p>";
?>


CasperSecurity Mini