![]() 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/api/ |
<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../config/database.php';
// Start session to get current user context
session_start();
// No caching - always get fresh data
// Get pagination parameters
$page = max(1, intval($_GET['page'] ?? 1));
$per_page = intval($_GET['per_page'] ?? 100); // Increased to show more tracks
$offset = ($page - 1) * $per_page;
try {
$pdo = getDBConnection();
// Check if we're in a personal library context
$is_personal_library = isset($_GET['personal']) && $_GET['personal'] === 'true';
$current_user_id = $_SESSION['user_id'] ?? null;
if ($is_personal_library && $current_user_id) {
// For personal library, show only user's own tracks
$stmt = $pdo->prepare("
SELECT
mt.id,
mt.task_id,
mt.title,
mt.prompt,
mt.audio_url,
mt.created_at,
u.name as artist_name,
u.id as user_id
FROM music_tracks mt
LEFT JOIN users u ON mt.user_id = u.id
WHERE mt.user_id = ?
AND mt.status = 'complete'
AND mt.audio_url IS NOT NULL
AND mt.audio_url != ''
ORDER BY mt.created_at DESC
LIMIT ? OFFSET ?
");
$stmt->execute([$current_user_id, $per_page, $offset]);
$tracks = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Apply title fallback logic and convert external URLs
foreach ($tracks as &$track) {
if (empty($track['title'])) {
if (!empty($track['prompt'])) {
$track['title'] = substr($track['prompt'], 0, 50);
if (strlen($track['prompt']) > 50) {
$track['title'] .= '...';
}
} else {
$track['title'] = 'Untitled Track';
}
}
// Decode HTML entities in title (e.g., ' → ')
$track['title'] = html_entity_decode($track['title'], ENT_QUOTES | ENT_HTML5, 'UTF-8');
if (!empty($track['artist_name'])) {
$track['artist_name'] = html_entity_decode($track['artist_name'], ENT_QUOTES | ENT_HTML5, 'UTF-8');
}
// Convert external audio URLs to proxy endpoint
$audioUrl = $track['audio_url'] ?? '';
$taskId = $track['task_id'] ?? '';
if (!empty($audioUrl)) {
if (strpos($audioUrl, 'http') === 0 ||
strpos($audioUrl, 'api.box') !== false ||
strpos($audioUrl, 'apiboxfiles.erweima.ai') !== false) {
// Only convert if we have task_id
if (!empty($taskId)) {
$track['audio_url'] = '/utils/audiofiles.php?id=' . urlencode($taskId);
} else {
// Log the issue for debugging
error_log("Track {$track['id']} ({$track['title']}) has external URL but no task_id");
}
}
}
}
unset($track); // Break reference
// Get total count for pagination
$count_stmt = $pdo->prepare("
SELECT COUNT(*) as total
FROM music_tracks mt
LEFT JOIN users u ON mt.user_id = u.id
WHERE mt.user_id = ?
AND mt.status = 'complete'
AND mt.audio_url IS NOT NULL
AND mt.audio_url != ''
");
$count_stmt->execute([$current_user_id]);
$total_tracks = $count_stmt->fetch()['total'];
echo json_encode([
'success' => true,
'tracks' => $tracks,
'context' => 'personal_library',
'user_id' => $current_user_id,
'pagination' => [
'page' => $page,
'per_page' => $per_page,
'total_tracks' => $total_tracks,
'total_pages' => ceil($total_tracks / $per_page),
'has_more' => ($page * $per_page) < $total_tracks
]
]);
} else {
// For community context, show all tracks (original behavior)
$stmt = $pdo->prepare("
SELECT
mt.id,
mt.task_id,
mt.title,
mt.prompt,
mt.audio_url, -- This is the CDN URL from Box API
mt.created_at,
u.name as artist_name,
u.id as user_id
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.is_public = 1 OR mt.is_public IS NULL)
ORDER BY mt.created_at DESC
LIMIT ? OFFSET ?
");
$stmt->execute([$per_page, $offset]);
$tracks = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Apply title fallback logic and convert external URLs
foreach ($tracks as &$track) {
if (empty($track['title'])) {
if (!empty($track['prompt'])) {
$track['title'] = substr($track['prompt'], 0, 50);
if (strlen($track['prompt']) > 50) {
$track['title'] .= '...';
}
} else {
$track['title'] = 'Untitled Track';
}
}
// Decode HTML entities in title (e.g., ' → ')
$track['title'] = html_entity_decode($track['title'], ENT_QUOTES | ENT_HTML5, 'UTF-8');
if (!empty($track['artist_name'])) {
$track['artist_name'] = html_entity_decode($track['artist_name'], ENT_QUOTES | ENT_HTML5, 'UTF-8');
}
// Convert external audio URLs to proxy endpoint
$audioUrl = $track['audio_url'] ?? '';
$taskId = $track['task_id'] ?? '';
if (!empty($audioUrl)) {
if (strpos($audioUrl, 'http') === 0 ||
strpos($audioUrl, 'api.box') !== false ||
strpos($audioUrl, 'apiboxfiles.erweima.ai') !== false) {
// Only convert if we have task_id
if (!empty($taskId)) {
$track['audio_url'] = '/utils/audiofiles.php?id=' . urlencode($taskId);
} else {
// Log the issue for debugging
error_log("Track {$track['id']} ({$track['title']}) has external URL but no task_id");
}
}
}
}
unset($track); // Break reference
// Get total count for pagination
$count_stmt = $pdo->prepare("
SELECT COUNT(*) as total
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.is_public = 1 OR mt.is_public IS NULL)
");
$count_stmt->execute();
$total_tracks = $count_stmt->fetch()['total'];
$response = [
'success' => true,
'tracks' => $tracks,
'context' => 'community',
'pagination' => [
'page' => $page,
'per_page' => $per_page,
'total_tracks' => $total_tracks,
'total_pages' => ceil($total_tracks / $per_page),
'has_more' => ($page * $per_page) < $total_tracks
]
];
echo json_encode($response);
}
} catch (Exception $e) {
echo json_encode([
'success' => false,
'error' => 'Failed to load community tracks'
]);
}
?>