Sindbad~EG File Manager

Current Path : /home/beeson/public_html/WebMathXpert/
Upload File :
Current File : //home/beeson/public_html/WebMathXpert/EnginePortal.php

<?php
// Start session first, before any output
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/php-error.log'); // Logs to a file in the same directory

session_start();
error_log("Session ID: " . session_id() . " - Request: " . ($_POST['message'] ?? 'No message'));
error_log("Session Cookies: " . json_encode($_COOKIE));

// Now send CORS headers
if (isset($_SERVER['HTTP_ORIGIN'])) {
    // Only allow specific trusted domains
    $allowed_origins = ["https://www.mathxpert.org", "https://mathxpert.org", "https://localhost:8443"];

    if (in_array($_SERVER['HTTP_ORIGIN'], $allowed_origins)) {
        header("Access-Control-Allow-Origin: " . $_SERVER['HTTP_ORIGIN']);
        header("Access-Control-Allow-Credentials: true"); // Required for cookies/sessions
    }
}
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Origin, Content-Type, Accept");

// Handle preflight OPTIONS request
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    http_response_code(200);
    exit();
}

?>
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$sessionId = session_id(); 
require_once("SendMessage.php");

// Engine connection details
if ($_SERVER['SERVER_NAME'] === 'localhost' || $_SERVER['SERVER_ADDR'] === '127.0.0.1') {
    $serverAddress = '127.0.0.1'; // Localhost for testing
} else {
    $serverAddress = '208.69.40.113'; // Monkey Brains server IP
}
$serverPort = 12349; // Adjust the server port

// echo("got to EnginePortal.php with $serverAddress");
// Check if message and param are set in the POST request
if (isset($_POST['message']) && isset($_POST['param'])) {
    $message = $_POST['message'];
    $param = $_POST['param'];

    // Open a socket connection to the Engine
	 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 
    if (!$socket) {
        // Handle error if the socket connection fails
        http_response_code(500);
        echo "Socket connection failed: $errstr ($errno)";
        exit;
    }
	 $result = socket_connect($socket, $serverAddress, $serverPort);
	 if ($result === false) {
	     die("Unable to connect to server: " . socket_strerror(socket_last_error($socket)));
	 }
    // Send message to the Engine using sendMessage function
    try {
        $response = sendMessage($socket, $message, $param);
    } catch (Exception $e) {
        // Handle any exceptions from sendMessage
        socket_close($socket);
        http_response_code(500);
        echo "Error sending message to Engine: " . $e->getMessage();
        exit;
    }

    // Close the socket connection
    socket_close($socket);

    // Respond back to the fetch request with the Engine's response
    echo $response;
} else {
    // If message or param is not set, respond with an error
    http_response_code(400); // Bad Request
    echo "Error: message and param are required.";
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists