![]() 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
session_start();
header('Content-Type: application/json');
require_once '../config/database.php';
if (!isset($_SESSION['user_id'])) {
echo json_encode(['success' => false, 'error' => 'Not authenticated']);
exit;
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode(['success' => false, 'error' => 'Invalid request method']);
exit;
}
$input = json_decode(file_get_contents('php://input'), true);
$crate_id = isset($input['crate_id']) ? (int)$input['crate_id'] : null;
$track_id = isset($input['track_id']) ? (int)$input['track_id'] : null;
if (!$crate_id || !$track_id) {
echo json_encode(['success' => false, 'error' => 'Crate ID and Track ID are required']);
exit;
}
try {
$pdo = getDBConnection();
// Verify crate belongs to user
$stmt = $pdo->prepare("SELECT id FROM artist_playlists WHERE id = ? AND user_id = ?");
$stmt->execute([$crate_id, $_SESSION['user_id']]);
if (!$stmt->fetch()) {
echo json_encode(['success' => false, 'error' => 'Crate not found or access denied']);
exit;
}
// Verify track exists and is available
// Allow: own tracks, purchased tracks, OR any public track
$stmt = $pdo->prepare("
SELECT mt.id, mt.user_id, mt.is_public, u.name as artist_name
FROM music_tracks mt
JOIN users u ON mt.user_id = u.id
WHERE mt.id = ? AND mt.status = 'complete'
AND (
mt.user_id = ? -- Own track
OR mt.is_public = 1 -- Any public track
OR mt.id IN (SELECT track_id FROM track_purchases WHERE user_id = ?) -- Purchased track
)
");
$stmt->execute([$track_id, $_SESSION['user_id'], $_SESSION['user_id']]);
$track = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$track) {
echo json_encode(['success' => false, 'error' => 'Track not found or not available']);
exit;
}
// Determine if this is the user's own track or from another artist
$is_own_track = ($track['user_id'] == $_SESSION['user_id']);
// Check if track is already in crate
$stmt = $pdo->prepare("SELECT id FROM playlist_tracks WHERE playlist_id = ? AND track_id = ?");
$stmt->execute([$crate_id, $track_id]);
if ($stmt->fetch()) {
echo json_encode(['success' => false, 'error' => 'Track is already in this crate']);
exit;
}
// Get next position
$stmt = $pdo->prepare("SELECT COALESCE(MAX(position), 0) + 1 as next_position FROM playlist_tracks WHERE playlist_id = ?");
$stmt->execute([$crate_id]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$next_position = $result['next_position'];
// Add track to crate
$stmt = $pdo->prepare("
INSERT INTO playlist_tracks (playlist_id, track_id, position, added_at)
VALUES (?, ?, ?, NOW())
");
$stmt->execute([$crate_id, $track_id, $next_position]);
// Update crate updated_at
$stmt = $pdo->prepare("UPDATE artist_playlists SET updated_at = NOW() WHERE id = ?");
$stmt->execute([$crate_id]);
echo json_encode([
'success' => true,
'message' => 'Track added to crate successfully',
'is_own_track' => $is_own_track,
'artist_name' => $track['artist_name'] ?? null
]);
} catch (Exception $e) {
error_log("Error adding track to crate: " . $e->getMessage());
echo json_encode(['success' => false, 'error' => 'Failed to add track to crate: ' . $e->getMessage()]);
}