<?php
session_start();
include_once('lib_main.php');

foreach($_GET as $campo => $valor){${$campo}=trim($valor);} 
foreach($_POST as $campo => $valor){${$campo}=trim($valor);} 

$user=strtolower($user);
     
if(val_email($user)){$user_email=$user;}
if(val_telefono($user)){$user_celular=$user;}
// if(val_telefono($wsapp)){$wsapp;}
if(ruc_tipo($cedula)=='N'){$user_cedula=$cedula;}

{$user_ok=valid_user($user,$user_err);}
{$pass_ok=valid_pass($pass,$pass_err);}

// include_once('recaptcha_v3.php');
// if($recaptcha==0){$pass_ok=0;}

$ipip=$_SERVER['REMOTE_ADDR'];


// if($user_ok)
{
     
$link_data=msql_connect('','','',$empr);
$sql_query ='select * from user0 ';
$sql_query.='where "'.$user.'"!="" ';
$sql_query.='and (trim(user_email)="'.$user.'" ';
$sql_query.='or trim(user_celular)="'.$user.'" ';
// $sql_query.='or trim(user_wsapp)="'.$user.'" ';
// $sql_query.='or trim(user_alias)="'.$user.'" ';
$sql_query.=') ';
$sql_query.='order by user_codigo ';
$sql_query.='limit 1';

$res_query=msql_query($sql_query,$link_data);
$user_codigo=msql_result($res_query,0,'user_codigo');
$user0=data_maes('user',$user_codigo);

$sql_query=str_replace('user','usrt',$sql_query);
     
$res_query=msql_query($sql_query,$link_data);
$usrt_codigo=msql_result($res_query,0,'usrt_codigo');
$usrt0=data_maes('usrt',$usrt_codigo);

}




////////////////////////////////////////////////
////////////////////////////////////////////////

$dom = new DOMDocument();
     $dom->encoding = 'utf-8';
     $dom->xmlVersion = '1.0';
     $dom->formatOutput = true;

$response=$dom->appendChild($dom->createElement("response"));
// $msgsys=$response->appendChild($dom->createElement("msgsys",$sql_query)); 
$msgswal=$response->appendChild($dom->createElement("msgswal")); 
     $msgswal->setAttribute("id","msgswal");



if(empty($empr))
     {
//           $msgalert=$response->appendChild($dom->createElement("msgalert",'Empresa NO EXISTE')); 
          $msgswal->appendChild($dom->createElement("icon","error")); 
          $msgswal->appendChild($dom->createElement("title","Error")); 
          $msgswal->appendChild($dom->createElement("text","Empresa NO EXISTE")); 
     }
elseif(!empty($user_codigo))
     {    
//           $msgalert=$response->appendChild($dom->createElement("msgalert",'Usuario : '.$user.' YA EXISTE')); 
          $msgswal->appendChild($dom->createElement("icon","error")); 
          $msgswal->appendChild($dom->createElement("title","Error")); 
          $msgswal->appendChild($dom->createElement("text",'Usuario '.$user.' YA EXISTE')); 
     }
elseif(!empty($usrt_codigo))
     {
//           $msgalert=$response->appendChild($dom->createElement("msgalert",'Usuario : '.$user.' YA REGISTRADO')); 
          $msgswal->appendChild($dom->createElement("icon","warning")); 
          $msgswal->appendChild($dom->createElement("title","Advertencia")); 
          $msgswal->appendChild($dom->createElement("text",'Usuario '.$user.' YA REGISTRADO')); 
          send_pin($usrt_codigo);
     
     }
elseif(!$user_ok)
     {
          $msgswal->appendChild($dom->createElement("icon","error")); 
          $msgswal->appendChild($dom->createElement("title","Error")); 
          $msgswal->appendChild($dom->createElement("text",$user_err)); 
     }
elseif(!$pass_ok)
     {
          $msgswal->appendChild($dom->createElement("icon","error")); 
          $msgswal->appendChild($dom->createElement("title","Error")); 
          $msgswal->appendChild($dom->createElement("text",$pass_err)); 
     }
else
     {
          $response->appendChild($dom->createElement("user_token",$user_token));
          $msgswal->appendChild($dom->createElement("icon","success")); 
          $msgswal->appendChild($dom->createElement("title","Registro OK")); 
          $msgswal->appendChild($dom->createElement("text",'Se ha enviado un PIN de confirmación a : '.$user)); 
     
          $msgswal->appendChild($dom->createElement("isConfirmed","location.href='?scri=usrt_confirm0&email=".$user."';")); 

		$sql_regis='insert into usrt0(usrt_idate,usrt_status)value(now(),"-")';
		$res_regis=msql_query($sql_regis,$link_data);
          $usrt_codigo=msql_insert_id($link_data);
     
//           $pin=rand(100000,999999);
     
//           $referx=$_COOKIE[$cfg_empr.'_referx'];
          $nickname=substr($user_email,0,strpos($user_email,'@'));
          if(empty($nickname)){$nickname=$user_celular;}
          if(empty($nombre)){$nombre=$nickname;}


     
		$sql_regis ='update usrt0 set ';
		$sql_regis.='usrt_descri="'.$nombre.' '.$apellido.'",';
		$sql_regis.='usrt_cedula="'.$user_cedula.'",';
		$sql_regis.='usrt_email="'.$user_email.'",';
		$sql_regis.='usrt_cedula="'.$user_cedula.'",';
		$sql_regis.='usrt_wsapp="'.$user_celular.'",';
		$sql_regis.='usrt_celular="'.$user_celular.'",';
		$sql_regis.='usrt_nickname="'.$nickname.'",';
// 		$sql_regis.='usrt_alias="'.$alias.'",';
		$sql_regis.='usrt_referx="'.$referx.'",';
// 		$sql_regis.='usrt_pin="'.$pin.'",';
		$sql_regis.='usrt_clave=md5("'.$pass.'"),';
		$sql_regis.='usrt_tipo="invi",';
		$sql_regis.='usrt_idate=now(),';
		$sql_regis.='usrt_ipip="'.$ipip.'",';
		$sql_regis.='usrt_status=usrt_status ';
		$sql_regis.='where usrt_codigo="'.$usrt_codigo.'"';
		$res_regis=msql_query($sql_regis,$link_data);
          
          send_pin($usrt_codigo);
     
     }

echo $dom->saveXML();

?>