![]() 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/ |
<?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;
$prompt = $input['prompt'] ?? null;
$style = $input['style'] ?? 'Pop';
$duration = $input['duration'] ?? 180;
if (!$track_id || !$prompt) {
http_response_code(400);
echo json_encode(['error' => 'Track ID and prompt required']);
exit;
}
try {
// Get track information
$stmt = $pdo->prepare("
SELECT id, user_id, title, prompt, music_type, metadata, created_at
FROM music_tracks
WHERE id = ? AND user_id = ? AND status = 'failed'
");
$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 eligible for retry']);
exit;
}
// Check if user has enough credits
$stmt = $pdo->prepare("SELECT credits FROM users WHERE id = ?");
$stmt->execute([$_SESSION['user_id']]);
$user = $stmt->fetch();
if ($user['credits'] < 1) {
http_response_code(400);
echo json_encode(['error' => 'Insufficient credits']);
exit;
}
// Parse metadata to get original parameters
$metadata = json_decode($track['metadata'], true) ?: [];
// Generate new task ID
$temp_task_id = 'retry_' . time() . '_' . rand(1000, 9999);
// Update track with new prompt and parameters
$stmt = $pdo->prepare("
UPDATE music_tracks
SET status = 'processing',
task_id = ?,
prompt = ?,
music_type = ?,
duration = ?,
updated_at = NOW()
WHERE id = ?
");
$stmt->execute([$temp_task_id, $prompt, $style, $duration, $track_id]);
// Deduct credit
$newCredits = $user['credits'] - 1;
$stmt = $pdo->prepare("UPDATE users SET credits = ? WHERE id = ?");
$stmt->execute([$newCredits, $_SESSION['user_id']]);
// Record credit transaction
$stmt = $pdo->prepare("
INSERT INTO credit_transactions (user_id, amount, type, description, created_at)
VALUES (?, -1, 'usage', 'Music track retry with new prompt: {$track['title']}', NOW())
");
$stmt->execute([$_SESSION['user_id']]);
// Update session credits
$_SESSION['credits'] = $newCredits;
// Call the music generation API with new parameters
$api_url = 'https://api.api.box/api/v1/generate';
$api_data = [
'prompt' => $prompt,
'model' => $metadata['model_name'] ?? 'melody',
'style' => $style,
'title' => $track['title'],
'customMode' => $metadata['customMode'] ?? 'false',
'instrumental' => $metadata['instrumental'] ?? 'false',
'duration' => $duration,
'callBackUrl' => 'https://soundstudiopro.com/callback.php'
];
// Add advanced parameters if they exist
if (!empty($metadata['tempo'])) $api_data['tempo'] = $metadata['tempo'];
if (!empty($metadata['key'])) $api_data['key'] = $metadata['key'];
if (!empty($metadata['scale'])) $api_data['scale'] = $metadata['scale'];
if (!empty($metadata['energy'])) $api_data['energy'] = $metadata['energy'];
if (!empty($metadata['excitement'])) $api_data['excitement'] = $metadata['excitement'];
if (!empty($metadata['mood'])) $api_data['mood'] = $metadata['mood'];
if (!empty($metadata['tags'])) $api_data['tags'] = $metadata['tags'];
if (!empty($metadata['variations']) && $metadata['variations'] > 1) $api_data['variations'] = $metadata['variations'];
// Music generation access key
$api_key = '63edba40620216c5aa2c04240ac41dbd';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($api_data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $api_key,
'Content-Type: application/json',
'User-Agent: SoundStudioPro-Music/2.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);
// Log the retry attempt with new prompt
error_log("Retry with new prompt API Request for track $track_id: " . json_encode($api_data));
error_log("Retry with new prompt API Response: HTTP $http_code, Response: " . $response);
// Handle response (same logic as original API)
if ($curl_error || $http_code !== 200 || !$response) {
// Keep in processing state even if API call fails
echo json_encode([
'success' => true,
'message' => 'Retry with new prompt initiated! Processing may take a few minutes.',
'track_id' => $track_id,
'status' => 'processing'
]);
exit;
}
$api_result = json_decode($response, true);
if (isset($api_result['taskId']) || isset($api_result['status']) && $api_result['status'] === 'processing') {
// Task is being processed
$real_task_id = $api_result['taskId'] ?? $api_result['id'] ?? $api_result['data']['taskId'] ?? null;
if ($real_task_id) {
// Update track with real task ID
$stmt = $pdo->prepare("UPDATE music_tracks SET task_id = ? WHERE id = ?");
$stmt->execute([$real_task_id, $track_id]);
}
echo json_encode([
'success' => true,
'message' => 'Retry with new prompt successful! Music generation started.',
'track_id' => $track_id,
'status' => 'processing'
]);
} else {
// Keep in processing state
echo json_encode([
'success' => true,
'message' => 'Retry with new prompt initiated! Processing may take a few minutes.',
'track_id' => $track_id,
'status' => 'processing'
]);
}
} catch (Exception $e) {
error_log("Retry with new prompt error for track $track_id: " . $e->getMessage());
http_response_code(500);
echo json_encode(['error' => 'Internal server error']);
}
?>