![]() 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();
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
header('Location: auth/login_new.php');
exit;
}
// Check if form was submitted
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: index.php#create');
exit;
}
require_once 'config/database.php';
require_once 'api_functions.php';
require_once 'includes/translations.php';
$pdo = getDBConnection();
$api = new APIBoxFunctions('63edba40620216c5aa2c04240ac41dbd');
// Get form data
$title = $_POST['title'] ?? 'Untitled Track Extension';
$audioUrl = $_POST['audioUrl'] ?? '';
$audioFile = $_FILES['audioFile'] ?? null;
$extensionDuration = $_POST['extensionDuration'] ?? '30'; // 15, 30, 60 seconds
$startTime = $_POST['startTime'] ?? '0'; // Start time in seconds
$style = $_POST['style'] ?? 'continue'; // continue, evolve, contrast
$maintainStructure = $_POST['maintainStructure'] ?? 'true'; // true, false
$enhanceQuality = $_POST['enhanceQuality'] ?? 'true'; // true, false
// Validate input
if (empty($audioUrl) && empty($audioFile['name'])) {
$_SESSION['error'] = 'Please provide an audio file or URL.';
header('Location: index.php#create');
exit;
}
// Calculate credit cost
$creditCost = 2; // Track extension costs more than basic generation
// Check user credits
$stmt = $pdo->prepare("SELECT credits FROM users WHERE id = ?");
$stmt->execute([$_SESSION['user_id']]);
$user = $stmt->fetch();
if (!$user || $user['credits'] < $creditCost) {
error_log("Insufficient credits for user {$_SESSION['user_id']}: has {$user['credits']}, needs $creditCost");
$_SESSION['error'] = "Insufficient credits. You need $creditCost credits to extend this track. Please purchase credits to continue.";
header('Location: index.php');
exit;
}
// Handle file upload if provided
$finalAudioUrl = $audioUrl;
if (!empty($audioFile['name'])) {
// SECURITY: Enhanced file upload validation
require_once 'includes/security.php';
$validation = validateFileUpload($audioFile, ['mp3', 'wav', 'm4a', 'ogg'], 50 * 1024 * 1024); // 50MB max for audio
if (!$validation['valid']) {
error_log("SECURITY: Invalid audio file upload attempt from user {$_SESSION['user_id']}: " . ($validation['error'] ?? 'unknown error'));
$_SESSION['error'] = 'Invalid audio file. ' . ($validation['error'] ?? 'Please upload a valid audio file (MP3, WAV, M4A, or OGG).');
header('Location: index.php#create');
exit;
}
$uploadDir = 'uploads/';
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0755, true);
}
// Use sanitized filename from validation
$fileName = time() . '_' . $_SESSION['user_id'] . '_' . $validation['filename'];
$uploadPath = $uploadDir . $fileName;
if (move_uploaded_file($audioFile['tmp_name'], $uploadPath)) {
$finalAudioUrl = 'https://soundstudiopro.com/' . $uploadPath;
} else {
error_log("SECURITY: Failed to move uploaded audio file for user {$_SESSION['user_id']}");
$_SESSION['error'] = 'Failed to upload audio file.';
header('Location: index.php#create');
exit;
}
}
// Create track record
$temp_task_id = 'track_extension_' . time() . '_' . $_SESSION['user_id'] . '_' . uniqid();
$metadata = json_encode([
'extensionDuration' => $extensionDuration,
'startTime' => $startTime,
'style' => $style,
'maintainStructure' => $maintainStructure,
'enhanceQuality' => $enhanceQuality,
'originalAudioUrl' => $finalAudioUrl
]);
$track_id = $api->createTrackRecord($_SESSION['user_id'], $temp_task_id, $title, 'Track extension from audio', 'extend', json_decode($metadata, true));
// Deduct credits
$newCredits = $user['credits'] - $creditCost;
$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 (?, ?, 'usage', 'Track extension: $title', NOW())
");
$stmt->execute([$_SESSION['user_id'], -$creditCost]);
$_SESSION['credits'] = $newCredits;
// Call the track extension API
$api_data = [
'audioUrl' => $finalAudioUrl,
'extensionDuration' => $extensionDuration,
'startTime' => $startTime,
'style' => $style,
'maintainStructure' => $maintainStructure,
'enhanceQuality' => $enhanceQuality,
'callBackUrl' => 'https://soundstudiopro.com/callback.php'
];
$result = $api->extendMusic($api_data);
if (isset($result['error'])) {
error_log("Track extension error: " . json_encode($result));
$_SESSION['success'] = t('success.track_extension.started');
} else {
// Extract task ID from response
$real_task_id = $result['taskId'] ?? $result['id'] ?? $result['data']['taskId'] ?? $temp_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]);
$_SESSION['success'] = t('success.track_extension.started');
}
// Redirect back to the create page with success message
header('Location: index.php#create');
exit;
?>