<?php

include("ApiGlobalFunctions.php"); 

$usuario=$_POST['user'];
$password=$_POST['pass'];
$url=$_POST['url'];
$keyFB=$_POST['Key_appfb'];

$include=includec($url);
include ($include);
$token_field=diccionario($url,'TokenFireBase');
$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){
    $data= mysqli_fetch_array($query);
    $pass=$data['user_hash'];
    $password=md5($password);
    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');
            echo json_encode(setDataJSON($userarray,1));
        }else{
			mysqli_query($link,"UPDATE vtiger_usuarioscf 
            INNER JOIN vtiger_usuarios ON vtiger_usuarios.usuariosid=vtiger_usuarioscf.usuariosid 
            SET $token_field='$keyFB' WHERE usuarios_tks_usuario='$usuario'");          $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');
        echo json_encode(setDataJSON($userarray,1));
    }
}else{
     $userarray[0]=array('id_user'=>'Username not found','user_key'=>$password,'opf_success'=>'0');
        echo json_encode(setDataJSON($userarray,1));
}

