![]() 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/.cursor-server/data/User/History/-12082379/ |
<?php
session_start();
header('Content-Type: application/json');
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
http_response_code(401);
echo json_encode(['error' => 'Not authenticated']);
exit;
}
require_once 'config/database.php';
$pdo = getDBConnection();
if (!$pdo) {
http_response_code(500);
echo json_encode(['error' => 'Database connection failed']);
exit;
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => 'Method not allowed']);
exit;
}
// Get request data
$input = json_decode(file_get_contents('php://input'), true);
$track_id = $input['track_id'] ?? null;
$variation_index = $input['variation_index'] ?? null;
if (!$track_id || $variation_index === null) {
http_response_code(400);
echo json_encode(['error' => 'Track ID and variation index required']);
exit;
}
try {
error_log("Variation selection request - User: {$_SESSION['user_id']}, Track: $track_id, Variation: $variation_index");
// Verify the track belongs to the user
$stmt = $pdo->prepare("
SELECT id, title, status, selected_variation
FROM music_tracks
WHERE id = ? AND user_id = ?
");
$stmt->execute([$track_id, $_SESSION['user_id']]);
$track = $stmt->fetch();
if (!$track) {
http_response_code(404);
echo json_encode(['error' => 'Track not found or not owned by user']);
exit;
}
if ($track['status'] !== 'complete') {
http_response_code(400);
echo json_encode(['error' => 'Can only select variations for completed tracks']);
exit;
}
// Verify the variation exists
$stmt = $pdo->prepare("
SELECT id, audio_url, duration, title, tags
FROM audio_variations
WHERE track_id = ? AND variation_index = ?
");
$stmt->execute([$track_id, $variation_index]);
$variation = $stmt->fetch();
if (!$variation) {
http_response_code(404);
echo json_encode(['error' => 'Variation not found']);
exit;
}
// Update the track with the selected variation
$stmt = $pdo->prepare("
UPDATE music_tracks
SET
selected_variation = ?,
audio_url = ?,
duration = ?,
updated_at = NOW()
WHERE id = ?
");
$stmt->execute([
$variation_index,
$variation['audio_url'],
$variation['duration'],
$track_id
]);
// Log the selection (optional - don't fail if logging fails)
try {
$stmt = $pdo->prepare("
INSERT INTO user_activity_log
(user_id, action, details, created_at)
VALUES (?, 'variation_selected', ?, NOW())
");
$stmt->execute([
$_SESSION['user_id'],
json_encode([
'track_id' => $track_id,
'track_title' => $track['title'],
'variation_index' => $variation_index,
'variation_title' => $variation['title'],
'previous_selection' => $track['selected_variation']
])
]);
} catch (Exception $e) {
// Log the error but don't fail the main operation
error_log("Failed to log variation selection activity: " . $e->getMessage());
}
echo json_encode([
'success' => true,
'message' => 'Variation selected successfully',
'track_id' => $track_id,
'variation_index' => $variation_index,
'audio_url' => $variation['audio_url'],
'duration' => $variation['duration'],
'title' => $variation['title']
]);
} catch (Exception $e) {
error_log("Variation selection error: " . $e->getMessage());
http_response_code(500);
echo json_encode(['error' => 'Internal server error']);
}
?>