539 lines
15 KiB
PHP
539 lines
15 KiB
PHP
<?php
|
|
include_once "obj-json-b64-codecs.php";
|
|
|
|
function echopre($arr) {
|
|
echo "<pre>", print_r($arr, 1), "</pre>";
|
|
}
|
|
|
|
function array_pick($array, $keys) {
|
|
$result = array();
|
|
if (!is_array($keys)) {
|
|
$keys = func_get_args();
|
|
unset($keys[0]);
|
|
}
|
|
foreach ($keys as $key) {
|
|
if (!is_null($array) && array_key_exists($key, $array)) {
|
|
$result[$key] = $array[$key];
|
|
|
|
} else {
|
|
$result[$key] = null;
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* @param $array
|
|
* @return array
|
|
* @deprecated in favore di array_unique_multi2
|
|
*/
|
|
function array_unique_multi($array) {
|
|
return array_map("unserialize", array_unique(array_map("serialize", $array)));
|
|
}
|
|
|
|
function array_unique_multi2($array) { // utilizza procedura piu performante
|
|
return array_intersect_key($array, array_unique(array_map("serialize", $array)));
|
|
}
|
|
|
|
function array_unique_key($array, $key) {
|
|
$result = array();
|
|
foreach ($array as &$v) {
|
|
if (!isset($result[$v[$key]])) {
|
|
$result[$v[$key]] =& $v;
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
function array_remove_item_by_value($array, $itemValue) {
|
|
if (($key = array_search($itemValue, $array)) !== false) {
|
|
unset($array[$key]);
|
|
}
|
|
return array_values($array);
|
|
}
|
|
|
|
function array_orderby() {
|
|
/** WARN: su webserver linux (su wamp non si è verificato) è risultata inefficace
|
|
* in un ordinamento di timestamp (interi). sostituita con espressione linq.
|
|
* CASO: list-planner.php, $arr_attivitaGiorno
|
|
**/
|
|
$args = func_get_args();
|
|
$data = array_shift($args);
|
|
foreach ($args as $n => $field) {
|
|
if (is_string($field)) {
|
|
$tmp = array();
|
|
foreach ($data as $key => $row) {
|
|
$tmp[$key] = array_get($row, $field);
|
|
}
|
|
$args[$n] = $tmp;
|
|
}
|
|
}
|
|
$args[] = &$data;
|
|
call_user_func_array("array_multisort", $args);
|
|
return array_pop($args);
|
|
}
|
|
|
|
function array_sort($array) {
|
|
sort($array);
|
|
return $array;
|
|
}
|
|
|
|
function array_unset($array, $arr_keys) {
|
|
if (!is_array($arr_keys)) {
|
|
$arr_keys = func_get_args();
|
|
unset($arr_keys[0]);
|
|
}
|
|
foreach ($arr_keys as $key) {
|
|
if (array_key_exists($key, $array)) {
|
|
unset($array[$key]);
|
|
}
|
|
}
|
|
return $array;
|
|
}
|
|
|
|
if (!function_exists("array_column")) {
|
|
function array_column(array $input, $columnKey, $indexKey = null) {
|
|
$array = array();
|
|
foreach ($input as $value) {
|
|
if (!array_key_exists($columnKey, $value)) {
|
|
// trigger_error("Key \"$columnKey\" does not exist in array");
|
|
return false;
|
|
}
|
|
if (is_null($indexKey)) {
|
|
$array[] = $value[$columnKey];
|
|
} else {
|
|
if (!array_key_exists($indexKey, $value)) {
|
|
// trigger_error("Key \"$indexKey\" does not exist in array");
|
|
return false;
|
|
}
|
|
if (!is_scalar($value[$indexKey])) {
|
|
// trigger_error("Key \"$indexKey\" does not contain scalar value");
|
|
return false;
|
|
}
|
|
$array[$value[$indexKey]] = $value[$columnKey];
|
|
}
|
|
}
|
|
return $array;
|
|
}
|
|
}
|
|
|
|
function zeroIfBlank($v) {
|
|
return zeroIfNull(nullIfBlank($v));
|
|
}
|
|
|
|
function zeroIfNull($v) {
|
|
return is_null($v) ? 0 : $v;
|
|
}
|
|
|
|
function nullIfBlank($v) {
|
|
return is_null($v) || strlen(trim($v)) == 0 ? null : $v;
|
|
}
|
|
|
|
function blankIfNull($v) {
|
|
return is_null($v) ? "" : $v;
|
|
}
|
|
|
|
function blankIfZero($v) {
|
|
return blankIfNull(nullIfZero($v));
|
|
}
|
|
|
|
function nullIfZero($v) {
|
|
$n = is_string($v) && is_numeric($v) ? (float)$v : $v;
|
|
return is_number($n) && $n == 0 ? null : $v;
|
|
|
|
/*
|
|
if(is_string($value) && is_numeric($value)){
|
|
if((float)$value!=0){
|
|
return $value;
|
|
}
|
|
} else if(is_number($value) && $value!=0){
|
|
return $value;
|
|
}
|
|
return null;*/
|
|
}
|
|
|
|
function is_number($v) {
|
|
return !is_string($v) && (is_float($v) || is_int($v));
|
|
}
|
|
|
|
|
|
/**
|
|
* @deprecated
|
|
*/
|
|
function formatDateGeneral($date, $format) {
|
|
$date = str_replace("/", "-", substr($date, 0, 10));
|
|
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) {// FORMATO AAAA-MM-GG
|
|
$time = strtotime($date);
|
|
} else /*if (preg_match('/^\d{2}-\d{2}-\d{4}$/', $date))*/ {// FORMATO GG-MM-AAAA
|
|
$date = implode('-', array_reverse(explode('-', $date)));
|
|
$time = strtotime($date);
|
|
}
|
|
return strftime($format, $time);
|
|
}
|
|
|
|
function roundUpToAny($n, $x = 5) {
|
|
return round($n / $x) * $x;
|
|
}
|
|
|
|
function roundUpToDecimal($value, $decimals = 2) {
|
|
$x = pow(10, $decimals);
|
|
return round($value * $x) / $x;
|
|
}
|
|
|
|
function isAbsolutePath($path) {
|
|
return substr($path, 0, 4) === "http";
|
|
}
|
|
|
|
function checkRemoteFile($url) {
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_NOBODY, true);
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 800);
|
|
curl_exec($ch);
|
|
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); //>= 400 -> not found, $retcode = 200, found.
|
|
curl_close($ch);
|
|
return $retcode == "200";
|
|
}
|
|
|
|
/**
|
|
* @param $needle
|
|
* @param $haystack
|
|
* @return bool
|
|
*
|
|
* in_array case insensitive
|
|
*/
|
|
function in_arrayi($needle, $haystack) {
|
|
return in_array(strtolower($needle), array_map("strtolower", $haystack));
|
|
}
|
|
|
|
function array_searchi($needle, $haystack) {
|
|
return array_search(strtolower($needle), array_map("strtolower", $haystack));
|
|
}
|
|
|
|
function array_equals($array1, $array2) {
|
|
return count(array_diff($array1, $array2)) === 0;
|
|
}
|
|
|
|
function boolValue($var, $trueVal = null, $falseVal = null) {
|
|
if (is_bool($var)) {
|
|
return $var;
|
|
} else if (is_numeric($var)) {
|
|
if (is_string($var)) {
|
|
$var = intval($var);
|
|
}
|
|
if (is_null($trueVal) && is_null($falseVal)) {
|
|
$trueVal = 1;
|
|
$falseVal = 0;
|
|
}
|
|
|
|
} else if (is_string($var)) {
|
|
$var = strtoupper($var);
|
|
|
|
if (is_null($trueVal) && is_null($falseVal)) { // deduco 1/0 o S/N
|
|
if (in_array($var, array("1", "0"))) {
|
|
$trueVal = "1";
|
|
$falseVal = "0";
|
|
} else {
|
|
$trueVal = "S";
|
|
$falseVal = "N";
|
|
}
|
|
}
|
|
|
|
} else if (is_null($var)) {
|
|
return false;
|
|
} else {
|
|
return false;
|
|
}
|
|
|
|
if ($var == $falseVal) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
function utf8html($s) {
|
|
return htmlentities(mb_convert_encoding($s, "UTF-8", "ASCII"), 8, "UTF-8");
|
|
}
|
|
|
|
function echojson($arr) {
|
|
echo json_format($arr);
|
|
}
|
|
|
|
/**
|
|
* Format a flat JSON string to make it more human-readable
|
|
*
|
|
* @param string $json The original JSON string to process
|
|
* When the input is not a string it is assumed the input is RAW
|
|
* and should be converted to JSON first of all.
|
|
* @return string Indented version of the original JSON string
|
|
*/
|
|
function json_format($json) {
|
|
if (!is_string($json)) {
|
|
if (phpversion() && phpversion() >= 5.4) {
|
|
return json_encode($json, JSON_PRETTY_PRINT);
|
|
}
|
|
$json = json_encode($json);
|
|
}
|
|
$result = "";
|
|
$pos = 0; // indentation level
|
|
$strLen = strlen($json);
|
|
$indentStr = " ";
|
|
$newLine = "<br/>";
|
|
$prevChar = "";
|
|
$outOfQuotes = true;
|
|
for ($i = 0; $i < $strLen; $i++) {
|
|
// Speedup: copy blocks of input which don't matter re string detection and formatting.
|
|
$copyLen = strcspn($json, $outOfQuotes ? " \t\r\n\",:[{}]" : "\\\"", $i);
|
|
if ($copyLen >= 1) {
|
|
$copyStr = substr($json, $i, $copyLen);
|
|
// Also reset the tracker for escapes: we won't be hitting any right now
|
|
// and the next round is the first time an 'escape' character can be seen again at the input.
|
|
$prevChar = '';
|
|
$result .= $copyStr;
|
|
$i += $copyLen - 1; // correct for the for(;;) loop
|
|
continue;
|
|
}
|
|
|
|
// Grab the next character in the string
|
|
$char = substr($json, $i, 1);
|
|
|
|
// Are we inside a quoted string encountering an escape sequence?
|
|
if (!$outOfQuotes && $prevChar === '\\') {
|
|
// Add the escaped character to the result string and ignore it for the string enter/exit detection:
|
|
$result .= $char;
|
|
$prevChar = '';
|
|
continue;
|
|
}
|
|
// Are we entering/exiting a quoted string?
|
|
if ($char === '"' && $prevChar !== '\\') {
|
|
$outOfQuotes = !$outOfQuotes;
|
|
}
|
|
// If this character is the end of an element,
|
|
// output a new line and indent the next line
|
|
else if ($outOfQuotes && ($char === '}' || $char === ']')) {
|
|
$result .= $newLine;
|
|
$pos--;
|
|
for ($j = 0; $j < $pos; $j++) {
|
|
$result .= $indentStr;
|
|
}
|
|
} // eat all non-essential whitespace in the input as we do our own here and it would only mess up our process
|
|
else if ($outOfQuotes && false !== strpos(" \t\r\n", $char)) {
|
|
continue;
|
|
}
|
|
// Add the character to the result string
|
|
$result .= $char;
|
|
// always add a space after a field colon:
|
|
if ($outOfQuotes && $char === ':') {
|
|
$result .= ' ';
|
|
}
|
|
// If the last character was the beginning of an element,
|
|
// output a new line and indent the next line
|
|
else if ($outOfQuotes && ($char === ',' || $char === '{' || $char === '[')) {
|
|
$result .= $newLine;
|
|
if ($char === '{' || $char === '[') {
|
|
$pos++;
|
|
}
|
|
for ($j = 0; $j < $pos; $j++) {
|
|
$result .= $indentStr;
|
|
}
|
|
}
|
|
$prevChar = $char;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
function parse_size($size) {
|
|
$unit = preg_replace('/[^bkmgtpezy]/i', "", $size);
|
|
|
|
// Remove the non-unit characters from the size.
|
|
$size = preg_replace('/[^0-9\\.]/', "", $size);
|
|
|
|
// Remove the non-numeric characters from the size.
|
|
if ($unit) {
|
|
$DRUPAL_KILOBYTE = 1024;
|
|
// Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
|
|
return round($size * pow($DRUPAL_KILOBYTE, stripos("bkmgtpezy", $unit[0])));
|
|
|
|
} else {
|
|
return round($size);
|
|
}
|
|
}
|
|
|
|
function is_logged() {
|
|
if (LOGIN_JWT) {
|
|
$arr_fields = array("accessToken", "refreshToken");
|
|
} else {
|
|
$arr_fields = array("username", "password", "cod_mdep", "user_code", "fullname", "gruppo");
|
|
}
|
|
|
|
foreach ($arr_fields as $field) {
|
|
if (!array_key_exists($field, $_SESSION)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
function if_null($v1, $v2) { // se $v1 è nullo, torna $v2
|
|
return !is_null($v1) ? $v1 : $v2;
|
|
}
|
|
|
|
if (!function_exists("array_key_first")) {
|
|
function array_key_first($arr) {
|
|
foreach ($arr as $k => $v) {
|
|
return $k;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function array_most_frequent_value($array) {
|
|
if (is_array($array) && count($array) > 0) {
|
|
$values = array_count_values($array);
|
|
arsort($values);
|
|
$values = array_keys($values);
|
|
return $values[0];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function exitjson($v = null) {
|
|
$json = @json_encode($v);
|
|
if (!is_null($json) && $json !== false) {
|
|
header("Content-Type: " . \Mime::JSON);
|
|
}
|
|
exit($json);
|
|
}
|
|
|
|
function camelCaseEncode($s, $noStrip = array()) {
|
|
$str = preg_replace("/[^a-z0-9" . implode("", $noStrip) . "]+/i", " ", $s);
|
|
return lcfirst(str_replace(" ", "", ucwords(trim($str))));
|
|
}
|
|
|
|
function camelCaseDecode($s) {
|
|
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $s, $matches);
|
|
return strtolower(implode("_", $matches[0]));
|
|
}
|
|
|
|
function snakeCaseEncode($string) {
|
|
$string = preg_replace('/(?<!^)[" *"]/', '_', $string);
|
|
$string = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $string));
|
|
return preg_replace('/(?<!^)_+/', '_', $string);
|
|
}
|
|
|
|
function array_get($array, $key, $defaultValue = null) {
|
|
if (!is_array($array)) {
|
|
return $defaultValue;
|
|
}
|
|
|
|
if (is_null($key)) {
|
|
return $array;
|
|
}
|
|
|
|
if (array_key_exists($key, $array)) {
|
|
return $array[$key];
|
|
}
|
|
|
|
foreach (explode(".", $key) as $segment) {
|
|
if (is_array($array) && array_key_exists($segment, $array)) {
|
|
$array = $array[$segment];
|
|
} else {
|
|
$keyCamelCase = camelCaseEncode($key);
|
|
|
|
if (is_array($array) && array_key_exists($keyCamelCase, $array)) {
|
|
return $array[$keyCamelCase];
|
|
}
|
|
|
|
$keySnakeCase = snakeCaseEncode($key);
|
|
|
|
if (is_array($array) && array_key_exists($keySnakeCase, $array)) {
|
|
return $array[$keySnakeCase];
|
|
}
|
|
|
|
return $defaultValue;
|
|
}
|
|
}
|
|
|
|
return $array;
|
|
}
|
|
|
|
function array_groupBy( $array, $key) {
|
|
return array_reduce($array, function ($carry, $item) use ($key) {
|
|
$groupKey = isset($item[$key]) ? $item[$key] : null;
|
|
if ($groupKey !== null) {
|
|
$carry[$groupKey][] = $item;
|
|
}
|
|
return $carry;
|
|
}, []);
|
|
}
|
|
|
|
function array_only($array, ...$keys) {
|
|
return array_intersect_key(
|
|
$array,
|
|
array_flip($keys)
|
|
);
|
|
}
|
|
|
|
function array_exclude($array, ...$keys) {
|
|
return array_diff_key($array, array_flip($keys));
|
|
}
|
|
|
|
function array_any(array $array, callable $fn) {
|
|
foreach ($array as $key => $value) {
|
|
if ($fn($value, $key)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
function array_every(array $array, callable $fn) {
|
|
foreach ($array as $key => $value) {
|
|
if (!$fn($value, $key)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
function array_any_null(array $array) {
|
|
return array_any($array, function ($var) {
|
|
return is_null($var);
|
|
});
|
|
}
|
|
|
|
function array_every_null(array $array) {
|
|
return array_every($array, function ($var) {
|
|
return is_null($var);
|
|
});
|
|
}
|
|
|
|
function array_keys_exists(array $keys, array $array) {
|
|
$diff = array_diff_key(array_flip($keys), $array);
|
|
|
|
return count($diff) === 0;
|
|
}
|
|
|
|
function includeWithVariables($filePath, $variables = array(), $print = true) {
|
|
// Extract the variables to a local namespace
|
|
extract($variables);
|
|
|
|
// Start output buffering
|
|
ob_start();
|
|
|
|
// Include the template file
|
|
include $filePath;
|
|
|
|
// End buffering and return its contents
|
|
$output = ob_get_clean();
|
|
|
|
if (!$print) {
|
|
return $output;
|
|
}
|
|
|
|
echo $output;
|
|
}
|