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

include ("mysqlconnect.inc.php");
include ("tools.php");



include "header.php";
$JSINCLUDE = "resultmanager.js";

/******* Init the interface  ******/
session_start(); //web side
if(isset($_REQUEST['id'])){
$id = $_REQUEST['id']; 
}
?>
<html>
<head>
<script type="text/javascript" src="<? echo $JSINCLUDE;?>">
</script>
<body>
<div id="content">
<br><br><br>
<form name="upload" enctype="multipart/form-data" method='post' action="<?$_SERVER['PHP_SELF']?>">
Input your ID Key:<br><br>
<input type="text" name="id">
<input type="submit" name="submit">
<br>
<br>
<br>

<?

if (!isset($id)){
	echo "You must give your Job Id <br>";
	if(isset($_REQUEST['id'])){
		echo "ID: ".$id."<br>";		
	}

	
} else {

	$machinetable =$GLOBALS['machinetable'];
	$query = "Select * from $jobtable Where dlocation='$id'";
	$result = mysql_fetch_assoc(mysql_query($query));
	if (!isset($result['dlocation'])){
		print("Invalid Key or Result already deleted <br> Please choose again<br>");
		die();
	}
	//init ssh2 connection to the machine
	$query = "Select remote_machines_id from $jobtable Where dlocation='$id'";
	$result = mysql_fetch_assoc(mysql_query($query));
	$machineid = $result['remote_machines_id'];

	$query = "Select iplocation from $machinetable Where id='$machineid'";
	$result = mysql_fetch_assoc(mysql_query($query));
	$conn = retConnection($result['iplocation']);	

	

	//Detect the result files
	$cmd = "ls -lrt $id \n";
        $shell = ssh2_shell($conn, 'xterm', null, 80, 24, SSH2_TERM_UNIT_CHARS);
        fwrite($shell,$cmd);
        sleep(1);

	$incoming = array();
	$i = 0;
        // Then u can fetch the stream to see what happens on stdio
        while($line = fgets($shell)) {
             flush();
             $incoming[$i++] = $line;
        }
        fclose($shell);



	?>
	<table border=1>
	<tr>
	<th>&nbsp</th>
	<th>File Name:</th>
	</tr>

	<?

	
	//extract files from the output
	$files = array();
	for ($n = 4; $n < count($incoming)-1; $n++){
		$pos = strpos($incoming[$n], ':');
		if ($pos > 0){
			$pos = strpos($incoming[$n], " ", $pos);
			$files[$n-4] = trim(substr($incoming[$n],$pos ));

			//print a selective list of files o display (get rid of the problem which files are which)
			print("
				<tr>
				<td><INPUT TYPE=\"radio\" NAME=\"update\" VALUE=\""."./".$id."/".$files[$n-4]."\" onClick=\"if (this.checked) updateResultInfo('"."./".$id."/".$files[$n-4]."','".$machineid."');\"></td>
				<td>".$files[$n-4]."</td>
				</tr>
			");
		}
	}

	?>
	</table>
	<div id="dataoutput">
	</div>

	<?

}
?>
</div>
</body>
</html>


