1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Misc functions holder |
4
|
|
|
* |
5
|
|
|
* @category Common |
6
|
|
|
* |
7
|
|
|
* @author Vitex <[email protected]> |
8
|
|
|
* @copyright 2019 [email protected] (G) |
9
|
|
|
* @license https://opensource.org/licenses/MIT GPL-2 |
10
|
|
|
* |
11
|
|
|
* PHP 7 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Ease; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Description of Functions |
18
|
|
|
* |
19
|
|
|
* @author Vítězslav Dvořák <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Functions |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Returns PATH modified for current operating system. |
25
|
|
|
* |
26
|
|
|
* @param string $path |
27
|
|
|
* |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
public static function sysFilename($path) |
31
|
|
|
{ |
32
|
|
|
return str_replace(['\\', '/'], constant('DIRECTORY_SEPARATOR'), $path); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Add params to url |
40
|
|
|
* |
41
|
|
|
* @param string $url originall url |
42
|
|
|
* @param array $addParams value to add |
43
|
|
|
* @param boolean $override replace already existing values ? |
44
|
|
|
* |
45
|
|
|
* @return string url with parameters added |
46
|
|
|
*/ |
47
|
|
|
public static function addUrlParams($url, $addParams, $override = false) |
48
|
|
|
{ |
49
|
|
|
$urlParts = parse_url($url); |
50
|
|
|
$urlFinal = ''; |
51
|
|
|
if (array_key_exists('scheme', $urlParts)) { |
52
|
|
|
$urlFinal .= $urlParts['scheme'].'://'.$urlParts['host']; |
53
|
|
|
} |
54
|
|
|
if (array_key_exists('port', $urlParts)) { |
55
|
|
|
$urlFinal .= ':'.$urlParts['port']; |
56
|
|
|
} |
57
|
|
|
if (array_key_exists('path', $urlParts)) { |
58
|
|
|
$urlFinal .= $urlParts['path']; |
59
|
|
|
} |
60
|
|
|
if (array_key_exists('query', $urlParts)) { |
61
|
|
|
parse_str($urlParts['query'], $queryUrlParams); |
62
|
|
|
$urlParams = $override ? array_merge($queryUrlParams, $addParams) : array_merge($addParams, |
63
|
|
|
$queryUrlParams); |
64
|
|
|
} else { |
65
|
|
|
$urlParams = $addParams; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (!empty($urlParams)) { |
69
|
|
|
$urlFinal .= '?'; |
70
|
|
|
if (is_array($urlParams)) { |
|
|
|
|
71
|
|
|
$urlFinal .= http_build_query($urlParams); |
72
|
|
|
} else { |
73
|
|
|
$urlFinal .= $urlParams; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
return $urlFinal; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Turn all URLs in clickable links. |
81
|
|
|
* |
82
|
|
|
* @author Arnold Daniels <[email protected]> |
83
|
|
|
* |
84
|
|
|
* @param string $value |
85
|
|
|
* @param array $protocols http/https, ftp, mail, twitter |
86
|
|
|
* @param array $attributes |
87
|
|
|
* @param string $mode normal or all |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public static function linkify($value, $protocols = array('http', 'mail'), |
92
|
|
|
array $attributes = array()) |
93
|
|
|
{ |
94
|
|
|
// Link attributes |
95
|
|
|
$attr = ''; |
96
|
|
|
foreach ($attributes as $key => $val) { |
97
|
|
|
$attr = ' '.$key.'="'.htmlentities($val).'"'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$links = array(); |
101
|
|
|
|
102
|
|
|
// Extract existing links and tags |
103
|
|
|
$value = preg_replace_callback('~(<a .*?>.*?</a>|<.*?>)~i', |
104
|
|
|
function ($match) use (&$links) { |
105
|
|
|
return '<'.array_push($links, $match[1]).'>'; |
106
|
|
|
}, $value); |
107
|
|
|
|
108
|
|
|
// Extract text links for each protocol |
109
|
|
|
foreach ((array) $protocols as $protocol) { |
110
|
|
|
switch ($protocol) { |
111
|
|
|
case 'http': |
112
|
|
|
case 'https': $value = preg_replace_callback('~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i', |
113
|
|
|
function ($match) use ($protocol, &$links, $attr) { |
114
|
|
|
if ($match[1]) $protocol = $match[1]; |
115
|
|
|
$link = $match[2] ?: $match[3]; |
116
|
|
|
return '<'.array_push($links, |
117
|
|
|
"<a $attr href=\"$protocol://$link\">$link</a>").'>'; |
118
|
|
|
}, $value); |
119
|
|
|
break; |
120
|
|
|
case 'mail': $value = preg_replace_callback('~([^\s<]+?@[^\s<]+?\.[^\s<]+)(?<![\.,:])~', |
121
|
|
|
function ($match) use (&$links, $attr) { |
122
|
|
|
return '<'.array_push($links, |
123
|
|
|
"<a $attr href=\"mailto:{$match[1]}\">{$match[1]}</a>").'>'; |
124
|
|
|
}, $value); |
125
|
|
|
break; |
126
|
|
|
default: $value = preg_replace_callback('~'.preg_quote($protocol, |
127
|
|
|
'~').'://([^\s<]+?)(?<![\.,:])~i', |
128
|
|
|
function ($match) use ($protocol, &$links, $attr) { |
129
|
|
|
return '<'.array_push($links, |
130
|
|
|
"<a $attr href=\"$protocol://{$match[1]}\">{$match[1]}</a>").'>'; |
131
|
|
|
}, $value); |
132
|
|
|
break; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// Insert all link |
137
|
|
|
return preg_replace_callback('/<(\d+)>/', |
138
|
|
|
function ($match) use (&$links) { |
139
|
|
|
return $links[$match[1] - 1]; |
140
|
|
|
}, $value); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Move data field $columName from $sourceArray to $destinationArray. |
145
|
|
|
* |
146
|
|
|
* @param array $sourceArray source |
147
|
|
|
* @param array $destinationArray destination |
148
|
|
|
* @param string $columName item to move |
149
|
|
|
*/ |
150
|
|
|
public static function divDataArray(&$sourceArray, &$destinationArray, |
151
|
|
|
$columName) |
152
|
|
|
{ |
153
|
|
|
$result = false; |
154
|
|
|
if (array_key_exists($columName, $sourceArray)) { |
155
|
|
|
$destinationArray[$columName] = $sourceArray[$columName]; |
156
|
|
|
unset($sourceArray[$columName]); |
157
|
|
|
|
158
|
|
|
$result = true; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $result; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Test for associative array. |
166
|
|
|
* |
167
|
|
|
* @param array $arr |
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
|
|
public static function isAssoc(array $arr) |
172
|
|
|
{ |
173
|
|
|
return array_keys($arr) !== range(0, count($arr) - 1); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Odstraní z textu diakritiku. |
178
|
|
|
* |
179
|
|
|
* @param string $text |
180
|
|
|
*/ |
181
|
|
|
public static function rip($text) |
182
|
|
|
{ |
183
|
|
|
$convertTable = [ |
184
|
|
|
'ä' => 'a', |
185
|
|
|
'Ä' => 'A', |
186
|
|
|
'á' => 'a', |
187
|
|
|
'Á' => 'A', |
188
|
|
|
'à' => 'a', |
189
|
|
|
'À' => 'A', |
190
|
|
|
'ã' => 'a', |
191
|
|
|
'Ã' => 'A', |
192
|
|
|
'â' => 'a', |
193
|
|
|
'Â' => 'A', |
194
|
|
|
'č' => 'c', |
195
|
|
|
'Č' => 'C', |
196
|
|
|
'ć' => 'c', |
197
|
|
|
'Ć' => 'C', |
198
|
|
|
'ď' => 'd', |
199
|
|
|
'Ď' => 'D', |
200
|
|
|
'ě' => 'e', |
201
|
|
|
'Ě' => 'E', |
202
|
|
|
'é' => 'e', |
203
|
|
|
'É' => 'E', |
204
|
|
|
'ë' => 'e', |
205
|
|
|
'Ë' => 'E', |
206
|
|
|
'è' => 'e', |
207
|
|
|
'È' => 'E', |
208
|
|
|
'ê' => 'e', |
209
|
|
|
'Ê' => 'E', |
210
|
|
|
'í' => 'i', |
211
|
|
|
'Í' => 'I', |
212
|
|
|
'ï' => 'i', |
213
|
|
|
'Ï' => 'I', |
214
|
|
|
'ì' => 'i', |
215
|
|
|
'Ì' => 'I', |
216
|
|
|
'î' => 'i', |
217
|
|
|
'Î' => 'I', |
218
|
|
|
'ľ' => 'l', |
219
|
|
|
'Ľ' => 'L', |
220
|
|
|
'ĺ' => 'l', |
221
|
|
|
'Ĺ' => 'L', |
222
|
|
|
'ń' => 'n', |
223
|
|
|
'Ń' => 'N', |
224
|
|
|
'ň' => 'n', |
225
|
|
|
'Ň' => 'N', |
226
|
|
|
'ñ' => 'n', |
227
|
|
|
'Ñ' => 'N', |
228
|
|
|
'ó' => 'o', |
229
|
|
|
'Ó' => 'O', |
230
|
|
|
'ö' => 'o', |
231
|
|
|
'Ö' => 'O', |
232
|
|
|
'ô' => 'o', |
233
|
|
|
'Ô' => 'O', |
234
|
|
|
'ò' => 'o', |
235
|
|
|
'Ò' => 'O', |
236
|
|
|
'õ' => 'o', |
237
|
|
|
'Õ' => 'O', |
238
|
|
|
'ő' => 'o', |
239
|
|
|
'Ő' => 'O', |
240
|
|
|
'ř' => 'r', |
241
|
|
|
'Ř' => 'R', |
242
|
|
|
'ŕ' => 'r', |
243
|
|
|
'Ŕ' => 'R', |
244
|
|
|
'š' => 's', |
245
|
|
|
'Š' => 'S', |
246
|
|
|
'ś' => 's', |
247
|
|
|
'Ś' => 'S', |
248
|
|
|
'ť' => 't', |
249
|
|
|
'Ť' => 'T', |
250
|
|
|
'ú' => 'u', |
251
|
|
|
'Ú' => 'U', |
252
|
|
|
'ů' => 'u', |
253
|
|
|
'Ů' => 'U', |
254
|
|
|
'ü' => 'u', |
255
|
|
|
'Ü' => 'U', |
256
|
|
|
'ù' => 'u', |
257
|
|
|
'Ù' => 'U', |
258
|
|
|
'ũ' => 'u', |
259
|
|
|
'Ũ' => 'U', |
260
|
|
|
'û' => 'u', |
261
|
|
|
'Û' => 'U', |
262
|
|
|
'ý' => 'y', |
263
|
|
|
'Ý' => 'Y', |
264
|
|
|
'ž' => 'z', |
265
|
|
|
'Ž' => 'Z', |
266
|
|
|
'ź' => 'z', |
267
|
|
|
'Ź' => 'Z', |
268
|
|
|
]; |
269
|
|
|
|
270
|
|
|
return iconv('UTF-8', 'ASCII//TRANSLIT', strtr($text, $convertTable)); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Encrypt. |
275
|
|
|
* Šifrování. |
276
|
|
|
* |
277
|
|
|
* @param string $textToEncrypt plaintext |
278
|
|
|
* @param string $encryptKey klíč |
279
|
|
|
* |
280
|
|
|
* @return string encrypted text |
281
|
|
|
*/ |
282
|
|
|
public static function easeEncrypt($textToEncrypt, $encryptKey) |
283
|
|
|
{ |
284
|
|
|
$encryption_key = base64_decode($encryptKey); |
285
|
|
|
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc')); |
286
|
|
|
$encrypted = openssl_encrypt($textToEncrypt, 'aes-256-cbc', |
287
|
|
|
$encryption_key, 0, $iv); |
288
|
|
|
return base64_encode($encrypted.'::'.$iv); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Decrypt |
293
|
|
|
* Dešifrování. |
294
|
|
|
* |
295
|
|
|
* @param string $textToDecrypt šifrovaný text |
296
|
|
|
* @param string $encryptKey šifrovací klíč |
297
|
|
|
* |
298
|
|
|
* @return string |
299
|
|
|
*/ |
300
|
|
|
public static function easeDecrypt($textToDecrypt, $encryptKey) |
301
|
|
|
{ |
302
|
|
|
$encryption_key = base64_decode($encryptKey); |
303
|
|
|
list($encrypted_data, $iv) = explode('::', |
304
|
|
|
base64_decode($textToDecrypt), 2); |
305
|
|
|
return openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, |
306
|
|
|
0, $iv); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Generování náhodného čísla. |
311
|
|
|
* |
312
|
|
|
* @param int $minimal |
313
|
|
|
* @param int $maximal |
314
|
|
|
* |
315
|
|
|
* @return float |
316
|
|
|
*/ |
317
|
|
|
public static function randomNumber($minimal = null, $maximal = null) |
318
|
|
|
{ |
319
|
|
|
mt_srand((float) microtime() * 1000000); |
|
|
|
|
320
|
|
|
if (isset($minimal) && isset($maximal)) { |
321
|
|
|
if ($minimal >= $maximal) { |
322
|
|
|
$rand = false; |
323
|
|
|
} else { |
324
|
|
|
$rand = mt_rand($minimal, $maximal); |
325
|
|
|
} |
326
|
|
|
} else { |
327
|
|
|
$rand = mt_rand(); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
return $rand; |
|
|
|
|
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Vrací náhodný řetězec dané délky. |
335
|
|
|
* |
336
|
|
|
* @param int $length |
337
|
|
|
* |
338
|
|
|
* @return string |
339
|
|
|
*/ |
340
|
|
|
public static function randomString($length = 6) |
341
|
|
|
{ |
342
|
|
|
return substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), |
343
|
|
|
0, $length); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Rekurzivně překóduje pole. |
348
|
|
|
* |
349
|
|
|
* @param string $in_charset |
350
|
|
|
* @param string $out_charset |
351
|
|
|
* @param array $arr originální pole |
352
|
|
|
* |
353
|
|
|
* @return array překódované pole |
354
|
|
|
*/ |
355
|
|
|
public static function recursiveIconv($in_charset, $out_charset, $arr) |
356
|
|
|
{ |
357
|
|
|
if (!is_array($arr)) { |
|
|
|
|
358
|
|
|
return \iconv($in_charset, $out_charset, $arr); |
359
|
|
|
} |
360
|
|
|
$ret = $arr; |
361
|
|
|
|
362
|
|
|
array_walk_recursive($ret, '\Ease\Functions::arrayIconv', [$in_charset, $out_charset, $arr] ); |
363
|
|
|
|
364
|
|
|
return $ret; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Pomocná funkce pro překódování vícerozměrného pole. |
369
|
|
|
* |
370
|
|
|
* @see recursiveIconv |
371
|
|
|
* |
372
|
|
|
* @param mixed $val |
373
|
|
|
* @param string $key |
374
|
|
|
* @param mixed $encodings |
375
|
|
|
*/ |
376
|
|
|
public static function arrayIconv(&$val, /** @scrutinizer ignore-unused */ $key, $encodings) |
377
|
|
|
{ |
378
|
|
|
$val = iconv($encodings[0], $encodings[1], $val); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Zobrazí velikost souboru v srozumitelném tvaru. |
383
|
|
|
* |
384
|
|
|
* @param int $filesize bytů |
385
|
|
|
* |
386
|
|
|
* @return string |
387
|
|
|
*/ |
388
|
|
|
public static function humanFilesize($filesize) |
389
|
|
|
{ |
390
|
|
|
if (is_numeric($filesize)) { |
|
|
|
|
391
|
|
|
$decr = 1024; |
392
|
|
|
$step = 0; |
393
|
|
|
$prefix = ['Byte', 'KB', 'MB', 'GB', 'TB', 'PB']; |
394
|
|
|
|
395
|
|
|
while (($filesize / $decr) > 0.9) { |
396
|
|
|
$filesize = $filesize / $decr; |
397
|
|
|
++$step; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
return round($filesize, 2).' '.$prefix[$step]; |
401
|
|
|
} else { |
402
|
|
|
return 'NaN'; |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Reindex Array by given key. |
408
|
|
|
* |
409
|
|
|
* @param array $data array to reindex |
410
|
|
|
* @param string $indexBy one of columns in array |
411
|
|
|
* |
412
|
|
|
* @return array |
413
|
|
|
*/ |
414
|
|
|
public static function reindexArrayBy($data, $indexBy = null) |
415
|
|
|
{ |
416
|
|
|
$reindexedData = []; |
417
|
|
|
|
418
|
|
|
foreach ($data as $dataID => $data) { |
|
|
|
|
419
|
|
|
if (array_key_exists($indexBy, $data)) { |
420
|
|
|
$reindexedData[$data[$indexBy]] = $data; |
421
|
|
|
} else { |
422
|
|
|
throw new \Exception(sprintf('Data row does not contain column %s for reindexing', |
423
|
|
|
$indexBy)); |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
return $reindexedData; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Filter Only letters from string. |
432
|
|
|
* Pouze malé a velké písmena. |
433
|
|
|
* |
434
|
|
|
* @return string text bez zvláštních znaků |
435
|
|
|
*/ |
436
|
|
|
public static function lettersOnly($text) |
437
|
|
|
{ |
438
|
|
|
return preg_replace('/[^(a-zA-Z0-9)]*/', '', $text); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Confirm that string is serialized |
443
|
|
|
* |
444
|
|
|
* @param string $data |
445
|
|
|
* |
446
|
|
|
* @return boolean |
447
|
|
|
*/ |
448
|
|
|
public static function isSerialized($data) |
449
|
|
|
{ |
450
|
|
|
// if it isn't a string, it isn't serialized |
451
|
|
|
if (!is_string($data)) return false; |
|
|
|
|
452
|
|
|
$data = trim($data); |
453
|
|
|
if ('N;' == $data) return true; |
454
|
|
|
if (!preg_match('/^([adObis]):/', $data, $badions)) return false; |
455
|
|
|
switch ($badions[1]) { |
456
|
|
|
case 'a' : |
457
|
|
|
case 'O' : |
458
|
|
|
case 's' : |
459
|
|
|
if (preg_match("/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data)) |
460
|
|
|
return true; |
461
|
|
|
break; |
462
|
|
|
case 'b' : |
463
|
|
|
case 'i' : |
464
|
|
|
case 'd' : |
465
|
|
|
if (preg_match("/^{$badions[1]}:[0-9.E-]+;\$/", $data)) |
466
|
|
|
return true; |
467
|
|
|
break; |
468
|
|
|
} |
469
|
|
|
return false; |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* Get Classname without namespace prefix |
474
|
|
|
* |
475
|
|
|
* @param object $object |
476
|
|
|
* |
477
|
|
|
* @return string |
478
|
|
|
*/ |
479
|
|
|
static public function baseClassName($object) |
480
|
|
|
{ |
481
|
|
|
return is_object($object) ? basename(str_replace('\\', '/', |
482
|
|
|
get_class($object))) : null; |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
} |
486
|
|
|
|