code.fastix.org

Dateiansicht:

Datei:Projekte -> PHP:Spam-Erkennung für Mail- und Kontaktformulare -> SpamDetector.php
md5:3da11d9bd29d5af83daf2a405663f628
sha1:08242237b782ed1942848dbd350ec58058022321
Download-Link:Download
  1. <?php
  2.  
  3. /**
  4. * @author: Jörg Reinholz, fastix WebDesign & Consult, Kassel - http://www.fastix.org/
  5. * @version: 1.1
  6. * @license: https://code.fastix.org/lizenz.php
  7. **/
  8.  
  9. class SpamDetector {
  10.     private $sWortliste     = 'known_spam.txt';
  11.     private $arSpamRegex    = false;
  12.     private $arReplacements = false;
  13.  
  14.     function __construct() {
  15.    
  16.         $this -> sWortliste = __DIR__ . '/' . $this -> sWortliste;
  17.  
  18.         # SpamRegex: Eintrag beginnt mit Link:
  19.  
  20.         $arURLRegex[] = '^ *https{0,1}:\/\/';
  21.         $arURLRegex[] = '^ *<a href *= *';
  22.         $arURLRegex[] = '^ *\[url *= *';
  23.  
  24.         #3 Links:
  25.  
  26.         $arURLRegex[] = 'https{0,1}:\/\/.*https{0,1}:\/\/.*https{0,1}:\/\/';
  27.  
  28.        
  29.         $this -> arURLRegex = $arURLRegex;
  30.  
  31.         #5 Sonderzeichen aufeinander:
  32.  
  33.         $arHexRegex[] = '&#[0-9a-f]{2,4};&#[0-9a-f]{2,4};&#[0-9a-f]{2,4};&#[0-9a-f]{2,4};&#[0-9a-f]{2,4};';
  34.         $this -> arHexRegex = $arHexRegex;
  35.  
  36.         #Spam-Begriffe:
  37.  
  38.         $ar = file( $this -> sWortliste );
  39.         foreach ( $ar as $wort ) {
  40.             $wort = trim( $wort );
  41.             if ( '' !==  $wort ) {
  42.                 $arSpamRegex[] = strtolower( $wort );
  43.             }
  44.         }
  45.         $this -> arSpamRegex = $arSpamRegex;
  46.  
  47.         #Ersetzungen:
  48.  
  49.         $arReplacements['i'] = '########I#######';
  50.         $arReplacements['1'] = '########I#######';
  51.         $arReplacements['l'] = '########I#######';
  52.         $arReplacements['o'] = '########O#######';
  53.         $arReplacements['a'] = '########A#######';
  54.         $arReplacements['c'] = '########C#######';
  55.         $arReplacements['z'] = '########C#######';
  56.  
  57.         $arReplacements['########I#######'] = '[il1]';
  58.         $arReplacements['########O#######'] = '[o0]';
  59.         $arReplacements['########A#######'] = '[a@]';
  60.         $arReplacements['########C#######'] = '[czxs]';
  61.  
  62.         $this -> arReplacements = $arReplacements;
  63.     }
  64.  
  65.     function detect( $str ) {
  66.  
  67.         $str = trim( strtolower( $str ) );
  68.         $str = str_replace("\t", "    ", $str );
  69.  
  70.         foreach ( $this -> arURLRegex as $strURLRegex ) {
  71.                         if ( preg_match( '#' . str_replace( '#', '\#',  $strURLRegex ) . '#',  $str ) ) {
  72.                                 return true;
  73.                         }
  74.         }
  75.        
  76.         foreach ( $this -> arHexRegex as $strHexRegex ) {
  77.                         if ( preg_match( '#' . str_replace( '#', '\#',  $strHexRegex ) . '#',  $str ) ) {
  78.                                 return true;
  79.                         }
  80.         }        
  81.        
  82.         $arKeys = array_keys( $this -> arReplacements );
  83.  
  84.         foreach ( $this -> arSpamRegex as $strSpamRegex ) {
  85.             foreach ( $arKeys as $key ) {
  86.                     $strSpamRegex = trim( str_replace( $key, $this -> arReplacements[$key], $strSpamRegex ) );
  87.             }
  88.             if ( preg_match( '#' . str_replace( '#', '\#',  $strSpamRegex ) . '#',  $str ) ) {
  89.                 return true;
  90.             }
  91.         }
  92.         return false;
  93.     }
  94. }
  95.