<?php
/*
	Hezecom Email Solution <Adapted From Email Sign up and Login>
	Author: Hezecom Technologies (http://hezecom.com) info@hezecom.net
	COPYRIGHT 2014 ALL RIGHTS RESERVED
	
	You must have purchased a valid license from CodeCanyon.com in order to have 
	access this file.

	You may only use this file according to the respective licensing terms 
	you agreed to when purchasing this item.
	
*/
	session_start();	
	include('../config/config.php');
	include('../language/eng.php');
	include('../libraries/functions.php');
	include_once('../libraries/class_dbcon.php');
	include_once('../libraries/upload_class.php');	
	include("../libraries/xmlapi.php");
    include('../libraries/class_cpanel.php');
	include('url_writer.php');
	$new = new MyCpanel();
	$db=DB::getInstance();
	$dbc=new dboptions();
	
	if($_POST)
	{
	$clocation=post('location');
	$cfname=post('fname');
	$clname=post('lname'); 
	$cusername=post('username');
	$cpassword=post('pwd');
	$cpassword2=post('pwd2');
	$captcha=(post('captcha'));
	$scap=(session('captcha'));
	//birthday
	$month=post('month');
	$day=post('day');
	$year=post('year');
	if($month<10){$nmonth='0'.$month;}else{$nmonth=$month;}
	if($day<10){$nday='0'.$day;}else{$nday=$day;}
	$cbirthday=$nmonth.'/'.$nday.'/'.$year;
	
	$cgender=post('gender');
	$cphone=post('phone');
	$cemail=post('email');
	$clocation=post('location');
	$rdate=date('d/m/Y');
	if(empty($cfname) || empty($clname) || empty($clname) || empty($cusername) || empty($cpassword))
	{die('<div class="alert alert-danger">'.LANG_REG_ALL_FIELDS_REQIRED.'</div>');}
	
	if(empty($clocation)){die('<div class="alert alert-danger">'.LANG_REG_SELECT_COUNTRY.'</div>');}
	if(empty($cgender)){die('<div class="alert alert-danger">'.LANG_REG_SELECT_GENDER.'</div>');}
	if(empty($captcha)){die('<div class="alert alert-danger">'.LANG_REG_ENTER_VERIFICATION.'</div>');}
	if(!(int)$day || !(int)$month || !(int)$year){die('<div class="alert alert-danger">'.LANG_REG_SELECT_BIRTHDAY.'</div>');}
	if(!(int)$cphone){die('<div class="alert alert-danger">'.LANG_REG_ENTER_PHONE.'</div>');}
	
	if(!ValidEmail($cemail))
		die('<div class="alert alert-danger">'.LANG_REG_INVALID_EMAIL.'</div>');
		
	if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $cusername))
		die('<div class="alert alert-danger">'.LANG_REG_INVALID_USERNAME.'</div>');

	if (!preg_match("#.*^(?=.{8,30})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$#", $cpassword))
    	die('<div class="alert alert-danger">'.LANG_INVALID_PASSWORD.'</div>');


	if(!empty($cpassword) and $cpassword!=$cpassword2)
	die('<div class="alert alert-danger">'.LANG_UNMATCHED_PASSWORD.'');	
	
	if(!empty($cemail)){
	$sql = "SELECT COUNT(cemail) AS email from mailusers WHERE cemail =:cemail LIMIT 1";
	$stmt=$db->prepare($sql);
	$stmt->bindParam(':cemail',$cemail);
	$stmt->execute();
	$row=$stmt->fetch(PDO::FETCH_OBJ);
	if($row->email>0){
	die('<div class="alert alert-danger">'.LANG_ALTERNATE_EMAIL_EXIST.'</div>');
	}
	}
	
	if(!empty($cusername)){
	$rea_email=$cusername.'@'.C_URL;
	$sql = "SELECT COUNT(cusername) AS username from mailusers WHERE cusername =:username LIMIT 1";
	$stmt=$db->prepare($sql);
	$stmt->bindParam(':username',$rea_email);
	$stmt->execute();
	$row=$stmt->fetch(PDO::FETCH_OBJ);
	if($row->username>0){
	die('<div class="alert alert-danger">'.LANG_REG_USERNAME_EXIT.'</div>');
	}
	}
	/** Validate captcha */
	if (!empty($captcha)) {
		if (empty($scap) || trim(strtoupper($captcha)) != $scap) {
			die('<div class="alert alert-danger">'.LANG_REG_INVALID_VERIFICATION.'</div>');
	}}
	if (!empty($captcha) and trim(strtoupper($captcha)) == $scap) {
	//create email
	if(post('custom_quota')){
	$email_quota=post('custom_quota');	
	}else{
	$email_quota=DISC_QUOTA;	
	}
	$response=$new->AddEmail($cusername.'@'.C_URL,$cpassword,$email_quota);
	if (strpos($response, 'error')=== false)  
	{ 
	$finalpwd=base64_encode($cpassword);
	$sql = $db->prepare("INSERT INTO `mailusers` (`cfname`, `clname`, `cusername`, `cpassword`, `cbirthday`, `cgender`, `cphone`, `cemail`, `clocation`,`rdate`,`user_status`) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
	$sql->bindValue(1, $cfname);
	$sql->bindValue(2, $clname);
	$sql->bindValue(3, $cusername.'@'.C_URL);
	$sql->bindValue(4, $finalpwd);
	$sql->bindValue(5, $cbirthday);
	$sql->bindValue(6, $cgender);
	$sql->bindValue(7, $cphone);
	$sql->bindValue(8, $cemail);
	$sql->bindValue(9, $clocation);
	$sql->bindValue(10, $rdate);
	$sql->bindValue(11, 1);
	$sql->execute();
	
	unset($scap);
	$_SESSION['cusername']=$cusername;
	if(get('view')){
	echo'<script>window.location.replace("'.H_ADMIN.'&view=mailusers&do=viewall");</script>';
	}else{
	
	echo'<script>window.location.replace("success.php");</script>';
	die('<div class="alert alert-success"><i class="fa fa-check-circle"></i> Process Completed</div>');
	}
	}else{//response
	die('<div class="alert alert-danger">Invalid Server Response</div>');
	}
	}
	}
	
?>
