Slimore
  • Namespace
  • Class

Namespaces

  • None
  • Slimore
    • Cache
      • Exception
    • Captcha
    • Database
    • Debug
    • Http
    • Image
    • Log
    • Middleware
    • Mvc
    • Pagination
    • Upload

Classes

  • Slimore\Cache\File
  • Slimore\Captcha\Builder
  • Slimore\Database\Manager
  • Slimore\Debug\Simpler
  • Slimore\Http\Client
  • Slimore\Image\Gd
  • Slimore\Log\Writer
  • Slimore\Middleware\Exceptions
  • Slimore\Mvc\Application
  • Slimore\Mvc\Controller
  • Slimore\Mvc\Model
  • Slimore\Mvc\View
  • Slimore\Pagination\Paginator
  • Slimore\Upload\Uploader

Exceptions

  • Slimore\Cache\Exception\File
  • Slimore\Captcha\Exception
  • Slimore\Database\Exception
  • Slimore\Http\Exception
  • Slimore\Mvc\Exception
  • Slimore\Pagination\Exception
  • Slimore\Upload\Exception

Functions

  • arrayToObject
  • console
  • controller
  • ctl
  • currentUrl
  • decrypt
  • detectDevice
  • encrypt
  • fileFormatSize
  • getDirectoryItems
  • getDpi
  • hexToRGB
  • html
  • imageDataUrl
  • iPad
  • iPhone
  • iPod
  • isAndroid
  • isApache
  • isBlackberry
  • isChrome
  • isCli
  • isFirefox
  • isFreeBSD
  • isIE
  • isIIS
  • isJson
  • isLinux
  • isMacOSX
  • isMobile
  • isNginx
  • isOpera
  • isSafari
  • isTablet
  • isUnix
  • isUnixLike
  • isWebOS
  • isWindows
  • js
  • jsonToArray
  • linkTag
  • password
  • phoneMaskCode
  • randomCharacters
  • replaceDirSeparator
  • rgbToHex
  • script
  • shortUrl
  • style
  1 <?php
  2 
  3 /**
  4  * Slimore - The fully (H)MVC framework based on the Slim PHP framework.
  5  *
  6  * @author      Pandao <slimore@ipandao.com>
  7  * @copyright   2015 Pandao
  8  * @link        http://github.com/slimore/slimore
  9  * @license     MIT License https://github.com/slimore/slimore#license
 10  * @version     0.1.0
 11  * @package     Slimore
 12  */
 13 
 14 /**
 15  * Make the route handle string for autoload (short name)
 16  *
 17  * @param  string $controller index
 18  * @param  string $action index
 19  * @param  string $namespace
 20  * @return string
 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  * ctl() Alias
 37  *
 38  * @param  string $controller index
 39  * @param  string $action index
 40  * @param  string $namespace
 41  * @return string
 42  */
 43 
 44 function controller($controller = 'index', $action = 'index', $namespace = '')
 45 {
 46     return ctl($controller, $action, $namespace);
 47 }
 48 
 49 /**
 50  * Create password, Using double md5 encode
 51  *
 52  * @param string $str
 53  * @param string $salt
 54  * @return string
 55  */
 56 
 57 function password($str, $salt)
 58 {
 59     return md5(md5($str . $salt) . $salt);
 60 }
 61 
 62 /**
 63  * Encrypt code
 64  *
 65  * @param string|number $string
 66  * @param string|number key
 67  * @return string
 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  * Decrypt code
 79  *
 80  * @param string|number $string
 81  * @param string|number $key
 82  * @return string
 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  * Create image data url from file
 92  *
 93  * @param string $file
 94  * @return string
 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  * Mask hidden phone number middle 5 chars
111  *
112  * @param string $phone
113  * @param string $mask
114  * @return string
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  * Generate random characters
126  *
127  * Supported chinese characters
128  *
129  * @param int $length
130  * @param string $characters null
131  * @example
132         echo randomCharacters(6);
133         echo randomCharacters(10);
134         echo randomCharacters(3, '中文随机生成的汉字');
135  *
136  * @return string
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  * File format size
164  *
165  * @param string|number $size
166  * @param int $decimals
167  * @param bool $blankSpace true
168  * @return string
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  * Judge whether JSON data
185  *
186  * @param string $json
187  * @return bool
188  */
189 
190 function isJson($json)
191 {
192     return (json_decode($json)) ? true : false;
193 }
194 
195 /**
196  * Using DIRECTORY_SEPARATOR replace directory path
197  *
198  * @param string $dir
199  * @return mixed
200  */
201 
202 function replaceDirSeparator($dir)
203 {
204     $dir = str_replace(['/', '\\'], [DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR], $dir);
205 
206     return $dir;
207 };
208 
209 /**
210  * Get directory all dirs & files
211  *
212  * @param $path
213  * @param bool $onlyFiles false
214  * @return array
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  * Get current full url
252  *
253  * @return string
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  * Json object to array
268  *
269  * @param object $json
270  * @return array
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  * Array to Object
287  *
288  * @param array $array
289  * @return ArrayObject
290  */
291 
292 function arrayToObject(array $array)
293 {
294     // or json_decode(json_encode($array), FALSE);
295     // or (object) $array;
296     return new ArrayObject($array);
297 }
298 
299 /**
300  * Generate short url
301  *
302  * @param string $url
303  * @return string
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  * Generate HTML(5) template
333  *
334  * @param string $title
335  * @param string|array $head
336  * @param string|array $body
337  * @param string $charset
338  * @param string $lang
339  * @return string
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  * Detect mobile client
363  *
364  * @return int
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  * Detect tablet client
376  *
377  * @return int
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  * Detect mobile device
389  *
390  * @param string $device iPhone,iPod,iPad,Android,webOS ...
391  * @return int
392  */
393 
394 function detectDevice($device = 'iPhone')
395 {
396     return (stripos($_SERVER['HTTP_USER_AGENT'], $device) !== false);
397 }
398 
399 /**
400  * Detect iPhone
401  *
402  * @return int
403  */
404 
405 function iPhone()
406 {
407     return detectDevice('iPhone');
408 }
409 
410 /**
411  * Detect iPad
412  *
413  * @return int
414  */
415 
416 function iPad()
417 {
418     return detectDevice('iPad');
419 }
420 
421 /**
422  * Detect iPod
423  *
424  * @return int
425  */
426 
427 function iPod()
428 {
429     return detectDevice('iPod');
430 }
431 
432 /**
433  * Detect Android device
434  *
435  * @return int
436  */
437 
438 function isAndroid()
439 {
440     return detectDevice('Android');
441 }
442 
443 /**
444  * Detect WebOS device
445  *
446  * @return int
447  */
448 
449 function isWebOS()
450 {
451     return detectDevice('webOS');
452 }
453 
454 /**
455  * Detect Blackberry device
456  *
457  * @return int
458  */
459 
460 function isBlackberry()
461 {
462     return detectDevice('Blackberry');
463 }
464 
465 /**
466  * Detect Windows
467  *
468  * @return bool
469  */
470 
471 function isWindows()
472 {
473     return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
474 }
475 
476 /**
477  * Detect Mac OS X
478  *
479  * @return bool
480  */
481 function isMacOSX()
482 {
483     return (PHP_OS === 'Darwin');
484 }
485 
486 /**
487  * Detect Linux
488  *
489  * @return bool
490  */
491 
492 function isLinux()
493 {
494     return (PHP_OS === 'Linux');
495 }
496 
497 /**
498  * Detect FreeBSD OS
499  *
500  * @return bool
501  */
502 
503 function isFreeBSD()
504 {
505     return (PHP_OS === 'FreeBSD');
506 }
507 
508 /**
509  * Detect Unix
510  *
511  * @return bool
512  */
513 
514 function isUnix()
515 {
516     return (PHP_OS === 'Unix');
517 }
518 
519 /**
520  * Detect Uinx / Linux / Mac OS ... Unix-likes
521  *
522  * @return bool
523  */
524 
525 function isUnixLike()
526 {
527     return (DIRECTORY_SEPARATOR == '/');
528 }
529 
530 /**
531  * Detect Apache server
532  *
533  * @return bool
534  */
535 
536 function isApache()
537 {
538     return (stripos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false || function_exists('apache_get_version'));
539 }
540 
541 /**
542  * Detect Nginx server
543  *
544  * @return bool
545  */
546 
547 function isNginx()
548 {
549     return (stripos($_SERVER["SERVER_SOFTWARE"], 'nginx') > -1);
550 }
551 
552 /**
553  * Detect IIS server
554  *
555  * @return bool
556  */
557 
558 function isIIS()
559 {
560     return (stripos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== false);
561 }
562 
563 /**
564  * Check CLI env
565  * @return string
566  */
567 
568 function isCli()
569 {
570     return strtoupper(substr(PHP_SAPI_NAME(), 0, 3) === 'CLI');
571 }
572 
573 /**
574  * Detect IE browser
575  *
576  * @return bool
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  * Detect Firefox browser
588  *
589  * @return int
590  */
591 
592 function isFirefox()
593 {
594     return detectDevice('Firefox');
595 }
596 
597 /**
598  * Detect Google Chrome browser
599  * @return int
600  */
601 
602 function isChrome()
603 {
604     return detectDevice('Chrome');
605 }
606 
607 /**
608  * Detect Safari browser
609  *
610  * @return int
611  */
612 
613 function isSafari()
614 {
615     return detectDevice('Safari');
616 }
617 
618 /**
619  * Detect Opera browser
620  *
621  * @return int
622  */
623 
624 function isOpera()
625 {
626     return detectDevice('Opera');
627 }
628 
629 /**
630  * Get device independent pixels (Dpi)
631  *
632  * @param int $width
633  * @param int $height
634  * @param number $inch
635  * @return float
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  * Using Javascript, Generate script tag
655  *
656  * @param string $script
657  * @param bool $wrap false
658  * @param string $type type="text/javascript"
659  * @return string
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  * js() alias
673  *
674  * @param $script
675  * @param bool $wrap
676  * @return string
677  */
678 
679 function script($script, $wrap = false)
680 {
681     return js($script, $wrap);
682 }
683 
684 /**
685  * Generate style tag
686  *
687  * @param string $style
688  * @param bool $wrap
689  * @return string
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  * Like javascript console.xxx()
703  *
704  * @param string $message
705  * @param string $type
706  * @param bool $wrap
707  * @return string
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  * Generate link tag
719  *
720  * @param string $href
721  * @param string $text
722  * @param string $attrs
723  * @param bool $wrap
724  * @return string
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  * Hex color To RGB color
738  *
739  * @param string $hex
740  * @example
741  *      hexToRGB('ff9900');
742  *      hexToRGB('#ff9900');
743  *
744  * @return array
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  * RGB to Hex
763  *
764  * @param array $rgb
765  * @example
766  *      rgbToHex([233,195,65]);
767  *      rgbToHex([255, 0, 0]);
768  *      rgbToHex([255, 255, 255]);
769  * @return string
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 }
Slimore API documentation generated by ApiGen