helpers.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. use App\Components\Curl;
  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. // 生成SS密码
  12. if(!function_exists('makeRandStr')){
  13. function makeRandStr($length = 6, $isNumbers = false) {
  14. // 密码字符集,可任意添加你需要的字符
  15. if(!$isNumbers){
  16. $chars = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789';
  17. }else{
  18. $chars = '0123456789';
  19. }
  20. $char = '';
  21. for($i = 0; $i < $length; $i++){
  22. $char .= $chars[mt_rand(0, strlen($chars) - 1)];
  23. }
  24. return $char;
  25. }
  26. }
  27. // base64加密(处理URL)
  28. if(!function_exists('base64url_encode')){
  29. function base64url_encode($data) {
  30. return strtr(base64_encode($data), ['+' => '-', '/' => '_', '=' => '']);
  31. }
  32. }
  33. // base64解密(处理URL)
  34. if(!function_exists('base64url_decode')){
  35. function base64url_decode($data) {
  36. return base64_decode(strtr($data, '-_', '+/'));
  37. }
  38. }
  39. // 根据流量值自动转换单位输出
  40. if(!function_exists('flowAutoShow')){
  41. function flowAutoShow($value = 0) {
  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. return $traffic * MB;
  60. }
  61. }
  62. if(!function_exists('toGB')){
  63. function toGB($traffic) {
  64. return $traffic * GB;
  65. }
  66. }
  67. if(!function_exists('flowToGB')){
  68. function flowToGB($traffic) {
  69. return $traffic / GB;
  70. }
  71. }
  72. // 文件大小转换
  73. if(!function_exists('formatBytes')){
  74. function formatBytes($bytes, $precision = 2) {
  75. $units = ['B', 'KB', 'MB', 'GB', 'TB'];
  76. $bytes = max($bytes, 0);
  77. $pow = floor(($bytes? log($bytes) : 0) / log(KB));
  78. $pow = min($pow, count($units) - 1);
  79. $bytes /= pow(KB, $pow);
  80. return round($bytes, $precision).' '.$units[$pow];
  81. }
  82. }
  83. // 秒转时间
  84. if(!function_exists('seconds2time')){
  85. function seconds2time($seconds) {
  86. $day = floor($seconds / Day);
  87. $hour = floor(($seconds % Day) / Hour);
  88. $minute = floor((($seconds % Day) % Hour) / Minute);
  89. if($day > 0){
  90. return $day.'天'.$hour.'小时'.$minute.'分';
  91. }else{
  92. if($hour != 0){
  93. return $hour.'小时'.$minute.'分';
  94. }else{
  95. return $minute.'分';
  96. }
  97. }
  98. }
  99. }
  100. // 获取访客真实IP
  101. if(!function_exists('getClientIP')){
  102. function getClientIP() {
  103. /*
  104. * 访问时用localhost访问的,读出来的是“::1”是正常情况
  105. * ::1说明开启了IPv6支持,这是IPv6下的本地回环地址的表示
  106. * 使用IPv4地址访问或者关闭IPv6支持都可以不显示这个
  107. */
  108. if(isset($_SERVER)){
  109. if(isset($_SERVER['HTTP_CF_CONNECTING_IP'])){
  110. $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
  111. $ip = $_SERVER['REMOTE_ADDR'];
  112. }elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
  113. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  114. }elseif(isset($_SERVER['HTTP_CLIENT_IP'])){
  115. $ip = $_SERVER['HTTP_CLIENT_IP'];
  116. }elseif(isset($_SERVER['REMOTE_ADDR'])){
  117. $ip = $_SERVER['REMOTE_ADDR'];
  118. }else{
  119. $ip = 'unknown';
  120. }
  121. }else{
  122. // 绕过CDN获取真实访客IP
  123. if(getenv('HTTP_X_FORWARDED_FOR')){
  124. $ip = getenv('HTTP_X_FORWARDED_FOR');
  125. }elseif(getenv('HTTP_CLIENT_IP')){
  126. $ip = getenv('HTTP_CLIENT_IP');
  127. }else{
  128. $ip = getenv('REMOTE_ADDR');
  129. }
  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. /*
  140. * {
  141. * "longitude": 105,
  142. * "latitude": 35,
  143. * "area_code": "0",
  144. * "dma_code": "0",
  145. * "organization": "AS23910 China Next Generation Internet CERNET2",
  146. * "country": "China",
  147. * "ip": "2001:da8:202:10::36",
  148. * "country_code3": "CHN",
  149. * "continent_code": "AS",
  150. * "country_code": "CN"
  151. * }
  152. *
  153. * {
  154. * "longitude": 105,
  155. * "latitude": 35,
  156. * "area_code": "0",
  157. * "dma_code": "0",
  158. * "organization": "AS9808 Guangdong Mobile Communication Co.Ltd.",
  159. * "country": "China",
  160. * "ip": "2409:8a74:487:1f30:5178:e5a5:1f36:3525",
  161. * "country_code3": "CHN",
  162. * "continent_code": "AS",
  163. * "country_code": "CN"
  164. * }
  165. */
  166. function getIPv6($ip) {
  167. $url = 'https://api.ip.sb/geoip/'.$ip;
  168. try{
  169. $result = json_decode(Curl::send($url), true);
  170. if(!is_array($result) || isset($result['code'])){
  171. throw new Exception('解析IPv6异常:'.$ip);
  172. }
  173. return $result;
  174. }catch(Exception $e){
  175. Log::error($e->getMessage());
  176. return [];
  177. }
  178. }
  179. }
  180. // 随机UUID
  181. if(!function_exists('createGuid')){
  182. function createGuid() {
  183. mt_srand((double) microtime() * 10000);
  184. $charid = strtoupper(md5(uniqid(rand(), true)));
  185. $hyphen = chr(45);
  186. $uuid = substr($charid, 0, 8).$hyphen.substr($charid, 8, 4).$hyphen.substr($charid, 12,
  187. 4).$hyphen.substr($charid, 16, 4).$hyphen.substr($charid, 20, 12);
  188. return strtolower($uuid);
  189. }
  190. }
  191. // 过滤emoji表情
  192. if(!function_exists('filterEmoji')){
  193. function filterEmoji($str) {
  194. $str = preg_replace_callback('/./u', function(array $match) {
  195. return strlen($match[0]) >= 4? '' : $match[0];
  196. }, $str);
  197. return $str;
  198. }
  199. }
  200. // 验证手机号是否正确
  201. if(!function_exists('isMobile')){
  202. function isMobile($mobile) {
  203. if(!is_numeric($mobile)){
  204. return false;
  205. }
  206. return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#',
  207. $mobile)? true : false;
  208. }
  209. }