helpers.php 7.2 KB

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