2020-08-19 14:55:38 +02:00
|
|
|
<?PHP
|
|
|
|
/**
|
|
|
|
* Gathers wrappers for handling inputs.
|
|
|
|
*/
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Standard class providing overrides of default PHP functions as static
|
|
|
|
* functions.
|
|
|
|
*/
|
2020-08-29 17:22:16 +02:00
|
|
|
final class MD_STD_IN {
|
2020-08-19 14:55:38 +02:00
|
|
|
|
|
|
|
/**
|
2020-08-21 13:58:24 +02:00
|
|
|
* Validates and sanitizes input integers to be in line with MySQL
|
|
|
|
* autoincrement IDs.
|
2020-08-19 14:55:38 +02:00
|
|
|
*
|
|
|
|
* @param mixed $input Input string.
|
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
final public static function sanitize_id($input):int {
|
|
|
|
|
2020-08-27 17:16:48 +02:00
|
|
|
$input = \filter_var($input, \FILTER_VALIDATE_INT, [
|
2020-08-19 14:55:38 +02:00
|
|
|
'options' => [
|
|
|
|
'min_range' => 1, // Minimum number of an ID generated.
|
|
|
|
'max_range' => 4294967295 // Max value for MySQL's int data type
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!($input)) {
|
|
|
|
throw new MDpageParameterNotNumericException("Value is not numeric.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $input;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-21 13:58:24 +02:00
|
|
|
/**
|
|
|
|
* Sanitizes and validates input integers to be either valid IDs or 0.
|
|
|
|
*
|
|
|
|
* @param mixed $input Input string.
|
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
final public static function sanitize_id_or_zero($input):int {
|
|
|
|
|
|
|
|
if ($input === "") return 0;
|
|
|
|
|
2020-08-27 17:16:48 +02:00
|
|
|
$input = \filter_var($input, \FILTER_VALIDATE_INT, [
|
2020-08-21 13:58:24 +02:00
|
|
|
'options' => [
|
|
|
|
'min_range' => 0, // Minimum number of an ID generated.
|
|
|
|
'max_range' => 4294967295 // Max value for MySQL's int data type
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2020-08-22 12:13:08 +02:00
|
|
|
if ($input === false) {
|
2020-08-21 13:58:24 +02:00
|
|
|
throw new MDpageParameterNotNumericException("Value is not numeric.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $input;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-19 14:55:38 +02:00
|
|
|
/**
|
|
|
|
* General string sanitization for all purposes. For use of inputs with MySQL's
|
|
|
|
* MATCH AGAINST, use the dedicated sanitization function.
|
|
|
|
*
|
|
|
|
* @param mixed $input Input string.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
final public static function sanitize_text($input):string {
|
|
|
|
|
|
|
|
$output = \filter_var($input,
|
|
|
|
FILTER_SANITIZE_STRING,
|
2020-08-20 11:08:27 +02:00
|
|
|
FILTER_FLAG_NO_ENCODE_QUOTES);
|
|
|
|
|
|
|
|
if ($output === false) return "";
|
2020-08-19 14:55:38 +02:00
|
|
|
|
|
|
|
return trim($output);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves HTTP input texts from GET or POST variables, whatever is provided.
|
|
|
|
* If neither is given, returns a provided default.
|
|
|
|
*
|
2020-08-19 15:32:15 +02:00
|
|
|
* @param string $var_name Variable name.
|
|
|
|
* @param string $default Default value for the output.
|
|
|
|
* @param array<string> $allowed List of allowed values. Defaults to empty (all values allowed).
|
2020-08-19 14:55:38 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-08-19 15:32:15 +02:00
|
|
|
final public static function get_http_input_text(string $var_name, string $default = "", array $allowed = []):string {
|
2020-08-19 14:55:38 +02:00
|
|
|
|
|
|
|
if (isset($_GET[$var_name])) {
|
2020-08-19 15:32:15 +02:00
|
|
|
$output = self::sanitize_text($_GET[$var_name]);
|
2020-08-19 14:55:38 +02:00
|
|
|
}
|
|
|
|
else if (isset($_POST[$var_name])) {
|
2020-08-19 15:32:15 +02:00
|
|
|
$output = self::sanitize_text($_POST[$var_name]);
|
2020-08-19 14:55:38 +02:00
|
|
|
}
|
2020-08-19 15:32:15 +02:00
|
|
|
else $output = self::sanitize_text($default);
|
|
|
|
|
2020-08-27 17:16:48 +02:00
|
|
|
if (!empty($allowed) and !\in_array($output, $allowed, true)) {
|
2020-08-19 15:32:15 +02:00
|
|
|
Throw new MDpageParameterNotFromListException("Parameter `{$var_name}` must be any of the allowed values: " . implode(', ', $allowed));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
2020-08-19 14:55:38 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves HTTP input texts from POST variables.
|
|
|
|
* If none is given, returns a provided default.
|
|
|
|
*
|
2020-08-19 15:32:15 +02:00
|
|
|
* @param string $var_name Variable name.
|
|
|
|
* @param string $default Default value for the output.
|
|
|
|
* @param array<string> $allowed List of allowed values. Defaults to empty (all values allowed).
|
2020-08-19 14:55:38 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-08-19 15:32:15 +02:00
|
|
|
final public static function get_http_post_text(string $var_name, string $default = "", array $allowed = []):string {
|
2020-08-19 14:55:38 +02:00
|
|
|
|
|
|
|
if (isset($_POST[$var_name])) {
|
2020-08-19 15:32:15 +02:00
|
|
|
$output = self::sanitize_text($_POST[$var_name]);
|
2020-08-19 14:55:38 +02:00
|
|
|
}
|
2020-08-19 15:32:15 +02:00
|
|
|
else $output = self::sanitize_text($default);
|
|
|
|
|
2020-08-27 17:16:48 +02:00
|
|
|
if (!empty($allowed) and !\in_array($output, $allowed, true)) {
|
2020-08-19 15:32:15 +02:00
|
|
|
Throw new MDpageParameterNotFromListException("Parameter `{$var_name}` must be any of the allowed values: " . implode(', ', $allowed));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
2020-08-19 14:55:38 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-09-04 00:46:45 +02:00
|
|
|
/**
|
|
|
|
* Sanitizes and validates a URL. An empty string passes.
|
|
|
|
*
|
|
|
|
* @param mixed $input Input string.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
final public static function sanitize_url($input):string {
|
|
|
|
|
2020-09-04 01:19:00 +02:00
|
|
|
if ($input === "") return "";
|
2020-09-04 00:46:45 +02:00
|
|
|
|
|
|
|
$output = \filter_var($input, FILTER_SANITIZE_URL);
|
2020-09-04 01:37:49 +02:00
|
|
|
if (($output = \filter_var($output, FILTER_VALIDATE_URL)) === false) {
|
2020-09-04 00:46:45 +02:00
|
|
|
throw new MDInvalidUrl("Invalid input URL");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitizes and validates an e-mail address. An empty string passes.
|
|
|
|
*
|
|
|
|
* @param mixed $input Input string.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
final public static function sanitize_email($input):string {
|
|
|
|
|
2020-09-04 01:19:00 +02:00
|
|
|
if ($input === "") return "";
|
2020-09-04 00:46:45 +02:00
|
|
|
|
|
|
|
$output = \filter_var($input, FILTER_SANITIZE_EMAIL);
|
2020-09-04 01:37:49 +02:00
|
|
|
if (($output = \filter_var($output, FILTER_VALIDATE_EMAIL)) === false) {
|
2020-09-04 00:46:45 +02:00
|
|
|
throw new MDInvalidEmail("Invalid input email address");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-19 14:55:38 +02:00
|
|
|
}
|