gest366/lib/html-helpers.class.php

65 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2021-02-24 20:40:04 +00:00
<?php
class HTMLHelper {
/**
* Function to read parameter from GET or POST request, escape it and filter it by its rules:
* string every string
* int integer value
* decimal decimal value (force conversion of commas into points: 0,01 become 0.01)
*/
public function form($param, $method = 'get', $escape = true, $rule = 'text') {
// method
if ($method == 'get') {
$value = $_GET[$param];
}
else {
$value = $_POST[$param];
}
if ($value == 'undefined') {
$value = '';
}
// Rules filter
if ($rule == 'int') {
$value = intval($value);
}
else if ($rule == 'decimal') {
$value = str_replace(",", ".", $value);
}
else if ($rule == 'date') {
if (preg_match("/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/", $value, $m)) {
$value = $m[3] . "-" . $m[2] . "-" . $m[1];
}
else if (preg_match("/^([0-9]{1})\-([0-9]{2})\-([0-9]{2})$/", $value, $m)) {
$value = $m[1] . "-" . $m[2] . "-" . $m[3];
}
}
if ($escape) $value = mres($value);
return $value;
}
}
function filter($param, $method = 'both', $rule = 'text') {
if ($method == 'both') {
if (isset($_GET[$param])) return filter($param, 'get');
else if (isset($_POST[$param])) return filter($param, 'post');
return null;
}
else if ($method == 'get' && isset($_GET[$param])) $value = $_GET[$param];
else if ($method == 'post' && isset($_POST[$param])) $value = $_POST[$param];
else return null;
if ($rule == 'int') $value = intval($value);
else if ($rule == 'decimal') $value = str_replace(",", ".", $value);
else if ($rule == 'date') {
if (preg_match("/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/", $value, $m)) $value = $m[3] . "-" . $m[2] . "-" . $m[1];
else if (preg_match("/^([0-9]{1})\-([0-9]{2})\-([0-9]{2})$/", $value, $m)) $value = $m[1] . "-" . $m[2] . "-" . $m[3];
}
return $value;
}
?>