Subversion Repositories php_antispam

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. // This is an example of the ViaThinkSoft AntiSpam 3.02
  4. // for ViaThinkSoft Sigma as filter plugin (modified $content)
  5. // Use it for your website!
  6.  
  7. // CONFIGURATION
  8.  
  9. define('CFG_MAKE_MAIL_ADDRESSES_CLICKABLE', true);
  10.  
  11. // CODE
  12.  
  13. function secure_email_triv($email)
  14. {
  15.         if (!function_exists('alas_js_crypt'))
  16.         {
  17.                 function alas_js_crypt($text)
  18.                 {
  19.                         $tmp = '';
  20.                         for ($i=0; $i<strlen($text); $i++)
  21.                         {
  22.                                 $tmp .= 'document.write("&#'.ord(substr($text, $i, 1)).';");';
  23.                         }
  24.                         return $tmp;
  25.                 }
  26.         }
  27.  
  28.         $aus = '';
  29.         if ($email != '')
  30.         {
  31.                 $aus .= '<script language="JavaScript" type="text/javascript"><!--'."\n";
  32.                 $aus .= alas_js_crypt($email);
  33.                 $aus .= '// --></script>';
  34.         }
  35.         return $aus;
  36. }
  37.  
  38. // PHP-AntiSpam-Funktion "secure_email", Version 3.02
  39. // von Daniel Marschall [www.daniel-marschall.de]
  40.  
  41. function secure_email($email, $linktext, $crypt_linktext)
  42. {
  43.         // No new lines to avoid a JavaScript error!
  44.         $linktext = str_replace("\r", ' ', $linktext);
  45.         $linktext = str_replace("\n", ' ', $linktext);
  46.  
  47.         if (!function_exists('alas_js_crypt'))
  48.         {
  49.                 function alas_js_crypt($text)
  50.                 {
  51.                         $tmp = '';
  52.                         for ($i=0; $i<strlen($text); $i++)
  53.                         {
  54.                                 $tmp .= 'document.write("&#'.ord(substr($text, $i, 1)).';");';
  55.                         }
  56.                         return $tmp;
  57.                 }
  58.         }
  59.  
  60.         if (!function_exists('alas_js_write'))
  61.         {
  62.                 function alas_js_write($text)
  63.                 {
  64.                         $text = str_replace('\\', '\\\\', $text);
  65.                         $text = str_replace('"', '\"', $text);
  66.                         $text = str_replace('/', '\/', $text); // W3C Validation </a> -> <\/a>
  67.                         return 'document.write("'.$text.'");';
  68.                 }
  69.         }
  70.  
  71.         $aus = '';
  72.         if ($email != '')
  73.         {
  74.                 $aus .= '<script language="JavaScript" type="text/javascript"><!--'."\n";
  75.                 $aus .= alas_js_write('<a href="');
  76.                 $aus .= alas_js_crypt('mailto:'.$email);
  77.                 $aus .= alas_js_write('">');
  78.                 $aus .= $crypt_linktext ? alas_js_crypt($linktext) : alas_js_write($linktext);
  79.                 $aus .= alas_js_write('</a>').'// --></script>';
  80.         }
  81.         return $aus;
  82. }
  83.  
  84. function getAddrSpec() {
  85.         // Ref: http://www.iamcal.com/publish/articles/php/parsing_email/
  86.  
  87.         $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
  88.         $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
  89.         $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
  90.                 '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
  91.         $quoted_pair = '\\x5c[\\x00-\\x7f]';
  92.         $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
  93.         $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
  94.         $domain_ref = $atom;
  95.         $sub_domain = "($domain_ref|$domain_literal)";
  96.         $word = "($atom|$quoted_string)";
  97.         $domain = "$sub_domain(\\x2e$sub_domain)*";
  98.         $local_part = "$word(\\x2e$word)*";
  99.         $addr_spec = "$local_part\\x40$domain";
  100.  
  101.         return $addr_spec;
  102. }
  103.  
  104. function is_valid_email_address($email){
  105.         $addr_spec = getAddrSpec();
  106.         return preg_match("!^$addr_spec$!", $email) ? true : false;
  107. }
  108.  
  109. function auto_secure_mail_addresses($content) {
  110.         $addr_spec = getAddrSpec();
  111.  
  112.         // Step 1: Parse links and make them secure
  113.  
  114.         if (!function_exists('link_cb_1')) {
  115.                 function link_cb_1($a) {
  116.                         $mailaddr = $a[2];
  117.                         $linktext = $a[14]; // Letztes
  118.  
  119.                         return secure_email($mailaddr, $linktext, is_valid_email_address($linktext));
  120.                 }
  121.         }
  122.  
  123.         $content = preg_replace_callback("/<a(.+?)mailto:($addr_spec)(.+?)>(.+?)<\/a>/sm", 'link_cb_1', $content); // TODO! Kann Greedy werden!
  124.  
  125.         // Step 2: Find all further mail addresses, make then clickable and prevent spam bots
  126.  
  127.         if (!function_exists('link_cb_2')) {
  128.                 function link_cb_2($a) {
  129.                         $mailaddr = $a[1]; // Letztes
  130.  
  131.                         if (CFG_MAKE_MAIL_ADDRESSES_CLICKABLE) {
  132.                                 return secure_email($mailaddr, $mailaddr, true);
  133.                         } else {
  134.                                 return secure_email_triv($mailaddr);
  135.                         }
  136.                 }
  137.         }
  138.  
  139.         $content = preg_replace_callback("/($addr_spec)/sm", 'link_cb_2', $content);
  140.  
  141.         // Output
  142.  
  143.         return $content;
  144. }
  145.  
  146. $content = auto_secure_mail_addresses($content);
  147.  
  148. ?>
  149.