1 <?php
2
3 4 5 6 7 8 9 10 11 12
13
14 15 16 17 18 19 20 21
22
23 function ctl($controller = 'index', $action = 'index', $namespace = '')
24 {
25 if ($namespace != '')
26 {
27 $namespace = ucwords($namespace);
28 }
29
30 $ctl = $namespace . ucwords($controller . 'Controller') . ':' . $action;
31
32 return $ctl;
33 }
34
35 36 37 38 39 40 41 42
43
44 function controller($controller = 'index', $action = 'index', $namespace = '')
45 {
46 return ctl($controller, $action, $namespace);
47 }
48
49 50 51 52 53 54 55
56
57 function password($str, $salt)
58 {
59 return md5(md5($str . $salt) . $salt);
60 }
61
62 63 64 65 66 67 68
69
70 function encrypt($string, $key)
71 {
72 $encrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5(md5($key)), $string, MCRYPT_MODE_CBC, md5($key));
73
74 return base64_encode($encrypt);
75 }
76
77 78 79 80 81 82 83
84
85 function decrypt($string, $key)
86 {
87 return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5(md5($key)), base64_decode($string), MCRYPT_MODE_CBC, md5($key));
88 }
89
90 91 92 93 94 95
96
97 function imageDataUrl($file)
98 {
99 if (!file_exists($file))
100 {
101 throw new \InvalidArgumentException('File ' . $file . ' not found.');
102 }
103
104 $dataUrl = 'data:' . mime_content_type($file) . ';base64,' . base64_encode(file_get_contents($file));
105
106 return $dataUrl;
107 }
108
109 110 111 112 113 114 115
116
117 function phoneMaskCode($phone, $mask = '*****')
118 {
119 $phone = preg_replace('#(\d{3})\d{5}(\d{3})#', '${1}' . $mask . '${2}', $phone);
120
121 return $phone;
122 }
123
124 125 126 127 128 129 130 131 132 133 134 135 136 137
138
139 function randomCharacters($length, $characters = null)
140 {
141 if ( !is_int($length) )
142 {
143 throw new \InvalidArgumentException("random characters length must be integer.");
144 }
145
146 $characters = ($characters) ? $characters : 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789';
147 $random = '';
148
149 if ( function_exists('mb_substr') )
150 {
151 $len = mb_strlen($characters, 'utf-8');
152
153 for ($i = 0; $i < $length; $i++)
154 {
155 $random .= mb_substr($characters, mt_rand(0, $len - 1), 1, 'utf-8');
156 }
157 }
158
159 return $random;
160 }
161
162 163 164 165 166 167 168 169
170
171 function fileFormatSize($size, $decimals = 2, $blankSpace = true)
172 {
173 $units = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
174
175 for($i = 0; $size >= 1024 && $i <= count($units); $i++)
176 {
177 $size = $size / 1024;
178 }
179
180 return round($size, $decimals) . (($blankSpace) ? ' ' : '') . $units[$i];
181 }
182
183 184 185 186 187 188
189
190 function isJson($json)
191 {
192 return (json_decode($json)) ? true : false;
193 }
194
195 196 197 198 199 200
201
202 function replaceDirSeparator($dir)
203 {
204 $dir = str_replace(['/', '\\'], [DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR], $dir);
205
206 return $dir;
207 };
208
209 210 211 212 213 214 215
216
217 function getDirectoryItems($path, $onlyFiles = false)
218 {
219 $items = glob($path . '/*');
220
221 for ($i = 0, $count = count($items); $i < $count; $i++)
222 {
223 if ( is_dir($items[$i]) )
224 {
225 $push = glob($items[$i] . '/*');
226 $items = array_merge($items, $push);
227 }
228 }
229
230 if ( $onlyFiles )
231 {
232 $files = [];
233
234 for ($i = 0, $count = count($items); $i < $count; $i++)
235 {
236 if (!is_dir($items[$i]))
237 {
238 $files[] = realpath($items[$i]);
239 }
240 }
241
242 return $files;
243 }
244
245 $items = array_map('realpath', $items);
246
247 return $items;
248 }
249
250 251 252 253 254
255
256 function currentUrl()
257 {
258 $port = ($_SERVER["SERVER_PORT"] != '80') ? ':' . $_SERVER["SERVER_PORT"] : '';
259
260 $url = 'http' . ( !empty($_SERVER['HTTPS']) ? 's' : '') . '://';
261 $url .= $_SERVER["SERVER_NAME"] . $port . $_SERVER["REQUEST_URI"];
262
263 return $url;
264 }
265
266 267 268 269 270 271
272
273 function jsonToArray($json)
274 {
275 $array = [];
276
277 foreach ($json as $key => $val)
278 {
279 $array[$key] = (is_object($val)) ? jsonToArray($val): $val;
280 }
281
282 return $array;
283 }
284
285 286 287 288 289 290
291
292 function arrayToObject(array $array)
293 {
294
295
296 return new ArrayObject($array);
297 }
298
299 300 301 302 303 304
305
306 function shortUrl($url)
307 {
308 $code = sprintf('%u', crc32($url));
309 $shortUrl = '';
310
311 while ($code)
312 {
313 $mod = $code % 62;
314
315 if ($mod > 9 && $mod <= 35)
316 {
317 $mod = chr($mod + 55);
318 }
319 elseif ($mod > 35)
320 {
321 $mod = chr($mod + 61);
322 }
323
324 $shortUrl .= $mod;
325 $code = floor($code / 62);
326 }
327
328 return $shortUrl;
329 }
330
331 332 333 334 335 336 337 338 339 340
341
342 function html($title, $head = '', $body = '', $charset = 'utf-8', $lang = 'zh')
343 {
344 $html = [
345 '<!DOCTYPE html>',
346 '<html lang="' . $lang . '">',
347 '<head>',
348 '<mate charset="' . $charset .'">',
349 '<title>' . $title . '</title>',
350 (is_array($head)) ? implode("\n", $head) : $head,
351 '</head>',
352 '<body>',
353 (is_array($body)) ? implode("\n", $body) : $body,
354 '</body>',
355 '</html>'
356 ];
357
358 return implode("\n", $html);
359 }
360
361 362 363 364 365
366
367 function isMobile()
368 {
369 $regex = '/(android|symbian|mobile|smartphone|iemobile|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|midp|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos|ios|iphone|ipod|huawei|htc|haier|lenovo)/i';
370
371 return preg_match($regex, strtolower($_SERVER["HTTP_USER_AGENT"]));
372 }
373
374 375 376 377 378
379
380 function isTablet()
381 {
382 $regex = '/(tablet|ipad|playbook|kindle)|(android(?!.*(mobi|opera mini)))/i';
383
384 return preg_match($regex, strtolower($_SERVER['HTTP_USER_AGENT']) );
385 }
386
387 388 389 390 391 392
393
394 function detectDevice($device = 'iPhone')
395 {
396 return (stripos($_SERVER['HTTP_USER_AGENT'], $device) !== false);
397 }
398
399 400 401 402 403
404
405 function iPhone()
406 {
407 return detectDevice('iPhone');
408 }
409
410 411 412 413 414
415
416 function iPad()
417 {
418 return detectDevice('iPad');
419 }
420
421 422 423 424 425
426
427 function iPod()
428 {
429 return detectDevice('iPod');
430 }
431
432 433 434 435 436
437
438 function isAndroid()
439 {
440 return detectDevice('Android');
441 }
442
443 444 445 446 447
448
449 function isWebOS()
450 {
451 return detectDevice('webOS');
452 }
453
454 455 456 457 458
459
460 function isBlackberry()
461 {
462 return detectDevice('Blackberry');
463 }
464
465 466 467 468 469
470
471 function isWindows()
472 {
473 return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
474 }
475
476 477 478 479 480
481 function isMacOSX()
482 {
483 return (PHP_OS === 'Darwin');
484 }
485
486 487 488 489 490
491
492 function isLinux()
493 {
494 return (PHP_OS === 'Linux');
495 }
496
497 498 499 500 501
502
503 function isFreeBSD()
504 {
505 return (PHP_OS === 'FreeBSD');
506 }
507
508 509 510 511 512
513
514 function isUnix()
515 {
516 return (PHP_OS === 'Unix');
517 }
518
519 520 521 522 523
524
525 function isUnixLike()
526 {
527 return (DIRECTORY_SEPARATOR == '/');
528 }
529
530 531 532 533 534
535
536 function isApache()
537 {
538 return (stripos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false || function_exists('apache_get_version'));
539 }
540
541 542 543 544 545
546
547 function isNginx()
548 {
549 return (stripos($_SERVER["SERVER_SOFTWARE"], 'nginx') > -1);
550 }
551
552 553 554 555 556
557
558 function isIIS()
559 {
560 return (stripos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== false);
561 }
562
563 564 565 566
567
568 function isCli()
569 {
570 return strtoupper(substr(PHP_SAPI_NAME(), 0, 3) === 'CLI');
571 }
572
573 574 575 576 577
578
579 function isIE()
580 {
581 $isIE = (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Trident') !== false );
582
583 return ( $isIE && strpos($_SERVER['HTTP_USER_AGENT'], 'Win') !== false);
584 }
585
586 587 588 589 590
591
592 function isFirefox()
593 {
594 return detectDevice('Firefox');
595 }
596
597 598 599 600
601
602 function isChrome()
603 {
604 return detectDevice('Chrome');
605 }
606
607 608 609 610 611
612
613 function isSafari()
614 {
615 return detectDevice('Safari');
616 }
617
618 619 620 621 622
623
624 function isOpera()
625 {
626 return detectDevice('Opera');
627 }
628
629 630 631 632 633 634 635 636
637
638 function getDpi($width, $height, $inch)
639 {
640 if (!is_int($width) || !is_int($height))
641 {
642 throw new \InvalidArgumentException('width && height must be integer');
643 }
644
645 if (!is_numeric($inch))
646 {
647 throw new \InvalidArgumentException('device inch must be number');
648 }
649
650 return ceil(sqrt(pow($width,2) + pow($height, 2)) / $inch);
651 }
652
653 654 655 656 657 658 659 660
661
662 function js($script, $wrap = false, $type = ' type="text/javascript"')
663 {
664 $wrap = ($wrap) ? "\n" : '';
665 $script = (is_array($script)) ? implode($wrap, $script) : $script;
666 $script = $wrap . '<script' . $type . '>' . $wrap . $script . $wrap . '</script>' . $wrap;
667
668 return $script;
669 }
670
671 672 673 674 675 676 677
678
679 function script($script, $wrap = false)
680 {
681 return js($script, $wrap);
682 }
683
684 685 686 687 688 689 690
691
692 function style($style, $wrap = false)
693 {
694 $wrap = ($wrap) ? "\n" : '';
695 $style = ( (is_array($style)) ? implode($wrap, $style) : $style);
696 $style = $wrap . '<style type="text/css">' . $wrap . $style . $wrap . '</style>' . $wrap;
697
698 return $style;
699 }
700
701 702 703 704 705 706 707 708
709
710 function console($message, $type = 'log', $wrap = false)
711 {
712 $wrap = ($wrap) ? "\n" : '';
713
714 return $wrap . '<script type="text/javascript">console.' . $type . '("' . $message . '");</script>' . $wrap;
715 }
716
717 718 719 720 721 722 723 724 725
726
727 function linkTag($href, $text, $attrs = '', $wrap = false)
728 {
729 $wrap = ($wrap) ? "\n" : '';
730 $attrs = ($attrs !== '') ? ' ' . $attrs : $attrs;
731 $link = $wrap . '<a href="'.$href.'"' . $attrs . '>' . $text . '</a>' . $wrap;
732
733 return $link;
734 }
735
736 737 738 739 740 741 742 743 744 745
746
747 function hexToRGB($hex)
748 {
749 $hex = str_replace('#', '', $hex);
750 $shorthand = (strlen($hex) == 4);
751
752 list($red, $green, $blue) = array_map('hexdec', str_split($hex, $shorthand ? 1 : 2));
753
754 return [
755 'red' => $red,
756 'green' => $green,
757 'blue' => $blue
758 ];
759 }
760
761 762 763 764 765 766 767 768 769 770
771
772 function rgbToHex(array $rgb)
773 {
774 $rgb = array_map(function($i) {
775 $i = dechex($i);
776 return (strlen($i) < 2) ? '0' . $i : $i;
777 }, $rgb);
778
779 $rgb = '#' . implode('', $rgb);
780
781 return $rgb;
782 }