<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

if(!function_exists('encrypt'))
{
    /**
    *@desc This function works as a wrapper for the CI encode function
    * @param string
    * @return string
    */
    function encrypt($value)
    {
        if(isset($value) && $value)
        {
            $CI =& get_instance();
            $CI->load->library('encrypt');
            $CI->encrypt->set_cipher ('MCRYPT_BLOWFISH');
            return $CI->encrypt->encode($value, $CI->config->item('credit_card_encryption_key'));
        }
        return '';
    }
}

if(!function_exists('decrypt'))
{
    /**
    *@desc This function works as a wrapper for the CI decode function
    * @param string
    * @return string
    */
    function decrypt($value)
    {
        if(isset($value) && $value)
        {
            $CI =& get_instance();
            $CI->load->library('encrypt');
            $CI->encrypt->set_cipher ('MCRYPT_BLOWFISH');
            return $CI->encrypt->decode($value, $CI->config->item('credit_card_encryption_key'));
        }
        return '';
    }
}

if(!function_exists('api_encrypt'))
{
    /**
    *@desc This function works as a wrapper for the CI encode function and specially used for generating API signature
    * @param string
    * @return string
    */
    function api_encrypt($value)
    {
        if(isset($value) && $value)
        {
            $value = str_pad($value, 11, '0', STR_PAD_LEFT);
            $CI =& get_instance();
            $CI->load->library('encrypt');
            $CI->encrypt->set_cipher ( 'MCRYPT_BLOWFISH' );
            return base64_encode($CI->encrypt->encode($value, $CI->config->item('api_encryption_key')));
        }
        return '';
    }
}

if(!function_exists('api_decrypt'))
{
    /**
    *@desc This function works as a wrapper for the CI decode function and specially used for decoding API signature
    * @param string
    * @return string
    */
    function api_decrypt($value)
    {
        if(isset($value) && $value)
        {
            $value = base64_decode($value);

            $CI =& get_instance();
            $CI->load->library('encrypt');
            $CI->encrypt->set_cipher ( 'MCRYPT_BLOWFISH' );
            $value = (int) $CI->encrypt->decode($value, $CI->config->item('api_encryption_key'));
            return $value;
        }
        return 0;
    }
}

if(!function_exists('mysqlEncrypt'))
{
    function mysqlEncrypt($value)
    {
        return " COMPRESS(AES_ENCRYPT('$value', '" . $CI->config->item('db_encryption_key') . "')) ";
    }
}

if(!function_exists('mysqlDecrypt'))
{
    function mysqlDecrypt($value)
    {
        return " AES_DECRYPT(UNCOMPRESS($value), '" . $CI->config->item('db_encryption_key') . "') ";
    }
}
?>
