<?php

include("ApiGlobalFunctions.php"); 

$usuario=$_POST['user'];
$password=$_POST['pass'];
$url=$_POST['url'];
    error_log("LOGINAPI: PARAMS -> $usuario, $password, $url 0"."\n", 3, "php.log");
$include=includec($url);
//echo $include;
include ($include);
$query=mysqli_query($link,"SELECT *
FROM vtiger_users 
INNER JOIN vtiger_user2role ON vtiger_user2role.userid=vtiger_users.id
INNER JOIN vtiger_role2profile ON vtiger_role2profile.roleid=vtiger_user2role.roleid
INNER JOIN vtiger_profile2tab ON vtiger_profile2tab.profileid=vtiger_role2profile.profileid
INNER JOIN vtiger_tab ON vtiger_tab.tabid=vtiger_profile2tab.tabid
WHERE vtiger_tab.name='Bodegas' AND user_name='$usuario'");
$num=mysqli_num_rows($query);
if($num==1){
  $password=md5($password);
	$data= mysqli_fetch_array($query);
    $pass=$data['user_hash'];
//    echo $pass;
  error_log("LOGINAPI: PARAMS ".$password."--".$pass."\n", 3, "php.log");
    if ($password == $pass) {
        $iduser=$data['id'];        
        $key_user=$data['accesskey'];
        $permissions=$data['permissions'];        
        if($permissions==1){
            $userarray[0]=array('id_user'=>$iduser,'user_key'=>"Not permissions",'opf_success'=>'2','msg'=>"Not permissions");
            echo json_encode(setDataJSON($userarray,1));
        }else{
            $userarray[0]=array('id_user'=>$iduser,'user_key'=>$key_user,'opf_success'=>'1');
            echo json_encode(setDataJSON($userarray,1));
        }
    }else{
    $userarray[0]=array('id_user'=>$usuario,'user_key'=>'Password Error','opf_success'=>'0','msg'=>'Password Error');
        echo json_encode(setDataJSON($userarray,1));
    }
}else{
     $userarray[0]=array('id_user'=>'Username not found','user_key'=>$password,'opf_success'=>'0','msg'=>'Username not found');
        echo json_encode(setDataJSON($userarray,1));
}

