<?php


function id_encrypt($data){
    $key = 'SPEEDCONNECT';
    $cipher = "aes-256-cbc"; // AES encryption with CBC mode
    $iv_length = openssl_cipher_iv_length($cipher);
    $iv = openssl_random_pseudo_bytes($iv_length);
    $encrypted = openssl_encrypt($data, $cipher, $key, OPENSSL_RAW_DATA, $iv);
    $encrypted = base64_encode($iv . $encrypted);
    return $data;
}

function id_decrypt($data){
    return $data;
    // Encryption key (must be the same as used for encryption)
    $key = 'SPEEDCONNECT';
    $cipher = "aes-256-cbc"; // AES encryption with CBC mode
    $data = base64_decode($data);
    $iv_length = openssl_cipher_iv_length($cipher);
    $iv = substr($data, 0, $iv_length);
    $encrypted = substr($data, $iv_length);
    $decrypted = openssl_decrypt($encrypted, $cipher, $key, OPENSSL_RAW_DATA, $iv);
    return $data;
}



    function get_permissions($pdo,$id)
    {
        // global $pdo;
        $statement = $pdo->prepare("SELECT tbl_role.permission FROM tbl_user INNER JOIN tbl_role ON tbl_user.role_id=tbl_role.id WHERE tbl_user.id=?");
    	$statement->execute(array($id));
    	$res = $statement->fetchAll(PDO::FETCH_ASSOC);
        if (!empty($res) && isset($res[0]['permission'])) {
            return json_decode($res[0]['permission'], true);
        } else {
            return 0;
        }
    }


function get_ext($pdo,$fname)
{

	$up_filename=$_FILES[$fname]["name"];
	$file_basename = substr($up_filename, 0, strripos($up_filename, '.')); // strip extention
	$file_ext = substr($up_filename, strripos($up_filename, '.')); // strip name
	return $file_ext;
}

function ext_check($pdo,$allowed_ext,$my_ext) 
{

	$arr1 = array();
	$arr1 = explode("|",$allowed_ext);	
	$count_arr1 = count(explode("|",$allowed_ext));	

	for($i=0;$i<$count_arr1;$i++)
	{
		$arr1[$i] = '.'.$arr1[$i];
	}
	

	$str = '';
	$stat = 0;
	for($i=0;$i<$count_arr1;$i++)
	{
		if($my_ext == $arr1[$i])
		{
			$stat = 1;
			break;
		}
	}

	if($stat == 1)
		return true; // file extension match
	else
		return false; // file extension not match
}


function get_ai_id($pdo,$tbl_name) 
{
	$statement = $pdo->prepare("SHOW TABLE STATUS LIKE '$tbl_name'");
	$statement->execute();
	$result = $statement->fetchAll(PDO::FETCH_ASSOC);
	foreach($result as $row)
	{
		$next_id = $row['Auto_increment'];
	}
	return $next_id;
}