2011-06-02 00:48:29 +02:00
|
|
|
<?php
|
2017-08-29 16:21:25 +02:00
|
|
|
|
2022-04-13 01:02:37 +02:00
|
|
|
use Engelsystem\Helpers\Carbon;
|
2019-09-08 02:25:49 +02:00
|
|
|
use Engelsystem\Http\Exceptions\HttpTemporaryRedirect;
|
2020-09-06 23:50:36 +02:00
|
|
|
use Engelsystem\Models\BaseModel;
|
2016-11-18 08:20:17 +01:00
|
|
|
use Engelsystem\ValidationResult;
|
2020-09-06 23:50:36 +02:00
|
|
|
use Illuminate\Support\Collection;
|
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.
|
|
|
|
*
|
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
|
2016-10-04 18:52:52 +02:00
|
|
|
*/
|
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);
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-07-18 21:38:53 +02: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
|
|
|
}
|
2017-07-18 21:38:53 +02: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);
|
2016-10-04 18:52:52 +02:00
|
|
|
}
|
|
|
|
|
2016-10-04 17:58:56 +02:00
|
|
|
/**
|
|
|
|
* 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
|
2016-10-04 17:58:56 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function parse_date($pattern, $value)
|
|
|
|
{
|
|
|
|
$datetime = DateTime::createFromFormat($pattern, trim($value));
|
2018-01-14 18:47:15 +01:00
|
|
|
if (!$datetime) {
|
2017-01-02 03:57:23 +01:00
|
|
|
return null;
|
|
|
|
}
|
2018-01-14 18:47:15 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return $datetime->getTimestamp();
|
2016-10-04 17:58:56 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2019-09-08 02:25:49 +02:00
|
|
|
function throw_redirect($url)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2019-09-08 02:25:49 +02:00
|
|
|
throw new HttpTemporaryRedirect($url);
|
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
|
|
|
*
|
2023-02-06 19:35:02 +01:00
|
|
|
* @param string $output String to display
|
2016-08-21 22:58:09 +02:00
|
|
|
*/
|
2017-01-21 13:58:53 +01:00
|
|
|
function raw_output($output = '')
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
|
|
|
echo $output;
|
|
|
|
die();
|
2016-08-21 22:58:09 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2020-09-06 23:50:36 +02:00
|
|
|
* @param array|Collection $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
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
2020-09-06 23:50:36 +02:00
|
|
|
* @return array|Collection
|
2016-10-03 18:32:25 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function select_array($data, $key_name, $value_name)
|
|
|
|
{
|
2020-09-06 23:50:36 +02:00
|
|
|
if ($data instanceof Collection) {
|
2022-12-01 18:31:48 +01:00
|
|
|
return $data->mapWithKeys(function (BaseModel $model) use ($key_name, $value_name) {
|
2020-09-06 23:50:36 +02:00
|
|
|
return [$model->{$key_name} => $model->{$value_name}];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-21 13:58:53 +01:00
|
|
|
$return = [];
|
2017-01-02 03:57:23 +01:00
|
|
|
foreach ($data as $value) {
|
2017-01-21 13:58:53 +01:00
|
|
|
$return[$value[$key_name]] = $value[$value_name];
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-21 13:58:53 +01:00
|
|
|
return $return;
|
2016-10-03 18:32:25 +02:00
|
|
|
}
|
|
|
|
|
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 = [])
|
|
|
|
{
|
2017-07-18 21:38:53 +02:00
|
|
|
$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.
|
|
|
|
*
|
2021-01-02 01:22:05 +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 bool $null_allowed is a null value allowed?
|
|
|
|
* @param bool $time_allowed is time allowed?
|
2016-09-27 17:24:18 +02:00
|
|
|
* @return ValidationResult containing the parsed date
|
|
|
|
*/
|
2021-01-02 01:22:05 +01:00
|
|
|
function check_request_date($name, $error_message = null, $null_allowed = false, $time_allowed = false)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
|
|
|
if (!$request->has($name)) {
|
2017-01-02 03:57:23 +01:00
|
|
|
return new ValidationResult($null_allowed, null);
|
|
|
|
}
|
2021-01-02 01:22:05 +01:00
|
|
|
return check_date($request->input($name), $error_message, $null_allowed, $time_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.
|
|
|
|
*
|
2021-01-02 01:22:05 +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 bool $null_allowed is a null value allowed?
|
|
|
|
* @param bool $time_allowed is time allowed?
|
2016-09-27 17:24:18 +02:00
|
|
|
* @return ValidationResult containing the parsed date
|
|
|
|
*/
|
2021-01-02 01:22:05 +01:00
|
|
|
function check_date($input, $error_message = null, $null_allowed = false, $time_allowed = false)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2022-04-13 01:02:37 +02:00
|
|
|
$trimmed_input = trim((string) $input);
|
|
|
|
|
2018-09-25 17:33:31 +02:00
|
|
|
try {
|
2022-04-13 01:02:37 +02:00
|
|
|
if ($time_allowed) {
|
|
|
|
$time = Carbon::createFromDatetime($trimmed_input);
|
|
|
|
} else {
|
|
|
|
$time = Carbon::createFromFormat('Y-m-d', $trimmed_input);
|
|
|
|
}
|
2018-09-25 17:33:31 +02:00
|
|
|
} catch (InvalidArgumentException $e) {
|
|
|
|
$time = null;
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2018-09-25 17:33:31 +02:00
|
|
|
|
|
|
|
if ($time) {
|
|
|
|
return new ValidationResult(true, $time);
|
|
|
|
}
|
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
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
|
|
|
/**
|
2016-11-11 17:00:51 +01:00
|
|
|
* Returns REQUEST value filtered or default value (null) if not set.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @param string $name
|
2022-06-16 23:00:56 +02:00
|
|
|
* @param string|null $default_value
|
2017-01-03 03:22:48 +01:00
|
|
|
* @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)
|
|
|
|
{
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
|
|
|
if ($request->has($name)) {
|
|
|
|
return strip_item($request->input($name));
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
return $default_value;
|
2011-06-02 23:41:50 +02:00
|
|
|
}
|
|
|
|
|
2022-07-20 19:08:51 +02:00
|
|
|
/**
|
|
|
|
* Returns REQUEST value or default value (null) if not set.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string|null $default_value
|
|
|
|
* @return mixed|null
|
|
|
|
*/
|
|
|
|
function strip_request_tags($name, $default_value = null)
|
|
|
|
{
|
|
|
|
$request = request();
|
|
|
|
if ($request->has($name)) {
|
|
|
|
return strip_tags($request->input($name));
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2017-08-29 16:21:25 +02:00
|
|
|
$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)
|
|
|
|
{
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
|
|
|
if ($request->has($name)) {
|
2018-12-28 22:32:36 +01:00
|
|
|
// Only allow letters, symbols, punctuation, separators, numbers and newlines without html tags
|
2017-08-29 16:21:25 +02:00
|
|
|
return preg_replace(
|
2022-06-16 23:00:56 +02:00
|
|
|
"/([^\p{L}\p{S}\p{P}\p{Z}\p{N}+\n]+)/ui",
|
2017-08-29 16:21:25 +02:00
|
|
|
'',
|
|
|
|
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)
|
|
|
|
{
|
2018-12-28 22:32:36 +01:00
|
|
|
// Only allow letters, symbols, punctuation, separators and numbers without html tags
|
2022-06-16 23:00:56 +02:00
|
|
|
return preg_replace("/([^\p{L}\p{S}\p{P}\p{Z}\p{N}+]+)/ui", '', strip_tags($item));
|
2011-12-21 22:20:06 +01:00
|
|
|
}
|
|
|
|
|
2011-12-26 15:55:17 +01:00
|
|
|
/**
|
2019-11-03 11:43:13 +01:00
|
|
|
* Validates an email address with support for IDN domain names.
|
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)
|
|
|
|
{
|
2019-11-03 11:43:13 +01:00
|
|
|
// Convert the domain part from idn to ascii
|
2022-10-18 19:15:22 +02:00
|
|
|
if (substr_count($email, '@') == 1) {
|
2019-11-03 11:43:13 +01:00
|
|
|
list($name, $domain) = explode('@', $email);
|
|
|
|
$domain = idn_to_ascii($domain, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
|
|
|
|
$email = $name . '@' . $domain;
|
|
|
|
}
|
2022-12-25 11:59:45 +01:00
|
|
|
return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
|
2011-12-26 15:55:17 +01:00
|
|
|
}
|