<?php
function html_start($title) {
	echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
	echo "<HTML>\n";
	echo "<HEAD>\n";
	echo "<META http-equiv=\"expires\" content=\"0\">\n";
	echo "<TITLE>\n";
	echo "$title\n";
	echo "</TITLE>\n";
	echo "<LINK rel=\"stylesheet\" href=\"style.css\">\n";
	echo "</HEAD>\n";
	echo "<BODY>\n";
}

function login_form($url, $keep = array(), $buttontext = "Login") {
	echo "<FORM action=\"$url\" method=\"post\">\n";
	while(list($key, $val) = each($keep)) {
		echo "<INPUT type=\"hidden\" name=\"$key\" value=\"$val\">\n";
	}
	echo "<LABEL for=\"user\">Username:</LABEL><BR>\n";
	echo "<INPUT type=\"text\" name=\"user\" id=\"user\" size=\"20\" maxlength=\"20\"><BR>\n";
	echo "<LABEL for=\"pass\">Password:</LABEL><BR>\n";
	echo "<INPUT type=\"text\" name=\"pass\" id=\"pass\" size=\"20\" maxlength=\"20\"><BR>\n";
	echo "<INPUT type=\"submit\" value=\"$buttontext\">\n";
	echo "</FORM>\n";
}

function db_connect() {
	include("settings.php");

	$connect = mysql_connect($dbHost, $dbUser, $dbPass, true)
	or die("Can't connect to DB server!");

	$selectDB = mysql_select_db($dbName, $connect)
	or die("Can't select DB <b>$dbName</b>!");

	return $connect;
}

function check_capa($id, $capa, $link) {
	$id = intval($id);
	$capa = mysql_real_escape_string($capa, $link);
	$query = "SELECT COUNT(*) AS count FROM capabilities WHERE id = $id AND capability = \"$capa\"";
	$result = mysql_query($query, $link)
	or die("Capability check failed with: ". mysql_error($link));

	$num = mysql_num_rows($result);
	if($num != 1) {
		return false;
	}

	$line = mysql_fetch_array($result);
	if($line["count"] != "1") {
		return false;
	}

	return true;
}

function testresults($sid) {
	$sid = intval($sid);
	$link = db_connect();
	$query = "SELECT COUNT(*) AS count FROM results WHERE sid = $sid";
	$result = mysql_query($query, $link) or die ("Can't count test results: ".mysql_error($link));
	$line = mysql_fetch_array($result);
	$count = $line["count"];

	if($count == 0) {
		mysql_close($link);
		return "untested";
	}

	$query = "SELECT COUNT(*) AS failed FROM results WHERE sid = $sid AND retval <> 0";
	$result = mysql_query($query, $link) or die ("Can't count failed tests: ".mysql_error($link));
	$line = mysql_fetch_array($result);
	$failed = $line["failed"];

	mysql_close($link);

	if($failed == 0) {
		return "testok";
	} else {
		return "testfailed";
	}
}

function tid_from_sid($sid) {
	$sid = intval($sid);
	$link = db_connect();
	$query = "SELECT tid FROM solutions WHERE sid = $sid";
	$result = mysql_query($query, $link) or die("Can't get tid from sid: ".mysql_error($link));
	$num = mysql_num_rows($result);
	if($num < 1) {
		mysql_close($link);
		return 0;
	} else if ($num > 1) {
		mysql_close($link);
		return 0;
	} else {
		$line = mysql_fetch_array($result);
		mysql_close($link);
		return $line["tid"];
	}
}

function user_from_id($id) {
	$link = db_connect();
	$id = intval($id);
	$query = "SELECT user FROM users WHERE id = $id";
	$result = mysql_query($query, $link) or die("Can't fetch username: ".mysql_error($link));
	if(mysql_num_rows($result) != 1) {
		die("This id does not belong to one user");
	}
	$line = mysql_fetch_array($result);
	mysql_close($link);
	return $line["user"];
}

function make_printable($string) {
	$out = "";
	for($i = 0; $i < strlen($string); $i++) {
		$char = ord($string{$i});
		if($char == 92) {
			$out .= "\\\\";
		} else if(($char) > 31 && ($char < 127)) {
			$out .= chr($char);
		} else if ($char == 9) {
			$out .= "\\t";
		} else if ($char == 10) {
			$out .= "\\n";
		} else if ($char == 13) {
			$out .= "\\r";
		} else {
			$out .= "\\x".sprintf("%02x" ,dechex($char));
		}
	}
	return $out;
}

function make_unprintable($string) {
	$out = "";
	for($i = 0; $i < strlen($string); $i++) {
		$char = ord($string{$i});
		if($char == ord("\\")) {
			$i++;
			$char = ord($string{$i});
			if($char == ord("t")) {
				$out .= "\t";
			} else if($char == ord("n")) {
				$out .= "\n";
			} else if($char == ord("r")) {
				$out .= "\r";
			} else if($char == ord("x")) {
				$out .= chr(hexdec(substr($string, $i, 2)));
				$i += 2;
			} else if($char == ord("\\")) {
				$out .= "\\";
			}
		} else {
			$out .= chr($char);
		}
	}
	return $out;
}
?>
