Guest User

Untitled

a guest
Jul 26th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.56 KB | None | 0 0
  1. <?php
  2.  
  3. function get_age($birthDate) {
  4.     $birthDate = explode("/", $birthDate);
  5.     //get age from date or birthdate
  6.     $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md") ? ((date("Y") - $birthDate[2]) - 1) : (date("Y") - $birthDate[2]));
  7.     return $age;
  8. }
  9.  
  10. function flash() {
  11.     $ci = & get_instance();
  12.     if ($ci->session->flashdata('msg')) {
  13.         echo
  14.         '<div class="alert alert-info alert-dismissible fade in" role="alert">
  15.            <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>'
  16.         . $ci->session->flashdata('msg') .
  17.         '</div>';
  18.     } else if ($ci->session->flashdata('error_msg')) {
  19.         echo
  20.         '<div class="alert alert-danger alert-dismissible fade in" role="alert">
  21.            <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>'
  22.         . $ci->session->flashdata('error_msg') .
  23.         '</div>';
  24.     }
  25. }
  26.  
  27. function check_user($check_index = 'current_user') {
  28.     $ci = & get_instance();
  29.     return ($ci->session->userdata($check_index)) ? $ci->session->userdata($check_index) : false;
  30. }
  31.  
  32. function check_front_user() {
  33.     $ci = & get_instance();
  34.     return ($ci->session->userdata('front_user')) ? $ci->session->userdata('front_user') : false;
  35. }
  36.  
  37. function get_userdata($index) {
  38.     $ci = & get_instance();
  39.     return $ci->session->userdata($index) ? $ci->session->userdata($index) : false;
  40. }
  41.  
  42. function php_thumb_image($url, $file, $width, $height, $crop = false, $fall_back_image) {
  43.     $filename = $file != '' && file_exists($url . $file) ? $file : $fall_back_image;
  44.     $path = base_url(substr($url, 2) . $filename);
  45.     echo
  46.     '
  47.        <img src="' . base_url() . 'assets/phpthumb/phpThumb.php?src=' . urlencode($path) . '&amp;w=' . $width . '&amp;h=' . $height . '&amp;zc=' . $crop . '">
  48.    ';
  49. }
  50.  
  51. function debug($data) {
  52.     echo "<pre>";
  53.     print_r($data);
  54.     echo "</pre>";
  55.     die;
  56. }
  57.  
  58. function getBrowser() {
  59.     $u_agent = $_SERVER['HTTP_USER_AGENT'];
  60.     $bname = 'Unknown';
  61.     $platform = 'Unknown';
  62.     $version = "";
  63.  
  64.     //First get the platform?
  65.     if (preg_match('/linux/i', $u_agent)) {
  66.         $platform = 'linux';
  67.     } elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
  68.         $platform = 'mac';
  69.     } elseif (preg_match('/windows|win32/i', $u_agent)) {
  70.         $platform = 'windows';
  71.     }
  72.  
  73.     // Next get the name of the useragent yes seperately and for good reason
  74.     if (preg_match('/MSIE/i', $u_agent) && !preg_match('/Opera/i', $u_agent)) {
  75.         $bname = 'Internet Explorer';
  76.         $ub = "MSIE";
  77.     } elseif (preg_match('/Firefox/i', $u_agent)) {
  78.         $bname = 'Mozilla Firefox';
  79.         $ub = "Firefox";
  80.     } elseif (preg_match('/Chrome/i', $u_agent)) {
  81.         $bname = 'Google Chrome';
  82.         $ub = "Chrome";
  83.     } elseif (preg_match('/Safari/i', $u_agent)) {
  84.         $bname = 'Apple Safari';
  85.         $ub = "Safari";
  86.     } elseif (preg_match('/Opera/i', $u_agent)) {
  87.         $bname = 'Opera';
  88.         $ub = "Opera";
  89.     } elseif (preg_match('/Netscape/i', $u_agent)) {
  90.         $bname = 'Netscape';
  91.         $ub = "Netscape";
  92.     }
  93.  
  94.     // finally get the correct version number
  95.     $known = array('Version', $ub, 'other');
  96.     $pattern = '#(?<browser>' . join('|', $known) .
  97.             ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
  98.     if (!preg_match_all($pattern, $u_agent, $matches)) {
  99.         // we have no matching number just continue
  100.     }
  101.  
  102.     // see how many we have
  103.     $i = count($matches['browser']);
  104.     if ($i != 1) {
  105.         //we will have two since we are not using 'other' argument yet
  106.         //see if version is before or after the name
  107.         if (strripos($u_agent, "Version") < strripos($u_agent, $ub)) {
  108.             $version = $matches['version'][0];
  109.         } else {
  110.             $version = $matches['version'][1];
  111.         }
  112.     } else {
  113.         $version = $matches['version'][0];
  114.     }
  115.  
  116.     // check if we have a number
  117.     if ($version == null || $version == "") {
  118.         $version = "?";
  119.     }
  120.  
  121.     return array(
  122.         'userAgent' => $u_agent,
  123.         'name' => $bname,
  124.         'version' => $version,
  125.         'platform' => $platform,
  126.         'pattern' => $pattern
  127.     );
  128. }
  129.  
  130. function get_user($id) {
  131.     $ci = & get_instance();
  132.     $q = $ci->db->get_where('tbl_user', array('id' => $id))->result_array();
  133.     if ($q)
  134.         return $q[0];
  135.     else
  136.         return false;
  137. }
  138.  
  139. function segment($seg) {
  140.     $ci = & get_instance();
  141.     return $ci->uri->segment($seg);
  142. }
  143.  
  144. function get_care_detail($care_type) {
  145.     $ci = & get_instance();
  146.     $q = $ci->db->get_where('tbl_care', array('id' => $care_type))->result_array();
  147.     return $q[0];
  148. }
  149.  
  150. function get_care() {
  151.     $ci = & get_instance();
  152.     $ci->db->order_by('service_by ASC');
  153.     $q = $ci->db->get_where('tbl_care')->result_array();
  154.     return $q;
  155. }
  156.  
  157. function get_unread_ticket_count() {
  158.     $ci = & get_instance();
  159.     $ci->db->like('view', '0');
  160.     $ci->db->from('tbl_tickets_history');
  161.     return $ci->db->count_all_results();
  162. }
  163.  
  164. function encrypt_decrypt($action, $string) {
  165.     $output = false;
  166.  
  167.     $key = '!@#$%^&*';
  168.  
  169.     // initialization vector
  170.     $iv = md5(md5($key));
  171.  
  172.     if ($action == 'encrypt') {
  173.         $output = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, $iv);
  174.         $output = base64_encode($output);
  175.     } else if ($action == 'decrypt') {
  176.         $output = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, $iv);
  177.         $output = rtrim($output, "");
  178.     }
  179.     return $output;
  180. }
  181.  
  182. function home_flash() {
  183.     $ci = & get_instance();
  184.     if ($ci->session->flashdata('msg')) {
  185.         echo '<script>$.jGrowl("' . $ci->session->flashdata('msg') . '")</script>';
  186.     }
  187. }
  188.  
  189. function set_flash($index, $msg) {
  190.     $ci = & get_instance();
  191.     $ci->session->set_flashdata($index, $msg);
  192. }
  193.  
  194. function generateString() {
  195.     $number = base64_encode(random_string('alnum', 20));
  196.     $newstr = preg_replace('/[^a-zA-Z0-9\']/', '', $number);
  197.     return substr($newstr, 0, 10); // return string of 10 random character
  198. }
  199.  
  200. function swiftsend($param) {
  201.     include_once APPPATH . 'third_party/swiftmailer/swift_required.php';
  202.  
  203.     $numSent = 0; // Var to store the number of successful email recipients
  204.  
  205.     if ($_SERVER['HTTP_HOST'] == 'localhost') {
  206.         ini_set("SMTP", "ssl://smtp.gmail.com");
  207.         ini_set("smtp_port", "465");
  208.         $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', '465', 'ssl')
  209.                 ->setUsername(SWIFT_EMAIL)
  210.                 ->setPassword(SWIFT_PASS);
  211.     } else {
  212.         $transport = Swift_MailTransport::newInstance();
  213.     }
  214.  
  215.     $mailer = Swift_Mailer::newInstance($transport);
  216.  
  217.     $message = Swift_Message::newInstance($param['subject'])
  218.             ->setFrom($param['from'])
  219.             ->setReplyTo($param['reply'])
  220.             ->setBody($param['msg'], 'text/html');
  221.  
  222.     // Sending bulk email such that the people in the list will not be aware of eachother
  223.     foreach ($param['recipients'] as $address => $name) {
  224.         if (is_int($address)) {
  225.             $message->setTo($name);
  226.         } else {
  227.             $message->setTo(array($address => $name));
  228.         }
  229.  
  230.         $numSent += $mailer->send($message, $failedRecipients);
  231.     }
  232.     $return = array('sent' => $numSent, 'failed' => $failedRecipients);
  233.  
  234.     return (isset($param['return']) && $param['return']) ? $return : true;
  235. }
  236.  
  237. function uniqueid($prefix, $length = 4, $search_table, $search_column) {
  238.     $ci = &get_instance();
  239.     $id = $prefix . substr(rand(1111111111, 9999999999), 0, $length);
  240.     $checks = $ci->db->get_where($search_table, array($search_column => $id));
  241.     if ($checks->num_rows() > 0)
  242.         uniqueid($prefix, $length, $search_table, $search_column);
  243.     else
  244.         return $id;
  245. }
  246.  
  247. function calDistance($inLatitude, $inLongitude, $outLatitude, $outLongitude, $unit) {
  248.     if (empty($inLatitude) || empty($inLongitude) || empty($outLatitude) || empty($outLongitude))
  249.         return 0;
  250.     $url = "http://maps.googleapis.com/maps/api/directions/json?origin=$inLatitude,$inLongitude&destination=$outLatitude,$outLongitude&alternatives=true";
  251.     $c = curl_init();
  252.     curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  253.     curl_setopt($c, CURLOPT_URL, $url);
  254.     $jsonResponse = curl_exec($c);
  255.     curl_close($c);
  256.  
  257.     $dataset = json_decode($jsonResponse);
  258.     $routes = $dataset->routes;
  259.  
  260.     if (!$dataset)
  261.         return 0;
  262.     if (!isset($dataset->routes[0]->legs[0]->distance->value))
  263.         return 0;
  264.  
  265.     //sort the routes based on the distance
  266.     // Code Reference: http://stackoverflow.com/questions/18574496/google-distance-matrix-json-shortest-path-php
  267.     usort($routes, create_function('$a,$b', 'return intval($a->legs[0]->distance->value) - intval($b->legs[0]->distance->value);'));
  268.  
  269.     $distance = $routes[0]->legs[0]->distance->value;
  270.     if ($unit == "K") {
  271.         return round(($distance / 1000), 2); //kilometer
  272.     } else if ($unit == "M") {
  273.         return round(($distance * 0.000621371), 2); //miles
  274.     } else {
  275.         return round($distance, 2); //meter
  276.     }
  277. }
  278.  
  279. /*
  280.  * Author: TheManish
  281.  */
  282.  
  283. function sendRegistrationSuccessEmail($cust_email, $cust_pass) {
  284.  
  285.     $ci = & get_instance();
  286.  
  287.     $emailData = array(
  288.         'email' => $cust_email,
  289.         'password' => $cust_pass
  290.     );
  291.  
  292.     $email_template = $ci->load->view('emailer/_emailer_customer_registration', $emailData, true);
  293.     //  print($email_template);die;
  294.  
  295.     $subject = "Registration Confirmation - " . SITE_NAME;
  296.  
  297.     $param = array(
  298.         'from' => array(SITE_EMAIL => SITE_NAME),
  299.         'reply' => array(SITE_EMAIL => SITE_NAME),
  300.         'recipients' => array($cust_email),
  301.         'subject' => $subject,
  302.         'msg' => $email_template
  303.     );
  304.  
  305.     swiftsend($param);
  306. }
  307.  
  308. function get_page($slug) {
  309.  
  310.     $ci = & get_instance();
  311.  
  312.     $page = $ci->common_model->get_where('tbl_pages', array('slug' => $slug));
  313.     if ($page)
  314.         echo $page[0]['desc'];
  315.     else
  316.         echo "Slug Not Found: " . $slug;
  317. }
  318.  
  319. function generateRandomString($length = 10) {
  320.     $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  321.     $charactersLength = strlen($characters);
  322.     $randomString = '';
  323.     for ($i = 0; $i < $length; $i++) {
  324.         $randomString .= $characters[rand(0, $charactersLength - 1)];
  325.     }
  326.     return $randomString;
  327. }
  328.  
  329. function getAllCountryCodes() {
  330.     $ci = &get_instance();
  331.  
  332.     $ci->load->helper('file');
  333.     $json = read_file(APPPATH . 'third_party/country_calling_codes.json');
  334.     $countryCodes = json_decode($json);
  335.  
  336.     usort($countryCodes, function($a, $b) {
  337.         return $a->code > $b->code;
  338.     });
  339.  
  340.     return $countryCodes;
  341. }
  342.  
  343. function isAirportOrStation($location) {
  344.     if (
  345.             stripos($location, 'airport') !== false ||
  346.             stripos($location, 'terminal') !== false ||
  347.             stripos($location, 'train') !== false ||
  348.             stripos($location, 'station') !== false) {
  349.         return true;
  350.     } else {
  351.         return false;
  352.     }
  353. }
  354.  
  355. function isAirport($location) {
  356.     if (
  357.             (stripos($location, 'airport') !== false || stripos($location, 'terminal') !== false) &&
  358.             stripos($location, 'station') === false) {
  359.  
  360.         return true;
  361.     } else {
  362.         return false;
  363.     }
  364. }
  365.  
  366. function isStation($location) {
  367.     if (
  368.             stripos($location, 'train') !== false ||
  369.             stripos($location, 'station') !== false) {
  370.         return true;
  371.     } else {
  372.         return false;
  373.     }
  374. }
  375.  
  376. function doDirectPayment($data) {
  377.  
  378.     // Set sandbox (test mode) to true/false.
  379.     $sandbox = TRUE;
  380.  
  381.     // Set PayPal API version and credentials.
  382.     $api_version = '85.0';
  383.     $api_endpoint = $sandbox ? 'https://api-3t.sandbox.paypal.com/nvp' : 'https://api-3t.paypal.com/nvp';
  384.     $api_username = $sandbox ? 'manish-facilitator_api1.digital-designinc.com' : 'LIVE_USERNAME_GOES_HERE';
  385.     $api_password = $sandbox ? '8HX9ZPLN7FP7HZXJ' : 'LIVE_PASSWORD_GOES_HERE';
  386.     $api_signature = $sandbox ? 'AGTq7y9-jRQtnp9vTs0G2WMk.fM9AGrs.oM6Xo.yS1uxhHz8Olnim7na' : 'LIVE_SIGNATURE_GOES_HERE';
  387.  
  388.     // Store request params in an array
  389.     $request_params = array
  390.         (
  391.         'METHOD' => 'DoDirectPayment',
  392.         'USER' => $api_username,
  393.         'PWD' => $api_password,
  394.         'SIGNATURE' => $api_signature,
  395.         'VERSION' => $api_version,
  396.         'PAYMENTACTION' => 'Sale',
  397.         'IPADDRESS' => $_SERVER['REMOTE_ADDR'],
  398.         'CREDITCARDTYPE' => $data['CREDITCARDTYPE'],
  399.         'ACCT' => $data['ACCT'],
  400.         'EXPDATE' => $data['EXPDATE'],
  401.         'CVV2' => $data['CVV2'],
  402.         'FIRSTNAME' => $data['FIRSTNAME'],
  403.         'LASTNAME' => $data['LASTNAME'],
  404.         'STREET' => $data['STREET'],
  405.         'CITY' => $data['CITY'],
  406.         'STATE' => $data['STATE'],
  407.         'COUNTRYCODE' => 'UK',
  408.         'ZIP' => $data['ZIP'],
  409.         'AMT' => $data['AMT'],
  410.         'CURRENCYCODE' => 'GBP',
  411.         'DESC' => $data['DESC']
  412.     );
  413.    
  414.     // Loop through $request_params array to generate the NVP string.
  415.     $nvp_string = '';
  416.     foreach ($request_params as $var => $val) {
  417.         $nvp_string .= '&' . $var . '=' . urlencode($val);
  418.     }
  419.  
  420.     // Send NVP string to PayPal and store response
  421.     $curl = curl_init();
  422.     curl_setopt($curl, CURLOPT_VERBOSE, 1);
  423.     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  424.     curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  425.     curl_setopt($curl, CURLOPT_URL, $api_endpoint);
  426.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  427.     curl_setopt($curl, CURLOPT_POSTFIELDS, $nvp_string);
  428.  
  429.     $result = curl_exec($curl);
  430.     curl_close($curl);
  431.    
  432.     // Parse the API response
  433.     $nvp_response_array = NVPToArray($result);
  434.     debug($nvp_response_array);
  435.     return $nvp_response_array;
  436. }
  437.  
  438. // Function to convert NTP string to an array
  439. function NVPToArray($NVPString)
  440. {
  441.     $proArray = array();
  442.     while(strlen($NVPString))
  443.     {
  444.         // name
  445.         $keypos= strpos($NVPString,'=');
  446.         $keyval = substr($NVPString,0,$keypos);
  447.         // value
  448.         $valuepos = strpos($NVPString,'&') ? strpos($NVPString,'&'): strlen($NVPString);
  449.         $valval = substr($NVPString,$keypos+1,$valuepos-$keypos-1);
  450.         // decoding the respose
  451.         $proArray[$keyval] = urldecode($valval);
  452.         $NVPString = substr($NVPString,$valuepos+1,strlen($NVPString));
  453.     }
  454.     return $proArray;
  455. }
Add Comment
Please, Sign In to add comment