Helper.php 3.6 KB

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