helpers.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. const MiB = 1048576;
  3. const GiB = 1073741824;
  4. const Minute = 60;
  5. const Hour = 3600;
  6. const Day = 86400;
  7. const Mbps = 125000;
  8. // base64加密(处理URL)
  9. if (! function_exists('base64url_encode')) {
  10. function base64url_encode(string $data): string
  11. {
  12. return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($data));
  13. }
  14. }
  15. // base64解密(处理URL)
  16. if (! function_exists('base64url_decode')) {
  17. function base64url_decode(string $data): false|string
  18. {
  19. return base64_decode(str_replace(['-', '_'], ['+', '/'], $data));
  20. }
  21. }
  22. // 根据流量值自动转换单位输出
  23. if (! function_exists('formatBytes')) {
  24. function formatBytes(int $bytes, string $base = '', int $precision = 2): string
  25. {
  26. $units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
  27. $bytes = max($bytes, 0);
  28. $power = floor(($bytes ? log($bytes) : 0) / log(1024));
  29. $power = min($power, count($units) - 1);
  30. $bytes /= 1024 ** $power;
  31. if ($base) {
  32. $basePower = array_search($base, $units);
  33. $power += max($basePower, 0);
  34. }
  35. return round($bytes, $precision).' '.$units[$power];
  36. }
  37. }
  38. // 秒转时间
  39. if (! function_exists('formatTime')) {
  40. function formatTime(int $seconds): string
  41. {
  42. $output = '';
  43. $units = [
  44. trans('validation.attributes.day') => 86400,
  45. trans('validation.attributes.hour') => 3600,
  46. trans('validation.attributes.minute') => 60,
  47. trans('validation.attributes.second') => 1,
  48. ];
  49. foreach ($units as $unit => $value) {
  50. if ($seconds >= $value) {
  51. $count = floor($seconds / $value);
  52. $output .= $count.$unit;
  53. $seconds %= $value;
  54. }
  55. }
  56. return $output;
  57. }
  58. }
  59. // 获取系统设置
  60. if (! function_exists('sysConfig')) {
  61. function sysConfig(string $key = '', string $default = ''): array|string
  62. {
  63. return $key ? config("settings.$key", $default) : config('settings');
  64. }
  65. }
  66. // Array values and indexes clean
  67. if (! function_exists('array_clean')) {
  68. function array_clean(array &$array): array
  69. {
  70. foreach ($array as $key => &$value) {
  71. if (is_array($value)) {
  72. $value = array_clean($value);
  73. }
  74. if (empty($value)) {
  75. unset($array[$key]);
  76. }
  77. }
  78. return $array;
  79. }
  80. }
  81. // string url safe sanitize
  82. if (! function_exists('string_urlsafe')) {
  83. function string_urlsafe($string, $force_lowercase = true, $anal = false): string
  84. {
  85. $strip = [
  86. '~', '`', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '=', '+', '[', '{', ']', '}', '\\', '|', ';', ':', '"', "'", '&#8216;', '&#8217;', '&#8220;',
  87. '&#8221;', '&#8211;', '&#8212;', '—', '–', ',', '<', '.', '>', '/', '?',
  88. ];
  89. $clean = trim(str_replace($strip, '_', strip_tags($string)));
  90. $clean = preg_replace('/\s+/', '-', $clean);
  91. $clean = ($anal) ? preg_replace('/[^a-zA-Z0-9]/', '', $clean) : $clean;
  92. if ($force_lowercase) {
  93. if (function_exists('mb_strtolower')) {
  94. $clean = mb_strtolower($clean, 'UTF-8');
  95. } else {
  96. $clean = strtolower($clean);
  97. }
  98. }
  99. return $clean;
  100. }
  101. }