helpers.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. use GuzzleHttp\Client;
  3. define('KB', 1024);
  4. define('MB', 1048576);
  5. define('GB', 1073741824);
  6. define('TB', 1099511627776);
  7. define('PB', 1125899906842624);
  8. define('Minute', 60);
  9. define('Hour', 3600);
  10. define('Day', 86400);
  11. define('Mbps', 125000);
  12. // 生成SS密码
  13. if(!function_exists('makeRandStr')){
  14. function makeRandStr($length = 6, $isNumbers = false) {
  15. // 密码字符集,可任意添加你需要的字符
  16. if(!$isNumbers){
  17. $chars = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789';
  18. }else{
  19. $chars = '0123456789';
  20. }
  21. $char = '';
  22. for($i = 0; $i < $length; $i++){
  23. $char .= $chars[random_int(0, strlen($chars) - 1)];
  24. }
  25. return $char;
  26. }
  27. }
  28. // base64加密(处理URL)
  29. if(!function_exists('base64url_encode')){
  30. function base64url_encode($data) {
  31. return strtr(base64_encode($data), ['+' => '-', '/' => '_', '=' => '']);
  32. }
  33. }
  34. // base64解密(处理URL)
  35. if(!function_exists('base64url_decode')){
  36. function base64url_decode($data) {
  37. return base64_decode(strtr($data, '-_', '+/'));
  38. }
  39. }
  40. // 根据流量值自动转换单位输出
  41. if(!function_exists('flowAutoShow')){
  42. function flowAutoShow($value = 0) {
  43. $value = abs($value);
  44. if($value >= PB){
  45. return round($value / PB, 2)."PB";
  46. }
  47. if($value >= TB){
  48. return round($value / TB, 2)."TB";
  49. }
  50. if($value >= GB){
  51. return round($value / GB, 2)."GB";
  52. }
  53. if($value >= MB){
  54. return round($value / MB, 2)."MB";
  55. }
  56. if($value >= KB){
  57. return round($value / KB, 2)."KB";
  58. }
  59. return round($value, 2)."B";
  60. }
  61. }
  62. if(!function_exists('toMB')){
  63. function toMB($traffic) {
  64. return $traffic * MB;
  65. }
  66. }
  67. if(!function_exists('toGB')){
  68. function toGB($traffic) {
  69. return $traffic * GB;
  70. }
  71. }
  72. if(!function_exists('flowToGB')){
  73. function flowToGB($traffic) {
  74. return $traffic / GB;
  75. }
  76. }
  77. // 文件大小转换
  78. if(!function_exists('formatBytes')){
  79. function formatBytes($bytes, $precision = 2) {
  80. $units = ['B', 'KB', 'MB', 'GB', 'TB'];
  81. $bytes = max($bytes, 0);
  82. $pow = floor(($bytes? log($bytes) : 0) / log(KB));
  83. $pow = min($pow, count($units) - 1);
  84. $bytes /= KB ** $pow;
  85. return round($bytes, $precision).' '.$units[$pow];
  86. }
  87. }
  88. // 秒转时间
  89. if(!function_exists('seconds2time')){
  90. function seconds2time($seconds) {
  91. $day = floor($seconds / Day);
  92. $hour = floor(($seconds % Day) / Hour);
  93. $minute = floor((($seconds % Day) % Hour) / Minute);
  94. if($day > 0){
  95. return $day.'天'.$hour.'小时'.$minute.'分';
  96. }
  97. if($hour != 0){
  98. return $hour.'小时'.$minute.'分';
  99. }
  100. return $minute.'分';
  101. }
  102. }
  103. // 获取访客真实IP
  104. if(!function_exists('getClientIP')){
  105. function getClientIP() {
  106. /*
  107. * 访问时用localhost访问的,读出来的是“::1”是正常情况
  108. * ::1说明开启了IPv6支持,这是IPv6下的本地回环地址的表示
  109. * 使用IPv4地址访问或者关闭IPv6支持都可以不显示这个
  110. */
  111. if(isset($_SERVER)){
  112. if(isset($_SERVER['HTTP_CF_CONNECTING_IP'])){
  113. $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
  114. $ip = $_SERVER['REMOTE_ADDR'];
  115. }elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
  116. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  117. }elseif(isset($_SERVER['HTTP_CLIENT_IP'])){
  118. $ip = $_SERVER['HTTP_CLIENT_IP'];
  119. }elseif(isset($_SERVER['REMOTE_ADDR'])){
  120. $ip = $_SERVER['REMOTE_ADDR'];
  121. }else{
  122. $ip = 'unknown';
  123. }
  124. }elseif(getenv('HTTP_X_FORWARDED_FOR')){
  125. $ip = getenv('HTTP_X_FORWARDED_FOR');
  126. }elseif(getenv('HTTP_CLIENT_IP')){
  127. $ip = getenv('HTTP_CLIENT_IP');
  128. }else{
  129. $ip = getenv('REMOTE_ADDR');
  130. }
  131. if(trim($ip) === '::1'){
  132. $ip = '127.0.0.1';
  133. }
  134. return $ip;
  135. }
  136. }
  137. // 获取IPv6信息
  138. if(!function_exists('getIPv6')){
  139. function getIPv6($ip) {
  140. $client = new Client(['timeout' => 5]);
  141. $request = $client->get('https://api.ip.sb/geoip/'.$ip);
  142. $message = json_decode($request->getBody(), true);
  143. if($request->getStatusCode() == 200){
  144. return $message;
  145. }
  146. Log::debug('解析IPv6异常:'.$ip.PHP_EOL.var_export($request, true));
  147. return false;
  148. }
  149. }
  150. // 过滤emoji表情
  151. if(!function_exists('filterEmoji')){
  152. function filterEmoji($str) {
  153. $str = preg_replace_callback('/./u', static function(array $match) {
  154. return strlen($match[0]) >= 4? '' : $match[0];
  155. }, $str);
  156. return $str;
  157. }
  158. }