<?
/**
* ThunderbirdJunkLogToPostfixHeaderCheck
* Copyright (C) 2009 Basilio Briceno Hernandez <hide@address.com>
*
* ThunderbirdJunkLogToPostfixHeaderCheck is free software: you can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, version 3 of the License.
*
* ThunderbirdJunkLogToPostfixHeaderCheck is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with ThunderbirdJunkLogToPostfixHeaderCheck.
* If not, see <http://www.gnu.org/licenses/lgpl.html>.
*/
/**
* Provides a way to convert the Mozilla Thunderbird's JunkLog into a Postfix's header_check file
*
* @author Basilio Briceno <hide@address.com>
* @license http://www.gnu.org/licenses/lgpl.html
* @copyright Copyright (c) 2009, Basilio Briceno
* @version 1.0
*/
class ThunderbirdJunkLogToPostfixHeaderCheck {
/**
* @var string Junklog's location
*/
public $junklog;
/**
* Sets the location of the junklog
*/
public function __construct ( $junklog )
{
$this->junklog = $junklog;
}
/**
* Parses the junklog and returns the headercheck string
*
* @return string
*/
public function parse ()
{
$file = file_get_contents( $this->junklog );
preg_match_all( '/.*\-\s(.*)\sat\s(.*)$/m', $file, $match );
unset( $file );
$array = array_unique( $match[1] );
unset( $match );
$response = '';
foreach( $array as $item ) {
$response .= '/^Subject: ' . self::clearString( $item ) .
"/\tREJECT Your email is considered SPAM.\n";
}
return $response;
}
/**
* Returns the parsed junklog
*
* @return string
*/
public function getParsedJunkLog ()
{
return $this->parse();
}
/**
* Write headercheck into file
*
* @param $path
*/
public function writeParsedJunkLog ( $path )
{
if ( !is_writable( $path ) ) {
return false;
}
if ( !file_put_contents( $path, $this->parse() ) ) {
return false;
}
return true;
}
/**
* Clear string from special characters
*
* @param string $log
* @return string
*/
public static function clearString ( $log )
{
$str = str_replace( '$', '\$', $str );
$str = str_replace( '?', '\?', $str );
$str = str_replace( '!', '\!', $str );
$str = str_replace( '-', '\-', $str );
$str = str_replace( '*', '\*', $str );
$str = str_replace( '#', '\#', $str );
$str = str_replace( "'", '\'', $str );
$str = str_replace( '"', '\"', $str );
$str = str_replace( '(', '\(', $str );
$str = str_replace( ')', '\)', $str );
$str = str_replace( '&', '\&', $str );
$str = str_replace( '.', '\.', $str );
$str = str_replace( '=', '\=', $str );
$str = str_replace( '[', '\[', $str );
$str = str_replace( ']', '\]', $str );
$str = str_replace( '{', '\{', $str );
$str = str_replace( '}', '\}', $str );
$str = str_replace( '~', '\~', $str );
return str_replace( '/', '\/', $str );
}
}