Last active
August 29, 2015 13:58
-
-
Save codescribblr/10404320 to your computer and use it in GitHub Desktop.
Helper Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Util { | |
| function __construct(){ | |
| } | |
| public static function log($action, $message="", $logfile=false) { | |
| $logfile = ($logfile) ? $logfile : $_SERVER['DOCUMENT_ROOT'].'/logs/log'.date("Y-m-d").'.log'; | |
| $new = file_exists($logfile) ? false : true; | |
| if($handle = fopen($logfile, 'a')) { // append | |
| $timestamp = strftime("%Y-%m-%d %H:%M:%S", time()); | |
| $content = "{$timestamp} | {$action}: {$message}\n"; | |
| fwrite($handle, $content); | |
| fclose($handle); | |
| if($new) { chmod($logfile, 0755); } | |
| } else { | |
| return false; | |
| } | |
| } | |
| public static function pprint_r($var){ | |
| echo "<pre>"; | |
| print_r($var); | |
| echo "</pre>"; | |
| } | |
| public static function file_get_contents_curl($url) { | |
| $ch = curl_init(); | |
| curl_setopt($ch, CURLOPT_HEADER, 0); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
| curl_setopt($ch, CURLOPT_URL, $url); | |
| curl_setopt($ch, CURLOPT_TIMEOUT, 120); | |
| $data = curl_exec($ch); | |
| // echo curl_getinfo($ch) . '<br/>'; | |
| // echo curl_errno($ch) . '<br/>'; | |
| // echo curl_error($ch) . '<br/>'; | |
| curl_close($ch); | |
| return $data; | |
| } | |
| public static function get_include_contents($filename) { | |
| if (is_file($filename)) { | |
| ob_start(); | |
| include $filename; | |
| return ob_get_clean(); | |
| } | |
| return false; | |
| } | |
| public static function my_array_unique($array, $keep_key_assoc = false) { | |
| $duplicate_keys = array(); | |
| $tmp = array(); | |
| foreach ($array as $key=>$val) { | |
| // convert objects to arrays, in_array() does not support objects | |
| if (is_object($val)) | |
| $val = (array)$val; | |
| if (!in_array($val, $tmp)) | |
| $tmp[] = $val; | |
| else | |
| $duplicate_keys[] = $key; | |
| } | |
| foreach ($duplicate_keys as $key) | |
| unset($array[$key]); | |
| return $keep_key_assoc ? $array : array_values($array); | |
| } | |
| public static function sortByDueDate($a, $b) { | |
| return $b['Due_Date'] - $a['Due_Date']; | |
| } | |
| public static function datetime_to_text($datetime="") { | |
| $unixdatetime = strtotime($datetime); | |
| return strftime("%B %d, %Y at %I:%M %p", $unixdatetime); | |
| } | |
| public static function base64_url_encode($input) { | |
| return strtr(base64_encode($input), '+/=', '-_,'); | |
| } | |
| public static function base64_url_decode($input) { | |
| return base64_decode(strtr($input, '-_,', '+/=')); | |
| } | |
| public static function dollars($number){ | |
| return ($number >= 0) ? "$".number_format($number, 2) : "<em class='red'>$".number_format($number, 2)."</em>"; | |
| } | |
| public static function nodollars($money){ | |
| return floatval(preg_replace('/[^\d\.]/', '', $money)); | |
| } | |
| public static function bit_debug($setting = E_ALL) { | |
| error_reporting($setting); | |
| ini_set('display_errors',1); | |
| } | |
| public static function strip_zeros_from_date( $marked_string="" ) { | |
| // first remove the marked zeros | |
| $no_zeros = str_replace('*0', '', $marked_string); | |
| // then remove any remaining marks | |
| $cleaned_string = str_replace('*', '', $no_zeros); | |
| return $cleaned_string; | |
| } | |
| public static function redirect_to( $location = NULL ) { | |
| if ($location != NULL) { | |
| header("Location: {$location}"); | |
| exit; | |
| } | |
| } | |
| public static function reload_cache($cache_file, $cache_data) { | |
| file_put_contents($cache_file, serialize($cache_data)); | |
| return $cache_data; | |
| } | |
| public static function read_cache($cache_file) { | |
| return unserialize(file_get_contents($cache_file)); | |
| } | |
| public static function check_cache($cache_file='cache.json', $cache_lifetime=600) { | |
| if (file_exists($cache_file)) { | |
| $modification_time = filemtime($cache_file); | |
| if (time() - $modification_time > $cache_lifetime) { | |
| return false; | |
| } else { | |
| $cache_data = self::read_cache($cache_file); | |
| return $cache_data; | |
| } | |
| } else { | |
| $cache_data = self::reload_cache($cache_file, $cache_data); | |
| } | |
| return false; | |
| } | |
| public static function increase_memory($mem="256M"){ | |
| ini_set('memory_limit', $mem); | |
| } | |
| public static function output_message() { | |
| if($_GET['message']):?> | |
| <div class="alert alert-<?php echo $_GET['message_type'];?> alert-dismissable status-message"> | |
| <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> | |
| <?php echo stripslashes(urldecode($_GET['message_text']));?> | |
| </div> | |
| <?php else: ?> | |
| <div class="alert alert-danger status-message" style="display:none;"> | |
| </div> | |
| <?php endif; | |
| } | |
| public static function google_get_adress_location($address) { | |
| $response = json_decode(file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=false®ion=us')); | |
| if ($response->results) { | |
| return $response->results[0]->geometry->location->lat . ',' . $response->results[0]->geometry->location->lng; | |
| } | |
| return false; | |
| } | |
| public static function get_geodistance($requested_location, $dealer_location) { | |
| $l = explode(',', $requested_location); | |
| $dl = explode(',', $dealer_location); | |
| if (count($l) != 2 || count($dl) != 2) { | |
| return 9999; | |
| } | |
| $distance = ( | |
| ( | |
| acos( | |
| sin(($l[0] * pi() / 180)) * sin(($dl[0] * pi() / 180)) + | |
| cos(($l[0] * pi() / 180)) * cos(($dl[0] * pi() / 180)) * | |
| cos((($l[1] - $dl[1]) * pi() / 180)) | |
| ) | |
| ) * 180 / pi() | |
| ) * 60 * 1.1515; | |
| return $distance; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function dollars(n) { | |
| return "$" + n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,"); | |
| } | |
| /*! A fix for the iOS orientationchange zoom bug. | |
| Script by @scottjehl, rebound by @wilto. | |
| MIT License. | |
| */ | |
| (function(w){ | |
| // This fix addresses an iOS bug, so return early if the UA claims it's something else. | |
| if( !( /iPhone|iPad|iPod/.test( navigator.platform ) && navigator.userAgent.indexOf( "AppleWebKit" ) > -1 ) ){ return; } | |
| var doc = w.document; | |
| if( !doc.querySelector ){ return; } | |
| var meta = doc.querySelector( "meta[name=viewport]" ), | |
| initialContent = meta && meta.getAttribute( "content" ), | |
| disabledZoom = initialContent + ",maximum-scale=1", | |
| enabledZoom = initialContent + ",maximum-scale=10", | |
| enabled = true, | |
| x, y, z, aig; | |
| if( !meta ){ return; } | |
| function restoreZoom(){ | |
| meta.setAttribute( "content", enabledZoom ); | |
| enabled = true; } | |
| function disableZoom(){ | |
| meta.setAttribute( "content", disabledZoom ); | |
| enabled = false; } | |
| function checkTilt( e ){ | |
| aig = e.accelerationIncludingGravity; | |
| x = Math.abs( aig.x ); | |
| y = Math.abs( aig.y ); | |
| z = Math.abs( aig.z ); | |
| // If portrait orientation and in one of the danger zones | |
| if( !w.orientation && ( x > 7 || ( ( z > 6 && y < 8 || z < 8 && y > 6 ) && x > 5 ) ) ){ | |
| if( enabled ){ disableZoom(); } } | |
| else if( !enabled ){ restoreZoom(); } } | |
| w.addEventListener( "orientationchange", restoreZoom, false ); | |
| w.addEventListener( "devicemotion", checkTilt, false ); | |
| })( this ); | |
| // make it safe to use console.log always | |
| (function(a){function b(){}for(var c="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","),d;!!(d=c.pop());){a[d]=a[d]||b;}}) | |
| (function(){try{console.log();return window.console;}catch(a){return (window.console={});}}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment