<?php

	//Security
	if( ! defined( 'IS_INIT' ) )
	{
		die( 'Unauthorized Access');
	}
	global $Handler;
	
	function MySQLClean($str)
	{
		return trim( mysql_real_escape_string ( $str ) );
	}
        
	if( isset( $_POST['username'] ) && isset( $_POST['password']) && isset( $_POST['password1'] ) && isset( $_POST['forumn'] ) && isset( $_POST['forump'] ) )
	{
		$pass = $_POST['password'];
		$user = $_POST['username'];
		if( $pass == $_POST['password1'] )
		{
			if( preg_match( '/[^a-zA-Z0-9]/' , $user ) == 0 )
			{
				if( strlen( $user) >= 3 && strlen( $user ) <= 15 )
				{
					if( strlen( $pass ) >= 5 && strlen( $pass) <= 16 )
					{
                                            $mysqli = new mysqli('209.59.150.117', 'risor125_1124', 'khNK$lJz,pf*#Rh', 'risor125_1224');
                                            if ($mysqli->connect_error) 
                                            {
                                                die('Connect Error (' . $mysqli->connect_errno . ') '
                                                 . $mysqli->connect_error);
                                            }
						if( $k = $mysqli->prepare( "Select members_pass_salt from members where name =? and member_banned = 0" ) )
						{
							$salt = null;
							$k->bind_param( "s", $_POST['forumn'] );
							$k->execute();
							$k->bind_result( $salt );
							if( $k->fetch() )
							{
								$k->close();
								$usrhash = md5( md5( $salt ) . md5( $_POST['forump'] ) );
								if( $do = $mysqli->prepare( "Select member_id , email from members where name =? and members_pass_hash =?" ) )
								{
									$member_id = null;
									$email = null;
									$do->bind_param( "ss" , $_POST['forumn'] , $usrhash );
									$do->execute();
									$do->bind_result( $member_id , $email );
									if( $do->fetch() )
									{
										$checkName = sqlsrv_prepare( $Handler->database->conn , "Select nUserNo from Account.dbo.tUser where sUserID = ?" , array( &$user ) );
										sqlsrv_execute( $checkName );
										if( sqlsrv_has_rows( $checkName ) == false )
										{
											$checkRegistered = sqlsrv_prepare( $Handler->database->conn , "Select GameName from Account.dbo.tUserLink where ForumID = ? " , array( &$member_id )  );
											sqlsrv_execute( $checkRegistered );
											if( sqlsrv_has_rows( $checkRegistered ) == false)
											{
												$pass = md5( $pass . SALT );
												$register = sqlsrv_prepare( $Handler->database->conn , "Insert into Account.dbo.tUser( sUserID , sUserPW , sUserName , sUserIP , nAuthID ) values ( ? , ? , '-' , ? , 0 ) ", array( &$user , &$pass , &$_SERVER['REMOTE_ADDR'] ) );
												sqlsrv_execute( $register );
												$getNo = sqlsrv_prepare( $Handler->database->conn , "Select nUserNo from Account.dbo.tUser where sUserID = ? " , array( &$user ) );
												sqlsrv_execute( $getNo );
												$ret = sqlsrv_fetch_array( $getNo , SQLSRV_FETCH_ASSOC );
												$userno = $ret['nUserNo'];
												$userOptions = sqlsrv_prepare( $Handler->database->conn , "Insert into Account.dbo.tUserOption( nUserNo , sEmail ) values ( ? , ? )" , array( &$userno , &$email ) );
												sqlsrv_execute( $userOptions );
												$tCash = sqlsrv_prepare( $Handler->database->conn , "Insert into Account.dbo.tCash values ( ? , 1 , 0 , 'true' ) " , array( &$userno ) );
												sqlsrv_execute( $tCash );
												$userLink = sqlsrv_prepare( $Handler->database->conn , "Insert into Account.dbo.tUserLink values ( ? , ? ) " , array( &$member_id , &$user ) );
												sqlsrv_execute( $userLink );
												$Handler->theme->SetValue( 'MESSAGE' , $Handler->theme->GetValue( 'SUCCESS_REGISTER' ) );
												$Handler->theme->SetValue( 'GO_BACK' , $Handler->theme->GetValue( 'GO_LOGIN' ) );
												$Handler->theme->SetValue( 'RED_URL' , MAIN_URL . "?view=login" );
												$Handler->theme->Parse( 'success.html' );
											}
											else
											{
												$Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_REGISTERED' ) );
												$Handler->theme->SetValue( 'GO_BACK' , $Handler->theme->GetValue( 'GO_BACK' ) );
												$Handler->theme->SetValue( 'RED_URL' , MAIN_URL . "?view=register" );
												$Handler->theme->Parse( 'error.html' );
											}
										}
										else
										{
											$Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_NAME_TAKEN' ) );
											$Handler->theme->SetValue( 'GO_BACK' , $Handler->theme->GetValue( 'GO_BACK' ) );
											$Handler->theme->SetValue( 'RED_URL' , MAIN_URL . "?view=register" );
											$Handler->theme->Parse( 'error.html' );
										}
									}
									else
									{
										$Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_BAD_INFO' ) );
										$Handler->theme->SetValue( 'GO_BACK' , $Handler->theme->GetValue( 'GO_BACK' ) );
										$Handler->theme->SetValue( 'RED_URL' , MAIN_URL . "?view=register" );
										$Handler->theme->Parse( 'error.html' );
									}
								}
								else
								{
									$Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_SERVER' ) );
									$Handler->theme->SetValue( 'GO_BACK' , $Handler->theme->GetValue( 'GO_BACK' ) );
									$Handler->theme->SetValue( 'RED_URL' , MAIN_URL . "?view=register" );
									$Handler->theme->Parse( 'error.html' );
									echo $usrhash;
								}
							}
							else
							{
								$k->close();
									$Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_BAD_INFO' ) );
									$Handler->theme->SetValue( 'GO_BACK' , $Handler->theme->GetValue( 'GO_BACK' ) );
									$Handler->theme->SetValue( 'RED_URL' , MAIN_URL . "?view=register" );
									$Handler->theme->Parse( 'error.html' );
							}
						}
						else
						{
							$Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_SERVER' ) );
							$Handler->theme->SetValue( 'GO_BACK' , $Handler->theme->GetValue( 'GO_BACK' ) );
							$Handler->theme->SetValue( 'RED_URL' , MAIN_URL . "?view=register" );
							$Handler->theme->Parse( 'error.html' );
						}
					}
					else
					{
						$Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_BAD_LENGTH_PASS' ) );
						$Handler->theme->SetValue( 'GO_BACK' , $Handler->theme->GetValue( 'GO_BACK' ) );
						$Handler->theme->SetValue( 'RED_URL' , MAIN_URL . "?view=register" );
						$Handler->theme->Parse( 'error.html' );
					}
				} 
				else
				{
					$Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_BAD_LENGTH_USER' ) );
					$Handler->theme->SetValue( 'GO_BACK' , $Handler->theme->GetValue( 'GO_BACK' ) );
					$Handler->theme->SetValue( 'RED_URL' , MAIN_URL . "?view=register" );
					$Handler->theme->Parse( 'error.html' );
				}
			}
			else
			{
				$Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_INVALID_CHARS' ) );
				$Handler->theme->SetValue( 'GO_BACK' , $Handler->theme->GetValue( 'GO_BACK' ) );
				$Handler->theme->SetValue( 'RED_URL' , MAIN_URL . "?view=register" );
				$Handler->theme->Parse( 'error.html' );
			}
		} 
		else
		{
			$Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_PASS_MISMATCH' ) );
			$Handler->theme->SetValue( 'GO_BACK' , $Handler->theme->GetValue( 'GO_BACK' ) );
			$Handler->theme->SetValue( 'RED_URL' , MAIN_URL . "?view=register" );
			$Handler->theme->Parse( 'error.html' );
		}
	} 
	else
	{
		$Handler->theme->Parse( 'register.html' );
	}

 ?>