PNG IHDR x sBIT|d pHYs + tEXtSoftware www.inkscape.org< ,tEXtComment
<?php
// Turn off error output to screen to prevent breaking the JSON response
ini_set('display_errors', 0);
error_reporting(E_ALL);
session_start();
require_once('includes/connect.php');
require_once('includes/functions.php');
// Tell the browser we are sending JSON data back
header('Content-Type: application/json');
// 1. Check Authentication
if (!isset($_SESSION['Email'])) {
echo json_encode(['status' => 'error', 'message' => 'Your session has expired. Please log in again.']);
exit();
}
// 2. CSRF Token Validation
if (!isset($_POST['csrf_token']) || !hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'])) {
echo json_encode(['status' => 'error', 'message' => 'Security token invalid. Please refresh the page and try again.']);
exit();
}
// 3. Fetch User Data
$user = GetMember1($_SESSION['Email']);
if (!$user) {
echo json_encode(['status' => 'error', 'message' => 'User account not found.']);
exit();
}
$userId = $user['ID'];
$userCapital = floatval($user['Capital']);
// 4. Sanitize and Validate POST Data
$expertId = isset($_POST['expert_id']) ? intval($_POST['expert_id']) : 0;
$amount = isset($_POST['investment_amount']) ? floatval($_POST['investment_amount']) : 0;
if ($expertId <= 0 || $amount <= 0) {
echo json_encode(['status' => 'error', 'message' => 'Invalid investment parameters.']);
exit();
}
try {
// 5. Verify the Master Trader exists and is active
$stmtTrader = $conn->prepare("SELECT name, min_startup, status FROM copy_traders WHERE id = ?");
$stmtTrader->bind_param("i", $expertId);
$stmtTrader->execute();
$resTrader = $stmtTrader->get_result();
if ($resTrader->num_rows === 0) {
echo json_encode(['status' => 'error', 'message' => 'Master Trader not found.']);
exit();
}
$trader = $resTrader->fetch_assoc();
$stmtTrader->close();
if ($trader['status'] !== 'active') {
echo json_encode(['status' => 'error', 'message' => 'This trader is currently inactive and cannot be copied.']);
exit();
}
$minStartup = floatval($trader['min_startup']);
// 6. Validate Minimum Investment Requirement
if ($amount < $minStartup) {
echo json_encode([
'status' => 'error',
'message' => 'The minimum allocation to copy ' . htmlspecialchars($trader['name']) . ' is ' . htmlspecialchars($user['sym']) . number_format($minStartup, 2) . '.'
]);
exit();
}
// 7. Check User's Available Capital
if ($amount > $userCapital) {
echo json_encode([
'status' => 'error',
'message' => 'Insufficient capital! You only have ' . htmlspecialchars($user['sym']) . number_format($userCapital, 2) . ' available.'
]);
exit();
}
// Start a database transaction so money is safely transferred
$conn->begin_transaction();
// 8. Deduct the amount from the user's Capital
$newCapital = $userCapital - $amount;
$updateCapStmt = $conn->prepare("UPDATE members SET Capital = ? WHERE ID = ?");
$updateCapString = strval($newCapital);
$updateCapStmt->bind_param("si", $updateCapString, $userId);
$updateCapStmt->execute();
$updateCapStmt->close();
// 9. Insert the Copy Trade into the `user_copy_trades` table
$insertCopyStmt = $conn->prepare("INSERT INTO user_copy_trades (user_id, trader_id, amount_invested, status) VALUES (?, ?, ?, 'ACTIVE')");
$insertCopyStmt->bind_param("iid", $userId, $expertId, $amount);
$insertCopyStmt->execute();
$insertCopyStmt->close();
// 10. FIXED BUG: Update the Trader's followers Count (+1)
$updateTraderStmt = $conn->prepare("UPDATE copy_traders SET followers = followers + 1 WHERE id = ?");
$updateTraderStmt->bind_param("i", $expertId);
$updateTraderStmt->execute();
$updateTraderStmt->close();
// 11. Log record into Transactions table
$txType = 'COPY_TRADE';
$txMethod = "Copied: " . $trader['name'];
$txStatus = 'COMPLETED';
$negAmount = -$amount;
$insertTxStmt = $conn->prepare("INSERT INTO transactions (user_id, type, amount, method, status) VALUES (?, ?, ?, ?, ?)");
$insertTxStmt->bind_param("isdss", $userId, $txType, $negAmount, $txMethod, $txStatus);
$insertTxStmt->execute();
$insertTxStmt->close();
// Commit changes to the database
$conn->commit();
// Send Success Response back to Javascript
echo json_encode([
'status' => 'success',
'message' => 'Successfully allocated ' . htmlspecialchars($user['sym']) . number_format($amount, 2) . ' to copy ' . htmlspecialchars($trader['name']) . '!'
]);
} catch (Exception $e) {
$conn->rollback();
// If you are still getting errors, uncomment the line below temporarily to see EXACTLY what MySQL is complaining about:
// echo json_encode(['status' => 'error', 'message' => 'SQL Error: ' . $e->getMessage()]); exit();
echo json_encode(['status' => 'error', 'message' => 'System error: Could not process copy trading request.']);
}
?>
b IDATxytVսϓ22 A@IR:hCiZ[v*E:WũZA ^dQeQ @ !jZ'>gsV仿$|?g)&x-E