<?
########## Confixx(R) 3.0 Professional ############
####### Copyright SWsoft, Inc. 2004-2005 ##########
#### http://www.sw-soft.com - info@sw-soft.com ####

### spamfilter-plugin by tm@incubatec.com 20030228
include_once( "auth.php" );
include_once( "../include/class.SQLFieldSet.php" );
include_once( "../include/class.SQLInteger.php" );

global $DEBUG;
//$DEBUG=1;

$pop3 = $PHP_AUTH_USER;
db_connect($db_host, $db_user, $db_pass);

$loKunde = new SQLString($kunde);
$id = safe_query2( "SELECT spamfilter FROM kunden WHERE kunde = ?", $loKunde);
$werte = db_fetch_array($id);

if($werte["spamfilter"] == 0)
{
	 db_close();
	 include("allgemein_start.php");
	 exit();		
}

$loAccount = new SQLString($pop3);

$id = safe_query2( "SELECT COUNT(*) AS cnt FROM pop3 WHERE account = ? AND kunde=?",
									 $loAccount, $loKunde );
$werte = db_fetch_array($id);

if (!$werte['cnt'])
{
	$fehlertext = ltext("empolo2_error1");
	db_close();
	include("allgemein_start.php");
	exit();
}


include("email_header.php");

include("../fehler.inc.php");

?>

<script>
function doChange(fn)
{
document.forms[fn].submit();
}
</script>

<?

$username = $pop3;

if($action) {
	$error=-1;
	if(!$address) {
		$error++;
		$errmsg[$error] = ltext("spam_error0"); 
	}
	if($address &&( (!ereg(
	"^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$",  $address)) &&
	(!ereg("^\*@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$",  $address)) ) ) {
		$error++;
		$errmsg[$error] = ltext("spam_error1");
	}
	if(!$preference) {
		$error++;
		$errmsg[$error] = ltext("spam_error2");
	}

	$loFields = new SQLFieldSet( new SQLField( 'username', $username, 'C' ),
															 new SQLField( 'value', $address, 'C' ) );

	if( $action == "new" ) {
		$cmd = "SELECT * FROM spampref WHERE ".$loFields->getSQlWhere();
		$result = db_query($cmd);
		//echo db_error($result);
		$num_rows = db_num_rows($result);
		if($num_rows) {
			$error++;
			$errmsg[$error] = ltext("spam_error3");
		}
	}
	if($error < 0) {
		$loFields->addField( 'preference' , $preference, 'C' );

		if( $action == "update" ) {
			$loFields->shiftField();
			$cmd = "UPDATE spampref SET " . $loFields->getSQLUpdate()." WHERE prefid = ?";
			$result = safe_query2( $cmd, new SQlInteger( $editid ) );

		} elseif ( $action == "new" ) {
			list($lcFlds,$lcVals) = $loFields->getSQLInsert();

			$cmd = "INSERT INTO spampref ( $lcFlds ) VALUES ( $lcVals )";

			$result= db_query($cmd);
		}
		//echo db_error($result);
	}
}
?>

<table class="tblbgcolor" align="left" border="1" cellpadding="3" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF" width="100%">
<tr>
	<td><? echo ltext("spam_desc2", array($username)); ?></td>
</tr>
<tr>
	<td>
<table class="tblbgcolor" align="left" border="1" cellpadding="3" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF" width="100%">
	<tr>
		<td colspan=3 class="titel-3"><? echo ltext("spam_title3"); ?></td>
	</tr>
	<tr>
		<td colspan=3><? echo ltext("spam_desc3"); ?></td>
	</tr>
	<tr>
		<td class="titel-3" background="<?=$strSkinName?>/pics/bg_tbl_header.jpg"><? echo ltext("spam_desc3_mail"); ?></td>
		<td class="titel-3" background="<?=$strSkinName?>/pics/bg_tbl_header.jpg"><? echo ltext("spam_desc3_type"); ?></td>
		<td class="titel-3" background="<?=$strSkinName?>/pics/bg_tbl_header.jpg"><? echo ltext("spam_desc3_action"); ?></td>
	</tr>
<?

if($delete) {
	$cmd = "DELETE FROM spampref WHERE prefid = ?";
	$result = safe_query2( $cmd, new SQLInteger($delete) );
?>
	<tr>
		<td colspan=3 align=center><? echo ltext("spam_email_removed"); ?></td>
	</tr>

<?
}

$cmd = "SELECT prefid, preference, value FROM spampref " .
	"WHERE username = ? AND (preference = 'whitelist_from' " .
	"OR preference = 'blacklist_from') ORDER BY preference DESC";

