4 * Some functions to help implement an external link filter for spam control.
6 * TODO: implement the filter. Currently these are just some functions to help
7 * maintenance/cleanupSpam.php remove links to a single specified domain. The
8 * next thing is to implement functions for checking a given page against a big
11 * Another cool thing to do would be a web interface for fast spam removal.
17 function matchEntry( $text, $filterEntry ) {
18 $regex = LinkFilter
::makeRegex( $filterEntry );
19 return preg_match( $regex, $text );
25 function makeRegex( $filterEntry ) {
27 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
28 $regex .= '([A-Za-z0-9.-]+\.|)';
29 $filterEntry = substr( $filterEntry, 2 );
31 $regex .= preg_quote( $filterEntry, '!' ) . '!Si';
36 * Make a string to go after an SQL LIKE, which will match the specified
37 * string. There are several kinds of filter entry:
38 * *.domain.com - Produces http://com.domain.%, matches domain.com
40 * domain.com - Produces http://com.domain./%, matches domain.com
41 * or domain.com/ but not www.domain.com
42 * *.domain.com/x - Produces http://com.domain.%/x%, matches
44 * domain.com/x - Produces http://com.domain./x%, matches
45 * domain.com/xy but not www.domain.com/xy
47 * Asterisks in any other location are considered invalid.
51 function makeLike( $filterEntry ) {
52 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
54 $filterEntry = substr( $filterEntry, 2 );
55 if ( $filterEntry == '' ) {
56 // We don't want to make a clause that will match everything,
57 // that could be dangerous
63 // No stray asterisks, that could cause confusion
64 // It's not simple or efficient to handle it properly so we don't
66 if ( strpos( $filterEntry, '*' ) !== false ) {
69 $slash = strpos( $filterEntry, '/' );
70 if ( $slash !== false ) {
71 $path = substr( $filterEntry, $slash );
72 $host = substr( $filterEntry, 0, $slash );
77 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
78 if ( substr( $host, -1, 1 ) !== '.' ) {
81 $like = "http://$host";
86 if ( !$subdomains ||
$path !== '/' ) {