<?php
/*=======================================================================
| UberCMS - Advanced Website and Content Management System for uberEmu
| #######################################################################
| Copyright (c) 2010, Roy 'Meth0d' and updates by Matthew 'MDK'
| http://www.meth0d.org & http://www.sulake.biz
| #######################################################################
| This program is free software: you can redistribute it and/or modify
| it under the terms of the GNU General Public License as published by
| the Free Software Foundation, either version 3 of the License, or
| (at your option) any later version.
| #######################################################################
| This program is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
| GNU General Public License for more details.
\======================================================================*/

require_once "global.php";
require_once "inc/recaptchalib.php";

if (LOGGED_IN)
{
	header("Location: " . WWW . "/me");
	exit;
}

if (isset($_GET['cancal']))
{
	unset($_SESSION['jjp']['register']);
	header("Location: " . WWW . "/");
	exit;	
}

$tpl->SetParam('errors', '');
if(isset($_GET['errors']))
{
	$error = '<div id="error-messages-container" class="cbb"> 
            	<div class="rounded" style="background-color: #cb2121;"> 
               <div id="error-title" class="error"> 
								'.$_GET['errors'].'
							 </div> 
            </div> 
        </div>'; 
	$tpl->SetParam('errors', $error);
}


$tpl->Init();

$tpl->AddGeneric('head-init');
$tpl->AddIncludeSet('register');
$tpl->WriteIncludeFiles();
$tpl->AddGeneric('head-bottom');

switch($_GET['stap'])
{
	case "1":
		if (isset($_SESSION['jjp']['register'][1]))
			header("Location: " . WWW . "/quickregister/email_password");
	
		$tpl->AddGeneric('page-register-1');
		break;
		
	case "2":
		$bday_day = $_POST['bean_day'];
		$bday_month = $_POST['bean_month'];
		$bday_year = $_POST['bean_year'];
		$gender = $_POST['bean_gender'];
		
		if (!is_numeric($bday_day) || !is_numeric($bday_month) || !is_numeric($bday_year) || $bday_day <= 0 || $bday_day > 31 ||
			$bday_month <= 0 || $bday_month > 12 || $bday_year < 1900 || $bday_year > 2010)
		{
			$errors = "Bitte gebe ein gültiges Geburtsdatum an.";
			
		}
		else if(!empty($gender))
		{
			$_SESSION['jjp']['register'][1]['bday_day'] = $bday_day;
			$_SESSION['jjp']['register'][1]['bday_month'] = $bday_month;
			$_SESSION['jjp']['register'][1]['bday_year'] = $bday_year;
			$_SESSION['jjp']['register'][1]['gender'] = $gender;
			header("Location: " . WWW . "/quickregister/email_password");
			exit;
		}
		else
		{
			$errors = "Kies een gelacht";
		}

		header("Location: " . WWW . "/quickregister/start/error/".$errors);
		exit;
		break;
	
	case "3":
		if (!isset($_SESSION['jjp']['register'][1]))
			header("Location: " . WWW . "/quickregister/start/error/Je moet eerst deze stap doen voor je verder kan");
		else if (isset($_SESSION['jjp']['register'][2]))
			header("Location: " . WWW . "/quickregister/captcha");
		
		$tpl->AddGeneric('page-register-2');
		break;		
	
	case "4":
		$name = $_POST['bean_name'];
		$email = $_POST['bean_email'];
		$pass1 = $_POST['bean_password'];
		$pass2 = $_POST['bean_retypedPassword'];
		
		if (strlen($name) < 1 and strlen($name) > 16)
		{
			$errors = "Der Name muss zwischen 1 und 16 Zeichen lang sein";
		}
		else if ($users->IsNameTaken($name))
		{
			$errors = "Dieser Name ist bereits im Gebrauch";
		}
		else if ($users->IsNameBlocked($name))
		{
			$errors = "In deinem Namen ist ein Wort das von den Hotel Managern geblockt wurde";
		}
		else if (!$users->IsValidName($name))
		{
			$errors = "Dein Name ist nicht gültig";
		}		
		else if (!$users->IsValidEmail($email))
		{
			$errors = "Deine Email-Adresse ist nicht gültig";
		}
		else if ($pass1 <> $pass2 and strlen($pass1) < 6)
		{
			$errors = "Die beiden Passwörter sind nicht identisch";
		}
		else if (isset($_POST['bean_termsOfServiceSelection']))
		{
			$_SESSION['jjp']['register'][2]['name'] = $name;
			$_SESSION['jjp']['register'][2]['email'] = $email;
			$_SESSION['jjp']['register'][2]['pass'] = $pass1;
			
			header("Location: " . WWW . "/quickregister/captcha");	
			exit;	
		}
		else
		{
			$errors = "Aktzeptiere die AGB";
		}
		
		header("Location: " . WWW . "/quickregister/email_password/error/".$errors);
		exit;
		break;
		
	case "5":
		if (!isset($_SESSION['jjp']['register'][1]))
			header("Location: " . WWW . "/quickregister/start/error/Je moet eerst deze stap doen voor je verder kan");
		else if (!isset($_SESSION['jjp']['register'][2]))
			header("Location: " . WWW . "/quickregister/email_password/error/Je moet eerst deze stap doen voor je verder kan");
	
	$tpl->SetParam('recaptcha_html', recaptcha_get_html("6Le-aQoAAAAAABnHRzXH_W-9-vx4B8oSP3_L5tb0"));
	$tpl->AddGeneric('page-register-3');
	break;	
	
	case "6":
		$resp = recaptcha_check_answer ('6Le-aQoAAAAAAKaqhlUT0lAQbjqokPqmj0F1uvQm', $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
							
		if (!$resp->is_valid)
		{
			$errors = "Captcha Code ist falsch";
		}	
		else
		{
			if ($_SESSION['jjp']['register'][1]['gender'] == "male")
			{
				$look = 'hr-893-39.hd-180-4.ch-210-91.lg-275-73.sh-290-62.he-1601.ca-1815-85,s-0.g-0.d-4.h-4.a-0,1a8f26c4cf87bf3113bc46448795faa2';
				$gender = 'M';
			}
			else
			{
				$look = 'hr-515-45.hd-600-5.ch-685-83.lg-700-81.sh-725-62,s-0.g-0.d-4.h-4.a-0,6719e461db91f99cef3b3c346f23307d';
				$gender = 'F';
			}
				
			$users->add($_SESSION['jjp']['register'][2]['name'], $core->uberHash($_SESSION['jjp']['register'][2]['pass']), $_SESSION['jjp']['register'][2]['email'], 1, $look, $gender);
			
			$_SESSION['SHOW_WELCOME'] = true;
			$_SESSION['UBER_USER_N'] = $_SESSION['jjp']['register'][2]['name'];
			$_SESSION['UBER_USER_H'] = $core->uberHash($_SESSION['jjp']['register'][2]['pass']);
			
			unset($_SESSION['jjp']['register']);
			
			header("Location: " . WWW . "/quickregister/complete");
			exit;
		}
		
		header("Location: " . WWW . "/quickregister/captcha/error/".$errors);
		exit;
		break;		
}

$tpl->Output();

?>