Helper.php 3.6 KB

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