helper_antitheft.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: helper_antitheft.php 33494 2013-06-26 05:26:25Z laoguozhang $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class helper_antitheft {
  12. public static function check($id, $idtype) {
  13. if((!isset($_GET['_dsign']) || $_GET['_dsign'] !== ($_dsign = dsign($id.$idtype, 8))) && !self::check_allow($id, $idtype)) {
  14. if(!isset($_dsign)) {
  15. $_dsign = dsign($id.$idtype, 8);
  16. }
  17. echo self::make_content($id, $idtype, $_dsign);exit;
  18. }
  19. }
  20. public static function get_sign($id, $idtype) {
  21. return !self::check_allow($id, $idtype) ? dsign($id.$idtype, 8) : '';
  22. }
  23. protected static function check_allow($id, $idtype) {
  24. global $_G;
  25. $ip = ip2long($_G['clientip']);
  26. if(!$ip || $ip == -1) return false;
  27. if($ip < 0) {
  28. $ip = sprintf('%u', $ip);
  29. }
  30. loadcache('antitheft');
  31. $antitheft = $_G['cache']['antitheft'];
  32. if(isset($antitheft['white'])){
  33. if(in_array($ip, (array)$antitheft['white']['single'], true)) {
  34. return true;
  35. }
  36. foreach($antitheft['white']['range'] as $_ip) {
  37. if($ip > $_ip['min'] && $ip < $_ip['max']) return true;
  38. }
  39. }
  40. if(isset($antitheft['black'])){
  41. if(in_array($ip, (array)$antitheft['black']['single'], true)) {
  42. return false;
  43. }
  44. foreach($antitheft['black']['range'] as $_ip) {
  45. if($ip > $_ip['min'] && $ip < $_ip['max']) return false;
  46. }
  47. }
  48. if(!($log = C::t('common_visit')->fetch($ip))) {
  49. C::t('common_visit')->insert(array(
  50. 'ip' => $ip,
  51. 'view' => 1,
  52. ));
  53. return true;
  54. } elseif($log['view'] >= $_G['setting']['antitheft']['max']) {
  55. return false;
  56. } else {
  57. C::t('common_visit')->inc($ip);
  58. return true;
  59. }
  60. }
  61. protected static function make_content($id, $idtype, $dsign) {
  62. $url = '';
  63. $urls = parse_url($_SERVER['REQUEST_URI']);
  64. $addstr = $urls['query'] ? $urls['query'].'&' : '';
  65. $url = $urls['path'].'?'.$addstr.'_dsign='.$dsign.($urls['fragment'] ? '#'.$urls['fragment'] : '');
  66. return self::make_js($url);
  67. }
  68. protected static function make_js($url){
  69. $js = '<script type="text/javascript">';
  70. $varname = array();
  71. $codes = array();
  72. $window = '_'.random(5);
  73. $location = '_'.random(5);
  74. $href = '_'.random(5);
  75. $replace = '_'.random(5);
  76. $assign = '_'.random(5);
  77. $codes[$window] = "$window = window;";
  78. $codes[$location] = "$location = location;";
  79. $codes[$href] = "$href = 'href';";
  80. $codes[$replace] = "$replace = 'replace';";
  81. $codes[$assign] = "$assign = 'assign';";
  82. $codes['getname'] = 'function getName(){var caller=getName.caller;if(caller.name){return caller.name} var str=caller.toString().replace(/[\s]*/g,"");var name=str.match(/^function([^\(]+?)\(/);if(name && name[1]){return name[1];} else {return \'\';}}';
  83. $jskeywords = array('for' => '', 'case' => '', 'if' => '', 'else' => '', 'try' => '', 'new' => '', 'eval' => '', 'var' => ''); //js关键字
  84. $methods = array(1,2,3,4,5,6,7);
  85. $lenths = array(2,2,3,4);
  86. for($i = 0, $l = strlen($url); $i < $l; $i++) {
  87. $len = $lenths[array_rand($lenths)];
  88. $cflag = $len % 2;
  89. $var = random($len);
  90. if(ctype_digit($var[0])) {
  91. $var = '_'.$var;
  92. }
  93. while(isset($varname[$var])) {
  94. $var = random(3);
  95. if(ctype_digit($var[0])) {
  96. $var = '_'.$var;
  97. }
  98. }
  99. $val = substr($url, $i, $len-1);
  100. $i = $i + $len - 2;
  101. switch ($methods[array_rand($methods)]) {
  102. case 1:
  103. if($cflag) {
  104. $varname[$var] = "'$val'";
  105. } else {
  106. $codes[] = "$var='$val';";
  107. $varname[$var] = $var;
  108. }
  109. break;
  110. case 2:
  111. if(!isset($jskeywords[$val]) && ctype_alnum($val) && !ctype_digit($val[0])) {
  112. $codes[] = "function $var({$var}_){function $val(){return getName();};return $val();return '{$var}'}";
  113. $varname[$var] = "$var('".random($len)."')";
  114. } else {
  115. $codes[] = "function $var(){'return $var';return '$val'}";
  116. $varname[$var] = $var.'()';
  117. }
  118. break;
  119. case 3:
  120. if($cflag) {
  121. $codes[] = "$var=function({$var}_){'return $var';return {$var}_;};";
  122. $varname[$var] = "$var('$val')";
  123. } else {
  124. $codes[] = "$var=function(){'return $var';return '$val';};";
  125. $varname[$var] = "$var()";
  126. }
  127. break;
  128. case 4:
  129. if($cflag) {
  130. $varname[$var] = "(function({$var}_){'return $var';return {$var}_})('$val')";
  131. } else {
  132. $varname[$var] = "(function(){'return $var';return '$val'})()";
  133. }
  134. break;
  135. case 5:
  136. if(!isset($jskeywords[$val]) && ctype_alnum($val) && !ctype_digit($val[0])) {
  137. $codes[] = "function $var({$var}_){function _{$var[0]}({$var}_){function $val(){return getName();}function {$var}_(){}return $val();return {$var}_}; return _{$var[0]}({$var}_);}";
  138. $varname[$var] = "$var('".random($len)."')";
  139. } else {
  140. $codes[] = "function $var(){'$var';function _{$var[0]}(){return '$val'}; return _{$var[0]}();}";
  141. $varname[$var] = $var.'()';
  142. }
  143. break;
  144. case 6:
  145. if($cflag) {
  146. $codes[] = "$var=function({$var}_){var _{$var[0]}=function({$var}_){'return $var';return {$var}_;}; return _{$var[0]}({$var}_);};";
  147. $varname[$var] = "$var('$val')";
  148. } else {
  149. $codes[] = "$var=function(){'$var';var _{$var[0]}=function(){return '$val'}; return _{$var[0]}();};";
  150. $varname[$var] = $var.'()';
  151. }
  152. break;
  153. case 7:
  154. if($cflag) {
  155. $varname[$var] = "(function({$var}_){return (function({$var}_){return {$var}_;})({$var}_);})('$val')";
  156. } else {
  157. $varname[$var] = "(function(){'return $var';return (function(){return '$val';})();})()";
  158. }
  159. break;
  160. }
  161. }
  162. shuffle($codes);
  163. $js .= implode('', $codes);
  164. $hrefheader = array('location.href=', 'location=', "{$location}[$href]=", "location[$href]=",
  165. 'location.replace(', 'location.assign(', "location[$assign](", "location[$replace](");
  166. $hreffooter = array('','','','',')',')',')',')');
  167. $index = array_rand($hrefheader);
  168. $js .= $hrefheader[$index]. implode('+', $varname).$hreffooter[$index].';';
  169. $fix = array("{$window}[$href]=", "{$window}['href']=", "{$window}.href=");
  170. $js .= $fix[array_rand($fix)].implode('+', array_slice($varname, 0, 8)).';';
  171. $js .= '</script>';
  172. return $js;
  173. }
  174. }
  175. ?>