summaryrefslogtreecommitdiff
path: root/includes/sys_page.php
blob: d00a9b70471b197dede4077479af54dad6470efe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php

use Carbon\Carbon;
use Engelsystem\Http\Exceptions\HttpTemporaryRedirect;
use Engelsystem\ValidationResult;

/**
 * Provide page/request helper functions
 */

/**
 * 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
 * @return int|null
 */
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);
    $request = request();

    if ($request->has($time_name) && preg_match('#^\d{1,2}:\d\d$#', trim($request->input($time_name)))) {
        $time = trim($request->input($time_name));
    }

    if ($request->has($date_name) && in_array($request->input($date_name), $allowed_days)) {
        $day = $request->input($date_name);
    }

    return parse_date('Y-m-d H:i', $day . ' ' . $time);
}

/**
 * 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 int|null The parsed unix timestamp
 */
function parse_date($pattern, $value)
{
    $datetime = DateTime::createFromFormat($pattern, trim($value));
    if (!$datetime) {
        return null;
    }

    return $datetime->getTimestamp();
}

/**
 * Leitet den Browser an die übergebene URL weiter und hält das Script an.
 *
 * @param string $url
 */
function throw_redirect($url)
{
    throw new HttpTemporaryRedirect($url);
}

/**
 * Echoes given output and dies.
 *
 * @param String $output String to display
 */
function raw_output($output = '')
{
    echo $output;
    die();
}

/**
 * Helper function for transforming list of entities into array for select boxes.
 *
 * @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
 */
function select_array($data, $key_name, $value_name)
{
    $return = [];
    foreach ($data as $value) {
        $return[$value[$key_name]] = $value[$value_name];
    }
    return $return;
}

/**
 * Returns an int[] from given request param name.
 *
 * @param string $name    Name of the request param
 * @param array  $default Default return value, if param is not set
 * @return array
 */
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');
    }
    return $default;
}

/**
 * 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  $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?
 * @return ValidationResult containing the parsed date
 */
function check_request_date($name, $error_message = null, $null_allowed = false)
{
    $request = request();
    if (!$request->has($name)) {
        return new ValidationResult($null_allowed, null);
    }
    return check_date($request->input($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)
{
    try {
        $time = Carbon::createFromFormat('Y-m-d', trim($input));
    } catch (InvalidArgumentException $e) {
        $time = null;
    }

    if ($time) {
        return new ValidationResult(true, $time);
    }

    if ($null_allowed) {
        return new ValidationResult(true, null);
    }

    error($error_message);
    return new ValidationResult(false, null);
}

/**
 * Returns REQUEST value filtered or default value (null) if not set.
 *
 * @param string $name
 * @param string $default_value
 * @return mixed|null
 */
function strip_request_item($name, $default_value = null)
{
    $request = request();
    if ($request->has($name)) {
        return strip_item($request->input($name));
    }
    return $default_value;
}

/**
 * Testet, ob der angegebene REQUEST Wert ein Integer ist, bzw.
 * eine ID sein könnte.
 *
 * @param string $name
 * @return int|false
 */
function test_request_int($name)
{
    $input = request()->input($name);
    if (is_null($input)) {
        return false;
    }

    return preg_match('/^\d+$/', $input);
}

/**
 * Gibt den gefilterten REQUEST Wert mit Zeilenumbrüchen zurück
 *
 * @param string $name
 * @param mixed  $default_value
 * @return mixed
 */
function strip_request_item_nl($name, $default_value = null)
{
    $request = request();
    if ($request->has($name)) {
        // Only allow letters, symbols, punctuation, separators, numbers and newlines without html tags
        return preg_replace(
            "/([^\p{L}\p{S}\p{P}\p{Z}\p{N}+\n]{1,})/ui",
            '',
            strip_tags($request->input($name))
        );
    }
    return $default_value;
}

/**
 * Entfernt unerwünschte Zeichen
 *
 * @param string $item
 * @return string
 */
function strip_item($item)
{
    // Only allow letters, symbols, punctuation, separators and numbers without html tags
    return preg_replace("/([^\p{L}\p{S}\p{P}\p{Z}\p{N}+]{1,})/ui", '', strip_tags($item));
}

/**
 * Validates an email address with support for IDN domain names.
 *
 * @param string $email
 * @return bool
 */
function check_email($email)
{
    // Convert the domain part from idn to ascii
    if(substr_count($email, '@') == 1) {
        list($name, $domain) = explode('@', $email);
        $domain = idn_to_ascii($domain, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
        $email = $name . '@' . $domain;
    }
    return (bool)filter_var($email, FILTER_VALIDATE_EMAIL);
}