$result = safe_query($cmd, new SQLString($username) );
while($row=db_fetch_array($result)) {
	if($row[preference] == "whitelist_from") {
		$type = ltext("spam_accept");
	} else {
		$type = ltext("spam_deny");
	}

?>
	<tr>
		<td><? echo $row[value]; ?></td>
		<td><? echo $type; ?></td>
		<td><a class="data" href="email_spam_config.php?pop3=<? echo $pop3; ?>&delete=<? echo $row[prefid]; ?>"><? echo ltext("spam_remove"); ?></a>&nbsp;<a class="data" href="email_spam_config.php?pop3=<? echo $pop3; ?>&edit=<? echo $row[prefid]; ?>"><? echo ltext("spam_modify"); ?></a></td>
	</tr>
<?
}
?>
</table></td>
</tr>
<tr>
	<td>

<?
if($edit) {
	$cmd = "SELECT value, preference FROM spampref WHERE username = ? AND prefid = ?";
	$result = safe_query2( $cmd, new SQLString($username), new SQLInteger($edit));
	$row = db_fetch_array($result);
	$address = $row[0];
	$type = $row[1];
	$header = ltext("spam_addr_mod");
	$formact = "update";
}
else {
	$header = ltext("spam_addr_new");
	$formact = "new";
	$address = '';
}
?>

<table class="tblbgcolor" align="left" border="1" cellpadding="3" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF" width="100%">
	<tr>
	<form action="email_spam_config.php?pop3=<? echo $pop3; ?>" method=post>
	<input type=hidden name=action value="<? echo $formact; ?>">
	<input type=hidden name=editid value="<? echo $edit; ?>">
		<td colspan=2><? echo $header; ?></td>
	</tr>
<?
if($error >=0) {
	$numerrors = count($errmsg);
	for($i=0; $i < $numerrors; $i++):
	?>
		<tr>
			<td colspan=2><? echo $errmsg[$i]; ?></td>
		</tr>
	<?
	endfor;
}
?>
	<tr>
		<td><? echo ltext("spam_desc3_mail"); ?></td>
		<td><input name=address type=text size=40 maxlength=100 value="<? echo $address; ?>" style="width:100%"></td>
	</tr>
	<tr>
		<td><? echo ltext("spam_desc3_type"); ?></td>
		<td><input type=radio name=preference value="whitelist_from" <? if($type == "whitelist_from") { print "checked"; } ?>>
			<? echo ltext("spam_accept"); ?>&nbsp;&nbsp;
		    <input type=radio name=preference value="blacklist_from" <? if($type == "blacklist_from") { print "checked"; } ?>>
			<? echo ltext("spam_deny"); ?>
		</td>
		
	</tr>
	<tr>
		<td colspan=2><? echo std_button($header, 'javascript:doChange(0)'); ?></td>
	</form>
	</tr>
</table>
	</td>
	</tr>
	<tr>
<td>

<?
function updateSpampref ($user,$pref,$value) {
			$loFields = new SQLFieldSet( new SQLField( 'username', $user, 'C' ),
																	 new SQLField( 'preference', $pref, 'C' )
																	 );		
			$where = $loFields->getSQLWhere();
			$sql = "SELECT COUNT(*) FROM spampref WHERE $where";
			$result = db_query( $sql );
			$row = db_fetch_array( $result );
			if ( $row[0] == 1 ) {
				$sql = "UPDATE spampref SET value=? WHERE $where";
				$result = safe_query2( $sql, new SQLString( $value ) );

			} else {
				db_query("DELETE FROM spampref WHERE $where");
				$loFields->addField( 'value', $value, 'C' );

				list($lcFlds,$lcVals) = $loFields->getSQLInsert();
				$sql = "INSERT INTO spampref($lcFlds) VALUES ($lcVals)";
				$result = db_query($sql);
			}

}

function cleanSpampref ($user,$pref){
	safe_query2( "DELETE FROM spampref WHERE username=? AND preference=?",
							 new SQLString($user), new SQLString($pref) );
}

