helpers.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. // 生成SS密码
  3. if (!function_exists('makeRandStr')) {
  4. function makeRandStr($length = 6, $isNumbers = false)
  5. {
  6. // 密码字符集,可任意添加你需要的字符
  7. if (!$isNumbers) {
  8. $chars = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789';
  9. } else {
  10. $chars = '0123456789';
  11. }
  12. $char = '';
  13. for ($i = 0; $i < $length; $i++) {
  14. $char .= $chars[mt_rand(0, strlen($chars) - 1)];
  15. }
  16. return $char;
  17. }
  18. }
  19. // base64加密(处理URL)
  20. if (!function_exists('base64url_encode')) {
  21. function base64url_encode($data)
  22. {
  23. return strtr(base64_encode($data), ['+' => '-', '/' => '_', '=' => '']);
  24. }
  25. }
  26. // base64解密(处理URL)
  27. if (!function_exists('base64url_decode')) {
  28. function base64url_decode($data)
  29. {
  30. return base64_decode(strtr($data, '-_', '+/'));
  31. }
  32. }
  33. // 根据流量值自动转换单位输出
  34. if (!function_exists('flowAutoShow')) {
  35. function flowAutoShow($value = 0)
  36. {
  37. $kb = 1024;
  38. $mb = 1048576;
  39. $gb = 1073741824;
  40. $tb = $gb * 1024;
  41. $pb = $tb * 1024;
  42. if (abs($value) > $pb) {
  43. return round($value / $pb, 2) . "PB";
  44. } elseif (abs($value) > $tb) {
  45. return round($value / $tb, 2) . "TB";
  46. } elseif (abs($value) > $gb) {
  47. return round($value / $gb, 2) . "GB";
  48. } elseif (abs($value) > $mb) {
  49. return round($value / $mb, 2) . "MB";
  50. } elseif (abs($value) > $kb) {
  51. return round($value / $kb, 2) . "KB";
  52. } else {
  53. return round($value, 2) . "B";
  54. }
  55. }
  56. }
  57. if (!function_exists('toMB')) {
  58. function toMB($traffic)
  59. {
  60. $mb = 1048576;
  61. return $traffic * $mb;
  62. }
  63. }
  64. if (!function_exists('toGB')) {
  65. function toGB($traffic)
  66. {
  67. $gb = 1048576 * 1024;
  68. return $traffic * $gb;
  69. }
  70. }
  71. if (!function_exists('flowToGB')) {
  72. function flowToGB($traffic)
  73. {
  74. $gb = 1048576 * 1024;
  75. return $traffic / $gb;
  76. }
  77. }
  78. // 文件大小转换
  79. if (!function_exists('formatBytes')) {
  80. function formatBytes($bytes, $precision = 2)
  81. {
  82. $units = ['B', 'KB', 'MB', 'GB', 'TB'];
  83. $bytes = max($bytes, 0);
  84. $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
  85. $pow = min($pow, count($units) - 1);
  86. $bytes /= pow(1024, $pow);
  87. return round($bytes, $precision) . ' ' . $units[$pow];
  88. }
  89. }