<?php

function CheckSesion($session, $nombre_var)
{      
    
    if (isset ($session[$nombre_var]) == "") {
        return "0";
    } else {
        return "1";
    }

}

function VariablesSession($session, $nombres_variables)
{

    $arra_var = [];

    for ($i = 0; $i <= count($nombres_variables); $i++) {
        array_push($arra_var, $session[$nombres_variables[$i]]);
    }

    return $arra_var;
}

function VarBoolIntToTexto($var_boo, $texto_true, $texto_false)
{
    if ($var_boo == "1") {
        return $texto_true;
    } else {
        return $texto_false;
    }
}

function JavaMensajeToastr($mensaje)
{
    echo '<script type="text/javascript">toastr.error("' . $mensaje . '");</script>';
}

function CheckEmail($email)
{
    $email_filt = filter_var($email, FILTER_SANITIZE_EMAIL);
    $email_filt_2 = "";
    if (filter_var($email_filt, FILTER_VALIDATE_EMAIL)) {
        $email_filt_2 = ($email);
    }
    if ($email_filt_2 == "") {
        return false;
    } else {
        return true;
    }

}

function ExisteEmailUsuarioBD($email, $pass, $con, $sesionDesde)
{
    $sqlVerificandoLogin = ("SELECT IdUser, nameUser, emailUser, passwordUser , UserAdmin FROM myusers WHERE emailUser COLLATE utf8_bin='$email'");
    $resultLogin = mysqli_query($con, $sqlVerificandoLogin) or die (mysqli_error($con));
    $numLogin = mysqli_num_rows($resultLogin);

    if ($numLogin == 1) {

        while ($rowData = mysqli_fetch_assoc($resultLogin)) {

            $passwordBD = $rowData['passwordUser'];
            if (password_verify($pass, $passwordBD)) {
                $user['ok'] = "1";
                $user['err'] = "0";
                $user['IdUser'] = $rowData['IdUser'];
                $user['nameUser'] = $rowData['nameUser'];
                $user['emailUser'] = $rowData['emailUser'];
                $user['UserAdmin'] = $rowData['UserAdmin'];

                return $user;
            }
        }
        $user['ok'] = "0";
        $user['err'] = "1"; // pass incorrecta
        return $user;
    } else {
        $user['ok'] = "0";
        $user['err'] = "2"; // usuario incorrecto
        return $user;
    }
}