1
0

helpers.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. }
  90. // 获取访客真实IP
  91. if (!function_exists('getClientIP')) {
  92. function getClientIP()
  93. {
  94. /*
  95. * 访问时用localhost访问的,读出来的是“::1”是正常情况
  96. * ::1说明开启了IPv6支持,这是IPv6下的本地回环地址的表示
  97. * 使用IPv4地址访问或者关闭IPv6支持都可以不显示这个
  98. */
  99. if (isset($_SERVER)) {
  100. if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
  101. $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
  102. $ip = $_SERVER['REMOTE_ADDR'];
  103. } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  104. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  105. } elseif (isset($_SERVER['HTTP_CLIENT_ip'])) {
  106. $ip = $_SERVER['HTTP_CLIENT_ip'];
  107. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  108. $ip = $_SERVER['REMOTE_ADDR'];
  109. } else {
  110. $ip = 'unknown';
  111. }
  112. } else {
  113. // 绕过CDN获取真实访客IP
  114. if (getenv('HTTP_X_FORWARDED_FOR')) {
  115. $ip = getenv('HTTP_X_FORWARDED_FOR');
  116. } elseif (getenv('HTTP_CLIENT_ip')) {
  117. $ip = getenv('HTTP_CLIENT_ip');
  118. } else {
  119. $ip = getenv('REMOTE_ADDR');
  120. }
  121. }
  122. if (trim($ip) == '::1') {
  123. $ip = '127.0.0.1';
  124. }
  125. return $ip;
  126. }
  127. }
  128. // 获取IPv6信息
  129. if (!function_exists('getIPv6')) {
  130. /*
  131. * {
  132. * "longitude": 105,
  133. * "latitude": 35,
  134. * "area_code": "0",
  135. * "dma_code": "0",
  136. * "organization": "AS23910 China Next Generation Internet CERNET2",
  137. * "country": "China",
  138. * "ip": "2001:da8:202:10::36",
  139. * "country_code3": "CHN",
  140. * "continent_code": "AS",
  141. * "country_code": "CN"
  142. * }
  143. */
  144. function getIPv6($ip)
  145. {
  146. $url = 'https://api.ip.sb/geoip/' . $ip;
  147. try {
  148. $ch = curl_init();
  149. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  150. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  151. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  152. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  153. curl_setopt($ch, CURLOPT_URL, $url);
  154. curl_setopt($ch, CURLOPT_POST, 0);
  155. $result = curl_exec($ch);
  156. curl_close($ch);
  157. $result = json_decode($result, true);
  158. if (!is_array($result) || isset($result['code'])) {
  159. throw new Exception('解析IPv6信息异常:' . $ip);
  160. }
  161. return $result;
  162. } catch (\Exception $e) {
  163. \Log::error($e->getMessage());
  164. return [];
  165. }
  166. }
  167. }
  168. // 随机UUID
  169. if (!function_exists('createGuid')) {
  170. function createGuid()
  171. {
  172. mt_srand((double)microtime() * 10000);
  173. $charid = strtoupper(md5(uniqid(rand(), true)));
  174. $hyphen = chr(45);
  175. $uuid = substr($charid, 0, 8) . $hyphen
  176. . substr($charid, 8, 4) . $hyphen
  177. . substr($charid, 12, 4) . $hyphen
  178. . substr($charid, 16, 4) . $hyphen
  179. . substr($charid, 20, 12);
  180. return strtolower($uuid);
  181. }
  182. }