engelsystem/includes/sys_page.php

221 lines
5.6 KiB
PHP
Raw Normal View History

2011-06-02 00:48:29 +02:00
<?php
2016-11-18 08:20:17 +01:00
use Engelsystem\ValidationResult;
/**
* Provide page/request helper functions
*/
/**
* Parse a date from da day and a time textfield.
*
2017-01-03 03:22:48 +01:00
* @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
* @return int|null
*/
2017-01-02 03:57:23 +01:00
function check_request_datetime($date_name, $time_name, $allowed_days, $default_value)
{
2017-01-03 14:12:17 +01:00
$time = date('H:i', $default_value);
$day = date('Y-m-d', $default_value);
$request = request();
2017-01-02 15:43:36 +01:00
if ($request->has($time_name) && preg_match('#^\d{1,2}:\d\d$#', trim($request->input($time_name)))) {
$time = trim($request->input($time_name));
2017-01-02 03:57:23 +01:00
}
if ($request->has($date_name) && in_array($request->input($date_name), $allowed_days)) {
$day = $request->input($date_name);
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-03 14:12:17 +01:00
return parse_date('Y-m-d H:i', $day . ' ' . $time);
}
/**
* Parse a date into unix timestamp
*
2017-01-03 03:22:48 +01:00
* @param string $pattern The date pattern (i.e. Y-m-d H:i)
* @param string $value The string to parse
* @return int|null The parsed unix timestamp
*/
2017-01-02 03:57:23 +01:00
function parse_date($pattern, $value)
{
$datetime = DateTime::createFromFormat($pattern, trim($value));
2018-01-14 17:47:26 +01:00
if (is_null($datetime)) {
2017-01-02 03:57:23 +01:00
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.
2017-01-03 03:22:48 +01:00
*
* @param string $url
2011-12-21 22:20:06 +01:00
*/
2017-01-02 03:57:23 +01:00
function redirect($url)
{
2017-01-03 14:12:17 +01:00
header('Location: ' . $url, true, 302);
raw_output('');
2011-12-21 22:20:06 +01:00
}
/**
* Echoes given output and dies.
2016-08-22 20:40:31 +02:00
*
2017-01-03 03:22:48 +01:00
* @param String $output String to display
*/
function raw_output($output = '')
2017-01-02 03:57:23 +01:00
{
echo $output;
die();
}
/**
* Helper function for transforming list of entities into array for select boxes.
*
2017-01-03 03:22:48 +01: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
*
* @return array
*/
2017-01-02 03:57:23 +01:00
function select_array($data, $key_name, $value_name)
{
$return = [];
2017-01-02 03:57:23 +01:00
foreach ($data as $value) {
$return[$value[$key_name]] = $value[$value_name];
2017-01-02 03:57:23 +01:00
}
return $return;
}
2016-10-02 23:00:01 +02:00
/**
* Returns an int[] from given request param name.
*
2017-01-03 03:22:48 +01:00
* @param string $name Name of the request param
* @param array $default Default return value, if param is not set
* @return array
2016-10-02 23:00:01 +02:00
*/
2017-01-02 03:57:23 +01:00
function check_request_int_array($name, $default = [])
{
$request = request();
if ($request->has($name) && is_array($request->input($name))) {
return array_filter($request->input($name), 'is_numeric');
2017-01-02 03:57:23 +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.
*
2017-01-03 03:22:48 +01:00
* @param string $name 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?
2016-09-27 17:24:18 +02:00
* @return ValidationResult containing the parsed date
*/
2017-01-02 03:57:23 +01:00
function check_request_date($name, $error_message = null, $null_allowed = false)
{
$request = request();
if (!$request->has($name)) {
2017-01-02 03:57:23 +01:00
return new ValidationResult($null_allowed, null);
}
return check_date($request->input($name), $error_message, $null_allowed);
2016-09-27 17:24:18 +02:00
}
/**
* Checks if given string can be parsed to a date.
* If not parsable, given error message is put into msg() and null is returned.
*
2017-01-03 03:22:48 +01:00
* @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?
2016-09-27 17:24:18 +02:00
* @return ValidationResult containing the parsed date
*/
2017-01-02 03:57:23 +01:00
function check_date($input, $error_message = null, $null_allowed = false)
{
2017-01-03 14:12:17 +01:00
if ($tmp = parse_date('Y-m-d H:i', trim($input) . ' 00:00')) {
2017-01-02 03:57:23 +01:00
return new ValidationResult(true, $tmp);
}
if ($null_allowed) {
return new ValidationResult(true, null);
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
error($error_message);
return new ValidationResult(false, null);
2016-09-27 17:24:18 +02:00
}
2011-12-21 22:20:06 +01:00
/**
* Returns REQUEST value filtered or default value (null) if not set.
2017-01-03 03:22:48 +01:00
*
* @param string $name
* @param string $default_value
* @return mixed|null
2011-12-21 22:20:06 +01:00
*/
2017-01-02 03:57:23 +01:00
function strip_request_item($name, $default_value = null)
{
$request = request();
if ($request->has($name)) {
return strip_item($request->input($name));
2017-01-02 03:57:23 +01:00
}
return $default_value;
}
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.
2017-01-03 03:22:48 +01:00
*
* @param string $name
* @return int|false
2011-12-21 22:20:06 +01:00
*/
2017-01-02 03:57:23 +01:00
function test_request_int($name)
{
$input = request()->input($name);
2017-08-30 14:59:27 +02:00
if (is_null($input)) {
return false;
}
return preg_match('/^\d+$/', $input);
2011-12-21 22:20:06 +01:00
}
/**
* Gibt den gefilterten REQUEST Wert mit Zeilenumbrüchen zurück
2017-01-03 03:22:48 +01:00
*
* @param string $name
* @param mixed $default_value
* @return mixed
2011-12-21 22:20:06 +01:00
*/
2017-01-02 03:57:23 +01:00
function strip_request_item_nl($name, $default_value = null)
{
$request = request();
if ($request->has($name)) {
return preg_replace(
"/([^\p{L}\p{S}\p{P}\p{Z}\p{N}+\n]{1,})/ui",
'',
strip_tags($request->input($name))
);
2017-01-02 03:57:23 +01:00
}
return $default_value;
2011-12-21 22:20:06 +01:00
}
/**
* Entfernt unerwünschte Zeichen
2017-01-03 03:22:48 +01:00
*
* @param string $item
* @return string
2011-12-21 22:20:06 +01:00
*/
2017-01-02 03:57:23 +01:00
function strip_item($item)
{
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.
2017-01-03 03:22:48 +01:00
*
* @param string $email
* @return bool
2011-12-26 15:55:17 +01:00
*/
2017-01-02 03:57:23 +01:00
function check_email($email)
{
2017-01-02 15:43:36 +01:00
return (bool)filter_var($email, FILTER_VALIDATE_EMAIL);
2011-12-26 15:55:17 +01:00
}