<?php

	if ($_GET['logut'] == 'logut') 
	{
		session_unregister($_SESSION['uid']);
		unset($_SESSION['uid']);
		session_unregister($_SESSION['upw']);
		unset($_SESSION['upw']);

		session_unset();
		session_destroy();
		
	}
	

		if($_POST['user2'] != '' && $_POST['user2'] != ''){
			require $_SERVER['DOCUMENT_ROOT'].'/engine/engine.php';
		
				 /* *** WHMCS XML API Sample Code *** */
				 
				 $url = "https://www.afterfuture.net/clientes/includes/api.php"; # URL to WHMCS API file goes here
					$username = "zardet"; # Admin username goes here
					$password = "ssbtp=1174"; # Admin password goes here
				 
					 $postfields["username"] = $username;
					 $postfields["password"] = md5($password);
					 $postfields["action"] = "validatelogin";
					 $postfields["email"] = $_POST['user2'];
					 $postfields["password2"] = $_POST['pass2'];  
				 
				 $ch = curl_init();
				 curl_setopt($ch, CURLOPT_URL, $url);
				 curl_setopt($ch, CURLOPT_POST, 1);
				 curl_setopt($ch, CURLOPT_TIMEOUT, 100);
				 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				 curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
				 $data = curl_exec($ch);
				 curl_close($ch);
				  
				 
				 $data = explode(";",$data);
				 foreach ($data AS $temp) {
				   $temp = explode("=",$temp);
				   $results[$temp[0]] = $temp[1];
				 }
				 
				 if ($results["result"]=="success") {
								$_SESSION['uid'] 	= $results['userid'];
								$_SESSION['upw'] 	= $results['passwordhash'];
								$_SESSION['mailcustomer'] = $_POST['user2'];
								
				 }
				 
		
		}	 


	
		 
if(isset($_SESSION['uid']) && isset($_SESSION['upw'])){	

	$whmcsurl = "https://www.afterfuture.net/clientes/dologin.php";
	$autoauthkey = "dxO=}HTTu[VT";
	
	$timestamp = time(); # Get current timestamp
	$email = $_SESSION['mailcustomer']; # Clients Email Address to Login
	$goto = "clientarea.php";
	
	$hash = sha1($email.$timestamp.$autoauthkey); # Generate Hash
	
	$urlcliente = $whmcsurl."?email=$email&timestamp=$timestamp&hash=$hash&goto=".urlencode($goto);

			$url = "https://www.afterfuture.net/clientes/includes/api.php"; # URL to WHMCS API file goes here
			$username = "zardet"; # Admin username goes here
			$password = "ssbtp=1174"; # Admin password goes here
		 
			 $postfields["username"] = $username;
			 $postfields["password"] = md5($password);
			 $postfields["action"] = "getclientsdetails";
			 $postfields["clientid"] = $_SESSION['uid'];
			 $postfields["stats"] = false;
 		 
		 $ch = curl_init();
		 curl_setopt($ch, CURLOPT_URL, $url);
		 curl_setopt($ch, CURLOPT_POST, 1);
		 curl_setopt($ch, CURLOPT_TIMEOUT, 100);
		 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		 curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
		 $data = curl_exec($ch);
		 curl_close($ch);
		  
		 
		 $data = explode(";",$data);
		 foreach ($data AS $temp) {
		   $temp = explode("=",$temp);
		   $results[$temp[0]] = $temp[1];
		 }
		
		
		$linkclientarea			= $urlcliente;
		$tpl->assign("LINKCLIENTAREA", $linkclientarea);
	
		$clientenombre			= $results['firstname'];
		$tpl->assign("CLIENTENOMBRE", $clientenombre);
	
	$loadtpl = 'loginwhmin.tpl';
 } 
 else 
 {
	$loadtpl = 'loginwhm.tpl';
 } 
 
$tpl->display(PLUGINS_DIR.'/'.$loadtpl);
?>