![]() 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 API.Box track synchronization and verification
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once 'config/database.php';
$api_key = '63edba40620216c5aa2c04240ac41dbd';
echo "<h1>๐ API.Box Track Synchronization</h1>";
$pdo = getDBConnection();
if (!$pdo) {
echo "<p style='color: red;'>โ Database connection failed</p>";
exit;
}
// Function to get API.Box logs
function getApiBoxLogs($api_key) {
$api_url = 'https://api.api.box/api/v1/tasks';
$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: SoundStudioPro-Sync/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);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_error = curl_error($ch);
curl_close($ch);
if ($curl_error) {
return ['error' => 'cURL Error: ' . $curl_error];
}
if ($http_code !== 200) {
return ['error' => 'HTTP Error: ' . $http_code];
}
return json_decode($response, true);
}
// Function to check individual track status
function checkTrackStatus($task_id, $api_key) {
$api_url = "https://api.api.box/api/v1/status/$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: SoundStudioPro-Sync/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);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code === 200) {
return json_decode($response, true);
}
return ['error' => 'HTTP ' . $http_code, 'response' => $response];
}
// Get API.Box logs
echo "<h2>๐ Fetching API.Box Logs</h2>";
$api_logs = getApiBoxLogs($api_key);
if (isset($api_logs['error'])) {
echo "<p style='color: red;'>โ Error fetching API.Box logs: {$api_logs['error']}</p>";
exit;
}
echo "<p style='color: green;'>โ
Successfully fetched API.Box logs</p>";
// Get all local tracks
$stmt = $pdo->prepare("
SELECT mt.*, u.name as user_name, u.email as user_email
FROM music_tracks mt
JOIN users u ON mt.user_id = u.id
ORDER BY mt.created_at DESC
");
$stmt->execute();
$local_tracks = $stmt->fetchAll();
echo "<h2>๐ API.Box vs Local Database Comparison</h2>";
// Create comparison table
echo "<table border='1' style='border-collapse: collapse; margin: 20px 0; width: 100%; font-size: 12px;'>";
echo "<tr style='background: #f0f0f0;'>";
echo "<th>Task ID</th><th>Title</th><th>User</th><th>API.Box Status</th><th>Local Status</th><th>Audio URL</th><th>Created</th><th>Actions</th>";
echo "</tr>";
$api_tracks = [];
$synced_count = 0;
$missing_count = 0;
$mismatch_count = 0;
// Process API.Box logs
if (isset($api_logs['data']) && is_array($api_logs['data'])) {
foreach ($api_logs['data'] as $log_entry) {
if (isset($log_entry['taskId']) && $log_entry['type'] === 'generate') {
$api_tracks[$log_entry['taskId']] = $log_entry;
}
}
}
// Compare with local tracks
foreach ($local_tracks as $track) {
$task_id = $track['task_id'];
$api_track = $api_tracks[$task_id] ?? null;
$status_color = $track['status'] === 'complete' ? 'green' : ($track['status'] === 'failed' ? 'red' : 'orange');
$api_status = $api_track ? ($api_track['status'] ?? 'unknown') : 'not_found';
$api_status_color = $api_status === 'success' ? 'green' : ($api_status === 'failed' ? 'red' : 'orange');
$audio_url = $track['audio_url'] ? 'โ
Has URL' : 'โ No URL';
$row_color = '';
if (!$api_track) {
$row_color = 'background: #ffe6e6;'; // Light red for missing
$missing_count++;
} elseif ($track['status'] !== 'complete' && $api_status === 'success') {
$row_color = 'background: #fff3cd;'; // Light yellow for mismatch
$mismatch_count++;
} else {
$synced_count++;
}
echo "<tr style='$row_color'>";
echo "<td style='font-family: monospace; font-size: 10px;'>{$task_id}</td>";
echo "<td>{$track['title']}</td>";
echo "<td>{$track['user_name']} ({$track['user_email']})</td>";
echo "<td style='color: $api_status_color;'>$api_status</td>";
echo "<td style='color: $status_color;'>{$track['status']}</td>";
echo "<td>$audio_url</td>";
echo "<td>{$track['created_at']}</td>";
echo "<td>";
echo "<button onclick='checkStatus(\"{$task_id}\")' style='background: #667eea; color: white; border: none; padding: 3px 6px; border-radius: 3px; cursor: pointer; font-size: 10px;'>๐ Check</button> ";
if ($track['status'] !== 'complete' && $api_status === 'success') {
echo "<button onclick='syncTrack({$track['id']}, \"{$task_id}\")' style='background: #48bb78; color: white; border: none; padding: 3px 6px; border-radius: 3px; cursor: pointer; font-size: 10px;'>๐ Sync</button>";
}
echo "</td>";
echo "</tr>";
}
echo "</table>";
// Summary statistics
echo "<h2>๐ Synchronization Summary</h2>";
echo "<div style='display: flex; gap: 20px; margin: 20px 0;'>";
echo "<div style='background: #48bb78; color: white; padding: 20px; border-radius: 10px; text-align: center;'>";
echo "<h3>Synced</h3><p style='font-size: 2rem;'>$synced_count</p>";
echo "</div>";
echo "<div style='background: #ed8936; color: white; padding: 20px; border-radius: 10px; text-align: center;'>";
echo "<h3>Mismatched</h3><p style='font-size: 2rem;'>$mismatch_count</p>";
echo "</div>";
echo "<div style='background: #e53e3e; color: white; padding: 20px; border-radius: 10px; text-align: center;'>";
echo "<h3>Missing in API.Box</h3><p style='font-size: 2rem;'>$missing_count</p>";
echo "</div>";
echo "</div>";
// Show API.Box only tracks
echo "<h2>๐ API.Box Only Tracks</h2>";
$local_task_ids = array_column($local_tracks, 'task_id');
$api_only_tracks = array_filter($api_tracks, function($task_id) use ($local_task_ids) {
return !in_array($task_id, $local_task_ids);
}, ARRAY_FILTER_USE_KEY);
if ($api_only_tracks) {
echo "<table border='1' style='border-collapse: collapse; margin: 20px 0; width: 100%;'>";
echo "<tr style='background: #f0f0f0;'>";
echo "<th>Task ID</th><th>Status</th><th>Created</th><th>Actions</th>";
echo "</tr>";
foreach ($api_only_tracks as $task_id => $track) {
echo "<tr>";
echo "<td style='font-family: monospace;'>{$task_id}</td>";
echo "<td style='color: " . ($track['status'] === 'success' ? 'green' : 'orange') . "'>{$track['status']}</td>";
echo "<td>" . (isset($track['createdAt']) ? $track['createdAt'] : 'Unknown') . "</td>";
echo "<td>";
echo "<button onclick='checkStatus(\"{$task_id}\")' style='background: #667eea; color: white; border: none; padding: 3px 6px; border-radius: 3px; cursor: pointer;'>๐ Check</button> ";
echo "<button onclick='importTrack(\"{$task_id}\")' style='background: #48bb78; color: white; border: none; padding: 3px 6px; border-radius: 3px; cursor: pointer;'>๐ฅ Import</button>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "<p>No API.Box-only tracks found.</p>";
}
// User profile verification
echo "<h2>๐ค User Profile Verification</h2>";
$stmt = $pdo->prepare("
SELECT u.id, u.name, u.email, COUNT(mt.id) as track_count,
SUM(CASE WHEN mt.status = 'complete' THEN 1 ELSE 0 END) as completed_tracks,
SUM(CASE WHEN mt.status = 'processing' THEN 1 ELSE 0 END) as processing_tracks,
SUM(CASE WHEN mt.status = 'failed' THEN 1 ELSE 0 END) as failed_tracks
FROM users u
LEFT JOIN music_tracks mt ON u.id = mt.user_id
GROUP BY u.id, u.name, u.email
ORDER BY track_count DESC
");
$stmt->execute();
$user_profiles = $stmt->fetchAll();
echo "<table border='1' style='border-collapse: collapse; margin: 20px 0; width: 100%;'>";
echo "<tr style='background: #f0f0f0;'>";
echo "<th>User</th><th>Email</th><th>Total Tracks</th><th>Completed</th><th>Processing</th><th>Failed</th><th>Actions</th>";
echo "</tr>";
foreach ($user_profiles as $profile) {
echo "<tr>";
echo "<td>{$profile['name']}</td>";
echo "<td>{$profile['email']}</td>";
echo "<td>{$profile['track_count']}</td>";
echo "<td style='color: green;'>{$profile['completed_tracks']}</td>";
echo "<td style='color: orange;'>{$profile['processing_tracks']}</td>";
echo "<td style='color: red;'>{$profile['failed_tracks']}</td>";
echo "<td>";
echo "<a href='/user_tracks.php?user_id={$profile['id']}' style='color: #667eea; margin-right: 10px;'>๐๏ธ View Tracks</a>";
echo "<button onclick='verifyUserTracks({$profile['id']})' style='background: #667eea; color: white; border: none; padding: 3px 6px; border-radius: 3px; cursor: pointer;'>๐ Verify</button>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
echo "<h3>๐ Quick Links</h3>";
echo "<p><a href='/library.php' style='color: #667eea;'>๐ View Library</a> | ";
echo "<a href='/debug_tracks.php' style='color: #667eea;'>๐ Debug Tracks</a> | ";
echo "<a href='/test_new_api.php' style='color: #667eea;'>๐งช Test API</a></p>";
?>
<script>
async function checkStatus(taskId) {
const apiKey = '<?php echo $api_key; ?>';
const apiUrl = `https://api.api.box/api/v1/status/${taskId}`;
try {
const response = await fetch(apiUrl, {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
const data = await response.json();
alert(`API.Box Status for ${taskId}:\n\n${JSON.stringify(data, null, 2)}`);
} catch (error) {
alert('Error checking status: ' + error.message);
}
}
async function syncTrack(trackId, taskId) {
if (confirm('Sync this track with API.Box?')) {
try {
const response = await fetch('/sync_track.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
track_id: trackId,
task_id: taskId
})
});
const data = await response.json();
if (data.success) {
alert('Track synced successfully!');
location.reload();
} else {
alert('Sync failed: ' + data.error);
}
} catch (error) {
alert('Error syncing track: ' + error.message);
}
}
}
async function importTrack(taskId) {
if (confirm('Import this track from API.Box?')) {
try {
const response = await fetch('/import_track.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
task_id: taskId
})
});
const data = await response.json();
if (data.success) {
alert('Track imported successfully!');
location.reload();
} else {
alert('Import failed: ' + data.error);
}
} catch (error) {
alert('Error importing track: ' + error.message);
}
}
}
async function verifyUserTracks(userId) {
try {
const response = await fetch(`/verify_user_tracks.php?user_id=${userId}`);
const data = await response.json();
if (data.success) {
alert(`User tracks verified!\n\nTotal: ${data.total}\nValid: ${data.valid}\nIssues: ${data.issues}`);
} else {
alert('Verification failed: ' + data.error);
}
} catch (error) {
alert('Error verifying user tracks: ' + error.message);
}
}
</script>