![]() 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/ |
<?php
/**
* API endpoint to save user's selected variation for a track
*/
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(['success' => false, 'error' => 'Not logged in']);
exit;
}
// Only allow POST requests
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['success' => false, 'error' => 'Method not allowed']);
exit;
}
require_once 'config/database.php';
// Get JSON input
$input = json_decode(file_get_contents('php://input'), true);
$track_id = (int)($input['track_id'] ?? 0);
$variation_index = (int)($input['variation_index'] ?? 0);
if (!$track_id) {
echo json_encode(['success' => false, 'error' => 'Track ID required']);
exit;
}
try {
$pdo = getDBConnection();
if (!$pdo) {
throw new Exception('Database connection failed');
}
$user_id = $_SESSION['user_id'];
// Verify the track belongs to the user
$stmt = $pdo->prepare("SELECT id, user_id FROM music_tracks WHERE id = ?");
$stmt->execute([$track_id]);
$track = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$track) {
echo json_encode(['success' => false, 'error' => 'Track not found']);
exit;
}
if ($track['user_id'] != $user_id) {
echo json_encode(['success' => false, 'error' => 'Not authorized to modify this track']);
exit;
}
// Get the variation to make sure it exists
$stmt = $pdo->prepare("SELECT id, audio_url, duration FROM audio_variations WHERE track_id = ? AND variation_index = ?");
$stmt->execute([$track_id, $variation_index]);
$variation = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$variation) {
echo json_encode(['success' => false, 'error' => 'Variation not found']);
exit;
}
// Update the track's selected variation
$stmt = $pdo->prepare("UPDATE music_tracks SET selected_variation = ? WHERE id = ?");
$stmt->execute([$variation_index, $track_id]);
// Also update the main track's audio_url and duration to match the selected variation
$stmt = $pdo->prepare("UPDATE music_tracks SET audio_url = ?, duration = ? WHERE id = ?");
$stmt->execute([$variation['audio_url'], $variation['duration'], $track_id]);
// Update or insert user preference
$stmt = $pdo->prepare("
INSERT INTO user_variation_preferences (user_id, track_id, variation_id, is_main_track, created_at, updated_at)
VALUES (?, ?, ?, 1, NOW(), NOW())
ON DUPLICATE KEY UPDATE variation_id = ?, is_main_track = 1, updated_at = NOW()
");
$stmt->execute([$user_id, $track_id, $variation['id'], $variation['id']]);
echo json_encode([
'success' => true,
'message' => 'Variation selection saved',
'track_id' => $track_id,
'variation_index' => $variation_index,
'audio_url' => $variation['audio_url'],
'duration' => $variation['duration']
]);
} catch (Exception $e) {
error_log("Error saving variation selection: " . $e->getMessage());
echo json_encode(['success' => false, 'error' => 'Database error: ' . $e->getMessage()]);
}