Virajsinh

My PhP Function

Jan 17th, 2022 (edited)
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.10 KB | None | 0 0
  1. <?php
  2. /**
  3.  *  Custom Helper Function Creted By VeeRa
  4.  *
  5.  * @package CodeIgniter
  6.  * @author  VeeRa
  7.  *
  8.  */
  9. // hashid function for id encryption
  10. if (!function_exists('hashid'))
  11. {
  12.     function hashid($string, $action = 'e')
  13.     {
  14.         $secret_key = '*BTzH38rZvEF';
  15.         $secret_iv = 'e3!eWt7oasLp';
  16.  
  17.         $output = false;
  18.         $encrypt_method = "AES-256-CBC";
  19.         $key = hash( 'sha256', $secret_key );
  20.         $iv = substr( hash( 'sha256', $secret_iv ), 0, 16 );
  21.  
  22.         if( $action == 'e' ) {
  23.             $output = base64_encode( openssl_encrypt( $string, $encrypt_method, $key, 0, $iv ) );
  24.         }
  25.         else if( $action == 'd' ){
  26.             $output = openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $iv );
  27.         }
  28.         return $output;
  29.     }
  30. }
  31.  
  32. // time elapsed ex. "2 min ago"
  33. if (!function_exists('time_elapsed_string'))
  34. {
  35.     /**
  36.      * [time_elapsed_string description]
  37.      * @param  [type]  $datetime [description]
  38.      * @param  boolean $full     [description]
  39.      * @return [string]          [description]
  40.      */
  41.     function time_elapsed_string($datetime, $full = false)
  42.     {
  43.         $now = new DateTime;
  44.         $ago = new DateTime($datetime);
  45.         $diff = $now->diff($ago);
  46.  
  47.         $diff->w = floor($diff->d / 7);
  48.         $diff->d -= $diff->w * 7;
  49.  
  50.         $string = array(
  51.             'y' => 'year',
  52.             'm' => 'month',
  53.             'w' => 'week',
  54.             'd' => 'day',
  55.             'h' => 'hour',
  56.             'i' => 'minute',
  57.             's' => 'second',
  58.         );
  59.         foreach ($string as $k => &$v) {
  60.             if ($diff->$k) {
  61.                 $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
  62.             } else {
  63.                 unset($string[$k]);
  64.             }
  65.         }
  66.  
  67.         if (!$full) $string = array_slice($string, 0, 1);
  68.         return $string ? implode(', ', $string) . ' ago' : 'just now';
  69.     }
  70. }
  71.  
  72. //OTP Generate
  73. if(!function_exists("otp_generate"))
  74. {
  75.     /**
  76.      * [otp_generate description]
  77.      * @param  integer $length    [default length is 6 digit]
  78.      * @return [integer]          [random digit]
  79.      */
  80.     function otp_generate($length=6)
  81.     {
  82.         if(is_numeric($length))
  83.         {
  84.             $chars      = "0123456789";
  85.             $otp        = substr(str_shuffle($chars), 0, $length);
  86.             return $otp;
  87.         }
  88.     }
  89. }
  90.  
  91. // date Format Change dd-mm-yyyy to yyyy-mm-dd
  92. if(!function_exists("dateFormatSQL"))
  93. {
  94.     /**
  95.      * [dateFormatSQL description]
  96.      * @param  string $date [description]
  97.      * @return [type]       [description]
  98.      */
  99.     function dateFormatSQL($date="")
  100.     {
  101.         if($date !="")
  102.         {
  103.             $output = date_format(date_create_from_format('d-m-Y', $date), 'Y-m-d');
  104.             return $output;
  105.         }
  106.     }
  107. }
  108.  
  109. // date Format Change  yyyy-mm-dd to dd-mm-yyyy
  110. if(!function_exists("dateFormatPHP"))
  111. {
  112.     /**
  113.      * [dateFormatPHP description]
  114.      * @param  string $date [description]
  115.      * @return [date]       [description]
  116.      */
  117.     function dateFormatPHP($date="")
  118.     {
  119.         if($date !="")
  120.         {
  121.             $output = date_format(date_create_from_format('Y-m-d', $date), 'd-m-Y');
  122.             return $output;
  123.         }
  124.     }
  125. }
  126.  
  127. // SQL DateTime
  128. if(!function_exists("SQL_DT"))
  129. {
  130.     function SQL_DT()
  131.     {
  132.         return date('Y-m-d H:i:s');
  133.     }
  134. }
  135.  
  136. // 24hr_to_12hr
  137. if(!function_exists("time_in_12_hour_format"))
  138. {
  139.     /**
  140.      * [time_in_12_hour_format description]
  141.      * @param  [type] $time [description]
  142.      * @return [type]       [description]
  143.      */
  144.     function time_in_12_hour_format($time)
  145.     {
  146.         return date("g:i A", strtotime($time));
  147.         // Input  : 08:12
  148.         // Output : 08:12 AM/PM
  149.     }
  150. }
  151.  
  152. // 12hr_to_24hr
  153. if(!function_exists("time_in_24_hour_format"))
  154. {
  155.     function time_in_24_hour_format($time)
  156.     {
  157.         return date("H:i", strtotime($time));
  158.         // Input  : 08:12 AM/PM
  159.         // Output : 08:12
  160.     }
  161. }
  162.  
  163. if(!function_exists("filesize_formatted"))
  164. {
  165.     function filesize_formatted($path)
  166.     {
  167.         $size = filesize($path);
  168.         $units = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  169.         $power = $size > 0 ? floor(log($size, 1024)) : 0;
  170.         return number_format($size / pow(1024, $power), 2, '.', ',') . ' ' . $units[$power];
  171.     }
  172. }
  173.  
  174. if(!function_exists("send_sms_api"))
  175. {
  176.     /**
  177.      * [send_sms_api description]
  178.      * @param  [string] $msg    [description]
  179.      * @param  [string] $mobile [description]
  180.      * @return [type]         [description]
  181.      */
  182.     function send_sms_api($message, $mobileNumber)
  183.     {
  184.         //Your authentication key
  185.         $authKey = "";
  186.  
  187.         //Multiple mobiles numbers separated by comma
  188.         // $mobileNumber = "9999999";
  189.  
  190.         //Sender ID, While using route4 sender id should be 6 characters long.
  191.         $senderId = "";
  192.  
  193.         //Your message to send, Add URL encoding here.
  194.         // $message = urlencode("Test message");
  195.  
  196.         //Define route
  197.         $route = "4";
  198.  
  199.         //Country
  200.         $country = "91";
  201.  
  202.         //Prepare you post parameters
  203.         $postData = array(
  204.             'authkey' => $authKey,
  205.             'mobiles' => $mobileNumber,
  206.             'message' => $message,
  207.             'sender' => $senderId,
  208.             'route' => $route,
  209.             'country' => $country
  210.         );
  211.  
  212.         //API URL
  213.         $url="http://api.msg91.com/api/sendhttp.php";
  214.  
  215.         // init the resource
  216.         $ch = curl_init();
  217.         curl_setopt_array($ch, array(
  218.             CURLOPT_URL => $url,
  219.             CURLOPT_RETURNTRANSFER => true,
  220.             CURLOPT_POST => true,
  221.             CURLOPT_POSTFIELDS => $postData
  222.             //,CURLOPT_FOLLOWLOCATION => true
  223.         ));
  224.  
  225.  
  226.         //Ignore SSL certificate verification
  227.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  228.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  229.  
  230.  
  231.         //get response
  232.         $output = curl_exec($ch);
  233.  
  234.         //Print error if any
  235.         // if(curl_errno($ch))
  236.         // {
  237.         //     echo 'error:' . curl_error($ch);
  238.         // }
  239.  
  240.         curl_close($ch);
  241.  
  242.         // echo $output;
  243.     }
  244. }
  245.  
  246. // Single File Upload
  247. if(!function_exists("file_upload"))
  248. {
  249.     /**
  250.      * [file_upload description]
  251.      * @param  [string] $path          [ folder path ]
  252.      * @param  [string] $file          [ upload controller whole array ]
  253.      * @param  [string] $allowed_types [ example : "jpg|jpeg|png|webp" ]
  254.      * @return [string]                [ file_name ]
  255.      */
  256.     function file_upload($path, $file, $allowed_types)
  257.     {
  258.         $ci =& get_instance();
  259.  
  260.         // Make Folder
  261.         if(file_exists(FCPATH.$path)){
  262.             // echo ("$file is a directory");
  263.         }else{
  264.             mkdir(FCPATH.$path, 0777, true);
  265.             // echo ("$file is not a directory");
  266.         }
  267.  
  268.         $name = explode('.', $file['name']);
  269.         $filename = time().rand(1000,9999).'.'.end($name);
  270.  
  271.         $config = array(
  272.             'upload_path'     => $path,
  273.             'upload_url'      => base_url().$path,
  274.             'allowed_types'   => $allowed_types,
  275.             'file_name'       => $filename
  276.         );
  277.  
  278.         $ci->load->library('upload', $config);
  279.  
  280.         $ci->upload->initialize($config);
  281.  
  282.         $_FILES['image']['name'] = $file['name'];
  283.         $_FILES['image']['type'] = $file['type'];
  284.         $_FILES['image']['tmp_name'] = $file['tmp_name'];
  285.         $_FILES['image']['error'] = $file['error'];
  286.         $_FILES['image']['size'] = $file['size'];
  287.  
  288.         if ($ci->upload->do_upload("image"))
  289.         {
  290.             $ci->upload->data();
  291.         } else {
  292.             // return false;
  293.             return $ci->upload->display_errors();;
  294.         }
  295.  
  296.         return $filename;
  297.     }
  298. }
  299.  
  300. // Multiple File Upload
  301. if(!function_exists("file_upload_multi"))
  302. {  
  303.     /**
  304.      * [file_upload_multi description]
  305.      * @param  [type] $path          [ folder path ]
  306.      * @param  [type] $files         [ upload controller whole array ]
  307.      * @param  [type] $allowed_types [ example : "jpg|jpeg|png|webp" ]
  308.      * @return [type]                [ file_name ]
  309.      */
  310.     function file_upload_multi($path, $files, $allowed_types)
  311.     {
  312.         $ci =& get_instance();
  313.  
  314.         // Make Folder
  315.         if(file_exists(FCPATH.$path)){
  316.             // echo ("$file is a directory");
  317.         }else{
  318.             mkdir(FCPATH.$path, 0777, true);
  319.             // echo ("$file is not a directory");
  320.         }
  321.  
  322.         $config = array(
  323.             'upload_path'     => $path,
  324.             'upload_url'      => base_url().$path,
  325.             'allowed_types'   => $allowed_types,
  326.         );
  327.  
  328.         $ci->load->library('upload', $config);
  329.  
  330.         $images = array();
  331.  
  332.         foreach ($files['name'] as $key => $image)
  333.         {
  334.             $_FILES['images[]']['name'] = $files['name'][$key];
  335.             $_FILES['images[]']['type'] = $files['type'][$key];
  336.             $_FILES['images[]']['tmp_name'] = $files['tmp_name'][$key];
  337.             $_FILES['images[]']['error'] = $files['error'][$key];
  338.             $_FILES['images[]']['size'] = $files['size'][$key];
  339.  
  340.             $name = explode('.', $files['name'][$key]);
  341.             $fileName = time().rand(1000,9999).'.'.end($name);
  342.  
  343.             $images[] = $fileName;
  344.  
  345.             $config['file_name'] = $fileName;
  346.  
  347.             $ci->upload->initialize($config);
  348.  
  349.             if ($ci->upload->do_upload('images[]'))
  350.             {
  351.                 $ci->upload->data();
  352.             } else {
  353.                 return false;
  354.             }
  355.         }
  356.  
  357.         return $images;
  358.     }
  359. }
  360. if(!function_exists("get_int_with_zero"))
  361. {
  362.     public function get_int_with_zero($value)
  363.     {
  364.         // get value = 1
  365.         // return value = 01
  366.         $num_padded = sprintf("%02d", $value);
  367.         return $num_padded;
  368.     }
  369. }
  370.  
  371. function DuplicateMySQLRecord($table, $primary_key_field, $primary_key_val, $update_value_array='')
  372. {  
  373.     $CI = & get_instance();
  374.  
  375.     /* generate the select query */
  376.     $CI->db->where($primary_key_field, $primary_key_val);
  377.     $query = $CI->db->get($table);
  378.     foreach ($query->result() as $row){  
  379.         foreach($row as $key=>$val){        
  380.             if($key != $primary_key_field){
  381.                 /* $CI->db->set can be used instead of passing a data array directly to the insert or update functions */
  382.                 if(array_key_exists($key, $update_value_array)){
  383.                     $CI->db->set($key, $update_value_array[$key]);              
  384.                 }else{
  385.                     $CI->db->set($key, $val);              
  386.                 }
  387.             }//endif
  388.         }//endforeach
  389.     }//endforeach
  390.    
  391.     /* insert the new record into table*/
  392.     $CI->db->insert($table);
  393.     $insert_id = $CI->db->insert_id();
  394.     return  $insert_id;
  395.     // return $CI->db->insert($table);
  396. }
  397.  
  398. getHostByName(getHostName()); // Get Local Area Current IP Address in Windows PC
  399.  
  400. // Get Current IP Address in Linux System
  401.  
  402. $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  403. $res = socket_connect($sock, '8.8.8.8', 53);
  404. // You might want error checking code here based on the value of $res
  405. socket_getsockname($sock, $addr);
  406. socket_shutdown($sock);
  407. socket_close($sock);
  408. echo $addr; // Get Current IP HERE
  409.  
  410. function delete_folder_files($folder_path = '')
  411. {
  412.     if(!empty($folder_path))
  413.     {
  414.         //Get a list of all of the file names in the folder.
  415.         $files = glob($folder_path . '/*');
  416.          
  417.         //Loop through the file list.
  418.         foreach($files as $file)
  419.         {
  420.             //Make sure that this is a file and not a directory.
  421.             if(is_file($file)) {
  422.                 //Use the unlink function to delete the file.
  423.                 unlink($file);
  424.             }
  425.         }
  426.     }
  427. }
  428. ?>
Add Comment
Please, Sign In to add comment