You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
5.6 KiB

3 years ago
3 years ago
3 years ago
  1. <?php
  2. // 应用公共文件
  3. use think\facade\Db;
  4. function get_data_dir($os = 'Linux'){
  5. return app()->getRootPath().'data/'.($os == 'Windows' ? 'win/' : '');
  6. }
  7. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  8. $ckey_length = 4;
  9. $key = md5($key);
  10. $keya = md5(substr($key, 0, 16));
  11. $keyb = md5(substr($key, 16, 16));
  12. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  13. $cryptkey = $keya.md5($keya.$keyc);
  14. $key_length = strlen($cryptkey);
  15. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  16. $string_length = strlen($string);
  17. $result = '';
  18. $box = range(0, 255);
  19. $rndkey = array();
  20. for($i = 0; $i <= 255; $i++) {
  21. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  22. }
  23. for($j = $i = 0; $i < 256; $i++) {
  24. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  25. $tmp = $box[$i];
  26. $box[$i] = $box[$j];
  27. $box[$j] = $tmp;
  28. }
  29. for($a = $j = $i = 0; $i < $string_length; $i++) {
  30. $a = ($a + 1) % 256;
  31. $j = ($j + $box[$a]) % 256;
  32. $tmp = $box[$a];
  33. $box[$a] = $box[$j];
  34. $box[$j] = $tmp;
  35. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  36. }
  37. if($operation == 'DECODE') {
  38. if(((int)substr($result, 0, 10) == 0 || (int)substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  39. return substr($result, 26);
  40. } else {
  41. return '';
  42. }
  43. } else {
  44. return $keyc.str_replace('=', '', base64_encode($result));
  45. }
  46. }
  47. function get_curl($url, $post=0, $referer=0, $cookie=0, $header=0, $ua=0, $nobody=0, $addheader=0)
  48. {
  49. $ch = curl_init();
  50. curl_setopt($ch, CURLOPT_URL, $url);
  51. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  52. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  53. $httpheader[] = "Accept: */*";
  54. $httpheader[] = "Accept-Encoding: gzip,deflate,sdch";
  55. $httpheader[] = "Accept-Language: zh-CN,zh;q=0.8";
  56. $httpheader[] = "Connection: close";
  57. if($addheader){
  58. $httpheader = array_merge($httpheader, $addheader);
  59. }
  60. curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
  61. if ($post) {
  62. curl_setopt($ch, CURLOPT_POST, 1);
  63. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  64. }
  65. if ($header) {
  66. curl_setopt($ch, CURLOPT_HEADER, true);
  67. }
  68. if ($cookie) {
  69. curl_setopt($ch, CURLOPT_COOKIE, $cookie);
  70. }
  71. if($referer){
  72. curl_setopt($ch, CURLOPT_REFERER, $referer);
  73. }
  74. if ($ua) {
  75. curl_setopt($ch, CURLOPT_USERAGENT, $ua);
  76. }
  77. else {
  78. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36");
  79. }
  80. if ($nobody) {
  81. curl_setopt($ch, CURLOPT_NOBODY, 1);
  82. }
  83. curl_setopt($ch, CURLOPT_ENCODING, "gzip");
  84. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  85. $ret = curl_exec($ch);
  86. curl_close($ch);
  87. return $ret;
  88. }
  89. function jsonp_decode($jsonp, $assoc = false)
  90. {
  91. $jsonp = trim($jsonp);
  92. if(isset($jsonp[0]) && $jsonp[0] !== '[' && $jsonp[0] !== '{') {
  93. $begin = strpos($jsonp, '(');
  94. if(false !== $begin)
  95. {
  96. $end = strrpos($jsonp, ')');
  97. if(false !== $end)
  98. {
  99. $jsonp = substr($jsonp, $begin + 1, $end - $begin - 1);
  100. }
  101. }
  102. }
  103. return json_decode($jsonp, $assoc);
  104. }
  105. function config_get($key, $default = null)
  106. {
  107. $value = config('sys.'.$key);
  108. return $value!==null ? $value : $default;
  109. }
  110. function config_set($key, $value)
  111. {
  112. $res = Db::name('config')->replace()->insert(['key'=>$key, 'value'=>$value]);
  113. return $res!==false;
  114. }
  115. function real_ip($type=0){
  116. $ip = $_SERVER['REMOTE_ADDR'];
  117. if($type<=0 && isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
  118. foreach ($matches[0] AS $xip) {
  119. if (filter_var($xip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
  120. $ip = $xip;
  121. break;
  122. }
  123. }
  124. } elseif ($type<=0 && isset($_SERVER['HTTP_CLIENT_IP']) && filter_var($_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
  125. $ip = $_SERVER['HTTP_CLIENT_IP'];
  126. } elseif ($type<=1 && isset($_SERVER['HTTP_CF_CONNECTING_IP']) && filter_var($_SERVER['HTTP_CF_CONNECTING_IP'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
  127. $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
  128. } elseif ($type<=1 && isset($_SERVER['HTTP_X_REAL_IP']) && filter_var($_SERVER['HTTP_X_REAL_IP'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
  129. $ip = $_SERVER['HTTP_X_REAL_IP'];
  130. }
  131. return $ip;
  132. }
  133. function getSubstr($str, $leftStr, $rightStr)
  134. {
  135. $left = strpos($str, $leftStr);
  136. $start = $left+strlen($leftStr);
  137. $right = strpos($str, $rightStr, $start);
  138. if($left < 0) return '';
  139. if($right>0){
  140. return substr($str, $start, $right-$start);
  141. }else{
  142. return substr($str, $start);
  143. }
  144. }
  145. function checkRefererHost(){
  146. if(!request()->header('referer'))return false;
  147. $url_arr = parse_url(request()->header('referer'));
  148. $http_host = request()->header('host');
  149. if(strpos($http_host,':'))$http_host = substr($http_host, 0, strpos($http_host, ':'));
  150. return $url_arr['host'] === $http_host;
  151. }
  152. function checkIfActive($string) {
  153. $array=explode(',',$string);
  154. $action = request()->action();
  155. if (in_array($action,$array)){
  156. return 'active';
  157. }else
  158. return null;
  159. }
  160. function errorlog($msg){
  161. $handle = fopen(app()->getRootPath()."record.txt", 'a');
  162. fwrite($handle, date('Y-m-d H:i:s')."\t".$msg."\r\n");
  163. fclose($handle);
  164. }