<?php
//Database
$USER = "spam_database";
$PASS = "PASSWORD";
$HOST = "localhost";
$DBNM = "spamdb";
$SPA_FILE = "/var/lib/spamassassin/3.004001/your_folder/60_prefix_spamrules.cf"; # Path to SPAMASSISSIN Rule File
$SPA_PREFIX = "YOURPREFIX";
$SPA_DESC_PREFIX = "MY SPAM Protection";
$SPA_CHECK_CMD = "spamassassin --lint";
$SPA_RELOAD_CMD = "/etc/init.d/spamassassin reload";
$PF_DESC_PREFIX = "YOURPREFIX_SPAM";
$PF_HEADER_FILE = "/etc/postfix/header_checks";
$PF_BODY_FILE = "/etc/postfix/body_checks";
$PF_ACTION = "REJECT";
$PF_RELOAD_CMD = "postfix reload";
$SPAM_VALUE = 4;
$PF_PRE_RULE_SET = "/^X-Spam-Flag: .YES/ REJECT SPAM" . "\n";
$PF_PRE_RULE_SET .= "/^\s*(Received: from)[^\\n]*(.*)/ REPLACE $1 [127.0.0.1] (localhost [127.0.0.1])$2" . "\n";
$PF_PRE_RULE_SET .= "/^\s*X-Originating-IP:/ IGNORE" . "\n";
$PF_PRE_RULE_SET .= "/^\s*X-Mailer: .*/ IGNORE" . "\n";
$PF_PRE_RULE_SET .= "/^\s*User-Agent/ IGNORE" . "\n";
$PF_PRE_RULE_SET .= "/^\s*X-Enigmail/ IGNORE" . "\n";
$PF_PRE_RULE_SET .= "/^\s*X-KMail .*/ IGNORE" . "\n";
$pdo = new PDO("mysql:host=localhost;dbname=$DBNM", $USER, $PASS);
$spa_rules = "";
$postfix_header_rules = "# ANONYMIZE HEADERS\n" . "# #####################################################################" . "\n" . $PF_PRE_RULE_SET . "\n\n" . "# SPAM RULES" . "\n" . "# #####################################################################" . "\n";
$postfix_body_rules = "# SPAM RULES" . "\n" . "# #####################################################################" . "\n";
$output = "";
$spa_count = 0;
$pf_header_count = 0;
$pf_body_count = 0;
$sql = "SELECT rule_id, type, header, value, score FROM rules";
foreach ($pdo->query($sql) as $row) {
$name = $SPA_PREFIX . "_" . $row['type'] . "_" . $row['rule_id'];
$name = strtoupper($name);
if ( !empty($row['header']) ) {
$delimiter = $row['header'] . ':=~';
} else{
$delimiter = '';
}
$spa_rules .= $row['type'] . " " . $name . "\t\t" . $delimiter . "/" . utf8_encode( spa_replace_special_chars($row['value']) ) . "/" . "\n";
$spa_rules .= "describe " . $name . "\t\t" . strtoupper($SPA_DESC_PREFIX . " " . $row['type'] . " " . $row['rule_id']) . "\n";
$spa_rules .= "score " . $name . "\t\t" . $row['score'] . "\n" . "\n";
$spa_count++;
// Postfix Header Rules
if (!empty($row['header']) && $row['score'] >= $SPAM_VALUE) {
$pf_value = utf8_encode( $row['value'] );
$postfix_header_rules .= "/^" . $row['header'] . ":.*" . postfix_replace_special_chars($pf_value) . "/" . "\t\t" . $PF_ACTION . " " . $PF_DESC_PREFIX . "_" . strtoupper($row['type']) . "_" . $row['rule_id'] . "\n";
$pf_header_count++;
}
// Postfix Body Rules
if ($row['type'] === 'body' && $row['score'] >= $SPAM_VALUE) {
$pf_value = utf8_encode( $row['value'] );
$postfix_body_rules .= "/" . postfix_replace_special_chars($pf_value) . "/" . "\t\t" . $PF_ACTION . " " . $PF_DESC_PREFIX . "_" . strtoupper($row['type']) . "_" . $row['rule_id'] . "\n";
$pf_body_count++;
}
}
$output .= "Start writing to Files" . "\n";
$spa_bytes = file_put_contents($SPA_FILE, $spa_rules);
if ($spa_bytes != false && $spa_bytes > 0){
$output .= $spa_count . " " . "SpamAssassin Rules created" . " with " . $spa_bytes . " Bytes" . "\n";
} else {
$output .= "Error writing SpamAssassin Rules". "\n";
}
$pf_header_bytes = file_put_contents($PF_HEADER_FILE, $postfix_header_rules);
if ($pf_header_bytes != false && $pf_header_bytes > 0){
$output .= $pf_header_count . " " . "Postfix HEADER Rules created" . " with " . $pf_header_bytes . " Bytes" . "\n";
} else {
$output .= "Error writing Postfix HEADER Rules". "\n";
}
$pf_body_bytes = file_put_contents($PF_BODY_FILE, $postfix_body_rules);
if ($pf_body_bytes != false && $pf_body_bytes > 0){
$output .= $pf_body_count . " " . "Postfix BODY Rules created" . " with " . $pf_body_bytes . " Bytes" . "\n";
} else {
$output .= "Error writing Postfix BODY Rules". "\n";
}
$output .= shell_exec($SPA_CHECK_CMD); // Restart Spamassassin
$output .= "Restart SpamAssassin:". "\n";
$output .= shell_exec($SPA_RELOAD_CMD); // Restart Spamassassin
$output .= "\n";
$output .= "Restart Postfix:". "\n";
$output .= shell_exec($PF_RELOAD_CMD); // Restart Postfix
$output .= "\n";
echo "$output";
function postfix_replace_special_chars($string){
$search = array("Ä", "Ö", "Ü", "ä", "å", "ö", "ü", "ß", "è", "é", "€", "@", "&", ">", "<", "°", "#", ":", " ", "-", "«", "»", "%","?", "/", "*");
$replace = array(".*", ".*", ".*", ".*", ".*", ".*", ".*", ".*", ".*", ".*", ".*", "\@", ".*", ".*", ".*", ".*", "\#", "\:", ".", ".", "\«", "\»", "\%", "\?", "\/", ".*");
return str_replace($search, $replace, $string);
}
function spa_replace_special_chars($string){
$search = array("€", "@", "&", ">", "<", "°", "#", ":", "%");
$replace = array(".*", "\@", ".*", ".*", ".*", ".*", "\#", "\:", "\%");
return str_replace($search, $replace, $string);
}