<?php
/*=====================================================================+
|| # HaddoCMS based HoloCMS - Website and Content Management System
|+=====================================================================+
|| # Copyright © 2008 Meth0d. All rights reserved.
|| # http://www.meth0d.org
|+===================================================+
|| # HoloCMS is provided "as is" and comes without
|| # warrenty of any kind. HoloCMS is free software!
|+=====================================================================+
|| # HaddoCMS v2.0 by Raikas77
|| # www.sciax2.it/forum/development/haddocms-v2-build-2100-a-535753.html
|+=====================================================================*/

	
	require_once '../core.php';
	
	
	/* receiving form vars */
	$get["t"] = FilterText($_POST["csrfmiddlewaretoken"]);        /* strange token for sessions I think */
	$get["e"] = FilterText(ltrim(rtrim($_POST["email"])));        /* email of user */
	$get["b"] = FilterText($_POST["birthday"]);                   /* date of birthday of the user */
	$get["i"] = FilterText($_POST["invited_by"]);                 /* bullshit */
	$get["a"] = $_POST["agree"] == "on" ? true:false; /* terms shit */
	$expBirthday = explode("-", $get["b"]);           /* get current year */
	     /* strange token for sessions I think */
	$email = FilterText(ltrim(rtrim($_POST["email"])));  
	$secret = FilterText($_POST["secret"]);
	$r = FilterText($_POST["referente"]);
	$get["u"] = FilterText($_POST["usernamex"]);
	$passwordxd = FilterText($_POST["passwordx"]);
   $get["p"] = HoloHash($passwordxd);
	########################################################################################################################################
	
	/* check if email field is empty */
	if($get["e"] == "Scrivi qui la tua E-mail"){
		$json["errors"]["email"] = "Inserisci la tua email"; $leon_farts = true; /* yes, he really does */
		/* check if email is valid */
	} elseif(!(preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $get["e"]))){
		$json["errors"]["email"] = "Inserisci un email valida"; $leon_farts = true;
		/* check if email is registred in database */
	} elseif(mysql_num_rows(mysql_query('SELECT * FROM users WHERE mail = "'.$get["e"].'"')) != 0){
		$json["errors"]["email"] = "Email gia' registrata"; $leon_farts = true;
	}
	
	
	/* check if password field is empty */
	if($passwordxd == ""){
		$json["errors"]["passwordx"] = "Inserisci la tua password"; $leon_farts = true; /* yes, he really does */
	}
	
	/* check if secret code field is empty */
	if($secret == ""){
		$json["errors"]["secret"] = "Inserisci il tuo Secret Code"; $leon_farts = true; /* yes, he really does */
	}
	
	/* check if user field is empty */
	if($get["u"] == "Scrivi qui il tuo Username"){
		$json["errors"]["usernamex"] = "Inserisci il tuo Username"; $leon_farts = true; /* yes, he really does */
		/* check if usernamex is valid */
	} elseif(!(preg_match("/^[0-9a-zA-Z\xE0-\xFF ]+$/i", $get["u"]))){
		$json["errors"]["usernamex"] = "Non usare simboli."; $leon_farts = true;
		/* check if usernamex is registred in database */
	} elseif(mysql_num_rows(mysql_query('SELECT * FROM users WHERE username = "'.$get["u"].'"')) != 0){
		$json["errors"]["usernamex"] = "Username gia' registrato"; $leon_farts = true;
	} 
	
	
	/* check if referente field is empty */
	if($r == "porcaccio dio. :|"){
		$json["errors"]["referente"] = "Inserisci referente"; $leon_farts = true; /* yes, he really does */
		/* check if referente is valid */
	} elseif($r == "Scrivi qui il tuo Referente"){
		$json["errors"]["referente"] = "Inserisci referente"; $leon_farts = true;
	}  elseif(mysql_num_rows(mysql_query('SELECT * FROM users_referenti WHERE ip_referente = "'.$_SERVER['REMOTE_ADDR'].'"')) != 0){
		$json["errors"]["referente"] = "Non fare il furbo"; $leon_farts = true;
	} elseif($r == $get["u"]){
		$json["errors"]["referente"] = "Non fare il furbo"; $leon_farts = true;
	}

	
	/* check if birthday is empty */
	if($get["b"] == "Year-Month-Day"){
		$json["errors"]["birthday"] = "Enter your date of birth"; $leon_farts = true;
		/* check if contains one of the initial words */
	}elseif(checkIfContains("Year", $get["b"]) || checkIfContains("Month", $get["b"]) || checkIfContains("Day", $get["b"])){
		$json["errors"]["birthday"] = "There are empty fields"; $leon_farts = true;
		/* check if you´re under 13 years old */
	} elseif($expBirthday[0] >= 1999){
		$json["errors"]["birthday"] = "[You must be 13 years old to sign up]";
		$update_data = true; /* block register */ $leon_farts = true;
	}
	
	/* check terms */
	if(!$get["a"]){ $json["errors"]["agree"] = "ACCETTA I TERMINI"; $leon_farts = true; }	
	
	########################################################################################################################################
	
	/* creating message to JSON */
	header('Content-type: application/json');
	
	/* check errors */
	if($leon_farts)  { $json["status"] = "NOK"; } else {
		/* now saving in database */
		$storeInData = mysql_query("INSERT INTO users (secret, vip_points, real_name, last_online, account_created, ip_last, ip_reg, datosreg, look, mail, gender, username, credits, activity_points, motto, password) VALUES ('". $secret ."', '15', '".FilterText($real_name)."', '".time()."', '". date('d-M-Y H:i:s') ."', '". $_SERVER['REMOTE_ADDR'] ."', '". $_SERVER['REMOTE_ADDR'] ."', '0', '".$config['defaultAvatar']."', '".$get['e']."', 'M', '". $get["u"] ."', '10000', '10000', 'Benvenuto in Hotel', '". $get["p"] ."')");
		$user_id = mysql_insert_id();
    mysql_query("INSERT INTO user_stats (id, RoomVisits, OnlineTime, Respect, RespectGiven, GiftsGiven, GiftsReceived, DailyRespectPoints, DailyPetRespectPoints) VALUES ('".$user_id."', 0, 0, 0, 0, 0, 0, 3, 3)");
    mysql_query("INSERT INTO user_info (user_id, bans, cautions, reg_timestamp, login_timestamp, cfhs, cfhs_abusive) VALUES ('".$user_id."', '0', '0', UNIX_TIMESTAMP(), '0', '0', '0')");
	mysql_query("INSERT INTO users_referenti (username, ip_referente, data) VALUES ('".$r."','".$_SERVER['REMOTE_ADDR']."','".time()."')");
		/* if data is stored with success */
		if($storeInData){
			/* no errors - be happy */
		
		

			$_SESSION['UBER_USER_N'] = $get["u"];
			$_SESSION['UBER_USER_H'] = $get["p"];
			$_SESSION['UBER_USER_EMAIL'] = $get["e"];
		$_SESSION['username'] = $get["u"];
				$_SESSION['password'] = $get["p"];
		
			$_SESSION["AS"]  = $get["t"];                /* store token of the user in a session */
			$_SESSION["IMG"] = $config["defaultAvatar"]; /* store initial avatar figure in session */
			$_SESSION["AG"]  = "M";                      /* store initial avatar gender in session */
			$json["status"]  = "OK"; $json["errors"] = null; /* :p */
		}
	}
	
	if($update_data) { $json["data"] = "E_UNDERAGE"; } else { $json["data"] = null; } echo json_encode($json); exit;
	
	########################################################################################################################################
?>