![]() 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/studio/ |
<?php
session_start();
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../api_functions.php';
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
header('Location: /auth/login.php');
exit;
}
// Get user data
$pdo = getDBConnection();
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$_SESSION['user_id']]);
$user = $stmt->fetch();
if (!$user) {
$_SESSION['error'] = 'User not found';
header('Location: /studio.php');
exit;
}
// Check credits (1 credit for Create Persona)
$creditCost = 1;
if ($user['credits'] < $creditCost) {
$_SESSION['error'] = "Insufficient credits. You need $creditCost credit to create a persona.";
header('Location: /studio.php');
exit;
}
// Get form data
$name = trim($_POST['name'] ?? '');
$description = trim($_POST['description'] ?? '');
$audioUrl = trim($_POST['audioUrl'] ?? '');
$audioFile = $_FILES['audioFile'] ?? null;
if (empty($name)) {
$_SESSION['error'] = 'Persona name is required';
header('Location: /studio.php');
exit;
}
// Determine audio source
$finalAudioUrl = null;
if ($audioUrl) {
$finalAudioUrl = $audioUrl;
}
if (!$finalAudioUrl && $audioFile && $audioFile['error'] === UPLOAD_ERR_OK) {
$uploadDir = 'uploads/personas/';
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0755, true);
}
$fileName = time() . '_' . $_SESSION['user_id'] . '_' . basename($audioFile['name']);
$uploadPath = $uploadDir . $fileName;
if (move_uploaded_file($audioFile['tmp_name'], $uploadPath)) {
$finalAudioUrl = 'https://soundstudiopro.com/' . $uploadPath;
} else {
$_SESSION['error'] = 'Failed to upload audio file.';
header('Location: /studio.php');
exit;
}
}
// Deduct credits
$newCredits = $user['credits'] - $creditCost;
$stmt = $pdo->prepare("UPDATE users SET credits = ? WHERE id = ?");
$stmt->execute([$newCredits, $_SESSION['user_id']]);
$stmt = $pdo->prepare("
INSERT INTO credit_transactions (user_id, amount, type, description, created_at)
VALUES (?, ?, 'usage', 'Create Persona: $name', NOW())
");
$stmt->execute([$_SESSION['user_id'], -$creditCost]);
$_SESSION['credits'] = $newCredits;
// Initialize API
$api_key = '63edba40620216c5aa2c04240ac41dbd';
$api = new APIBoxFunctions($api_key);
// Prepare API request
$api_params = [
'name' => $name,
'description' => $description,
'callBackUrl' => 'https://soundstudiopro.com/callback.php'
];
if ($finalAudioUrl) {
$api_params['audioUrl'] = $finalAudioUrl;
}
// Call Generate Persona API
$result = $api->generatePersona($api_params);
if (isset($result['error'])) {
error_log("Generate Persona API error: " . json_encode($result));
$_SESSION['error'] = 'Failed to create persona. Please try again.';
header('Location: /studio.php');
exit;
}
// Save persona to database (if personas table exists)
// TODO: Create personas table
try {
$taskId = $result['data']['taskId'] ?? $result['taskId'] ?? null;
if ($taskId) {
// Store persona info for later retrieval from callback
$stmt = $pdo->prepare("
INSERT INTO personas (user_id, name, description, audio_url, task_id, status, created_at)
VALUES (?, ?, ?, ?, ?, 'processing', NOW())
");
$stmt->execute([$_SESSION['user_id'], $name, $description, $finalAudioUrl, $taskId]);
}
} catch (Exception $e) {
// Table might not exist yet, that's okay
error_log("Persona save error (table might not exist): " . $e->getMessage());
}
$_SESSION['success'] = "Persona is being created. You'll be notified when it's ready!";
header('Location: /studio.php');
exit;
?>