<?php
    session_start();

    function isLoggedIn(){
    if(isset($_SESSION['razon'])){
    return true;
    }else{
    return false;
    }
    }

function control_acceso_nivel($nivel0){
$niveles=strtoupper(encrypt_decrypt('decrypt',$_SESSION["tmpidentity0"]));

    if (in_array($niveles, $nivel0)) {
    return true;
    }else{
    return false;
    }
exit;
}

function calculaedad($fechanacimiento){
  list($ano,$mes,$dia) = explode("-",$fechanacimiento);
  $ano_diferencia  = date("Y") - $ano;
  $mes_diferencia = date("m") - $mes;
  $dia_diferencia   = date("d") - $dia;
  if ($dia_diferencia < 0 || $mes_diferencia < 0)
    $ano_diferencia--;
  return $ano_diferencia;
}

    function isErrorIn(){
            if(!isset($_SESSION['error_msg'])){
            $error_msj="";
            }else{
            $error_msj=$_SESSION['error_msg'];
            }
            unset($_SESSION['error_msg']);
            return $error_msj;
    }

    function ver_may($cadena){
    //$subject = 'Hola Mundo'; //cadena completa
    //$search = 'Mundo'; //cadena de búsqueda
    //$replace = 'Jairo'; //cadena de reemplazo

    //strtr('Jairo', 'i', '1'); //devuelve Ja1ro

    $cadena= strtr($cadena,"á","Á");
    $cadena= strtr($cadena,"é","É");
    $cadena= strtr($cadena,"í","Í");
    $cadena= strtr($cadena,"ñ","Ñ");
    ECHO $cadena;
    exit;
    return $cadena;
    }

     function encrypt_decrypt($action, $string)
 {
     /* =================================================
      * ENCRYPTION-DECRYPTION
      * =================================================
      * ENCRYPTION: encrypt_decrypt('encrypt', $string);
      * DECRYPTION: encrypt_decrypt('decrypt', $string) ;
      */
     $output = false;
     $encrypt_method = "AES-256-CBC";
//     $secret_key = 'WS-SERVICE-KEY';
//     $secret_iv = 'WS-SERVICE-VALUE';
     $secret_key = '00158221brtrY/FfddSdwedeweeWS-SERVICE-KEY';
     $secret_iv = '00158221brtrY/FfddSdwedeweeSERVICE-VALUE';
     // hash
     $key = hash('sha256', $secret_key);
     // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
     $iv = substr(hash('sha256', $secret_iv), 0, 16);
     if ($action == 'encrypt') {
         $output = base64_encode(openssl_encrypt($string, $encrypt_method, $key, 0, $iv));
     } else {
         if ($action == 'decrypt') {
             $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
         }
     }
     return $output;
 }
