b2evolution

Multilingual multiuser multiblog engine

b2evolution Technical Documentation (0.9.x) [ class tree: evocore ] [ index: evocore ] [ all elements ]

Source for file _functions_antispam.php

Documentation is available at _functions_antispam.php

  1. <?php
  2. /**
  3.  * Antispam handling functions
  4.  * 
  5.  * b2evolution - {@link http://b2evolution.net/}
  6.  * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
  7.  * @copyright (c)2003-2005 by Francois PLANQUE - {@link http://fplanque.net/}
  8.  *
  9.  * @package evocore
  10.  */
  11. if!defined('DB_USER') ) die'Please, do not access this page directly.' );
  12.  
  13.  
  14. /**
  15.  * antispam_create(-)
  16.  *
  17.  * Insert a new abuse string into DB
  18.  */
  19. function antispam_create$abuse_string$aspm_source 'local' )
  20. {
  21.     global $DB$querycount$tableantispam;
  22.     
  23.     // Cut the crap if the string is empty:
  24.     $abuse_string trim$abuse_string );
  25.     ifempty$abuse_string ) ) return false;
  26.     
  27.     // Check if the string already is in the blacklist:
  28.     ifantispam_check($abuse_string) ) return false;
  29.     
  30.     // Insert new string into DB:
  31.     $sql "INSERT INTO $tableantispam( aspm_string, aspm_source )
  32.                     VALUES( '".$DB->escape($abuse_string)."', '$aspm_source' )";
  33.     $DB->query$sql );
  34.  
  35.     return true;
  36. }
  37.  
  38.  
  39. /**
  40.  * antispam_update_source(-)
  41.  *
  42.  * Note: We search by string because we sometimes don't know the ID
  43.  * (e-g when download already in list/cache)
  44.  */
  45. function antispam_update_source$aspm_string$aspm_source )
  46. {
  47.     global $DB$tableantispam$querycount;
  48.     
  49.     $sql "UPDATE $tableantispam 
  50.                     SET aspm_source = '$aspm_source
  51.                     WHERE aspm_string = '".$DB->escape($aspm_string)."'";
  52.     $DB->query$sql );
  53. }
  54.  
  55. /*
  56.  * antispam_delete(-)
  57.  *
  58.  * Remove an entry from the ban list
  59.  */
  60. function antispam_delete$string_ID )
  61. {
  62.     global $tableantispam$DB;
  63.  
  64.     $sql "DELETE FROM $tableantispam 
  65.                     WHERE aspm_ID = $string_ID";
  66.     $DB->query$sql );
  67. }
  68.  
  69.  
  70. /**
  71.  * Check if a string contains abusive substrings
  72.  *
  73.  * Note: Letting the database do the LIKE %% match is a little faster than doing in it PHP,
  74.  * not to mention the incredibly long overhead of preloading the list into PHP
  75.  *
  76.  * @return string balcklisted keyword found or false if no spam detected
  77.  */
  78. function antispam_check$haystack )
  79. {
  80.      global $DB;
  81.  
  82.     if$block $DB->get_var"SELECT aspm_string
  83.                                    FROM  T_antispam
  84.                            WHERE ".$DB->quote($haystack)." LIKE CONCAT('%',aspm_string,'%')
  85.                            LIMIT 0, 1"00'Check URL against antispam balcklist' ) )
  86.     {
  87.             debug_log'Spam block: '.$block );
  88.             return $block;    // SPAM detected!
  89.     }
  90.  
  91.     return false;    // no problem.
  92.  
  93. }
  94.  
  95.  
  96. /*
  97.  * list_antiSpam(-)
  98.  *
  99.  * Extract anti-spam
  100.  */
  101. function list_antiSpam()
  102. {
  103.     global $DB$querycount$tableantispam$res_stats;
  104.  
  105.     $sql "SELECT aspm_ID, aspm_string, aspm_source 
  106.                     FROM $tableantispam 
  107.                     ORDER BY aspm_string ASC";
  108.     $res_stats $DB->get_results$sqlARRAY_A );
  109. }
  110.  
  111. /*
  112.  * antiSpam_ID(-)
  113.  */
  114. function antiSpam_ID()
  115. {
  116.     global $row_stats;
  117.     echo $row_stats['aspm_ID'];
  118. }
  119.  
  120. /**
  121.  * {@internal antiSpam_domain(-)}}
  122.  *
  123.  * @param mixed max length or false if we don't want to display
  124.  */
  125. function antiSpam_domain$dispmax 80 )
  126. {
  127.     global $row_stats;
  128.     $domain $row_stats['aspm_string'];
  129.     if$dispmax )
  130.         return $domain;
  131.  
  132.     $domain htmlspecialchars($domain);
  133.  
  134.     ifstrlen$domain $dispmax )
  135.         echo substr$domain0$dispmax )'...';
  136.     else
  137.         echo $domain;
  138. }
  139.  
  140.  
  141. /*
  142.  * antiSpam_domain(-)
  143.  */
  144. function antispam_source$disp true$raw false )
  145. {
  146.     global $row_stats$aspm_sources;
  147.     $asp_source $row_stats['aspm_source'];
  148.     if$raw )
  149.         $asp_source T_(    $aspm_sources[$asp_source);
  150.     if$disp )
  151.         echo $asp_source;
  152.     else
  153.         return $asp_source;
  154. }
  155.  
  156.  
  157. // -------------------- XML-RPC callers ---------------------------
  158.  
  159. /*
  160.  * b2evonet_report_abuse(-)
  161.  *
  162.  * pings b2evolution.net to report abuse from a particular domain
  163.  */
  164. function b2evonet_report_abuse$abuse_string$display true )
  165. {
  166.     global $debug$antispamsrv_host$antispamsrv_port$antispamsrv_uri;
  167.  
  168.     global $baseurl;
  169.     if$display )
  170.     {    
  171.         echo "<div class=\"panelinfo\">\n";
  172.         echo '<h3>'.T_('Reporting abuse to').' '.$antispamsrv_host."...</h3>\n";
  173.     }
  174.     if!preg_match'#^http://localhost[/:]#'$baseurl|| $antispamsrv_host == 'localhost' ) )
  175.     {    // Local install can only report to local test server
  176.         // Construct XML-RPC client:
  177.         $client new xmlrpc_client$antispamsrv_uri$antispamsrv_host$antispamsrv_port);
  178.         $client->debug $debug;
  179.         
  180.         // Construct XML-RPC message:
  181.         $message new xmlrpcmsg
  182.                                     'b2evo.reportabuse',                                                 // Function to be called
  183.                                     array
  184.                                         new xmlrpcval(0,'int'),                                        // Reserved
  185.                                         new xmlrpcval('annonymous','string'),            // Reserved
  186.                                         new xmlrpcval('nopassrequired','string'),    // Reserved
  187.                                         new xmlrpcval($abuse_string,'string'),        // The abusive string to report
  188.                                         new xmlrpcval($baseurl,'string'),                    // The base URL of this b2evo
  189.                                     )  
  190.                                 );
  191.         $result $client->send($message);
  192.         if$ret xmlrpc_displayresult$result ) )
  193.         {    // Remote operation successful:
  194.             antispam_update_source$abuse_string'reported' );
  195.         }
  196.  
  197.         if$display echo '<p>'T_('Done.')"</p>\n</div>\n";
  198.         return($ret);
  199.     
  200.     else 
  201.     {
  202.         if$display echo "<p>"T_('Aborted (Running on localhost).')"</p>\n</div>\n";
  203.         return(false);
  204.     }
  205. }
  206.  
  207.  
  208. /*
  209.  * b2evonet_poll_abuse(-)
  210.  *
  211.  * request abuse list from central blacklist
  212.  */
  213. function b2evonet_poll_abuse$display true )
  214. {
  215.     global $Settings$baseurl$debug$antispamsrv_host$antispamsrv_port$antispamsrv_uri;
  216.     
  217.     if$display )
  218.     {    
  219.         echo "<div class=\"panelinfo\">\n";
  220.         echo '<h3>'T_('Requesting abuse list from').' '.$antispamsrv_host.'...'"</h3>\n";
  221.     }
  222.  
  223.     // Construct XML-RPC client:
  224.     $client new xmlrpc_client$antispamsrv_uri$antispamsrv_host$antispamsrv_port);
  225.     $client->debug $debug;
  226.     
  227.     // Get datetime from last update, because we only want newer stuff...
  228.     $m $Settings->get'antispam_last_update' );
  229.     // Encode it in the XML-RPC format
  230.     echo '<p>'T_('Latest update timestamp')': '$m'</p>';
  231.     $startat mysql2date'Ymd\TH:i:s'$m );
  232.     //$startat = iso8601_encode( mktime(substr($m,11,2),substr($m,14,2),substr($m,17,2),substr($m,5,2),substr($m,8,2),substr($m,0,4)) );
  233.     
  234.     // Construct XML-RPC message:
  235.     $message new xmlrpcmsg
  236.                                 'b2evo.pollabuse',                                                         // Function to be called
  237.                                 array
  238.                                     new xmlrpcval(0,'int'),                                            // Reserved
  239.                                     new xmlrpcval('annonymous','string'),                // Reserved
  240.                                     new xmlrpcval('nopassrequired','string'),        // Reserved
  241.                                     new xmlrpcval($startat,'dateTime.iso8601'),    // Datetime to start at
  242.                                     new xmlrpcval(0,'int')                                            // Reserved
  243.                                 )  
  244.                             );
  245.     $result $client->send($message);
  246.     
  247.     if$ret xmlrpc_displayresult$result ) )
  248.     {    // Response is not an error, let's process it:
  249.         $response $result->value();
  250.         if$response->kindOf(== 'struct' )
  251.         {    // Decode struct:
  252.             $response xmlrpc_decode_recurse($response);
  253.             if!isset$response['strings'|| !isset$response['lasttimestamp') )
  254.             {    
  255.                 echo T_('Incomplete reponse.')."\n";
  256.                 $ret false;
  257.             }
  258.             else
  259.             {    // Start registering strings: 
  260.                 $value $response['strings'];
  261.                 ifcount($value== )
  262.                 {
  263.                     echo '<p>'T_('No new blacklisted strings are available.')'</p>';
  264.                 }
  265.                 else
  266.                 {    // We got an array of strings:
  267.                     echo '<p>'T_('Adding strings to local blacklist')':</p><ul>';
  268.                     foreach($value as $banned_string)
  269.                     {
  270.                         echo '<li>'T_('Adding:')' ['$banned_string'] : ';
  271.                         ifantispam_create$banned_string'central' ) )
  272.                         {    // Creation successed
  273.                             echo T_('OK.');
  274.                         }
  275.                         else
  276.                         // Was already handled
  277.                             echo T_('Not necessary! (Already handled)');
  278.                             antispam_update_source$banned_string'central' );
  279.                         }
  280.                         echo '</li>';
  281.                     }
  282.                     echo '</ul>';
  283.                     // Store latest timestamp:
  284.                     $endedat date('Y-m-d H:i:s'iso8601_decode($response['lasttimestamp']) );
  285.                     echo '<p>'T_('New latest update timestamp')': '$endedat'</p>';
  286.                     
  287.                     $Settings->set'antispam_last_update'$endedat );
  288.                     $Settings->updateDB();
  289.                 }                
  290.             }
  291.         }
  292.         else
  293.         {
  294.             echo T_('Invalid reponse.')."\n";
  295.             $ret false;
  296.         }
  297.     }
  298.  
  299.     if$display echo '<p>'T_('Done.')"</p>\n</div>\n";
  300.     return($ret);
  301. }
  302.  
  303.  
  304. ?>

Documentation generated on Tue, 20 May 2008 01:54:33 +0200 by phpDocumentor 1.4.2