helpers.php 4.3 KB

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