<?php
function prepare_test($sid, $link) {
	$out = array("error" => 0, "stderr" => "");

	$dirname = "/tmp/webtask_".mt_rand();
	if(!mkdir($dirname, 0700)) {
		$out["interror"] = 4;
		$out["stderr"] = "Can't create temporary directory";
		return $out;
	}
	$sid = intval($sid);
	$query = "SELECT solution FROM solutions WHERE sid=$sid";
	$result = mysql_query($query, $link) or die("Can't fetch solution: ".mysql_error($link));
	if(mysql_num_rows($result) != 1) {
		$out["interror"] = 5;
		$out["stderr"] = "Can't fetch solution from database";
		return $out;
	}
	$line = mysql_fetch_array($result);
	$solution = $line["solution"];
	$classname = get_classname($solution);
	$s_filename = $dirname."/".$classname.".java";

	$out = put_file($s_filename, $solution);

	$out["dirname"] = $dirname;
	$out["classname"] = $classname;
	$out["sourcefile"] = $s_filename;
	return $out;
}

function cleanup_dir($dirname) {
	if(!($dp = opendir($dirname))) {
		die("Can't open dir");
	}
	while(($file = readdir($dp)) != FALSE) {
		if ($file != "." && $file != "..") {
			if(is_dir($file)) {
				cleanup_dir($dirname."/".$file);
			} else {
				unlink($dirname."/".$file);
			}
		}
	}
	closedir($dp);
	rmdir($dirname);
}

function save_test($ret, $testno, $sid, $link) {
	if($ret["interror"] != 0) {
		echo "Something in the testscript broke <BR>\n";
	} else if($ret["retval"] != 0) {
		echo "Test failed <BR>\n";
	} else {
		echo "Test succeeded <BR>\n";
	}
	echo "Return value: {$ret["retval"]} <BR>\n";
	echo "Content of stdout: ".htmlspecialchars($ret["stdout"])." <BR>\n";
	echo "Content of stderr: ".htmlspecialchars($ret["stderr"])." <BR>\n";

	$mysql_retval = mysql_real_escape_string($ret["retval"], $link);
	$mysql_stdout = mysql_real_escape_string(htmlspecialchars($ret["stdout"]), $link);
	$mysql_stderr = mysql_real_escape_string(htmlspecialchars($ret["stderr"]), $link);
	$sid = intval($sid);
	$testno = intval($testno);

	$query = "DELETE FROM results WHERE sid = $sid AND testno = $testno";
	$result = mysql_query($query, $link) or die ("Can't save test results: ".mysql_error($link));

	$query = "INSERT INTO results SET sid = $sid, testno = $testno, retval = $mysql_retval, stdout = \"$mysql_stdout\", stderr = \"$mysql_stderr\"";
	$result = mysql_query($query, $link) or die ("Can't save the test results: ".mysql_error($link));
}

function put_file($name, $content) {
	$out = array("interror" => 0, "stderr" => "");
	$fp = fopen($name, "w+");
	if(!$fp) {
		$out["interror"] = 6;
		$out["stderr"] = "Can't create file $name";
		return $out;
	}
	$len = fwrite($fp, $content);
	if($len != strlen($content)) {
		$out["interror"] = 7;
		$out["stderr"] = "Can't write content to $name";
		return $out;
	}
	if(!fclose($fp)) {
		$out["interror"] = 8;
		$out["stderr"] = "Can't close file $name";
		return $out;
	}
	return $out;
}

function get_classname($source) {
	$start = strpos($source, "class")+strlen("class");
	$end = strpos($source, "{", $start);
	$string = trim(substr($source, $start, $end-$start));
	$string = preg_replace('/[^(\x30-\x7A)]*/','', $string);
	$string = preg_replace('/[\x3A-\x40]*/','', $string);
	$string = preg_replace('/[\x5B-\x5E]*/','', $string);
	$string = preg_replace('/\x60*/','', $string);
	return $string;
}
?>
