169 lines
5.0 KiB
PHP
169 lines
5.0 KiB
PHP
<?php
|
|
/******************************************************************************
|
|
* YMS - Yacht Management Software
|
|
* Copyright (C) 2024-2026 Thomas Hooge
|
|
*
|
|
* SPDX-License-Identifier: WTFPL
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
// ========== GPC FUNCTIONS ===================================================
|
|
|
|
function gpc_get_string(&$GPC, $varname, $maxlen = NULL, $default = NULL) {
|
|
// TODO Parameter nullval: single value or array of values which considered as null
|
|
// Default value for nullval is "-1"
|
|
if (!isset($GPC[$varname]) || (strlen(trim($GPC[$varname])) == 0)) {
|
|
return $default;
|
|
}
|
|
$s = trim($GPC[$varname]);
|
|
if (isset($maxlen)) {
|
|
return substr($s, 0, $maxlen);
|
|
}
|
|
return $s;
|
|
}
|
|
|
|
function gpc_get_int(&$GPC, $varname, $default = NULL) {
|
|
if (!isset($GPC[$varname]) || $GPC[$varname] === '') {
|
|
return $default;
|
|
}
|
|
return (int)$GPC[$varname];
|
|
}
|
|
|
|
function gpc_get_uint(&$GPC, $varname, $default = NULL) {
|
|
if (!isset($GPC[$varname]) || $GPC[$varname] === '') {
|
|
return $default;
|
|
}
|
|
$val = (int)$GPC[$varname];
|
|
if ($val < 0) {
|
|
$val = $default;
|
|
}
|
|
return $val;
|
|
}
|
|
|
|
function gpc_get_float(&$GPC, $varname, $default = NULL) {
|
|
global $g_lconv;
|
|
if (!isset($GPC[$varname]) || (strlen(trim($GPC[$varname])) == 0)) {
|
|
return $default;
|
|
}
|
|
$var = str_replace($g_lconv['thousands_sep'], '', $GPC[$varname]);
|
|
if ($g_lconv['decimal_point'] != '.') {
|
|
$var = str_replace($g_lconv['decimal_point'], '.', $var);
|
|
}
|
|
return (double)$var;
|
|
}
|
|
|
|
function gpc_get_currency(&$GPC, $varname, $default = NULL) {
|
|
global $g_lconv;
|
|
if (!isset($GPC[$varname]) || (strlen(trim($GPC[$varname])) == 0)) {
|
|
return $default;
|
|
}
|
|
$var = str_replace($g_lconv['thousands_sep'], '', $GPC[$varname]);
|
|
if ($g_lconv['decimal_point'] != '.') {
|
|
$var = str_replace($g_lconv['decimal_point'], '.', $var);
|
|
}
|
|
return (double)$var;
|
|
}
|
|
|
|
function gpc_get_bool(&$GPC, $varname, $default = FALSE) {
|
|
if (!isset($GPC[$varname])) {
|
|
return $default;
|
|
}
|
|
$gpcvar = trim($GPC[$varname]);
|
|
if (strcasecmp('off', $gpcvar) == 0 ||
|
|
strcasecmp('no', $gpcvar) == 0 ||
|
|
strcasecmp('false', $gpcvar) == 0 ||
|
|
strcasecmp('no', $gpcvar) == 0 ||
|
|
strcasecmp('0', $gpcvar) == 0 ||
|
|
strcasecmp('nein', $gpcvar) == 0)
|
|
{
|
|
return FALSE;
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
function gpc_get_date(&$GPC, $varname, $default = NULL) {
|
|
global $g_opt_datefmt;
|
|
if (!isset($GPC[$varname]) || !is_scalar($GPC[$varname])) {
|
|
return $default;
|
|
}
|
|
$value = trim((string)$GPC[$varname]);
|
|
if ($value === '') {
|
|
return $default;
|
|
}
|
|
foreach (array_keys($g_opt_datefmt) as $format) {
|
|
if (preg_match_all('/[Ymd]/', $format, $tokens) !== 3) {
|
|
continue;
|
|
}
|
|
$regex = preg_quote($format, '/');
|
|
$regex = str_replace('d', '(\d{2})', $regex);
|
|
$regex = str_replace('m', '(\d{2})', $regex);
|
|
$regex = str_replace('Y', '(\d{4})', $regex);
|
|
if (!preg_match('/^' . $regex . '$/', $value, $matches)) {
|
|
continue;
|
|
}
|
|
$year = $month = $day = NULL;
|
|
foreach ($tokens[0] as $i => $token) {
|
|
switch ($token) {
|
|
case 'Y':
|
|
$year = (int)$matches[$i + 1];
|
|
break;
|
|
case 'm':
|
|
$month = (int)$matches[$i + 1];
|
|
break;
|
|
case 'd':
|
|
$day = (int)$matches[$i + 1];
|
|
break;
|
|
}
|
|
}
|
|
if (checkdate($month, $day, $year)) {
|
|
return sprintf('%04d-%02d-%02d', $year, $month, $day);
|
|
}
|
|
}
|
|
return $default;
|
|
}
|
|
|
|
function gpc_get_datetime(&$GPC, $varname, $default = NULL) {
|
|
// TODO improve
|
|
if (!isset($GPC[$varname]) || $GPC[$varname] == '') {
|
|
return $default;
|
|
}
|
|
return $GPC[$varname];
|
|
}
|
|
|
|
function gpc_get_color(&$GPC, $varname, $default = NULL) {
|
|
if (!isset($GPC[$varname]) || (strlen(trim($GPC[$varname])) == 0)) {
|
|
return $default;
|
|
}
|
|
$color = $GPC[$varname];
|
|
if ((strlen($color) == 7) && (substr($color, 0, 1) == '#')) {
|
|
$color = substr($color, 1);
|
|
}
|
|
if ((strlen($color) != 6) || (! ctype_xdigit($color))) {
|
|
return $default;
|
|
}
|
|
return strtoupper($color);
|
|
}
|
|
|
|
function gpc_get_enum(&$GPC, $varname, $values, $default = NULL) {
|
|
if (!isset($GPC[$varname]) || (strlen(trim($GPC[$varname])) == 0)) {
|
|
return $default;
|
|
}
|
|
if (! in_array($GPC[$varname], $values)) {
|
|
return $default;
|
|
}
|
|
return $GPC[$varname];
|
|
}
|
|
|
|
function gpc_get_set(&$GPC, $varname, $allowed, $as_string = false, $default = []) {
|
|
if (!isset($GPC[$varname])) {
|
|
return $default;
|
|
}
|
|
$selected = array_intersect($allowed, array_keys($GPC[$varname]));
|
|
if ($as_string) {
|
|
return implode(',', $selected);
|
|
} else {
|
|
return array_values($selected);
|
|
}
|
|
}
|