<?php
if ( !eregi("admin.php", $PHP_SELF) ) { 
	die ("Access Denied"); 
}
if ( $xoopsUser->isAdmin($xoopsModule->mid()) ) {

$op = "add";

if (isset($HTTP_POST_VARS)) {
	foreach ($HTTP_POST_VARS as $k=>$v) {
		$$k = $v;
	}
}

if ($op == "add") {
	$words = "";
	foreach ($xoopsBadWords as $xbw) {
		$words .= $xbw."\r\n";
	}
	$ips = "";
	foreach ($xoopsBadIps as $xbi) {
		$ips .= $xbi."\r\n";
	}
	include_once(XOOPS_ROOT_PATH."/modules/system/cache/badunames.php");
	$unames = "";
	foreach ($xoopsBadUnames as $xbu) {
		$unames .= $xbu."\r\n";
	}
	xoops_cp_header();
	OpenTable();
	include(XOOPS_ROOT_PATH."/modules/system/admin/filter/filterform.php");
	CloseTable();
	xoops_cp_footer();
}

if ( $op == "save" ) {
	$myts =& MyTextSanitizer::getInstance();
	// convert \n, \r\n, \r to <br /> so that it could be used for delimeter in explode()
	$words = explode("<br />", $myts->oopsNl2Br($myts->oopsStripSlashesGPC(trim($words))));
	$ips = explode("<br />", $myts->oopsNl2Br($myts->oopsStripSlashesGPC(trim($ips))));
	$unames = explode("<br />", $myts->oopsNl2Br($myts->oopsStripSlashesGPC(trim($unames))));
	$errors = array();
	$fileheader = "<"."?php\r\n";
	$filefooter = "?".">";
	$filetext = "";
	if (!$file = fopen(XOOPS_ROOT_PATH."/modules/system/cache/badwords.php","w")) {
		$errors[] = sprintf(_MUSTWABLE, XOOPS_ROOT_PATH."/modules/system/cache/badwords.php");
	} else {
		foreach ($words as $w) {
			if ( !empty($w) ) {
				$filetext .= "\$xoopsBadWords[] = \"".addSlashes($myts->oopsHtmlSpecialChars($w))."\";\r\n";
			}
		}
		$filetext = $fileheader.$filetext.$filefooter;
		if (fwrite($file, $filetext) == -1) {
			$errors[] = sprintf(_NGWRITE, XOOPS_ROOT_PATH."/modules/system/cache/badwords.php");
		}
		fclose($file);
	}
	if ( !$file = fopen(XOOPS_ROOT_PATH."/modules/system/cache/badips.php","w") ) {
		$errors[] = sprintf(_MUSTWABLE, XOOPS_ROOT_PATH."/modules/system/cache/badips.php");
	} else {
		$filetext = "";
		foreach ($ips as $ip) {
			if ( !empty($ip) ) {
				$filetext .= "\$xoopsBadIps[] = \"".addSlashes($myts->oopsHtmlSpecialChars($ip))."\";\r\n";
			}
		}
		$filetext = $fileheader.$filetext.$filefooter;
		if (fwrite($file, $filetext) == -1) {
			$errors[] = sprintf(_NGWRITE, XOOPS_ROOT_PATH."/modules/system/cache/badips.php");
		}
		fclose($file);
	}

	if (!$file = fopen(XOOPS_ROOT_PATH."/modules/system/cache/badunames.php","w")) {
		$errors[] = sprintf(_MUSTWABLE, XOOPS_ROOT_PATH."/modules/system/cache/badunames.php");
	} else {
		$filetext = "";
		foreach ($unames as $u) {
			if ( !empty($u) ) {
				$filetext .= "\$xoopsBadUnames[] = \"".addSlashes($myts->oopsHtmlSpecialChars($u))."\";\r\n";
			}
		}
		$filetext = $fileheader.$filetext.$filefooter;
		if (fwrite($file, $filetext) == -1) {
			$errors[] = sprintf(_NGWRITE, XOOPS_ROOT_PATH."/modules/system/cache/badunames.php");
		}
		fclose($file);
	}
	if (count($errors) > 0) {
		xoops_cp_header();
		OpenTable();
		foreach ($errors as $er) {
			echo $er."<br />";
		}
		CloseTable();
		xoops_cp_footer();
	} else {
		redirect_header("admin.php?fct=filter",1,_MD_AM_DBUPDATED);
	}
}
}
?>