Showing posts with label helper_functions. Show all posts
Showing posts with label helper_functions. Show all posts

Monday 20 January 2020

Encrypt decrypt aes ecb openssl

This class provide a way of Encryption & decrypt GST PHP code helps illustrate how to encryption & decript, to protect sensitive data. Using openssl_encrypt and openssl_decrypt cryptographic functions.

you don't need to intantiate the class object because its funciton are static. you can directly access the methods.certificate_file_path is your cert file path it can be sandbox or production cert files ,you can either generate it or get it from providers.

You can also use below class to generate token for any web application , $filepath will be the path of security certificate (.cer) on your local machine or your server path where you kept the .cer file. If you don't have a .cer file you can just google for it to "How To Generate .Cer File". AES known as Advanced Encryption Standard is a symmetric-key encryption algorithm.

class GSTAPIENC {
    static function test(){
        return 1;
    }
    static function generateappKey ($appkey,$filepath){
        openssl_public_encrypt($appkey, $encrypted, file_get_contents($filepath));
        return base64_encode($encrypted);   
    }
    static function encryptOTP($otp_code,$appkey) {
       return base64_encode(openssl_encrypt($otp_code, "AES-256-ECB", $appkey, OPENSSL_RAW_DATA));
    }
    static function encryptData($data, $key) {
        return base64_encode(openssl_encrypt($data, "AES-256-ECB", $key, OPENSSL_RAW_DATA));
    }
    static function mac256($ent, $key) {
        $res = hash_hmac('sha256', $ent, $key, true); 
        return $res;
    }
    static function decryptData($data, $key) {
        return openssl_decrypt(base64_decode($data), "AES-256-ECB", $key, OPENSSL_RAW_DATA);
    }
    static function decodeJsonResponse($out, $rek, $ek) {
        $apiEK = GSTAPIENC::decryptData($rek, $ek);
        return base64_decode(GSTAPIENC::decryptData($out, $apiEK));
    }
    static function keygen($length = 10) {
        $key = '';
        list($usec, $sec) = explode(' ', microtime());
        mt_srand((float) $sec + ((float) $usec * 100000));
        $inputs = array_merge(range('z', 'a'), range(0, 9), range('A', 'Z'));
        for ($i = 0; $i < $length; $i++) {
            $key .= $inputs{mt_rand(0, 61)};
        }
        return base64_encode($key);
    } 
}

You can use it like below to generate API KEY

$certificate_file_path = "PATH OF YOUR SANDBOX OR PRODUCTION cert file.";
$generated_key = GSTAPIENC::keygen(32);
$appKey = GSTAPIENC::generateappKey(base64_decode($generated_key), $certificate_file_path);

You can also view download source

https://github.com/boy108zon/Encryption-Decription-GST

Wednesday 8 January 2020

Popover jquery content with ajax php

Let say if you have a user listing Or any kind of dynamic listing which is displaying information from database like mysql etc.. , so some time we want information of each users or product to display over a jQuery Popover with tab based. When popover will open it shows tab based information so that more information can be displayed over it.Below is the html link where user can click to get information.

Basically i had a loop of listing where i have to show up jquery popover with tab based so, when user click on button , viewSearchPayer function call and has one argument which can be userid or any thing. You can also take any unique ids or your data array element which is comming unique in Array. popoverId is important so that we can easily destroy or close the popover.

Response from your server side script like PHP etc will be html of tab ul li with user information or as per your requirements you can set accordingly.

<a class='btn btn-xs btn-warning' onclick='viewSearchPayer('EENPS9');'>gtInfo</a>

Below is the javascript function which is called when user click on button which have onclick event.

function viewSearchPayer(id) {
var popoverId = 'clientinfo_' + id;
var elem='Information 
<button type="button" onclick="closePop('+id+')" id="close" class="close">×<button>';
    $.ajax({
        type: 'POST',
        url: 'your_url_here',
        data: {"method": "viewSearchPayer", "id": id},
        dataType: 'json',
        async: true,
        success: function (response) {
            var Result;
            if (response.RESULT == 'SUCCESS') {
                Result = response.data;
            } else {
                Result = response.data;
            }
            $("."+popoverId).popover({
                container: 'body',
                placement: 'top',
                html: true,
                title: elem,
                content: Result
            }).popover('show');
        }
    });
}
Below is the javascript function which is called when user click on close button over popover so that we can close it. popoverId should be unique or you can adjust it according to your need.
function closePop(popoverId) {
     var closepopoverId = 'clientinfo_' + popoverId;
     $("."+closepopoverId).popover('hide');
  }

Below is the simple tab based html markup which you can revert back from your server side language like PHP , In this tab based you can also get dynamic data and display accordingly from database to these tabs.

 <ul class="nav nav-tabs">
<li class="active"><a href="#tab1" data-toggle="tab">Info</a></li>
<li class="tab"><a href="#tab2" data-toggle="tab">Track</a></li>
</ul><div class="tab-content">
<div class="tab-pane active" id="tab1"> 
tab1</div>
<div class="tab-pane" id="tab2">
tab2</div></div>

Sunday 10 April 2016

Php helper functions

Below are some usefull functions which you can use in your projects in any PHP framwork.
 
1. Below function will generate string as every string come with single quotes like ('Php','vTiger','Yii','Lavavel').
function in_clause($string){
   return "in ('" . str_replace(",", "','", $string) . "') ";
}
2. To count digit in a given numer.
function count_digit($number) {
    $digit = 0;
    do {
        $number /= 10;      //$number = $number / 10;
        $number = intval($number);
        $digit++;
    } while ($number != 0);
    return $digit;
}
2. To format an array as string which is need for running IN clause.
function format_in_clause($array) {
    if (isset($array)) {
        $realArray = explode(',', $array);
        if (is_array($realArray)) {
            $stringForIn = "'" . implode("','", $realArray) . "'";
            return $stringForIn;
        } 
    } else {
        return NULL;
    }
}
3. To converting an array to string.
function get_comma_seperator($array) {
    if (is_array($array)) {
        return $str = implode(",", $array);
    } else if (is_object($array)) {
        return implode(",", (array) $array);
    } else {
        return $array;
    }
}
4. To find a string contain comma in it or not.
function find_comma($string) {
    if (strpos($string, ",") !== false) {
        return 1;
    } else {
        return 0;
    }
}
5. To find a url contain http:// , https:// in it or not.
function check_http($url) {
    if (strpos($url, "http://") !== false) {
        return 1;
    } else {
        if (strpos($url, "https://") !== false) {
            return 1;
        } else {
            return 0;
        }
    }
}
6. To generate unique ids using mysql.
function gen_uuid() {
        global $adb;
        $order_id = NULL;
        $res = $adb->pquery("SELECT uuid() As order_id", array());
        $norows = $adb->num_rows($res);
        if ($norows > 0) {
            $order_id = $adb->query_result($res, 0, 'order_id');
            $order_id = str_replace('-', '', $order_id);
        }
        return $order_id;
    }