if($update) {
	// update users's rewrite_subject 

	if(!$required_hits) { $required_hits = 5; }
	if(!$rewrite_subject) { $rewrite_subject = 0; }
	if(!$report_header) { $report_header = 0; }
	if(!$defang_mime) { $defang_mime = 0; }

	if ( $spamAssVersion >= 3 ) {
		if ( $rewrite_subject ) {
			updateSpampref($username,'rewrite_header subject','*****SPAM*****');
		} else {
			cleanSpampref($username,'rewrite_header subject');
		}
		updateSpampref($username,'required_score', $required_hits);

		updateSpampref($username,'report_safe',$report_header); 
		if ( $use_terse_report ) {
			updateSpampref($username,'add_header spam Report','_REPORT_'); 
		} else {
			cleanSpampref($username,'add_header spam Report');
		}

	} else {
		updateSpampref($username,'rewrite_subject',$rewrite_subject);

		updateSpampref($username,'required_hits', $required_hits);

		updateSpampref($username,'report_header',$report_header); // ????
		updateSpampref($username,'defang_mime',$defang_mime); // ????
		updateSpampref($username,'use_terse_report',$use_terse_report); // ????

	}
}

if ( $spamAssVersion >= 3 ) {
	$rwrSubjPref = 'rewrite_header subject';
} else {
	$rwrSubjPref = 'rewrite_subject';
}

// default settings
$rewrite_subject = 0; 
$report_header = 0; 
$defang_mime = 0; 
$use_terse_report = 0; 
$required_hits = 5; 


$sql = "SELECT preference,value FROM spampref WHERE username = ?";
$result = safe_query2( $sql, new SQLString( $username ) );
while ( $row = db_fetch_array( $result ) ) {
	$pref = $row[0];
	$val = $row[1];
	if ( $spamAssVersion >= 3 ) {
		switch ( $pref ) {
		case 'rewrite_header subject':
			$rewrite_subject = 1;
			break;
		case 'required_score':
			$required_hits = $val;
			break;
		case 'add_header spam Report':
			$use_terse_report = 1;
			break;
		case 'report_safe':
			$report_header = $val;
			break;
		}
	} else {
		switch ( $pref ) {
		case 'rewrite_subject':
			$rewrite_subject = $val;
			break;
		case 'required_hits':
			$required_hits = $val;
			break;
		case 'report_header':
			$report_header = $val;
			break;
		case 'defang_mime':
			$defang_mime = $val;
			break;
		case 'use_terse_report':
			$use_terse_report = $val;
			break;
		}
	}
}


?>

<br>
<table class="tblbgcolor" align="left" border="1" cellpadding="3" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF" width="100%">
	<tr>
		<td class="titel-3" background="<?=$strSkinName?>/pics/bg_tbl_header.jpg" colspan="3"><? echo ltext("spam_general_sett"); ?></td>
	</tr>
	<form action="email_spam_config.php?pop3=<? echo $pop3; ?>" method=post>
	<input type=hidden name=update value=1>
	<tr>
		<td><input type=text size=2 maxlength=2 name=required_hits value="<? echo $required_hits; ?>"></td>
		<td class="titel-3"><? echo ltext("spam_max_hits"); ?></td>
		<td><? echo ltext("spam_max_hits_desc"); ?></td>
	</tr>
	<tr>
		<td colspan=2>&nbsp;</td>
		<td><? echo ltext("spam_max_hits_desc2"); ?></td>
	</tr>
	<tr>
		<td><input type=checkbox name="rewrite_subject" value=1 <? if($rewrite_subject == 1) { print "checked"; }?>></td>
		<td wrap=nowrap class="titel-3"><? echo ltext("spam_rewrite_subject"); ?></td>
		<td><? echo ltext("spam_rewrite_subject_desc"); ?>
		</td>
	</tr>
	<tr>
		<td><input type=checkbox name="report_header" value=1 <? if($report_header == 1) { print "checked"; }?>></td>
		<td class="titel-3"><? echo ltext("spam_report_header"); ?></td>
		<td><? echo ltext("spam_report_header_desc"); ?></td>
	</tr>
<!--
	<tr>
		<td><input type=checkbox name="defang_mime" value=1 <? if($defang_mime == 1) { print "checked"; }?>></td>
		<td class="titel-3"><? echo ltext("spam_defang_mime"); ?></td>
		<td><? echo ltext("spam_defang_mime_desc"); ?></td>
	</tr>
-->
	<tr>
		<td><input type=checkbox name="use_terse_report" value=1 <? if($use_terse_report == 1) { print "checked"; }?>></td>
		<td class="titel-3"><? echo ltext("spam_use_terse_report"); ?></td>
		<td><? echo ltext("spam_use_terse_report_desc"); ?>
		</td>
	</tr>
	<tr>
		<td colspan=3><? echo std_button(ltext("spam_update"), 'javascript:doChange(1)'); ?></td>
	</tr>
	</form>
</table>
	
	</td>
</tr>
</table>



<?
include("email_footer.php");

db_close();
?>
