<?PHP
//
//  mt-blacklist.php
//
//  Version 1.0     Initial Release
//  Version 1.1   - 04/28/2005
//  Version 1.21  - 07/20/2005
//  Version 1.22  - 08/01/2005
//  Version 1.3   - 08/23/2005
//  Version 1.31  - 08/29/2005
//  Version 1.4   - 10/24/2005
//  Version 1.5   - 08/21/2006
//

$eMailAddress = "";		// Change this to your e-mail address or "" if you don't want e-mail
$lockTime = 0;				// Change this to the number of seconds to wait - this may tie up a thread on the spammers server

function check_Blacklist( $iStr ) {
	//  $arcDir is the Default archive directory 
	//    Enter either a fully qualified path or a path relative to the wiki root 
	//
	//  MUST be writable!
	//
	//  '' 					= parent of "cookbook" directory.
	//  'cookbook/'	= "cookbook" directory.
	//
	$arcDir = '';  

  //  Decode encoded URL's
  $str = html_entity_decode( rawurldecode( $iStr ) );
  $str = preg_replace( "/&#([0-9]+);/e", "chr( \\1 )", $str );

	//  Build list of back lists to check
	$BlackLists = array(
		  //  MT-Blacklist no longer available 10/24/2005
			//'MT-Blacklist'	=>	'http://www.jayallen.org/comment_spam/blacklist.txt',
  		'Chongqed' => 'http://blacklist.chongqed.org/',
      'MoinMaster' => 'http://moinmaster.wikiwikiweb.de/BadContent?action=raw' );

	//  Loop through black lists
	foreach( $BlackLists as $MTB_name => $MTB_url ) {

		// Automatic File Update
  	$MTB_time = 86400;		// Time in seconds since last update of master file - 0 = don't update																									
		$MTB_fname = $arcDir . $MTB_name . ".txt";		// local file name
	
		//  Check to see if Blacklist file is in current directory and it's current
  	if( $MTB_time > 0 )
  			if( ( ! ($MTStat = @stat( $MTB_fname ) ) ) || ( $MTStat["mtime"] < (time()-($MTB_time)) ) ) {
	 				//  Download file 
	 				if( ( $MTB = @file($MTB_url) ) )
	 						if( $MTB_file = fopen( $MTB_fname, "wb" ) ) {
	  						fwrite( $MTB_file, implode( '', $MTB ) );
	  						fclose( $MTB_file );
	 						}
	  		}
  
  	if(( ! isset( $MTB ) ) || ( ! $MTB ) )
  			$MTB = file( $MTB_fname );
  		
  	//  Check post against regular expressions		
  
  	$count[ $MTB_name ] = 0;
  	$expcnt[ $MTB_name ] = 0;
  	foreach( $MTB as $MTB_check ) {  	
	  	$MTB_check = preg_replace( "/( *#.*)*$/", "", trim( $MTB_check ) );

      //  Quote the "." to avoid false positives
      if( ! strpos( $MTB_check, "\\" ) )
          $MTB_check = preg_replace( "/\./", "\\.", $MTB_check );

	  	if( $MTB_check != "" ) 
	  	    if( ($matched = preg_match_all( "~" . $MTB_check . "~i", $str, $matches ) ) ) {
	  					$count[$MTB_name] += $matched;
	  					$expcnt[ $MTB_name ] ++;
  				}
	  	}
	  	  
	  //  Clear Regular Expression list
		unset( $MTB );
 	}

 	//  Build return string
	$ret = "";
	foreach( $count as $MTB_name => $found )
			if( $found > 0 )
					$ret .= sprintf( "Found %6d expression(s) with %6d total hit(s) using %s\n", $expcnt[ $MTB_name ], $found, $MTB_name );
 				
	//  Return found counts
	if( $ret == "" )
 			return( FALSE );
 	else
 			return( $ret );
}

// Skip the Blocklist Cookbook control file (so you can add "special" words in addition to this
if( ($pagename != "Main.Blocklist") && ($pagename != "Site.Blocklist") )
  if( $ret = check_Blacklist( $_POST['text'] ) ) { 
	  
	  //  E-Mail the administrator if an e-mail address is entered
    if( $eMailAddress != "" )
        mail( $eMailAddress, "Blocked Wiki Comment", 
    			sprintf( "BLOCKED Wiki Post\n\nHTTP_REFERER: %s\nREMOTE_ADDR: %s\n\n%s\n\n-------- Blocked Content ---------\n%s", 
        		$_SERVER['HTTP_REFERER'], $_SERVER['REMOTE_ADDR'], $ret, $_POST['text'] ) );
        		
    $EnablePost = 0; 
    unset($_POST['post']); 
    unset($_POST['postattr']);
    
    //
    //  This is the message returned to the user.
    //
    $EditMessageFmt .= "<h3 class='wikimessage'>$[This post has been blocked by the administrator]</h3>";
    
    //  Version 1.5 changes - Thanks John!
    //  Added support for pmwiki-2.1.14 
    global $MessagesFmt;
    $MessagesFmt[] = $EditMessageFmt;
    
    //  sleep (Lock) the process for LockTime seconds    
    if( $lockTime > 0 )    
        sleep( $lockTime );
  }
?>