<?php
define('THIS_SCRIPT', 'badword');
include_once 'global.php';
$lang = load_lang($lang,'badword');
checklogin();
header("content-Type: text/html; charset=".$charset);
head();
if ( strpos($sysinfo['flag'],'26,') === false ){
	show_msg($lang['readme_tit'],$lang['error.str1'],0,'','','','');
	exit;
}
PageLoad();
footer();
function Pageload(){
	global $dv,$db,$lang,$reaction,$action,$wordlist,$reg_limit,$IP_CACHE;
	global $body_sipallow,$sIPgame,$body_sipestop,$sipfrominfo,$sipregnum,$sbeestopnumberreg,$sbeinputtime,$sdyitem,$readme;
	global $findword,$findforuminfo;
	if($action=="editok"){
		badword_setting();
	}
	if($action=="updateok"){
		update_reginfo();
	}
	if($reaction=="badword"){
		$wordlist=badword_list();
		$readme=$lang['suc.info11'];
	}
	elseif($reaction=="splitreg"){
		reg_limit();
		ipcachelist();
	}
	elseif($reaction=='regleach'){
		reg_limit();
		ipcachelist();
		$readme=$lang['reg.info3'];	
	}
	elseif($reaction=='allowreg'){
		reg_limit();
		ipcachelist();
		$readme=$lang['reg.info9'].'£º'.$lang['reg.info10'];		
	}
	elseif($reaction=='estopreg'){
		reg_limit();
		ipcachelist();
		$readme=$lang['reg.info11'].'£º'.$lang['reg.info12'];		
	}
	elseif($action=='transgress_info'){
		Transgress_Forum_Info();
	}
	elseif($action=='deltransgress_info'){
		del_transgress_info();
	}
include printout('badword');
}
function badword_setting(){
	global $dv,$db,$lang,$badwords;
	if($badwords==""){
		show_msg($lang['sys.info'],$lang['suc.info3'],0,'','','badword.php?reaction=badword',$lang['comback']);
	exit;
	}
	else{
		$badwords= preg_split('/\r\n|\n/i',trim($badwords));
		if( is_array($badwords) ){
			foreach( $badwords as $key=>$val ){
				if(preg_match('/^.+? *= *.+?$/i',$val)===0){
					show_msg($lang['sys.info'],$lang['suc.info4'],0,'','','badword.php?reaction=badword',$lang['comback']);
					exit;
				}
				list($tmpbadwords,$tmpreplbadwords) = explode('=',$val);
				if( $strbadwords != '' ){
					$strbadwords .= '|';
					$strreplbadwords .= '|';
				}
				$strbadwords .= $tmpbadwords;
				$strreplbadwords .= $tmpreplbadwords;
			}
		} 
		else{
			show_msg($lang['sys.info'],$lang['suc.info4'],0,'','','badword.php?reaction=badword',$lang['comback']);
			exit;
		}
		if($result=$db->query("update {$dv}setup set forum_badwords='".$db->escape_string(htmlspecialchars($strbadwords))."' ,forum_rbadword='".$db->escape_string(htmlspecialchars($strreplbadwords))."'")){
			require_once INC_PATH.'BoardCache.class.php';
			$mycache =& new BoardCache($db);
			$mycache->createForumSetting();			show_msg($lang['sys.info'],$lang['suc.info1'],1,'','','badword.php?reaction=badword',$lang['comback']);
		exit;
		}
		else{		
			show_msg($lang['sys.info'],$lang['suc.info2'],0,'','','badword.php',$lang['comback']);
		exit;
		}
	}
}
function badword_list(){
	global $dv,$db,$lang,$badwords;
	$result=$db->query("select forum_badwords,forum_rbadword from {$dv}setup");
	$word_arr=$db->fetch_array($result);
	$tmpbadword=explode("|",$word_arr['forum_badwords']);
	$retmpbadword=explode("|",$word_arr['forum_rbadword']);
	foreach($tmpbadword as $key=>$val ){
		$return_val .= $val.'='.$retmpbadword[$key]."\r\n";
	}
	return $return_val;
}
function reg_limit(){
	global $dv,$db,$reg_limit;
	$result=$db->query("select forum_setting from {$dv}setup");
	$temarr=$db->fetch_array($result);
	$reglist=explode("|||",$temarr['forum_setting']);
	$reg_limit=$reglist[4];
}
function update_reginfo(){
	global $dv,$db,$lang,$filtrate;
	$filtrate=$_POST['filtrate'];
	$result=$db->query("select forum_setting from {$dv}setup");
	$tmparr=$db->fetch_array($result);
	$reglist=explode("|||",$tmparr['forum_setting']);
	$IPgame=$_POST['IPgame'];
	$allowregip=$_POST['allowregip'];
	$estopregip=$_POST['estopregip'];
	$ipfrominfo=$_POST['ipfrominfo'];
	$ipregnum=$_POST['ipregnum'];
	$beestopnumberreg=$_POST['beestopnumberreg'];
	$beinputtime=$_POST['beinputtime'];
	$dyitem=$_POST['dyitem'];
	$geturl=$_SERVER['HTTP_REFERER'];
	createipcache($IPgame,$allowregip,$estopregip,$ipfrominfo,$ipregnum,$beestopnumberreg,$beinputtime,$dyitem);
	$reglist[4] = str_replace('|||', '', $filtrate);
	$forum_setting=$db->escape_string(implode('|||', $reglist));
	if($upresult=$db->query("update {$dv}setup set forum_setting='".$forum_setting."'")){
		require_once INC_PATH.'BoardCache.class.php';
		$mycache =& new BoardCache($db);
		$mycache->createForumSetting();
		show_msg($lang['sys.info'],$lang['suc.info1'],1,'','',$geturl,$lang['comback']);
		exit;
	}
	else{		
		show_msg($lang['sys.info'],$lang['suc.info2'],0,'','',$geturl,$lang['comback']);
		exit;
	}
}
function createipcache($ipswitch,$allow_ip,$ip_estop,$ipfrom,$ip_regnum,$ipnumber_estop,$inputcurrtime,$dynamic_teitem) {
	global $dv,$db,$lang;
	$ipswitch=$ipswitch;
	if(intval($ipswitch)!=1){
		$ipswitch='0';
	}
	$ipfrom=$ipfrom;
	if(intval($ipfrom)!=1){
		$ipfrom='0';
	}
	$ip_regnum=$ip_regnum;
	$ipnumber_estop=$ipnumber_estop;
	if(intval($ipnumber_estop)!=1){
		$ipnumber_estop='0';
	}
	$inputcurrtime=$inputcurrtime;
	if(intval($inputcurrtime)!=1){
		$inputcurrtime='0';
	}
	$dynamic_teitem=$dynamic_teitem;
	if(intval($dynamic_teitem)!=1){
		$dynamic_teitem='0';
	}
	if(!empty($ip_regnum)){
		if(!is_numeric($ip_regnum)){		
			show_msg($lang['sys.info'],$lang['suc.info4'],0,'','','badword.php?reaction=splitreg',$lang['comback']);
			exit;
		}
	}
	$allow_ip=preg_replace('/\r\n|\n/i',"|",str_replace('|','',$allow_ip));
	$ip_estop=preg_replace('/\r\n|\n/i',"|",str_replace('}','',$ip_estop));
	$body.= "<?php\r\n\$IP_CACHE['allowip'] = array(\r\n";
	$tmpbodys='';
	$body.= <<<EOT
\t'ipswitch' =>'{$ipswitch}',
\t'ipfrom' => '{$ipfrom}',
\t'ip_regnum' => '{$ip_regnum}',
\t'ipnumber_estop' => '{$ipnumber_estop}',
\t'inputcurrtime' => '{$inputcurrtime}',
\t'dynamic_teitem' => '{$dynamic_teitem}',
EOT;
	$allow_iparr=explode('|',$allow_ip);
	$allownum=count($allow_iparr);
	$body.="\r\n";
	if(!empty($allow_iparr)){
		for ($i=0;$i<$allownum;$i++) {
			if( $tmpbodys == '' ) {
				$tmpbodys = '1';
			} else{
				$body.= ",\r\n";
			}
			$body .= <<<EOT
\t'{$i}' => array(
\t\t'IP_allow' => '{$allow_iparr[$i]}'
\t)
EOT;
		}
	}
	if(empty($body) ){
		return false;
	} else{
		$body.= "\r\n);\r\n";
	}
	$ip_estoparr=explode('|',$ip_estop);
	$estopnum=count($ip_estoparr);
	if(!empty($ip_estoparr)){
		$body.= "\r\n\$IP_CACHE['estopip'] = array(\r\n";
		$tmpbodys='';
		for ($j=0;$j<$estopnum;$j++) {
			if( $tmpbodys == '' ) {
				$tmpbodys = '1';
			} else{
				$body.= ",\r\n";
			}
			$body .= <<<EOT
\t'{$j}' => array(
\t\t'IP_estop' => '{$ip_estoparr[$j]}'
\t)
EOT;
		}
	}
	if( empty($body) ){
		return false;
	} else{
		$body .= "\r\n);\r\n".'?'.'>';
		$ipfileurl=CACHE_PATH.'Forum_IPgame_cache.php';
		return write_ipcachefile($body, $ipfileurl);
	}
}
function ipcachelist() {
	global $IP_CACHE;
	global $body_sipallow,$sIPgame,$body_sipestop,$sipfrominfo,$sipregnum,$sbeestopnumberreg,$sbeinputtime,$sdyitem;
	include_once CACHE_PATH.'Forum_IPgame_cache.php';
	$sIPgame=$IP_CACHE['allowip']['ipswitch'];
	$i=0;
	if(!empty($IP_CACHE['allowip'])){
		foreach ($IP_CACHE['allowip'] as $sipallow){
			$i++;
			if($i<7){
				continue;
			}
			$body_sipallow.=$sipallow['IP_allow']."\r\n";
		}
	}
	if(!empty($IP_CACHE['estopip'])){
		foreach ($IP_CACHE['estopip'] as $sipestop){
			$body_sipestop.=$sipestop['IP_estop']."\r\n";
		}
	}
	$sipfrominfo=$IP_CACHE['allowip']['ipfrom'];
	$sipregnum=$IP_CACHE['allowip']['ip_regnum'];
	$sbeestopnumberreg=$IP_CACHE['allowip']['ipnumber_estop'];
	$sbeinputtime=$IP_CACHE['allowip']['inputcurrtime'];
	$sdyitem=$IP_CACHE['allowip']['dynamic_teitem'];
}
function write_ipcachefile(&$contents,$file,$append=false){
	if( function_exists('file_put_contents') ){
		if ($append===false) {
			return ( @file_put_contents($file, $contents) > 0 ? true : false );
		}
		else {
			if( $fp = fopen($file, ($append !== false ? 'ab' : 'wb')) ){
				@flock($fp, 2);
				$writed = @fwrite($fp, $contents, strlen($contents));
				@fclose($fp);
				@chmod($file,0755);
				return ( $writed === false ? false : true );
			} else{
				return false;
			}
		}
	} else{
		if( $fp = fopen($file, ($append !== false ? 'ab' : 'wb')) ){
			@flock($fp, 2);
			$writed = @fwrite($fp, $contents, strlen($contents));
			@fclose($fp);
			@chmod($file,0755);
			return ( $writed === false ? false : true );
		} else{
			return false;
		}
	}
}

