helpers.php 5.5 KB

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