helpers.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 = null, 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. $power += max(array_search($base, $units), 0);
  33. }
  34. return round($bytes, $precision).' '.$units[$power];
  35. }
  36. }
  37. // 秒转时间
  38. if (! function_exists('formatTime')) {
  39. function formatTime(int $seconds): string
  40. {
  41. $timeString = '';
  42. $units = [
  43. trans('validation.attributes.day') => 86400,
  44. trans('validation.attributes.hour') => 3600,
  45. trans('validation.attributes.minute') => 60,
  46. trans('validation.attributes.second') => 1,
  47. ];
  48. foreach ($units as $unitName => $secondsInUnit) {
  49. if ($seconds >= $secondsInUnit) {
  50. $timeString .= floor($seconds / $secondsInUnit).' '.$unitName.' ';
  51. $seconds %= $secondsInUnit;
  52. }
  53. }
  54. return trim($timeString);
  55. }
  56. }
  57. // 获取系统设置
  58. if (! function_exists('sysConfig')) {
  59. function sysConfig(string $key = null, string $default = null): array|string|null
  60. {
  61. return $key ? config("settings.$key", $default) : config('settings');
  62. }
  63. }
  64. // Array values and indexes clean
  65. if (! function_exists('array_clean')) {
  66. function array_clean(array &$array): array
  67. {
  68. foreach ($array as $key => &$value) {
  69. if (is_array($value)) {
  70. $value = array_clean($value);
  71. }
  72. if (empty($value)) {
  73. unset($array[$key]);
  74. }
  75. }
  76. return $array;
  77. }
  78. }
  79. // string url safe sanitize
  80. if (! function_exists('string_urlsafe')) {
  81. function string_urlsafe($string, $force_lowercase = true, $anal = false): string
  82. {
  83. $clean = preg_replace('/[~`!@#$%^&*()_=+\[\]{}\\|;:"\'<>,.?\/]/', '_', strip_tags($string));
  84. $clean = preg_replace('/\s+/', '-', $clean);
  85. $clean = ($anal) ? preg_replace('/[^a-zA-Z0-9]/', '', $clean) : $clean;
  86. if ($force_lowercase) {
  87. $clean = function_exists('mb_strtolower') ? mb_strtolower($clean, 'UTF-8') : strtolower($clean);
  88. }
  89. return $clean;
  90. }
  91. }