<?php
include ("db_functions.inc.php");
if($_SERVER["REQUEST_METHOD"] == "POST") {
	
		if ($_POST['recaptcha'] == 'yes') {	
			include("securimage/securimage.php");
			$img = new Securimage();
			$valid = $img->check($_POST['code']);
			if (!$valid) {
				$passed = false;
			} else {
				$passed = true;
			}
		}
		elseif ($_POST['recaptcha'] == 'no') {
			$answer = array('10', 'ten');
			if(!in_array(strtolower($_POST['security']),$answer)) {
				$passed = false;
			} else {
				$passed = true;
			}
		}
					
		if ($passed) {
			$username = yasDB_clean($_POST["username"]);
			$password = md5(yasDB_clean($_POST["password"]));
			$name = yasDB_clean($_POST["name"]);
			$email = yasDB_clean($_POST["email"]);;
			$website = yasDB_clean($_POST["website"]);
			$date = time() + (0 * 24 * 60 * 60);  
			$plays = 0;
			$points = 0;
			$stmt=yasDB_select("SELECT * FROM user WHERE username LIKE '$username'");
			if($stmt->num_rows == 0){
				$stmt = yasDB_insert("INSERT INTO `user` (username, password, name, email, website, plays, points, date) VALUES ('$username','$password','$name','$email','$website','$plays','$points', '$date')",false);
				if ($stmt) {
					echo "<h3>Registered: You can now log in!</h3>";
				} else {
					$stmt->close();
					echo "<h3>Registration failed!</h3>";
				}
			} else {  
				$stmt->close();
				echo "<h3>Sorry, username or email exists. Please try again.</h3>";
			} 
		} else {
			echo '<h3><span style="color:red;">The security question was answered incorrectly. Please try again.</span></h3>';
		}
	
} else {
	echo 'Invalid request.';
}	
?>