<?php	
/*																
This program creates account of the new user, tests the validity of input including e-mail domain
and redirect into display page. Function create_table_user() is used to create database of new user, it has 6 field				
Only 4 fields are get from user. It is left commented since table can be created only  			
once. Input form and process form as well as form validation are processed in same file			
*/
session_start();
require_once("includes/mysql_login.php"); 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>IEEE Student Chater | University of Alaska Acnhorage</title>
<link rel="stylesheet" href="includes/style.css" type="text/css" /></head>
<body> 
<?php
include("header.htm");
echo "<div class=left>";
include("includes/left.htm");
echo "</div>";
echo "<div class=body>";
if(isset($_POST['submit']))
{
	function dump_info_mysql()
	{
		$username = mysql_real_escape_string(trim($_POST['username']));
		$password = mysql_real_escape_string(trim($_POST['password']));
		$email = mysql_real_escape_string(trim($_POST['email']));
		$name = mysql_real_escape_string(trim($_POST['name'])); 
		$admin = 0;
		$dump_user_database = "INSERT INTO `user` values('$indentification', '$username', md5('$password'), '$email', '$name', '$admin', now())";
		$dump_user_database = mysql_query($dump_user_database);
			if(!$dump_user_database)
			{
				echo " Error found on database " . mysql_error();
			} else 
			{
				$result = mysql_query("SELECT * FROM user WHERE password=md5('$password') AND username='$username'") or die("Couldn't query the user-database.");
				$row_user = mysql_fetch_array($result);
				$_SESSION['identification'] = $row_user['identification'];
				$_SESSION['autheticate'] = true;
				echo "<meta http-equiv = \"refresh\" content = \"1; URL=index.php\">";
			}
	}
/*
E-mail validation, check the false domain
*/
	function check_domain()
		{
			$email = mysql_real_escape_string(trim($_POST['email']));
			function customCheckDnsrr($host,$recType='')
	 		  {
 					if(!empty($host))
			 		{
  						if($recType=='') $recType="MX";
  							exec("nslookup -type=$recType $host",$output);
  								foreach($output as $line) 
									{
   										if(preg_match("/^$host/", $line))
								 		{
								 			//echo $line . "<br>";
    										return true;
   								 		}
  									}
  								return false;
 					}
 					return false;
			}
			function splitDomain($email)
	  		{
	  				list($username, $domain) = split('@', $email);
	  				//echo "<br>Username is: " .$username . "<br>";
	  				//echo "Domain is :" . $domain . "<br>";
	  				if(customCheckDnsrr($domain))
	  				{
	  					//echo "Domain found";
						dump_info_mysql();
	   				} else
	   				{
	    				//echo "Domain Not found";
						echo "<meta http-equiv = \"refresh\" content = \"2; URL=frame_user_apply.php\">";

	   				}
	  
	  		}
			if(isset($email))
		 	{
				splitDomain($email);
			}
		}
/*
Check for valid inputs. If the all the user inputs are valid check for false domian by calling 
subroutine check_domain() 
*/
	function verify_user_input()
		{
			$username = mysql_real_escape_string(trim($_POST['username']));
			$password = mysql_real_escape_string(trim($_POST['password']));
			$email = mysql_real_escape_string(trim($_POST['email']));
			$name = mysql_real_escape_string(trim($_POST['name']));
			$errors = array();
			if(($username == "")||(strlen($username)>50))
			{
				$errors[] = " Your username field is either blank or longer then 50 charecters.";
			}
			if(($password == "")||(strlen($password)>50))
			{
				$errors[] = " Your password field is either blank or longer then 50 charecters.  ";
			}
			if(($email == "")||(strlen($email)>50))
			{
				$errors[] = " Your email field is either blank or longer then 50 charecters.";
			}
			if(empty($errors))
			{
		
			check_domain();
			} else 
			{
				foreach ($errors as $error) 
				{
					echo "- ". $error; 
					echo "<br>";
				}
				echo "<meta http-equiv = \"refresh\" content = \"2; URL=frame_user_apply.php\">";
				echo "<br> Try again! ";
			}
	}
/*
Function call for user-input validity
*/
	verify_user_input();
	function create_table_user()
	{
			$create_table_user = 'CREATE TABLE `user`(`identification` int(6) auto_increment NOT NULL, 
											 					`username` varchar(200) NOT NULL,
											  					`password` varchar(200) NOT NULL,
											   					`email` varchar(200) NOT NULL,
																 `name` varchar(200), 
																 `admin` tinyint NOT NULL, 
																`date_created` timestamp, PRIMARY KEY(`identification`)
																 )';  
				$table_user_result = mysql_query($create_table_user);
				if(!$table_user_result)
				{
					echo "Error: ".mysql_error();
				}														
	}
	//create_table_user();
}
else 
{
echo "<h3>Create your account </h3><br><h4> Your information won't be shared to any party at any circumstances. </h4>";
echo "<p><br>Required fields are marked by *"; 
echo "<form action=\"$PHP_SELF\" method=\"POST\">";
echo "Username:
<input name=username type=text>*<br> (Minimum 5 charectors required)
<br> <br>
Password: &nbsp;
<input name=password type=password>* <br> (Minimum 5 charectors required)
<br><br>
E-mail: &nbsp; &nbsp; &nbsp;
<input name=email type=text>*<br><br>
<br>
Name :&nbsp; &nbsp;
  <input name=name type=text><br><br>
  <input name=submit type=submit value=Create>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=reset type=reset value=Oops!></p>
</form>";
}
echo "</div>";
include("bottom.htm");
?>
</body>
</html>