<?php
require_once("includes/SQLConnect.php");

ini_set('error_reporting',ÊE_ALL);
error_reporting(2048);

if($_POST['name_concat']!=""){
	if($_POST['email']!=""){
		if($_POST['password']!=""){
			if(strpos($_POST['name_concat'],' ') != FALSE){
				$first_name = ucfirst(trim(substr($_POST['name_concat'],0,strpos($_POST['name_concat'],' '))));
				$last_name = ucfirst(trim(substr($_POST['name_concat'],strpos($_POST['name_concat'],' '))));
				$email = trim($_POST['email']);
				$password = trim($_POST['password']);
				

				require('includes/classes/PasswordHash.php');
				$hasher = new PasswordHash(8, FALSE);
				$password_hash = $hasher->HashPassword($password);
				
				$query = sprintf("INSERT INTO Users(first_name,last_name,email,password) VALUES('%s','%s','%s','%s')",
					mysql_real_escape_string($first_name),
                    mysql_real_escape_string($last_name),
                    mysql_real_escape_string($email),
					$password_hash);
				if(mysql_query($query)){
					//If this person signs up because someone recommended them, report that then send the inviter a message
					if(isset($_POST['ID']) && (int)$_POST['ID']>0){
						$from_user_ID = mysql_insert_id();

						$invite_ID = (int)$_POST['ID'];
						mysql_query("UPDATE Users_Invites SET open_flag = 1 WHERE ID = ".$invite_ID." LIMIT 1"); //Set open_flag

						$to_user_ID = mysql_result(mysql_query("SELECT user_ID FROM Users_Invites WHERE ID=".$invite_ID." LIMIT 1"),0);
						$subject = "Your friend $first_name has joined.";
						$body = "They just accepted your invite and should be online shortly. You can see their profile by <a href='http://".$_SERVER['SERVER_NAME']."/profile_user.php?ID=".$from_user_ID."'>clicking here</a>.";
						
						$sql = "INSERT INTO User_Messages(from_user_ID,to_user_ID,subject,body) 
							VALUES(".$from_user_ID.",".$to_user_ID.",'".mysql_real_escape_string(substr($subject,0,255))."','".mysql_real_escape_string($body)."')";
						
						mysql_query($sql); //Fail or succeed silently - No point in bothering the current user with this
					}

					//Send confirmation email
					$send_name = "District-by-District Organizing Tool"; //senders name
					$email = "confirm-accounts@districtbydistrict.summerhilldesign.com"; //senders e-mail adress
					$recipient = $_POST['email']; //recipient
					$confirm_url = "http://districtbydistrict.summerhilldesign.com/register_confirm.php?ID=".mysql_insert_id();
					$mail_body = "Welcome to District-by-District. \r\n\r\n Please visit this URL to confirm your account: ".$confirm_url."\r\n"; //mail body
					$subject = "Welcome to District-by-District, Please Confirm Your Account"; //subject
					$header = "From: ". $send_name . " <" . $email . ">\r\n"; //optional headerfields
					
					ini_set('sendmail_from', 'confirm-accounts@districtbydistrict.summerhilldesign.com'); //Suggested by "Some Guy"
					
					if(mail($recipient, $subject, $mail_body, $header))
						header("Location:index.php?success=Your account has been registered. You should shortly receive an email to confirm your registration.");
					else
						$error = "We experienced a problem sending to your email address (".$_POST['email']."). Please try creating your account with a different email address.";
				}
				else
					$error = "Your account wasn't created. You probably already have an account and haven't confirmed it yet.";
			}
			else
				$validation = "Please enter your full name, both your first name and surname.";
		}
		else
			$validation = "Your password can't be blank.";
	}
	else
		$validation = "You have to enter your email. This is how you'll login and how we'll contact you.";
}

include_once("includes/header.php");
?>

<?php
if(isset($_POST['email']))
	$email = $_POST['email'];
elseif(isset($_GET['email']))
	$email = $_GET['email'];
else
	$email = "";

if(isset($_POST['name_concat']))
	$name =$_POST['name_concat'];
elseif(isset($_GET['name']))
	$name = $_GET['name'];
else
	$name = "";

?>


<form method="POST" class="compact">
	<h2>Registration</h2>
	
	<?php
	if(isset($_GET['ID']) && (int)$_GET['ID']>0){
		?>
		<input type="hidden" class="hidden" name="ID" value="<?=(int)$_GET['ID'];?>">
		<?php
	}
	?>

	<label>Name:</label>
	<input type="text" name="name_concat" value="<?=$name;?>">
	
	<label>Email:</label>
	<input type="text" name="email" value="<?=$email;?>">
	
	<label>Password:</label>
	<input type="password" name="password">
	
	<input type="submit" class="submit" value="Join">


</form>
<?php
include_once("includes/footer.php");
?>
