<?php
 /*=======================================================================*\
 | Copyright (c) 2012, Leo 'Met0do' - http://www.met0do.org               |
 | For more stuffs and data, http://www.met0do.org - leo@met0do.org       |
 | ====================================================================== |
 | 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.                           |
 \*=======================================================================*/
 
    /* requirements */
	require_once '../config.php';
	
	/* receiving form vars */
	$get["t"] = $_POST["csrfmiddlewaretoken"];        /* strange token for sessions I think */
	$get["e"] = ltrim(rtrim($_POST["email"]));        /* email of user */
	$get["b"] = $_POST["birthday"];                   /* date of birthday of the user */
	$get["i"] = $_POST["invited_by"];                 /* bullshit */
	$get["a"] = $_POST["agree"] == "on" ? true:false; /* terms shit */
	$expBirthday = explode("-", $get["b"]);           /* get current year */
	
	########################################################################################################################################
	
	/* check if email field is empty */
	if($get["e"] == "Please enter your e-mail address here"){
		$json["errors"]["email"] = "Enter your 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"] = "Please, enter a valid email"; $leon_farts = true;
		/* check if email is registred in database */
	} elseif(mysql_num_rows(mysql_query('SELECT * FROM sessions WHERE email = "'.$get["e"].'"')) != 0){
		$json["errors"]["email"] = "Email already registred"; $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"] = "This field is required"; $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 sessions (session, figure, email, birthday, gender) VALUES ("'.$get["t"].'", "'.$config["defaultAvatar"].'", "'.$get["e"].'", "'.$get["b"].'", "M")');
		/* if data is stored with success */
		if($storeInData){
			/* no errors - be happy */
			$_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;
	
	########################################################################################################################################
?>