<?php
if ((isset($_GET["d"]) && !empty($_GET["d"]) && is_numeric($_GET["d"])) && (isset($_GET["e"]) && !empty($_GET["e"]) && preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $_GET["e"])) && (isset($_GET["u"]) && !empty($_GET["u"]) && is_numeric($_GET["u"]) && ($_GET["u"]==1 ||$_GET["u"]==8)))
{	
if (file_exists("functions/site_functions.php"))
	{
	include_once("functions/site_functions.php");
	}
else
	{
include_once("../../functions/site_functions.php");
	}

$d=$_GET["d"];
$e=$_GET["e"];
$u=$_GET["u"];
if ($u==8)
	{
	$u=0;
	}
db_open();
$sql="SELECT * FROM users WHERE email='$e' OR device_id='$d'";
$result=mysql_numrows(mysql_query($sql));
if ($result==0)
	{
	$private_id=rand(1999,99999);
	$password=rand(1,9999999999);
	$check=device_password($d);
	$a=password($option=1,$u,$e,$password,$d,$private_id);
	if ($a==1 && $check==1)
		{
		$txt="<html>Thank you for using our product. SmartGreenhouse welcomes you to our community.Please verify your account by visiting the follow link:
		<a href='index.php?q=$private_id'>Press Here</a><br />After, you will be able to use your device properly by logging in with your email and the random auto-generated password:$password.</html>";
		$headers = "from:smartgreenhouse \r\n";
		$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
		$to="$e";
		$subject="SmartGreenhouse - Customer Registration!";
		date_default_timezone_set("Europe/Athens");
		$a=mail($e,$subject,$txt,$headers);
		if ($a==1)
			{
			echo"User Added!";
			}
		}
	else
		{
		echo" Database Problem!";
		}
	}
db_close();
}
?>