Helper.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Utils;
  3. class Helper
  4. {
  5. public static function uuidToBase64($uuid, $length)
  6. {
  7. return base64_encode(substr($uuid, 0, $length));
  8. }
  9. public static function getShadowsocksServerKey($timestamp, $length)
  10. {
  11. return base64_encode(substr(md5($timestamp), 0, $length));
  12. }
  13. public static function guid($format = false)
  14. {
  15. if (function_exists('com_create_guid') === true) {
  16. return md5(trim(com_create_guid(), '{}'));
  17. }
  18. $data = openssl_random_pseudo_bytes(16);
  19. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  20. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  21. if ($format) {
  22. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  23. }
  24. return md5(vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)) . '-' . time());
  25. }
  26. public static function generateOrderNo(): string
  27. {
  28. $randomChar = rand(10000, 99999);
  29. return date('YmdHms') . $randomChar;
  30. }
  31. public static function exchange($from, $to)
  32. {
  33. $result = file_get_contents('https://api.exchangerate.host/latest?symbols=' . $to . '&base=' . $from);
  34. $result = json_decode($result, true);
  35. return $result['rates'][$to];
  36. }
  37. public static function randomChar($len, $special = false)
  38. {
  39. $chars = array(
  40. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
  41. "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
  42. "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
  43. "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
  44. "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
  45. "3", "4", "5", "6", "7", "8", "9"
  46. );
  47. if ($special) {
  48. $chars = array_merge($chars, array(
  49. "!", "@", "#", "$", "?", "|", "{", "/", ":", ";",
  50. "%", "^", "&", "*", "(", ")", "-", "_", "[", "]",
  51. "}", "<", ">", "~", "+", "=", ",", "."
  52. ));
  53. }
  54. $charsLen = count($chars) - 1;
  55. shuffle($chars);
  56. $str = '';
  57. for ($i = 0; $i < $len; $i++) {
  58. $str .= $chars[mt_rand(0, $charsLen)];
  59. }
  60. return $str;
  61. }
  62. public static function multiPasswordVerify($algo, $salt, $password, $hash)
  63. {
  64. switch($algo) {
  65. case 'md5': return md5($password) === $hash;
  66. case 'sha256': return hash('sha256', $password) === $hash;
  67. case 'md5salt': return md5($password . $salt) === $hash;
  68. default: return password_verify($password, $hash);
  69. }
  70. }
  71. public static function emailSuffixVerify($email, $suffixs)
  72. {
  73. $suffix = preg_split('/@/', $email)[1];
  74. if (!$suffix) return false;
  75. if (!is_array($suffixs)) {
  76. $suffixs = preg_split('/,/', $suffixs);
  77. }
  78. if (!in_array($suffix, $suffixs)) return false;
  79. return true;
  80. }
  81. public static function trafficConvert(int $byte)
  82. {
  83. $kb = 1024;
  84. $mb = 1048576;
  85. $gb = 1073741824;
  86. if ($byte > $gb) {
  87. return round($byte / $gb, 2) . ' GB';
  88. } else if ($byte > $mb) {
  89. return round($byte / $mb, 2) . ' MB';
  90. } else if ($byte > $kb) {
  91. return round($byte / $kb, 2) . ' KB';
  92. } else if ($byte < 0) {
  93. return 0;
  94. } else {
  95. return round($byte, 2) . ' B';
  96. }
  97. }
  98. public static function getSubscribeUrl($path)
  99. {
  100. $subscribeUrls = explode(',', config('v2board.subscribe_url'));
  101. $subscribeUrl = $subscribeUrls[rand(0, count($subscribeUrls) - 1)];
  102. if ($subscribeUrl) return $subscribeUrl . $path;
  103. return url($path);
  104. }
  105. public static function randomPort($range) {
  106. $portRange = explode('-', $range);
  107. return rand($portRange[0], $portRange[1]);
  108. }
  109. }