Sindbad~EG File Manager
<?php
// This file is meant to be 'required' by other files
// so in particular, it doesn't need session_start
function my_send($socket, $message)
// returns the number of bytes sent
{
$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;
}
// echo("Sent $sent bytes");
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);
if($errcode != 0)
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 = "/home/beeson/MathXpert/bin/restartEngine.sh"; // Command to kill any job listening to the Engine's port and restart the Engine.
$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 a second for the server to start up
sleep(1);
}
}
function createClientSocket($serverAddress, $serverPort, $timeout)
// create the client socket, connect it to the server, and return the socket.
// won't work if there is password protection on the server.
// 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>";
echo ("serverAddress and port: $serverAddress : $serverPort" . "<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 $message with empty parameter<br>");
return;
}
// Check if running on localhost
if ($_SERVER['SERVER_NAME'] === 'localhost' || $_SERVER['REMOTE_ADDR'] === '127.0.0.1') {
set_time_limit(300); // Extend the time limit when running on localhost
}
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