helpers.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. 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[mt_rand(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. }elseif($value >= TB){
  47. return round($value / TB, 2)."TB";
  48. }elseif($value >= GB){
  49. return round($value / GB, 2)."GB";
  50. }elseif($value >= MB){
  51. return round($value / MB, 2)."MB";
  52. }elseif($value >= KB){
  53. return round($value / KB, 2)."KB";
  54. }else{
  55. return round($value, 2)."B";
  56. }
  57. }
  58. }
  59. if(!function_exists('toMB')){
  60. function toMB($traffic) {
  61. return $traffic * MB;
  62. }
  63. }
  64. if(!function_exists('toGB')){
  65. function toGB($traffic) {
  66. return $traffic * GB;
  67. }
  68. }
  69. if(!function_exists('flowToGB')){
  70. function flowToGB($traffic) {
  71. return $traffic / GB;
  72. }
  73. }
  74. // 文件大小转换
  75. if(!function_exists('formatBytes')){
  76. function formatBytes($bytes, $precision = 2) {
  77. $units = ['B', 'KB', 'MB', 'GB', 'TB'];
  78. $bytes = max($bytes, 0);
  79. $pow = floor(($bytes? log($bytes) : 0) / log(KB));
  80. $pow = min($pow, count($units) - 1);
  81. $bytes /= pow(KB, $pow);
  82. return round($bytes, $precision).' '.$units[$pow];
  83. }
  84. }
  85. // 秒转时间
  86. if(!function_exists('seconds2time')){
  87. function seconds2time($seconds) {
  88. $day = floor($seconds / Day);
  89. $hour = floor(($seconds % Day) / Hour);
  90. $minute = floor((($seconds % Day) % Hour) / Minute);
  91. if($day > 0){
  92. return $day.'天'.$hour.'小时'.$minute.'分';
  93. }else{
  94. if($hour != 0){
  95. return $hour.'小时'.$minute.'分';
  96. }else{
  97. return $minute.'分';
  98. }
  99. }
  100. }
  101. }
  102. // 获取访客真实IP
  103. if(!function_exists('getClientIP')){
  104. function getClientIP() {
  105. /*
  106. * 访问时用localhost访问的,读出来的是“::1”是正常情况
  107. * ::1说明开启了IPv6支持,这是IPv6下的本地回环地址的表示
  108. * 使用IPv4地址访问或者关闭IPv6支持都可以不显示这个
  109. */
  110. if(isset($_SERVER)){
  111. if(isset($_SERVER['HTTP_CF_CONNECTING_IP'])){
  112. $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
  113. $ip = $_SERVER['REMOTE_ADDR'];
  114. }elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
  115. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  116. }elseif(isset($_SERVER['HTTP_CLIENT_IP'])){
  117. $ip = $_SERVER['HTTP_CLIENT_IP'];
  118. }elseif(isset($_SERVER['REMOTE_ADDR'])){
  119. $ip = $_SERVER['REMOTE_ADDR'];
  120. }else{
  121. $ip = 'unknown';
  122. }
  123. }else{
  124. // 绕过CDN获取真实访客IP
  125. if(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. }
  133. if(trim($ip) == '::1'){
  134. $ip = '127.0.0.1';
  135. }
  136. return $ip;
  137. }
  138. }
  139. // 获取IPv6信息
  140. if(!function_exists('getIPv6')){
  141. /*
  142. * {
  143. * "longitude": 105,
  144. * "latitude": 35,
  145. * "area_code": "0",
  146. * "dma_code": "0",
  147. * "organization": "AS23910 China Next Generation Internet CERNET2",
  148. * "country": "China",
  149. * "ip": "2001:da8:202:10::36",
  150. * "country_code3": "CHN",
  151. * "continent_code": "AS",
  152. * "country_code": "CN"
  153. * }
  154. *
  155. * {
  156. * "longitude": 105,
  157. * "latitude": 35,
  158. * "area_code": "0",
  159. * "dma_code": "0",
  160. * "organization": "AS9808 Guangdong Mobile Communication Co.Ltd.",
  161. * "country": "China",
  162. * "ip": "2409:8a74:487:1f30:5178:e5a5:1f36:3525",
  163. * "country_code3": "CHN",
  164. * "continent_code": "AS",
  165. * "country_code": "CN"
  166. * }
  167. */
  168. function getIPv6($ip) {
  169. $url = 'https://api.ip.sb/geoip/'.$ip;
  170. try{
  171. $result = json_decode(Curl::send($url), true);
  172. if(!is_array($result) || isset($result['code'])){
  173. throw new Exception('解析IPv6异常:'.$ip);
  174. }
  175. return $result;
  176. }catch(Exception $e){
  177. Log::error($e->getMessage());
  178. return [];
  179. }
  180. }
  181. }
  182. // 随机UUID
  183. if(!function_exists('createGuid')){
  184. function createGuid() {
  185. mt_srand((double) microtime() * 10000);
  186. $charid = strtoupper(md5(uniqid(rand(), true)));
  187. $hyphen = chr(45);
  188. $uuid = substr($charid, 0, 8).$hyphen.substr($charid, 8, 4).$hyphen.substr($charid, 12,
  189. 4).$hyphen.substr($charid, 16, 4).$hyphen.substr($charid, 20, 12);
  190. return strtolower($uuid);
  191. }
  192. }
  193. // 过滤emoji表情
  194. if(!function_exists('filterEmoji')){
  195. function filterEmoji($str) {
  196. $str = preg_replace_callback('/./u', function(array $match) {
  197. return strlen($match[0]) >= 4? '' : $match[0];
  198. }, $str);
  199. return $str;
  200. }
  201. }
  202. // 验证手机号是否正确
  203. if(!function_exists('isMobile')){
  204. function isMobile($mobile) {
  205. if(!is_numeric($mobile)){
  206. return false;
  207. }
  208. 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}$#',
  209. $mobile)? true : false;
  210. }
  211. }