<?php
	require("settings.php");
	require_once("helper.php");

	session_start();
	html_start("Register");

	echo "<H1>Register</H1>\n";

	if(!isset($_REQUEST["user"]) || !isset($_REQUEST["pass"])) {
		echo "Please pick a Username and Password:<BR><BR>\n";
		login_form("register.php");
	} else {
		$link = db_connect();
		$user = mysql_real_escape_string(htmlspecialchars($_REQUEST["user"]), $link);
		$pass = md5($_REQUEST["pass"]);

		$query = "INSERT INTO users SET user = \"$user\", pass = \"$pass\" ";
		$result = mysql_query($query, $link)
		or die("Query failed in file". __FILE__ ." line ". __LINE__ ." with: ". mysql_error($link));
		$id = mysql_insert_id($link);

		if($id == 0) {
			echo "Sorry. Registration failed.";
			die();
		}

		$query = "INSERT INTO capabilities SET id = $id, capability = \"login\" ";
		$result = mysql_query($query, $link)
		or die("Query failed in file". __FILE__ ." line ". __LINE__ ." with: ". mysql_error($link));
		$num = mysql_affected_rows($link);
		mysql_close($link);

		if($num != 1) {
			echo "Sorry. Registration failed. ($num)";
			die();
		}

		echo "Registration successfull. Please login in.<BR><BR>\n";
		login_form("index.php");
	}
?>
 </BODY>
</HTML>
