<?php
if ($_GET['cat'] == ''): 				$rcat.= '/'.SEO; endif;
if (isset($_GET['cat'])): 				$rcat.= '/'.$_GET['cat']; endif;
if (isset($_GET['pag'])): 				$rcat.= '/'.$_GET['pag']; endif;
if (isset($_GET['publicacion_id'])): 	$rcat.= '/'.$_GET['publicacion_id']; endif;
if (isset($_GET['publicacion_seo'])): 	$rcat.= '-'.$_GET['publicacion_seo']; endif;

$tpl->assign("RUTA_LOGIN", SITE_URL.$rcat);


/*======= / LOGIN ========*/

	if ($_GET['logut'] == 'logut') 
	{
		session_unregister($_SESSION['user_email']);
		unset($_SESSION['user_email']);
		session_unregister($_SESSION['user_password']);
		unset($_SESSION['user_password']);

		session_unset();
		session_destroy();
		
	}
	
		if($_POST['user_email'] != '' && $_POST['user_password'] != ''){
						
				 /* *** WHMCS XML API Sample Code *** */
				 
					$url = "http://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['user_email'];
					 $postfields["password2"] = $_POST['user_password'];  
				 
					 $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['user_email'] 	= $results['userid'];
								$_SESSION['user_password'] 	= $results['passwordhash'];
								$_SESSION['mailcustomer'] = $_POST['user_email'];
				 }
				 
		
		}	 


	
		 
if(isset($_SESSION['user_email']) && isset($_SESSION['user_password'])){	

	$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['user_email'];
			 $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 = 'login_min.tpl';
 } 
 else 
 {
	$loadtpl = 'login.tpl';
 } 
 
$tpl->display(PLUGINS_DIR.'/'.$loadtpl);
?>