1
0

iplogs.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * 添加方法日志,和IP限制判断
  4. */
  5. function ipwhiteshow($ip, $rock){
  6. $iplist = ''.ROOT_PATH.'/config/iplist.php';
  7. $bool = 0;
  8. if(file_exists($iplist)){
  9. $iparr = require($iplist);
  10. }else{
  11. $iparr = array(
  12. 'blackip' => '',
  13. 'whiteip' => '',
  14. 'gaptime' => 0,
  15. 'gapnums' => 0
  16. );
  17. }
  18. //白名单判断
  19. $whiteip = $iparr['whiteip'];
  20. if($whiteip!=''){
  21. $whiteipa = explode(',', $whiteip);
  22. foreach($whiteipa as $ips){
  23. $bo = strpos($ip, $ips);
  24. if($bo===0 || $ips=='*'){
  25. $bool = 1; //可以访问
  26. break;
  27. }
  28. }
  29. }
  30. //黑名单判断
  31. if($bool==0){
  32. $blackip = $iparr['blackip'];
  33. if($blackip!=''){
  34. $blackipa = explode(',', $blackip);
  35. foreach($blackipa as $ips){
  36. $bo = strpos($ip, $ips);
  37. if($bo===0 || $ips=='*'){
  38. $bool = 2;//不能访问
  39. break;
  40. }
  41. }
  42. }
  43. }
  44. //创建访问日志
  45. if(getconfig('accesslogs')){
  46. $str = '';
  47. foreach($_SERVER as $k=>$v)$str.='['.$k.']:'.$v.chr(10).'';
  48. $str1 = '';
  49. foreach($_GET as $k=>$v)$str1.='['.$k.']:'.$v.chr(10).'';
  50. $str2 = '';
  51. foreach($_POST as $k=>$v)$str2.='['.$k.']:'.$v.chr(10).'';
  52. $act = arrvalue($_SERVER,'REQUEST_METHOD');
  53. if($act=='POST' && $str2==''){
  54. $str2 = arrvalue($GLOBALS, 'HTTP_RAW_POST_DATA');
  55. }
  56. $logs = ''.UPDIR.'/logs/'.date('Y-m-d').'/'.date('H').'/'.date('H.i.s').'_'.$act.'_'.$ip.'_'.rand(100,999).'.log';
  57. $logstr = '[datetime]:'.$rock->now.'
  58. [URL]:'.$rock->nowurl().'
  59. [ACTION]:'.$act.'
  60. [IP]:'.$ip.'
  61. [GET]
  62. '.$str1.'
  63. [POST]
  64. '.$str2.'
  65. [SERVER]
  66. '.$str.'
  67. ';
  68. $rock->createtxt($logs, $logstr);
  69. }
  70. $msg = '';
  71. if($bool==2){
  72. $msg = '您IP['.$ip.']禁止访问我们站点';
  73. }
  74. $gaptime = (int)arrvalue($iparr, 'gaptime', '0');
  75. $gapnums = (int)arrvalue($iparr, 'gapnums', '0');
  76. $adminid = (int)$rock->session('adminid',0);
  77. if($bool==0 && !$msg && $gapnums > 0 && $adminid==0){
  78. $key = 'accessstate';
  79. $cish = (int)$rock->session($key.'cishu','0');
  80. $ltime= floatval($rock->session($key, '0'));
  81. $ntime= floatval(time());
  82. $jtime= $ntime - $ltime;
  83. if($jtime > $gaptime)$cish= 0;
  84. $cish ++;
  85. if($jtime <= $gaptime && $cish > $gapnums){
  86. $msg = '您访问速度太快了的'.$cish.'';
  87. $cish= 0;
  88. }
  89. $rock->setsession($key, $ntime);
  90. $rock->setsession($key.'cishu', $cish);
  91. }
  92. //区域限制的
  93. $whitecity = arrvalue($iparr, 'whitecity');
  94. if(!$msg && $bool==0 && $whitecity && !c('check')->isneiurl('http://'.$ip)){
  95. $key = 'ip_'.$ip.'';$cache = c('cache');
  96. $result= $cache->get($key);
  97. if(!$result){
  98. $result = c('curl')->getcurl(''.base64_decode('aHR0cDovL3d3dy5yb2Nrb2EuY29tLz9tPWlwJmE9cXVlcnkmaXA9').''.$ip.'&xinhukey='.getconfig('xinhukey').'');//查询IP归属地
  99. if($result && contain($result, 'country')){
  100. $cache->set($key, $result);
  101. }else{
  102. $msg = $result;
  103. $result = '';
  104. }
  105. }
  106. if(!$result){
  107. $msg = '接口失效无法识别访问区域'.$msg.'';
  108. }else{
  109. $json = json_decode($result, true);
  110. $country = arrvalue($json, 'country');
  111. $region = arrvalue($json, 'region');
  112. $city = arrvalue($json, 'city');
  113. $xian = arrvalue($json, 'xian');
  114. $whitea = explode(',', $whitecity);
  115. $inbool = false;
  116. foreach($whitea as $ctys){
  117. if($country && stripos($country, $ctys)===0)$inbool = true;
  118. if(!$inbool && $region && stripos($region, $ctys)===0)$inbool = true;
  119. if(!$inbool && $city && stripos($city, $ctys)===0)$inbool = true;
  120. if(!$inbool && $xian && stripos($xian, $ctys)===0)$inbool = true;
  121. if($inbool)break;
  122. }
  123. if(!$inbool)$msg = '您的IP['.$ip.''.$country.$region.$city.$xian.']区域禁止访问我们站点';
  124. }
  125. }
  126. if($msg){
  127. @file_put_contents(''.UPDIR.'/phperrors.log','['.$rock->now.']'.$ip.''.$msg.''.chr(10).'',FILE_APPEND);
  128. $cfrom = $rock->get('cfrom');
  129. $msg .= ',有问题请联系我们';
  130. if($cfrom == 'nppandroid' || $cfrom == 'nppios')$msg = json_encode(returnerror($msg));
  131. exit($msg);
  132. }
  133. }
  134. function ipwhiteshows($ips, $rock){
  135. $ipa = explode(',', $ips);
  136. foreach($ipa as $ip)ipwhiteshow($ip, $rock);
  137. }
  138. ipwhiteshows($rock->ip, $rock);
粤ICP备19079148号