Helper.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Utils;
  3. use App\Models\Server;
  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 exchange($from, $to)
  23. {
  24. $result = file_get_contents('https://api.exchangerate.host/latest?symbols=' . $to . '&base=' . $from);
  25. $result = json_decode($result, true);
  26. return $result['rates'][$to];
  27. }
  28. public static function randomChar($len, $special = false)
  29. {
  30. $chars = array(
  31. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
  32. "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
  33. "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
  34. "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
  35. "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
  36. "3", "4", "5", "6", "7", "8", "9"
  37. );
  38. if ($special) {
  39. $chars = array_merge($chars, array(
  40. "!", "@", "#", "$", "?", "|", "{", "/", ":", ";",
  41. "%", "^", "&", "*", "(", ")", "-", "_", "[", "]",
  42. "}", "<", ">", "~", "+", "=", ",", "."
  43. ));
  44. }
  45. $charsLen = count($chars) - 1;
  46. shuffle($chars);
  47. $str = '';
  48. for ($i = 0; $i < $len; $i++) {
  49. $str .= $chars[mt_rand(0, $charsLen)];
  50. }
  51. return $str;
  52. }
  53. public static function multiPasswordVerify($algo, $password, $hash)
  54. {
  55. switch($algo) {
  56. case 'md5': return md5($password) === $hash;
  57. case 'sha256': return hash('sha256', $password) === $hash;
  58. default: return password_verify($password, $hash);
  59. }
  60. }
  61. public static function emailSuffixVerify($email, $suffixs)
  62. {
  63. $suffix = preg_split('/@/', $email)[1];
  64. if (!$suffix) return false;
  65. if (!is_array($suffixs)) {
  66. $suffixs = preg_split('/,/', $suffixs);
  67. }
  68. if (!in_array($suffix, $suffixs)) return false;
  69. return true;
  70. }
  71. public static function trafficConvert(int $byte)
  72. {
  73. $kb = 1024;
  74. $mb = 1048576;
  75. $gb = 1073741824;
  76. if ($byte > $gb) {
  77. return round($byte / $gb, 2) . ' GB';
  78. } else if ($byte > $mb) {
  79. return round($byte / $mb, 2) . ' MB';
  80. } else if ($byte > $kb) {
  81. return round($byte / $kb, 2) . ' KB';
  82. } else if ($byte < 0) {
  83. return 0;
  84. } else {
  85. return round($byte, 2) . ' B';
  86. }
  87. }
  88. }