1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* #################### |
5
|
|
|
* ### VALIDATE ### |
6
|
|
|
* #################### |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @param string $email |
11
|
|
|
* @return bool |
12
|
|
|
*/ |
13
|
|
|
function is_email(string $email): bool |
14
|
|
|
{ |
15
|
|
|
return filter_var($email, FILTER_VALIDATE_EMAIL); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param string $password |
20
|
|
|
* @return bool |
21
|
|
|
*/ |
22
|
|
|
function is_passwd(string $password): bool |
23
|
|
|
{ |
24
|
|
|
if (password_get_info($password)['algo'] || (mb_strlen($password) >= CONF_PASSWD_MIN_LEN && mb_strlen($password) <= CONF_PASSWD_MAX_LEN)) { |
25
|
|
|
return true; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return false; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* ################## |
33
|
|
|
* ### STRING ### |
34
|
|
|
* ################## |
35
|
|
|
*/ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $string |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
function str_slug(string $string): string |
42
|
|
|
{ |
43
|
|
|
$string = filter_var(mb_strtolower($string), FILTER_SANITIZE_STRIPPED); |
44
|
|
|
$formats = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜüÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿRr"!@#$%&*()_-+={[}]/?;:.,\\\'<>°ºª'; |
45
|
|
|
$replace = 'aaaaaaaceeeeiiiidnoooooouuuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyRr '; |
46
|
|
|
|
47
|
|
|
$slug = str_replace( |
48
|
|
|
["-----", "----", "---", "--"], |
49
|
|
|
"-", |
50
|
|
|
str_replace( |
51
|
|
|
" ", |
52
|
|
|
"-", |
53
|
|
|
trim(strtr(utf8_decode($string), utf8_decode($formats), $replace)) |
54
|
|
|
) |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
return $slug; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $string |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
function str_studly_case(string $string): string |
65
|
|
|
{ |
66
|
|
|
$string = str_slug($string); |
67
|
|
|
$studlyCase = str_replace( |
68
|
|
|
" ", |
69
|
|
|
"", |
70
|
|
|
mb_convert_case(str_replace("-", " ", $string), MB_CASE_TITLE) |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
return $studlyCase; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $string |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
function str_camel_case(string $string): string |
81
|
|
|
{ |
82
|
|
|
return lcfirst(str_studly_case($string)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $string |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
function str_title(string $string): string |
90
|
|
|
{ |
91
|
|
|
return mb_convert_case(filter_var($string, FILTER_SANITIZE_SPECIAL_CHARS), MB_CASE_TITLE); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $string |
96
|
|
|
* @param int $limit |
97
|
|
|
* @param string $pointer |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
function str_limit_words(string $string, int $limit, string $pointer = "..."): string |
101
|
|
|
{ |
102
|
|
|
$string = trim(filter_var($string, FILTER_SANITIZE_SPECIAL_CHARS)); |
103
|
|
|
$arrWords = explode(" ", $string); |
104
|
|
|
$numWords = count($arrWords); |
105
|
|
|
|
106
|
|
|
if ($numWords < $limit) { |
107
|
|
|
return $string; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$words = implode(" ", array_slice($arrWords, 0, $limit)); |
111
|
|
|
return "{$words}{$pointer}"; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $string |
116
|
|
|
* @param int $limit |
117
|
|
|
* @param string $pointer |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
function str_limit_chars(string $string, int $limit, string $pointer = "..."): string |
121
|
|
|
{ |
122
|
|
|
$string = trim(filter_var($string, FILTER_SANITIZE_SPECIAL_CHARS)); |
123
|
|
|
if (mb_strlen($string) <= $limit) { |
124
|
|
|
return $string; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$chars = mb_substr($string, 0, mb_strrpos(mb_substr($string, 0, $limit), " ")); |
128
|
|
|
return "{$chars}{$pointer}"; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $text |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
function base64UrlEncode(string $text): string |
136
|
|
|
{ |
137
|
|
|
return str_replace( |
138
|
|
|
['+', '/', '='], |
139
|
|
|
['-', '_', ''], |
140
|
|
|
base64_encode($text) |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* ############### |
146
|
|
|
* ### URL ### |
147
|
|
|
* ############### |
148
|
|
|
*/ |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $path |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
function url(string $path = null): string |
155
|
|
|
{ |
156
|
|
|
if (strpos($_SERVER["HTTP_HOST"], "localhost")) { |
157
|
|
|
if ($path) { |
158
|
|
|
return CONF_URL_TEST . "/" . ($path[0] == "/" ? mb_substr($path, 1) : $path); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return CONF_URL_TEST; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if ($path) { |
165
|
|
|
return CONF_URL_BASE . "/" . ($path[0] == "/" ? mb_substr($path, 1) : $path); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return CONF_URL_BASE; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
function url_back(string $path = null): string |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
return ($_SERVER['HTTP_REFERER'] ?? url()); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string $url |
178
|
|
|
*/ |
179
|
|
|
function redirect(string $url): void |
180
|
|
|
{ |
181
|
|
|
header("HTTP/1.1 302 Redirect"); |
182
|
|
|
if (filter_var($url, FILTER_VALIDATE_URL)) { |
183
|
|
|
header("Location: {$url}"); |
184
|
|
|
exit; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (filter_input(INPUT_GET, "route", FILTER_DEFAULT) != $url) { |
188
|
|
|
$location = url($url); |
189
|
|
|
header("Location: {$location}"); |
190
|
|
|
exit; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* ################ |
196
|
|
|
* ### DATE ### |
197
|
|
|
* ################ |
198
|
|
|
*/ |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param string $date |
202
|
|
|
* @param string $format |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
|
|
function date_fmt(string $date = "now", string $format = "d/m/Y H\hi"): string |
206
|
|
|
{ |
207
|
|
|
return (new DateTime($date))->format($format); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string $date |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
function date_fmt_br(string $date = "now"): string |
215
|
|
|
{ |
216
|
|
|
return (new DateTime($date))->format(CONF_DATE_BR); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param string $date |
221
|
|
|
* @return string |
222
|
|
|
*/ |
223
|
|
|
function date_fmt_app(string $date = "now"): string |
224
|
|
|
{ |
225
|
|
|
return (new DateTime($date))->format(CONF_DATE_APP); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* #################### |
230
|
|
|
* ### PASSWORD ### |
231
|
|
|
* #################### |
232
|
|
|
*/ |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param string $password |
236
|
|
|
* @return string |
237
|
|
|
*/ |
238
|
|
|
function passwd(string $password): string |
239
|
|
|
{ |
240
|
|
|
if (!empty(password_get_info($password)['algo'])) { |
241
|
|
|
return $password; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return password_hash($password, CONF_PASSWD_ALGO, CONF_PASSWD_OPTION); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param string $password |
249
|
|
|
* @param string $hash |
250
|
|
|
* @return bool |
251
|
|
|
*/ |
252
|
|
|
function passwd_verify(string $password, string $hash): bool |
253
|
|
|
{ |
254
|
|
|
return password_verify($password, $hash); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param string $hash |
259
|
|
|
* @return bool |
260
|
|
|
*/ |
261
|
|
|
function passwd_rehash(string $hash): bool |
262
|
|
|
{ |
263
|
|
|
return password_needs_rehash($hash, CONF_PASSWD_ALGO, CONF_PASSWD_OPTION); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* #################### |
268
|
|
|
* ### HTTP ### |
269
|
|
|
* #################### |
270
|
|
|
*/ |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param int $statusCode |
274
|
|
|
* @return string |
275
|
|
|
*/ |
276
|
|
|
function handleStatusCode(int $statusCode): string |
277
|
|
|
{ |
278
|
|
|
$statsCodeHashTable = [ |
279
|
|
|
100 => 'Continue', |
280
|
|
|
101 => 'Switching Protocols', |
281
|
|
|
200 => 'OK', |
282
|
|
|
201 => 'Created', |
283
|
|
|
202 => 'Accepted', |
284
|
|
|
203 => 'Non-Authoritative Information', |
285
|
|
|
204 => 'No Content', |
286
|
|
|
205 => 'Reset Content', |
287
|
|
|
206 => 'Partial Content', |
288
|
|
|
300 => 'Multiple Choices', |
289
|
|
|
301 => 'Moved Permanently', |
290
|
|
|
302 => 'Moved Temporarily', |
291
|
|
|
303 => 'See Other', |
292
|
|
|
304 => 'Not Modified', |
293
|
|
|
305 => 'Use Proxy', |
294
|
|
|
400 => 'Bad Request', |
295
|
|
|
401 => 'Unauthorized', |
296
|
|
|
402 => 'Payment Required', |
297
|
|
|
403 => 'Forbidden', |
298
|
|
|
404 => 'Not Found', |
299
|
|
|
405 => 'Method Not Allowed', |
300
|
|
|
406 => 'Not Acceptable', |
301
|
|
|
407 => 'Proxy Authentication Required', |
302
|
|
|
408 => 'Request Time-out', |
303
|
|
|
409 => 'Conflict', |
304
|
|
|
410 => 'Gone', |
305
|
|
|
411 => 'Length Required', |
306
|
|
|
412 => 'Precondition Failed', |
307
|
|
|
413 => 'Request Entity Too Large', |
308
|
|
|
414 => 'Request-URI Too Large', |
309
|
|
|
415 => 'Unsupported Media Type', |
310
|
|
|
500 => 'Internal Server Error', |
311
|
|
|
501 => 'Not Implemented', |
312
|
|
|
502 => 'Bad Gateway', |
313
|
|
|
503 => 'Service Unavailable', |
314
|
|
|
504 => 'Gateway Time-out', |
315
|
|
|
505 => 'HTTP Version not supported' |
316
|
|
|
]; |
317
|
|
|
|
318
|
|
|
return $statsCodeHashTable[$statusCode] ?? 'Not Implemented'; |
319
|
|
|
} |
320
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.