<?php

use Illuminate\Support\Facades\DB;

if (!function_exists('hackKey')) {// To be deleted
    function hackKey($encrypt = true, $string = "")
    {
        $output = false;
        $encrypt_method = "AES-256-CBC";
        $secret_key = '6xhsYisB2urVyntVE9st';
        $secret_iv = '6xhsYisB2urVyntVE9st';

        // hash
        $key = hash('sha256', $secret_key);
        // iv - encrypt method AES-256-CBC expects 16 bytes
        $iv = substr(hash('sha256', $secret_iv), 0, 16);
        if ($encrypt) {
            $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
            $output = base64_encode($output);
        } else if (!$encrypt) {
            $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
        }
        return $output;
    }
}

if (!function_exists('takeBD')) {
    function takeSiteBd($bd)
    {
        return explode("site_", $bd)[1];
    }
}


if(!function_exists('main_secure_me')){
    function main_secure_me($chaine, $unsecured){
        $key_password = "hhfs&GS(*43rsdthfgsresgsgsgreas45)@@GGS";
        // CRYPTER
        if ($unsecured) { // if true crypt
            $secure_string = openssl_encrypt($chaine, "AES-256-ECB" ,$key_password);
        } else {        // else decrypt
            $secure_string = openssl_decrypt($chaine, "AES-256-ECB" , $key_password);
        }
        return  $secure_string;
    }
}


if(!function_exists('secure_me')){
    function secure_me($chaine, $unsecured = true){
        $output = false;
        $encrypt_method = "AES-256-CBC";
        $secret_key = 'hhfs&GSszfcdvhdhwreas452dtf@GGS';
        $secret_iv = 'hhfs&GSszfcdvhdhwreas452dtf@GGS';

        // hash
        $key = hash('sha256', $secret_key);
        // iv - encrypt method AES-256-CBC expects 16 bytes
        $iv = substr(hash('sha256', $secret_iv), 0, 16);
        // CRYPTER
        if ($unsecured) { // if true crypt
            $output = openssl_encrypt($chaine, $encrypt_method, $key, 0, $iv);
            $output = base64_encode($output);
        } else {        // else decrypt
            $output = openssl_decrypt(base64_decode($chaine), $encrypt_method, $key, 0, $iv);
        }
        return  $output;
    }
}


if(!function_exists('client_secure_me')){
    function client_secure_me($chaine, $unsecured = true){
        $output = false;
        $encrypt_method = "AES-256-CBC";
        $secret_key = 'hhfsyhrtgfegshwreas452dtf@GGS';
        $secret_iv = 'hhfsyhrtgfegshwreas452dtf@GGS';

        // hash
        $key = hash('sha256', $secret_key);
        // iv - encrypt method AES-256-CBC expects 16 bytes
        $iv = substr(hash('sha256', $secret_iv), 0, 16);
        // CRYPTER
        if ($unsecured) { // if true crypt
            $output = openssl_encrypt($chaine, $encrypt_method, $key, 0, $iv);
            $output = base64_encode($output);
        } else {        // else decrypt
            $output = openssl_decrypt(base64_decode($chaine), $encrypt_method, $key, 0, $iv);
        }
        return  $output;
    }
}

if(!function_exists('account_secure_me')){
    function account_secure_me($chaine, $unsecured = true){
        $output = false;
        $encrypt_method = "AES-256-CBC";
        $secret_key = 'hhfbszfcdvadwreas452hdhwreas452dtdfd';
        $secret_iv = 'hhfbszfcdvadwreas452hdhwreas452dtdfd';

        // hash
        $key = hash('sha256', $secret_key);
        // iv - encrypt method AES-256-CBC expects 16 bytes
        $iv = substr(hash('sha256', $secret_iv), 0, 16);
        // CRYPTER
        if ($unsecured) { // if true crypt
            $output = openssl_encrypt($chaine, $encrypt_method, $key, 0, $iv);
            $output = base64_encode($output);
        } else {        // else decrypt
            $output = openssl_decrypt(base64_decode($chaine), $encrypt_method, $key, 0, $iv);
        }
        return  $output;
    }
}

if(!function_exists('getFullPPPath')){
    function getFullPPPath($path, $type_user, $meta = [], $storage = false){
        
        if ($path) {
            if ($storage) {
                $gestStudent = "https://progiciel.geststudent.net/";

                switch ($type_user) {
                    case '0': // étudiant
                        $dir = "Inscription/";
                        break;
                    case '4': // professeur
                        // $path = "enseignantImages/".$meta['etablissement_id']."/".$path;
                        $dir = "Admin/";
                        break;
                    default:
                        $dir = "";
                        break;
                }
    
                $path = $gestStudent.$dir.$path;
            } else {

                $baseUrl = config('app.aws_url');
    
                $path = $baseUrl.$path;
            }

        }

        return  $path;
    }
}

if(!function_exists('getImgPath')){
    /**
     * @param string $type - type of image we want
     * @param mixed $meta - any aditionnal data to get the path
     */
    function getImgPath($type, $meta = null){
        $gestStudent = "https://progiciel.geststudent.net/";

        if ($type == "PROFIL") {
            // here $meta will be type_user
            switch ($meta) {
                case '0': // étudiant
                    $dir = "Inscription/";
                    break;
                case '4': // professeur
                    // $path = "enseignantImages/".$meta['etablissement_id']."/".$path;
                    $dir = "Admin/";
                    break;
                default:
                    $dir = "";
                    break;
            }
        } else {
            $dir = "";
        }

        return $gestStudent.$dir;
    }
}

if(!function_exists('current_acad')){
    function current_acad(){
        $acad = DB::connection('tenant')->table('periodeinscription')
        ->where('affiche', 'YES')
        ->select('annee_acad')
        ->first();

        return $acad->annee_acad;
        // return $acad ? $acad->annee_acad : -1;
    }
}

if(!function_exists('get_ip')){
    function get_ip() {
        if($_SERVER) {
            if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            }elseif(isset($_SERVER['HTTP_CLIENT_IP'])) {
                $ip = $_SERVER['HTTP_CLIENT_IP'];
            }else {
                $ip = $_SERVER['REMOTE_ADDR'];
            }
        }else {
            if(getenv('HTTP_X_FORWARDED_FOR')) {
                $ip = getenv('HTTP_X_FORWARDED_FOR');
            }elseif(getenv('HTTP_CLIENT_IP')) {
                $ip = getenv('HTTP_CLIENT_IP');
            }else {
                $ip = getenv('REMOTE_ADDR');
            }
        }
        return $ip;
    }
}

if(!function_exists('getJour')){
    function getJour($day) {
        $jour["Monday"] = "Lundi";
        $jour["Tuesday"] = "Mardi";
        $jour["Wednesday"] = "Mercredi";
        $jour["Thursday"] = "Jeudi";
        $jour["Friday"] = "Vendredi";
        $jour["Saturday"] = "Samedi";
        $jour["Sunday"] = "Dimanche";
        return $jour[$day];
    }
}


