Sindbad~EG File Manager

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

<?php
// This file is meant to be 'required'  by other files

 function my_send($socket, $message)
 {
    $sessionId = session_id(); 
    $data = $sessionId . "|" . $message;
	$sent = 0;
	$messagelength = strlen($data);
	// first send six characters with the messagelength
	$messagelength_string = sprintf("%6d",strval($messagelength));
	while($sent < 6)
	{   
		$more = socket_write($socket,$messagelength_string,6-$sent);	
 		if ($more === false || $more < 0)
 		  {   echo "Socket write failed: " . socket_strerror(socket_last_error()) . "<br>";
 			  die();
 		  }
 		$sent = $sent + $more;
	}
	$sent = 0;
	// echo("sent 6 bytes, now to send $data of length $messagelength <br>");
	while($sent < $messagelength)
	{  $resttosend = substr($data, $sent);
		$bytestogo = $messagelength - $sent;
	       // echo "About to write $bytestogo bytes<br>";
 	   $more = socket_write($socket,$resttosend ,$bytestogo);
	       // echo "The moving finger writes  $more bytes out of $messagelength<br>";
	   if($more == 0)
		   { 
			   echo("Goodbye, cruel world, server has disconnected.");
			   return 0;
		   }
		if ($more < 0)
		  {   echo "Socket write failed: " . socket_strerror(socket_last_error()) . "<br>";
			  return $more;
		  }
		$sent = $sent + $more;
	}
	return $sent;		   
 }
 
 function my_receive($socket, $bufferSize)
	 // return the response from the server on $socket,  or false for failure
 {
	 // first we have to get the length of the response, which will be sent as MAXDIGITS digits
	 $MAXDIGITS = 7;
	 $received_bytes= 0;
	 $received = "";
	 // echo "Entered my_receive";
	 while($received_bytes < $MAXDIGITS)
		 {   // echo(" $received_bytes, $MAXDIGITS");
			 $next = socket_read($socket, $MAXDIGITS-$received_bytes);
			 if ($next == false) 
			    {
					$errcode = socket_last_error($socket);
					$message = socket_strerror($errcode);
					echo "Socket_read error58: $message<br>";
					return false;
				}
			$received = $received . $next;
			$received_bytes = $received_bytes + strlen($next);
		}
	$datalength = intval($received);  // remove blank padding
	// echo "<br> datalength = $datalength";
	// now get the data itself, now that we know how many bytes to expect
    $received_bytes= 0;
    $received = "";
	while($received_bytes < $datalength)
	{
		$response = socket_read($socket, $datalength - $received_bytes);
		if ($response === false)
			{
				$errcode = socket_last_error($socket);
				$message = socket_strerror($errcode);
				echo "Socket_read error2: $message<br>";
				return false;
			}
		$received = $received . $response;
		$received_bytes = $received_bytes + strlen($response);
	}
	return $received;
 } 
     
 function isServerRunning() 
 {
     $command = 'ps aux | grep Engine | grep -v grep | wc -l';
     $output = shell_exec($command);
     $processCount = intval(trim($output));
     return $processCount > 0;
 }
 
if (!isServerRunning()) 
 {
     // Server is not running, start it
	// $startCommand = "/Users/beeson/Library/Developer/Xcode/DerivedData/MathXpert_Calculus_Assistant-hjjtzdcfsfpxajfuxfdhzcbdsajn/Build/Products/Debug/ParseAndDisplayX";
	 $startCommand = "/Users/beeson/Library/Developer/Xcode/DerivedData/MathXpert_Calculus_Assistant-hjjtzdcfsfpxajfuxfdhzcbdsajn/Build/Products/Debug/Engine.app";
     $startCommand = $startCommand .  ' > /dev/null 2>&1 &';     // Command to start the server in the background
     // echo("Server is not running, so it will now be started.\n");  // no need to even mention it to the user
     // echo("Running $startCommand \n");
	$output = [];
	$return_var = 0;
	exec($startCommand, $output, $return_var);   // actually start the server

	if ($return_var !== 0)
	{
		echo "Error starting server: " . implode("\n", $output);
	} 
	else 
	{
	 	// echo "Server started successfully!";   // no need to even mention it to the user
		// Wait for the server to start up
		sleep($startupDelay);
	}

 }
 
 function createClientSocket($serverAddress, $serverPort, $timeout)
 // create the client socket, connect it to the server, and return the socket.
 //  return false on failure.
 {  
     $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

     if ($socket === false) 
     {
         echo "Socket creation failed: " . socket_strerror(socket_last_error()) . "<br>";
		 return false;
     } 
     socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => $timeout, "usec" => 0));
     $connected = socket_connect($socket, $serverAddress, $serverPort);
	 if($connected === false)
		 { echo("Cannot connect to server <br>");
		   echo "because: " . socket_strerror(socket_last_error()) . "<br>";
		   die();
		 }
	 return $socket;
 }
	 

 function sendMessage($socket, $message, $param)
 {   
	 // echo "<Sending $message<br>";
	 if(! is_string($message) || strlen($message) == 0)
	 { echo ("Cannot send an empty message<br>");
	   return;
	 }
	 if(! is_string($param) || strlen($param) == 0)
	 {
		 echo("Cannot send a message with empty parameter<br>");
		 return;
	 }
	 my_send($socket, "$message|$param");
     $response = my_receive($socket, 1 << 21);  // 1 << 21 is the buffersize
	 return $response;
 }
            
		
?>

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