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.
16 * Check whether $text contains a link to $filterEntry
18 * @param $text String: text to check
19 * @param $filterEntry String: domainparts, see makeRegex() for more details
20 * @return Integer: 0 if no match or 1 if there's at least one match
22 static function matchEntry( $text, $filterEntry ) {
23 $regex = LinkFilter
::makeRegex( $filterEntry );
24 return preg_match( $regex, $text );
28 * Builds a regex pattern for $filterEntry.
30 * @param $filterEntry String: URL, if it begins with "*.", it'll be
31 * replaced to match any subdomain
32 * @return String: regex pattern, for preg_match()
34 private static function makeRegex( $filterEntry ) {
36 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
37 $regex .= '(?:[A-Za-z0-9.-]+\.|)';
38 $filterEntry = substr( $filterEntry, 2 );
40 $regex .= preg_quote( $filterEntry, '!' ) . '!Si';
45 * Make an array to be used for calls to DatabaseBase::buildLike(), which
46 * will match the specified string. There are several kinds of filter entry:
47 * *.domain.com - Produces http://com.domain.%, matches domain.com
49 * domain.com - Produces http://com.domain./%, matches domain.com
50 * or domain.com/ but not www.domain.com
51 * *.domain.com/x - Produces http://com.domain.%/x%, matches
53 * domain.com/x - Produces http://com.domain./x%, matches
54 * domain.com/xy but not www.domain.com/xy
56 * Asterisks in any other location are considered invalid.
58 * @param $filterEntry String: domainparts
59 * @param $prot String: protocol
60 * @return Array to be passed to DatabaseBase::buildLike() or false on error
62 public static function makeLikeArray( $filterEntry , $prot = 'http://' ) {
63 $db = wfGetDB( DB_MASTER
);
64 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
66 $filterEntry = substr( $filterEntry, 2 );
67 if ( $filterEntry == '' ) {
68 // We don't want to make a clause that will match everything,
69 // that could be dangerous
75 // No stray asterisks, that could cause confusion
76 // It's not simple or efficient to handle it properly so we don't
78 if ( strpos( $filterEntry, '*' ) !== false ) {
81 $slash = strpos( $filterEntry, '/' );
82 if ( $slash !== false ) {
83 $path = substr( $filterEntry, $slash );
84 $host = substr( $filterEntry, 0, $slash );
89 // Reverse the labels in the hostname, convert to lower case
90 // For emails reverse domainpart only
91 if ( $prot == 'mailto:' && strpos($host, '@') ) {
92 // complete email adress
93 $mailparts = explode( '@', $host );
94 $domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
95 $host = $domainpart . '@' . $mailparts[0];
96 $like = array( "$prot$host", $db->anyString() );
97 } elseif ( $prot == 'mailto:' ) {
98 // domainpart of email adress only. do not add '.'
99 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
100 $like = array( "$prot$host", $db->anyString() );
102 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
103 if ( substr( $host, -1, 1 ) !== '.' ) {
106 $like = array( "$prot$host" );
109 $like[] = $db->anyString();
111 if ( !$subdomains ||
$path !== '/' ) {
113 $like[] = $db->anyString();
120 * Filters an array returned by makeLikeArray(), removing everything past first pattern placeholder.
122 * @param $arr array: array to filter
123 * @return array filtered array
125 public static function keepOneWildcard( $arr ) {
126 if( !is_array( $arr ) ) {
130 foreach( $arr as $key => $value ) {
131 if ( $value instanceof LikeMatch
) {
132 return array_slice( $arr, 0, $key +
1 );