Sindbad~EG File Manager

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

<?php
// SaveThreeBody.php
// Code to save an .svg file to the /images directory and save a document to ThreeBodyDatabase.db  
// echo "<pre>";  // for debugging to see what is posted
// print_r($_POST);
// echo "</pre>";
// die();

ini_set('display_startup_errors', 1);
session_start();
$sessionId = session_id();  // guaranteed not to contain a tilde
error_reporting(E_ALL);
// Server connection settings
if ($_SERVER['SERVER_NAME'] == 'localhost') {
    $serverAddress = 'localhost';
    $username = $password = null;
} else {
    $serverAddress = 'mathxpert.org';
    $username = 'beeson';
    $password = 'Turing2024';
}
$serverPort = 12350;
$timeout = 3600;
$startupDelay = 5;

// Force HTTPS if not in use
if (!(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS']=='on' || $_SERVER['HTTPS']==1)
   || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO']=='https')) {
    $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: ' . $redirect);
    exit();
}

require("SendMessage.php");
$clientSocket = createClientSocket($serverAddress, $serverPort, $timeout);

$dbFile = 'ThreeBodyDatabase.db';
$db = new SQLite3($dbFile);
require_once("NormalizeColor.php"); 

// Process the form submission.
if ($_SERVER['REQUEST_METHOD'] === 'POST') { 
    if (!isset($_POST['mass']) || !is_array($_POST['mass'])) {
        die("No body data submitted.");
    }
    $nbodies = count($_POST['mass']);
    if ($nbodies < 1 || $nbodies > 12) {
        die("Number of bodies must be between 1 and 12.");
    }
    
    // Get additional fields.
    $caption = isset($_POST['caption']) ? substr(trim($_POST['caption']), 0, 128) : "";
    $tmax = isset($_POST['tmax']) ? (int) $_POST['tmax'] : 1000; 
    $integrationMethod = isset($_POST['integrationMethod']) ? $_POST['integrationMethod'] : "rk4";
    
	 // Construct a new filename for the SVG.
	 $targetDir = "./images/";
	 if (!is_dir($targetDir)) {
	     mkdir($targetDir, 0755, true);
	 }

	 // Base filename using the current timestamp.
	 $baseName = "sim_" . time();
	 $newFileName = $baseName . ".svg";

	 // Ensure the filename is unique in the target directory.
	 $counter = 1;
	 while (file_exists($targetDir . $newFileName)) {
	     $newFileName = $baseName . "_" . $counter . ".svg";
	     $counter++;
	 }

	 $image_filename = $newFileName;    
	 // echo("Sending saveSVG message with $image_filename"); 
	// if (!$clientSocket) {
	//     die("Error: clientSocket is not valid.");
	// } 
	 // var_dump($clientSocket); 
	 $response = false;
	 $response = sendMessage($clientSocket, "saveSVG", $image_filename);
    // $response will be "ok", since we checked the file was not there and since then, any other user would have 
    // used a different timestamp, so our filename is successfully reserved.   Since we don't need the response  
    // we don't need to 'await'  it.  

	 // Wait for 500ms (500,000 microseconds)
	 // usleep(500000);

	// if ($response === false) {
	//     die("Failed to receive response for saveSVG message from the Engine.");
	//  } 
	
	 // echo("Received saveSVG response: $response<br>"); 
	
    // Insert into documents table.
    $stmtDoc = $db->prepare("INSERT INTO documents (nbodies, caption, image_filename, tmax,integrationMethod) VALUES (:nbodies, :caption, :image_filename, :tmax,:integrationMethod)");
    $stmtDoc->bindValue(':nbodies', $nbodies, SQLITE3_INTEGER);
    $stmtDoc->bindValue(':caption', $caption, SQLITE3_TEXT);
    $stmtDoc->bindValue(':image_filename', $image_filename, SQLITE3_TEXT);
    $stmtDoc->bindValue(':tmax', $tmax, SQLITE3_INTEGER); 
    $stmtDoc->bindValue(':integrationMethod', $integrationMethod, SQLITE3_TEXT);
    
	 $result = $stmtDoc->execute();
	 if (!$result) {
	     die("Error inserting document record: " . $db->lastErrorMsg());
	 }
    // Get the DocumentNumber of the inserted record.
    $documentId = $db->lastInsertRowID();
    
    // Insert each body into the bodies table.
    $stmtBody = $db->prepare("INSERT INTO bodies (DocumentNumber, mass, color, r, x0, y0, p0, q0)
                              VALUES (:doc, :mass, :color, :r, :x0, :y0, :p0, :q0)"); 
    for ($i = 0; $i < $nbodies; $i++) {
        $stmtBody->bindValue(':doc', $documentId, SQLITE3_INTEGER);
        $stmtBody->bindValue(':mass', (float)$_POST['mass'][$i], SQLITE3_FLOAT);
        $stmtBody->bindValue(':color', normalized_color($_POST['color'][$i]), SQLITE3_TEXT);
        $stmtBody->bindValue(':r', (float)$_POST['r'][$i], SQLITE3_FLOAT);
        $stmtBody->bindValue(':x0', (float)$_POST['x0'][$i], SQLITE3_FLOAT);
        $stmtBody->bindValue(':y0', (float)$_POST['y0'][$i], SQLITE3_FLOAT);
        $stmtBody->bindValue(':p0', (float)$_POST['p0'][$i], SQLITE3_FLOAT);
        $stmtBody->bindValue(':q0', (float)$_POST['q0'][$i], SQLITE3_FLOAT);
        if (!$stmtBody->execute()) {
            die("Error inserting body data for body " . ($i + 1));
        }
    }
}
?>
<!DOCTYPE html>
<html>
<body>
<script>
alert("Problem saved successfully as Document Number <?php echo("$documentId.")?> ");
window.location.href = 'BrowseThreeBody.php';
</script>
</body>
</html>

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