Last active
January 8, 2022 17:24
-
-
Save loranger/8664919 to your computer and use it in GitHub Desktop.
My casual helpers
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 | |
/** | |
* Toolbox | |
* A pool of useful functions | |
* | |
* @author loranger | |
*/ | |
/** | |
* Return a post parameter value | |
* | |
* @param string $name of the requested value | |
* @param mixed $default value | |
* @return mixed value of the post parameter, or default value, or false | |
* @author loranger | |
**/ | |
function getPostParam($name, $default = false) | |
{ | |
if( array_key_exists($name, $_POST) ) | |
{ | |
return $_POST[$name]; | |
} | |
return $default; | |
} | |
/** | |
* Return a raw post parameter value | |
* | |
* @param string $name of the requested value | |
* @param mixed $default value | |
* @return mixed value of the post parameter, or default value, or false | |
* @author loranger | |
**/ | |
function getRawPostParam($name, $default = false) | |
{ | |
// Get raw data from curl connections (webservices) | |
$raw = json_decode( file_get_contents('php://input'), true ); | |
if( $raw && is_array($raw) && array_key_exists($name, $raw) ) | |
{ | |
return $raw[$name]; | |
} | |
return $default; | |
} | |
/** | |
* Return a post or get parameter value | |
* | |
* @param string $name of the requested value | |
* @param mixed $default value | |
* @return mixed value of the post or get parameter, or default value, or false | |
* @author loranger | |
**/ | |
function getParam($name, $default = false) | |
{ | |
if( $raw = getRawPostParam($name) ) | |
{ | |
return $raw; | |
} | |
elseif( $post = getPostParam($name) ) | |
{ | |
return $post; | |
} | |
else if( array_key_exists($name, $_GET) ) | |
{ | |
return $_GET[$name]; | |
} | |
return $default; | |
} | |
/** | |
* Detect if a post or get parameter exists | |
* | |
* @param string $name of the requested value | |
* @return boolean | |
* @author loranger | |
**/ | |
function hasParam($name) | |
{ | |
return ( getParam($name) !== false ); | |
} | |
/** | |
* Return a session value | |
* | |
* @param string $name of the requested value | |
* @param mixed $default value | |
* @return mixed value of the session variable, or default value, or false | |
* @author loranger | |
**/ | |
function getSessionVar($name, $default = false) | |
{ | |
if( isset($_SESSION) && array_key_exists($name, $_SESSION) ) | |
{ | |
return $_SESSION[$name]; | |
} | |
return $default; | |
} | |
/** | |
* Detect if a session variable exists | |
* | |
* @param string $name of the requested value | |
* @return boolean | |
* @author loranger | |
**/ | |
function hasSessionVar($name) | |
{ | |
return ( getSessionVar($name) !== false ); | |
} | |
/** | |
* Escapes special characters in a string for use in an SQL statement | |
* | |
* @param string $string to be protected | |
* @return string | |
* @author loranger | |
**/ | |
function escapeString($string) | |
{ | |
return DB()->escape_string($string); | |
} | |
/** | |
* Return a byte value from a string formated size | |
* | |
* @param string $value such as '1 G' or "200M" | |
* @return integer value in bytes | |
* @author loranger | |
**/ | |
function getBytes($val) | |
{ | |
$val = trim($val); | |
$last = strtolower(substr($val, -1)); | |
if($last == 'g') | |
$val = $val*1024*1024*1024; | |
if($last == 'm') | |
$val = $val*1024*1024; | |
if($last == 'k') | |
$val = $val*1024; | |
return $val; | |
} | |
/** | |
* Output a debug message into the syslog | |
* | |
* @param mixed messages to display, using sprintf format | |
* @author loranger | |
**/ | |
function dbg() | |
{ | |
$args = func_get_args(); | |
$message = vsprintf(array_shift($args), $args); | |
syslog(LOG_WARNING, 'DBG : '.$message); | |
} | |
/** | |
* Fetch a user IP adress | |
* | |
* @author vince | |
**/ | |
function getUserIP() | |
{ | |
$ip = false; | |
if ( array_key_exists('REMOTE_ADDR', $_SERVER) ) | |
{ | |
$ip = $_SERVER['REMOTE_ADDR']; | |
} | |
if ( array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER) ) | |
{ | |
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
} | |
if ( array_key_exists('HTTP_CLIENT_IP', $_SERVER) ) | |
{ | |
$ip = $_SERVER['HTTP_CLIENT_IP']; | |
} | |
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') { // TODO : Remove when haproxy handles ssl and forwardfor | |
if ($last = User()->getLastKnownIP()) { | |
$ip = $last; | |
} | |
} | |
$ip .= ',N/A'; | |
$ips = array_filter(explode(',', $ip)); | |
return reset($ips); | |
} | |
/** | |
* return the number of days between two dates | |
* | |
* @param integer timestamp date start | |
* @param integer timestamp date end | |
* @return integer days number | |
* @author vince | |
**/ | |
function getDiffDates($timestamp1, $timestamp2) | |
{ | |
if ( is_object($timestamp1) && is_a($timestamp1, 'DateTime') ) { | |
$timestamp1 = $timestamp1->format('U'); | |
} | |
if ( is_object($timestamp2) && is_a($timestamp2, 'DateTime')) { | |
$timestamp2 = $timestamp2->format('U'); | |
} | |
$diff = $timestamp2 - $timestamp1; | |
return round($diff/60/60/24); | |
} | |
/** | |
* Generate human readable string such as password | |
* | |
* @param integer length of the desired string | |
* @return string $string | |
* @author Laurent Goussard | |
**/ | |
function generateRandomString($length = 8) | |
{ | |
$string = ''; | |
$alphabet = range('a', 'z'); | |
$vowels = array('a', 'e', 'i', 'o', 'u', 'y'); | |
$consonants = array_diff($alphabet, $vowels); | |
for ($i=0; $i < $length ; $i++) | |
{ | |
$string .= ($i%2 == 0) ? $consonants[array_rand($consonants)] : $vowels[array_rand($vowels)]; | |
} | |
return $string; | |
} | |
/** | |
* Format a price depending on its currency | |
* | |
* @param float price | |
* @param string currency code | |
* @return string $string | |
* @author Laurent Goussard | |
**/ | |
function display_price($price, $currency = 'EUR') | |
{ | |
$symbol = display_currency_symbol($currency); | |
switch ( $currency ) { | |
case 'GBP': | |
$mask = '%2$s %1$01.2f'; | |
break; | |
case 'CHF': | |
case 'EUR': | |
$mask = '%1$01.2f %2$s'; | |
break; | |
default: | |
$mask = '%1$01.2f %2$s'; | |
break; | |
} | |
return sprintf($mask, $price/100, $symbol); | |
} | |
/** | |
* Get a currency symbol | |
* | |
* @param string currency code | |
* @return string $string | |
* @author Laurent Goussard | |
**/ | |
function display_currency_symbol( $currency = 'EUR' ) | |
{ | |
switch ( $currency ) { | |
case 'GBP': | |
return "£"; | |
break; | |
case 'CHF': | |
return "CHF"; | |
break; | |
case 'EUR': | |
return "€"; | |
break; | |
default: | |
return $currency; | |
break; | |
} | |
} | |
/** | |
* Flatten a multi-dimensionnal array into a "flat" one | |
* | |
* @return array $arr | |
* @author Laurent Goussard | |
**/ | |
function arrayFlatten($array, $arr = array()) | |
{ | |
// for($x = 0; $x <= count($array); $x++) | |
foreach ($array as $x => $value) | |
{ | |
if ( array_key_exists($x, $array) ) | |
{ | |
if(is_array($array[$x])) | |
{ | |
$arr = call_user_func(__FUNCTION__, $array[$x], $arr); | |
} | |
else | |
{ | |
$arr[] = $array[$x]; | |
} | |
} | |
} | |
return $arr; | |
} | |
/** | |
* Get a color from a string (such as ip or name) | |
* | |
* @param string $str | |
* @return hexadecimal code | |
* @author Laurent Goussard | |
**/ | |
function stringToColorCode($str) { | |
$code = dechex(crc32($str)); | |
$code = substr($code, 0, 6); | |
return sprintf('#%s', $code); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment