2011-06-02 00:48:29 +02:00
|
|
|
<?php
|
2016-11-18 08:20:17 +01:00
|
|
|
use Engelsystem\ValidationResult;
|
2011-06-02 23:41:50 +02:00
|
|
|
|
2016-10-04 17:58:56 +02:00
|
|
|
/**
|
|
|
|
* Provide page/request helper functions
|
|
|
|
*/
|
|
|
|
|
2016-10-04 18:52:52 +02:00
|
|
|
/**
|
|
|
|
* Parse a date from da day and a time textfield.
|
|
|
|
*
|
|
|
|
* @param string $date_name
|
|
|
|
* Name of the textfield containing the day (format Y-m-d)
|
|
|
|
* @param string $time_name
|
|
|
|
* Name of the textfield containing the time (format H:i)
|
|
|
|
* @param string[] $allowed_days
|
|
|
|
* List of allowed days in format Y-m-d
|
|
|
|
* @param int $default_value
|
|
|
|
* Default value unix timestamp
|
|
|
|
*/
|
|
|
|
function check_request_datetime($date_name, $time_name, $allowed_days, $default_value) {
|
|
|
|
$time = date("H:i", $default_value);
|
|
|
|
$day = date("Y-m-d", $default_value);
|
|
|
|
|
|
|
|
if (isset($_REQUEST[$time_name]) && preg_match('#^\d{1,2}:\d\d$#', trim($_REQUEST[$time_name]))) {
|
|
|
|
$time = trim($_REQUEST[$time_name]);
|
|
|
|
}
|
|
|
|
if (isset($_REQUEST[$date_name]) && in_array($_REQUEST[$date_name], $allowed_days)) {
|
|
|
|
$day = $_REQUEST[$date_name];
|
|
|
|
}
|
|
|
|
|
|
|
|
return parse_date("Y-m-d H:i", $day . " " . $time);
|
|
|
|
}
|
|
|
|
|
2016-10-04 17:58:56 +02:00
|
|
|
/**
|
|
|
|
* Parse a date into unix timestamp
|
|
|
|
*
|
|
|
|
* @param string $pattern
|
|
|
|
* The date pattern (i.e. Y-m-d H:i)
|
|
|
|
* @param string $value
|
|
|
|
* The string to parse
|
|
|
|
* @return The parsed unix timestamp
|
|
|
|
*/
|
|
|
|
function parse_date($pattern, $value) {
|
|
|
|
$datetime = DateTime::createFromFormat($pattern, trim($value));
|
|
|
|
if ($datetime == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $datetime->getTimestamp();
|
|
|
|
}
|
|
|
|
|
2011-12-21 22:20:06 +01:00
|
|
|
/**
|
|
|
|
* Leitet den Browser an die übergebene URL weiter und hält das Script an.
|
|
|
|
*/
|
2016-10-01 10:48:19 +02:00
|
|
|
function redirect($url) {
|
|
|
|
header("Location: " . $url, true, 302);
|
2016-08-22 19:32:54 +02:00
|
|
|
raw_output("");
|
2011-12-21 22:20:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-21 22:58:09 +02:00
|
|
|
/**
|
|
|
|
* Echoes given output and dies.
|
2016-08-22 20:40:31 +02:00
|
|
|
*
|
2016-10-04 17:58:56 +02:00
|
|
|
* @param String $output
|
|
|
|
* String to display
|
2016-08-21 22:58:09 +02:00
|
|
|
*/
|
|
|
|
function raw_output($output) {
|
|
|
|
echo $output;
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
2016-10-03 18:32:25 +02:00
|
|
|
/**
|
|
|
|
* Helper function for transforming list of entities into array for select boxes.
|
2016-10-04 17:58:56 +02:00
|
|
|
*
|
2016-10-03 18:32:25 +02:00
|
|
|
* @param array $data
|
|
|
|
* The data array
|
|
|
|
* @param string $key_name
|
|
|
|
* name of the column to use as id/key
|
|
|
|
* @param string $value_name
|
|
|
|
* name of the column to use as displayed value
|
|
|
|
*/
|
|
|
|
function select_array($data, $key_name, $value_name) {
|
|
|
|
$ret = [];
|
|
|
|
foreach ($data as $value) {
|
|
|
|
$ret[$value[$key_name]] = $value[$value_name];
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2016-10-02 23:00:01 +02:00
|
|
|
/**
|
|
|
|
* Returns an int[] from given request param name.
|
|
|
|
*
|
|
|
|
* @param String $name
|
|
|
|
* Name of the request param
|
2016-11-15 22:00:17 +01:00
|
|
|
* @param array<int> $default
|
|
|
|
* Default return value, if param is not set
|
2016-10-02 23:00:01 +02:00
|
|
|
*/
|
2016-11-15 22:00:17 +01:00
|
|
|
function check_request_int_array($name, $default = []) {
|
2016-10-02 23:00:01 +02:00
|
|
|
if (isset($_REQUEST[$name]) && is_array($_REQUEST[$name])) {
|
|
|
|
return array_filter($_REQUEST[$name], 'is_numeric');
|
|
|
|
}
|
2016-11-15 22:00:17 +01:00
|
|
|
return $default;
|
2016-10-02 23:00:01 +02:00
|
|
|
}
|
|
|
|
|
2016-09-27 17:24:18 +02:00
|
|
|
/**
|
|
|
|
* Checks if given request item (name) can be parsed to a date.
|
|
|
|
* If not parsable, given error message is put into msg() and null is returned.
|
|
|
|
*
|
|
|
|
* @param string $input
|
|
|
|
* String to be parsed into a date.
|
|
|
|
* @param string $error_message
|
|
|
|
* the error message displayed if $input is not parsable
|
|
|
|
* @param boolean $null_allowed
|
|
|
|
* is a null value allowed?
|
|
|
|
* @return ValidationResult containing the parsed date
|
|
|
|
*/
|
|
|
|
function check_request_date($name, $error_message = null, $null_allowed = false) {
|
2016-09-29 11:28:42 +02:00
|
|
|
if (! isset($_REQUEST[$name])) {
|
2016-09-27 17:24:18 +02:00
|
|
|
return new ValidationResult($null_allowed, null);
|
2016-09-29 11:28:42 +02:00
|
|
|
}
|
2016-09-27 17:24:18 +02:00
|
|
|
return check_date($_REQUEST[$name], $error_message, $null_allowed);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if given string can be parsed to a date.
|
|
|
|
* If not parsable, given error message is put into msg() and null is returned.
|
|
|
|
*
|
|
|
|
* @param string $input
|
|
|
|
* String to be parsed into a date.
|
|
|
|
* @param string $error_message
|
|
|
|
* the error message displayed if $input is not parsable
|
|
|
|
* @param boolean $null_allowed
|
|
|
|
* is a null value allowed?
|
|
|
|
* @return ValidationResult containing the parsed date
|
|
|
|
*/
|
|
|
|
function check_date($input, $error_message = null, $null_allowed = false) {
|
2016-11-24 09:25:17 +01:00
|
|
|
if ($tmp = parse_date("Y-m-d H:i", trim($input) . " 00:00")) {
|
2016-10-04 17:58:56 +02:00
|
|
|
return new ValidationResult(true, $tmp);
|
2016-09-29 11:28:42 +02:00
|
|
|
}
|
|
|
|
if ($null_allowed) {
|
2016-09-27 17:24:18 +02:00
|
|
|
return new ValidationResult(true, null);
|
2016-09-29 11:28:42 +02:00
|
|
|
}
|
2016-09-27 17:24:18 +02:00
|
|
|
|
|
|
|
error($error_message);
|
|
|
|
return new ValidationResult(false, null);
|
|
|
|
}
|
|
|
|
|
2011-12-21 22:20:06 +01:00
|
|
|
/**
|
2016-11-11 17:00:51 +01:00
|
|
|
* Returns REQUEST value filtered or default value (null) if not set.
|
2011-12-21 22:20:06 +01:00
|
|
|
*/
|
2016-11-11 17:00:51 +01:00
|
|
|
function strip_request_item($name, $default_value = null) {
|
|
|
|
if (isset($_REQUEST[$name])) {
|
|
|
|
return strip_item($_REQUEST[$name]);
|
|
|
|
}
|
|
|
|
return $default_value;
|
2011-06-02 23:41:50 +02:00
|
|
|
}
|
|
|
|
|
2011-12-21 22:20:06 +01:00
|
|
|
/**
|
2013-10-13 00:52:44 +02:00
|
|
|
* Testet, ob der angegebene REQUEST Wert ein Integer ist, bzw.
|
|
|
|
* eine ID sein könnte.
|
2011-12-21 22:20:06 +01:00
|
|
|
*/
|
|
|
|
function test_request_int($name) {
|
2016-08-22 20:40:31 +02:00
|
|
|
if (isset($_REQUEST[$name])) {
|
2013-10-13 00:52:44 +02:00
|
|
|
return preg_match("/^[0-9]*$/", $_REQUEST[$name]);
|
2016-08-22 20:40:31 +02:00
|
|
|
}
|
2013-10-13 00:52:44 +02:00
|
|
|
return false;
|
2011-12-21 22:20:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gibt den gefilterten REQUEST Wert mit Zeilenumbrüchen zurück
|
|
|
|
*/
|
2016-11-17 13:03:28 +01:00
|
|
|
function strip_request_item_nl($name, $default_value = null) {
|
|
|
|
if (isset($_REQUEST[$name])) {
|
|
|
|
return preg_replace("/([^\p{L}\p{S}\p{P}\p{Z}\p{N}+\n]{1,})/ui", '', strip_tags($_REQUEST[$name]));
|
|
|
|
}
|
|
|
|
return $default_value;
|
2011-12-21 22:20:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Entfernt unerwünschte Zeichen
|
|
|
|
*/
|
|
|
|
function strip_item($item) {
|
2013-10-13 00:52:44 +02:00
|
|
|
return preg_replace("/([^\p{L}\p{S}\p{P}\p{Z}\p{N}+]{1,})/ui", '', strip_tags($item));
|
2011-12-21 22:20:06 +01:00
|
|
|
}
|
|
|
|
|
2011-12-26 15:55:17 +01:00
|
|
|
/**
|
|
|
|
* Überprüft eine E-Mail-Adresse.
|
|
|
|
*/
|
|
|
|
function check_email($email) {
|
2015-08-26 15:19:22 +02:00
|
|
|
return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
|
2011-12-26 15:55:17 +01:00
|
|
|
}
|
|
|
|
|
2011-06-02 23:41:50 +02:00
|
|
|
?>
|