<?php
declare(strict_types=0);
// session_start();
include_once('lib_main.php');
foreach($_GET  as $key => $value){${$key}=trim($value);$_PARAMS[$key]=${$key};} 
foreach($_POST as $key => $value){${$key}=trim($value);$_PARAMS[$key]=${$key};}

if(!empty($empr)){$link_data=msql_connect('','','',$empr); $cfg_empr=$empr;}

$user_codigo=$_SESSION['user_codigo'];


// echo '<br>=====>'.$user_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($user_codigo))
{
     
$user=trim($user);

$link_data=msql_connect('','','',$empr);
     
$sql_updt='update user0 set user_caduca="'.date('Y-12-31').'" where ifnull(user_caduca,"")="" or user_tipo="sist" ';
$res_updt=msql_query($sql_updt,$link_data);     
     
$sql_query ='select * from user0 ';
$sql_query.='where "'.$user.'"!="" ';
$sql_query.='and (user_codigo="'.$user.'" ';
$sql_query.='or trim(user_cedula)="'.$user.'" ';
$sql_query.='or trim(user_wsapp)="'.$user.'"';
$sql_query.='or trim(lower(user_email))="'.strtolower($user).'" ';
$sql_query.='or trim(lower(user_nickname))="'.strtolower($user).'") ';
$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');
$user_token=md5($user_codigo.date('His'));
$user0=data_maes('user',$user_codigo);
$_SESSION['user_codigo']='';
$_SESSION['user_token']='';


// if($user0['user_codigo']!='2'){die();}


if(empty($empr))
     {
          $msgswal->appendChild($dom->createElement("icon","error")); 
          $msgswal->appendChild($dom->createElement("title","Error")); 
          $msgswal->appendChild($dom->createElement("text","Empresa NO EXISTE")); 
     }
elseif(empty($user_codigo))
     {
          $msgswal->appendChild($dom->createElement("icon","error")); 
          $msgswal->appendChild($dom->createElement("title","Error")); 
          $msgswal->appendChild($dom->createElement("text",'Usuario : '.$user.' NO EXISTE')); 
//           $msgswal->appendChild($dom->createElement("text",'Usuario : '.$sql_query.' NO EXISTE')); 
     }
elseif($user0['user_status']=="I")
     {
          $msgswal->appendChild($dom->createElement("icon","error")); 
          $msgswal->appendChild($dom->createElement("title","Error")); 
          $msgswal->appendChild($dom->createElement("text",'Usuario : '.$user.' INACTIVO')); 
     }
elseif($user0['user_clave']!=md5($pass))
     {
          $msgswal->appendChild($dom->createElement("icon","error")); 
          $msgswal->appendChild($dom->createElement("title","Error")); 
          $msgswal->appendChild($dom->createElement("text",'Clave INCORRECTA')); 
     }
elseif($user0['user_caduca']<date('Y-m-d'))
     {
          $msgswal->appendChild($dom->createElement("icon","error")); 
          $msgswal->appendChild($dom->createElement("title","Error")); 
          $msgswal->appendChild($dom->createElement("text",'Usuario CADUCADO')); 
     }
else
     {
//           $msgalert=$response->appendChild($dom->createElement("msgalert",'Acceso OK'));     
          $msgswal->appendChild($dom->createElement("icon","success")); 
          $msgswal->appendChild($dom->createElement("title","Acceso OK")); 
          $msgswal->appendChild($dom->createElement("text",'Bienvenid@ '.$user0['user_descri'])); 
          $msgswal->appendChild($dom->createElement("timer",'1000')); 
          if(mb_stristr($locahref,'user_login') or mb_stristr($locahref,'logout'))
          {
               $msgswal->appendChild($dom->createElement("isConfirmed",'{clrscr();window.location.href="?"}'));
               $msgswal->appendChild($dom->createElement("dismiss",'{clrscr();window.location.href="?"}'));
          }
          else
          {
               $msgswal->appendChild($dom->createElement("isConfirmed",'{location.reload();}'));
               $msgswal->appendChild($dom->createElement("dismiss",'{location.reload();}'));
          }
          
          $result=$response->appendChild($dom->createElement("result",""));
               $result->appendChild($dom->createElement("user_token",$user_token));
     
//           if(!empty($user_codigo))
//          {setcookie('user_codigo',$user_codigo,time()+(60*60*24*365));}
     
          $_SESSION['user_login1']=$user;
          $user0=data_maes('user',$user_codigo);
          foreach($user0 as $key=>$value){$_SESSION[$key]=$value;}
          $empr0=data_maes('empr',$empr);
          foreach($empr0 as $key=>$value){$_SESSION[$key]=$value;}
          $_SESSION['user_token']=$user_token;
     
          
     
          $sql_query ='update user0 set user_login=now() ';
          $sql_query.='where user_codigo="'.$user_codigo.'" ';
          $sql_query.='limit 1';
          $res_query=msql_query($sql_query,$link_data);
        
          $sql_query ='';
     
     
     
     }
}
else
{
          $msgswal->appendChild($dom->createElement("icon","warning")); 
          $msgswal->appendChild($dom->createElement("title","Salir")); 
          $msgswal->appendChild($dom->createElement("showCancel","Salir")); 
          $msgswal->appendChild($dom->createElement("confirmButtonText","Cerrar Sesión")); 
          $msgswal->appendChild($dom->createElement("isConfirmed",'window.location.href="?logout"')); 

}

echo $dom->saveXML();

// mkdir('../info',0755,true);

// updt_xpres();
?>