phpDocumentor pond
[ class tree: pond ] [ index: pond ] [ all elements ]

Source for file _antispam.funcs.php

Documentation is available at _antispam.funcs.php

  1. <?php
  2. /**
  3.  * This file implements Antispam handling functions.
  4.  *
  5.  * This file is part of the Quam Plures project - {@link http://quamplures.net/}.
  6.  * See also {@link https://launchpad.net/quam-plures}.
  7.  *
  8.  * @copyright (c) 2009 - 2011 by the Quam Plures developers - {@link http://quamplures.net/}
  9.  * @copyright (c)2003-2009 by Francois PLANQUE - {@link http://fplanque.net/}.
  10.  *  Parts of this file are copyright (c)2004-2005 by Daniel HAHLER - {@link http://thequod.de/contact}.
  11.  *  Parts of this file are copyright (c)2004 by Vegar BERG GULDAL - {@link http://funky-m.com/}.
  12.  *  Parts of this file are copyright (c)2005 by The University of North Carolina at Charlotte as
  13.  *  contributed by Jason Edgecombe {@link http://tst.uncc.edu/}.
  14.  *
  15.  * @license http://quamplures.net/license.html GNU General Public License (GPL)
  16.  *
  17.  *  {@internal Open Source relicensing agreement:
  18.  *  Daniel HAHLER grants Francois PLANQUE the right to license
  19.  *  Daniel HAHLER's contributions to this file and the b2evolution project
  20.  *  under any OSI approved OSS license (http://www.opensource.org/licenses/).
  21.  *
  22.  *  Vegar BERG GULDAL grants Francois PLANQUE the right to license
  23.  *  Vegar BERG GULDAL's contributions to this file and the b2evolution project
  24.  *  under any OSI approved OSS license (http://www.opensource.org/licenses/).
  25.  *
  26.  *  The University of North Carolina at Charlotte grants Francois PLANQUE the right to license
  27.  *  Jason EDGECOMBE's contributions to this file and the b2evolution project
  28.  *  under the GNU General Public License (http://www.opensource.org/licenses/gpl-license.php)
  29.  *  and the Mozilla Public License (http://www.opensource.org/licenses/mozilla1.1.php).
  30.  *   }}}
  31.  *
  32.  *  {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  33.  * @author blueyed: Daniel HAHLER.
  34.  * @author fplanque: Francois PLANQUE.
  35.  * @author vegarg: Vegar BERG GULDAL.
  36.  *
  37.  * @package pond
  38.  */
  39. if!defined('QP_MAIN_INIT') ) die'Please, do not access this page directly.' );
  40.  
  41.  
  42. /**
  43.  * antispam_create(-)
  44.  *
  45.  * Insert a new abuse string into DB
  46.  */
  47. function antispam_create$abuse_string$aspm_source 'local' )
  48. {
  49.     global $DB;
  50.  
  51.     // Cut the crap if the string is empty:
  52.     $abuse_string trim$abuse_string );
  53.     ifempty$abuse_string ) )
  54.     {
  55.         return false;
  56.     }
  57.  
  58.     // Check if the string already is in the blacklist:
  59.     ifantispam_check($abuse_string) )
  60.     {
  61.         return false;
  62.     }
  63.  
  64.     // Insert new string into DB:
  65.     $sql "INSERT INTO T_antispam( aspm_string, aspm_source )
  66.                     VALUES( '".$DB->escape($abuse_string)."', '$aspm_source' )";
  67.     $DB->query$sql );
  68.  
  69.     return true;
  70. }
  71.  
  72.  
  73. /**
  74.  * antispam_update_source(-)
  75.  *
  76.  * Note: We search by string because we sometimes don't know the ID
  77.  * (e-g when download already in list/cache)
  78.  */
  79. function antispam_update_source$aspm_string$aspm_source )
  80. {
  81.     global $DB;
  82.  
  83.     $sql "UPDATE T_antispam
  84.                     SET aspm_source = '$aspm_source'
  85.                     WHERE aspm_string = '".$DB->escape($aspm_string)."'";
  86.     $DB->query$sql );
  87. }
  88.  
  89. /*
  90.  * antispam_delete(-)
  91.  *
  92.  * Remove an entry from the ban list
  93.  */
  94. function antispam_delete$string_ID )
  95. {
  96.     global $DB;
  97.  
  98.     $sql "DELETE FROM T_antispam
  99.                     WHERE aspm_ID = $string_ID";
  100.     $DB->query$sql );
  101. }
  102.  
  103.  
  104. /**
  105.  * Check if a string contains abusive substrings
  106.  *
  107.  * Note: Letting the database do the LIKE %% match is a little faster than doing in it PHP,
  108.  * not to mention the incredibly long overhead of preloading the list into PHP
  109.  *
  110.  * @todo dh> IMHO this method is too generic used! It gets used for:
  111.  *            - comment author name
  112.  *            - comment/message author email
  113.  *            - comment content
  114.  *            - message (email) content
  115.  *            - validate_url
  116.  *            ..and validates all this against the antispam blacklist!
  117.  *            We should rather differentiate here more and make it pluggable!
  118.  *
  119.  * @return string blacklisted keyword found or false if no spam detected
  120.  */
  121. function antispam_check$haystack )
  122. {
  123.     global $DB$Debuglog$Timer;
  124.  
  125.     // TODO: 'SELECT COUNT(*) FROM T_antispam WHERE aspm_string LIKE "%'.$url.'%" ?
  126.  
  127.     $Timer->resume'antispam_url' )// resuming to get the total number..
  128.     $block $DB->get_var(
  129.         'SELECT aspm_string
  130.            FROM  T_antispam
  131.           WHERE '.$DB->quote($haystack).' LIKE CONCAT("%",aspm_string,"%")
  132.           LIMIT 0, 1'00'Check URL against antispam blacklist' );
  133.     if$block )
  134.     {
  135.             $Debuglog->add'Spam block: '.$block );
  136.             return $block;    // SPAM detected!
  137.     }
  138.     $Timer->pause'antispam_url' );
  139.  
  140.     return false;    // no problem.
  141. }
  142.  
  143.  
  144. /**
  145.  * Get the base domain that could be blacklisted from an URL.
  146.  *
  147.  * We want to concentrate on the main domain and we want to prefix it with either . or // in order not
  148.  * to blacklist too large.
  149.  *
  150.  * {@internal This function gets tested in _misc.funcs.simpletest.php}}
  151.  *
  152.  * @param string URL or domain
  153.  * @return string|falsethe pattern to match this domain in the blacklist; false if we could not extract the base domain
  154.  */
  155. function get_ban_domain$url )
  156. {
  157.     // Remove http:// part + everything after the last path element ( '/' alone is ignored on purpose )
  158.     $domain preg_replace'~^ ([a-z]+://)? ([^/#]+) (/ ([^/]*/)+ )? .* ~xi''\\2\\3'$url );
  159.  
  160.     ifpreg_match'~^[0-9.]+$~'$domain ) )
  161.     {    // All numeric = IP address, don't try to cut it any further
  162.         return '//'.$domain;
  163.     }
  164.  
  165.     // Remove any www*. prefix:
  166.     $base_domain preg_replace'~^(www \w* \. )~xi'''$domain );
  167.  
  168.     ifempty($base_domain) )
  169.     {
  170.         return false;
  171.     }
  172.  
  173.     ifstrlen$base_domain strlen$domain ) )
  174.     {    // The guy is spamming with subdomains (or www):
  175.         return '.'.$base_domain;
  176.     }
  177.  
  178.     // The guy is spamming with the base domain:
  179.     return '//'.$base_domain;
  180. }
  181.  
  182.  
  183. ?>