<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 if ( ! function_exists('password_decrypt')) {
	function password_decrypt($password) {
		return md5($password);
	}
} 

if ( ! function_exists('setPageCookieMsg')) {
	function setPageCookieMsg($msgNo, $path = false)
	{
		if ($path === false) {
			$path = dirname($_SERVER['REQUEST_URI']);
		}
		
		$path = current_url();
		
		setcookie('perPage', $msgNo, 0, $path);
	}
}


if ( ! function_exists('mail_url')) {
	function mail_url() {
		return $GLOBALS['ARR_CONFIG']['mail_url'];
	}
}

if ( ! function_exists('site_name')) {
	function site_name() {
		return $GLOBALS['ARR_CONFIG']['site_name'];
	}
}

if ( ! function_exists('site_from_email')) {
	function site_from_email() {		
		return $GLOBALS['ARR_CONFIG']['site_from_email'];
	}
}

if ( ! function_exists('captcha_config')) {
	function captcha_config() {
 		$CAPTCHA_CONFIG = 	$GLOBALS['CAPTCHA_CONFIG'];
		// $CAPTCHA_CONFIG['img_path'] = base_url().'captcha/';
		  $CAPTCHA_CONFIG['img_url'] = base_url().'captcha/';
 		return $CAPTCHA_CONFIG;
	}
}
if ( ! function_exists('showMessage')) {
	function showMessage($message,$type,$auto_hide=false) {
	    if($auto_hide)
	        $set_auto_hide='alert-hide';
        else $set_auto_hide='';
	    
	     unset($_SESSION['successMsg']);
	    unset($_SESSION['errMsg']);
	    unset($_SESSION['warningMsg']);
	    
  		if($type == 'error'){
			return  '<div class="row"><div class="col-md-12 msg_div" > <div class="alert alert-dismissable alert-danger '.$set_auto_hide.'"  ><button aria-hidden="true" data-dismiss="alert" class="close" type="button"><span aria-hidden="true">&times;</span></button> '.$message.'</div></div></div>';	
		}else if($type == 'success'){
			return '<div class="row"><div class="col-md-12 msg_div" > <div class="alert alert-dismissable alert-success '.$set_auto_hide.' "  ><button aria-hidden="true" data-dismiss="alert" class="close" type="button"><span aria-hidden="true">&times;</span></button> '.$message.'</div></div></div>';	
		} else if($type == 'warning'){
			return '<div class="row"><div class="col-md-12 msg_div" > <div class="alert alert-dismissable alert-warning alert-hide"  ><button aria-hidden="true" data-dismiss="alert" class="close" type="button"><span aria-hidden="true">&times;</span></button> '.$message.'</div></div></div>';	
		} 
	}
}

if ( ! function_exists('searchArr')) {
	function searchArr($arr,$search_key,$field) {
		$resArr = array();
		if(!empty($arr)){
			foreach($arr as $aRow){
				if(isset($aRow[$field]) && ($aRow[$field] == $search_key || strpos(strtolower($aRow[$field]), strtolower($search_key)) !== false)){
					$resArr[] = $aRow;
				} 	
			}	
		}
  	 return $resArr;
	}
}

if ( ! function_exists('sortArr')) {
	function sortArr($arrList, $field,$orderBy) {
		 foreach ($arrList as $key => $row) {
			$arr[$key] = $row[$field];
		}
		array_multisort($arr, SORT_ASC, $arrList);
		if($orderBy == 'desc') {
			array_multisort($arr, SORT_DESC, $arrList);
 		}
		return $arrList;
 	}
}
 


if ( ! function_exists('hex2bin')) {
   function hex2bin($hexdata)
    {
        $bindata = '';

        for ($i = 0; $i < strlen($hexdata); $i += 2) {
            $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
        }

        return $bindata;
    }
}
if ( ! function_exists('encrypt')) {
function encrypt($str, $isBinary = false)
    {
		if(!empty($str)){
        $iv = iv;
        $str = $isBinary ? $str : utf8_decode($str);

        $td = mcrypt_module_open('rijndael-128', ' ', 'cbc', $iv);

        mcrypt_generic_init($td, key, $iv);
        $encrypted = mcrypt_generic($td, $str);

        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);

        return $isBinary ? $encrypted : bin2hex($encrypted);
		}
    }
}
if ( ! function_exists('decrypt')) {

    /**
     * @param string $code
     * @param bool $isBinary whether to decrypt as binary or not. Default is: false
     * @return string Decrypted data
     */
    function decrypt($code, $isBinary = false)
    {
        $code = $isBinary ? $code : hex2bin($code);
        $iv = iv;

        $td = mcrypt_module_open('rijndael-128', ' ', 'cbc', $iv);
 
        mcrypt_generic_init($td, key, $iv);
        $decrypted = mdecrypt_generic($td, $code);

        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);

        return $isBinary ? trim($decrypted) : utf8_encode(trim($decrypted));
    }
}
