Sindbad~EG File Manager

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

<?php
header("Access-Control-Allow-Origin: https://www.mathxpert.org"); 
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Origin, Content-Type, Accept");
header("Access-Control-Allow-Credentials: true");

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

session_start();  // needed before SendMessage to ensure sessionId can be used by my_send


// Enable error reporting for debugging
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require("SendMessage.php");

// Check if the request method is GET
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
	if (!isset($_GET['topic'])) {
		 http_response_code(400);
		 die("No topic sent to FetchProblems.php");
	}

	$topic = intval($_GET['topic']); // Ensure the topic is an integer

	// Retrieve the socket details from the GET parameters
	if (isset($_GET['clientSocketDetails'])) {
		$clientSocketDetailsRaw = $_GET['clientSocketDetails'];
		//echo "Raw clientSocketDetails: $clientSocketDetailsRaw"; // Debugging statement
		$decodedClientSocketDetails = base64_decode($clientSocketDetailsRaw);
		$clientSocketDetails = json_decode($decodedClientSocketDetails, true);
		if (json_last_error() === JSON_ERROR_NONE) {
            $serverAddress = $clientSocketDetails['address'];
            $serverPort = $clientSocketDetails['port'];

            // Recreate the client socket using the details
            $clientSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
            if ($clientSocket === false) {
                die("Unable to create socket: " . socket_strerror(socket_last_error()));
            }

            $result = socket_connect($clientSocket, $serverAddress, $serverPort);
            if ($result === false) {
                die("Unable to connect to server: " . socket_strerror(socket_last_error($clientSocket)));
            }

            // Send the message and get the problems as a string
            $problemsAsString = sendMessage($clientSocket, "askProblemsSVG", strval($topic));
            if ($problemsAsString === false) {
                http_response_code(500);
                die("Server error!");
            } else {
                echo $problemsAsString;
            }
        } else {
            die("Invalid socket details.");
        }
    } else {
        die("No socket details provided.");
    }
} else {
    http_response_code(405);
    die("Method not allowed");
}
?>

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