function Transgress_Forum_Info(){
	global $dv,$db,$lang,$findword,$findforuminfo;
	$forum_leach=$_POST['forum_leach'];
	$transgress_word=$_POST['transgress_word'];
	$findword=array();
	$findforuminfo=array();
	if(intval($forum_leach==1)){
		$fountain_badword=$db->scalar("select forum_badwords from {$dv}setup");
		$fountain_badwordset=explode('|',$fountain_badword);
	}
	else{
		if(!empty($transgress_word)){
			$fountain_badwordset=explode('|',$transgress_word);
		}
		else{		
			show_msg($lang['sys.info'],$lang['badword.info6'],0,'','','badword.php?reaction=transgress',$lang['comback']);
			exit;
		}
	}
	$count=count($fountain_badwordset);
	foreach ($fountain_badwordset as $fountain_badwordlist => $value){
		$conf_topic.=" topic LIKE '%".$value."%' or ";
		$conf_body.=" body LIKE '%".$value."%' or ";
	}
	$conf_topic=substr($conf_topic,0,-4);
	$conf_body=substr($conf_body,0,-4);
	$conf_str=$conf_topic.' or '.$conf_body;
	$sql=$db->query("select tablename from {$dv}tablelist");
	while ($tablearr=$db->fetch_array($sql)){
		$stmt=$db->query("select rootid,topic,boardid,username,dateandtime,ip from {$tablearr['tablename']} where {$conf_str}");
		while ($infolist=$db->fetch_array($stmt)){
			if($db->scalar("select count(1) from {$dv}topic where topicid IN (".$infolist['rootid'].")")){
				$findforuminfo[]=$infolist;
			}
			else{
				continue;
			}
		}
	}
	$db->free_result($stmt);
	$db->free_result($sql);
}

function del_transgress_info(){
	global $db,$lang,$dv;
	$select_all=$_POST['select_all'];
	$selected_id=$_POST['selected_id'];
	$buttontype=$_POST['submit'];
	foreach ($selected_id as $selected_idlist => $values){
		$forumid.=$values.',';
	}
	$forumid=substr($forumid,0,-1);
	if($buttontype == $lang['badword.info9']){
		$db->query("Delete From {$dv}topic where topicid in ({$forumid})");
		$db->query("Delete From {$dv}savvy_topic where topicid in ({$forumid})");
	}
	elseif($buttontype == $lang['badword.info8']){
		$db->query("Update {$dv}topic Set boardid=444 where topicid in ({$forumid})");
		$db->query("Update {$dv}savvy_topic Set boardid=444 where topicid in ({$forumid})");		
	}
	show_msg($lang['sys.info'],$lang['badword.info7'],1,'','','badword.php?reaction=transgress',$lang['comback']);
	exit;
}
?>