3 * Functions to help implement an external link filter for spam control.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Some functions to help implement an external link filter for spam control.
27 * @todo implement the filter. Currently these are just some functions to help
28 * maintenance/cleanupSpam.php remove links to a single specified domain. The
29 * next thing is to implement functions for checking a given page against a big
32 * Another cool thing to do would be a web interface for fast spam removal.
37 * Check whether $text contains a link to $filterEntry
39 * @param $text String: text to check
40 * @param $filterEntry String: domainparts, see makeRegex() for more details
41 * @return Integer: 0 if no match or 1 if there's at least one match
43 static function matchEntry( $text, $filterEntry ) {
44 $regex = LinkFilter
::makeRegex( $filterEntry );
45 return preg_match( $regex, $text );
49 * Builds a regex pattern for $filterEntry.
51 * @param $filterEntry String: URL, if it begins with "*.", it'll be
52 * replaced to match any subdomain
53 * @return String: regex pattern, for preg_match()
55 private static function makeRegex( $filterEntry ) {
57 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
58 $regex .= '(?:[A-Za-z0-9.-]+\.|)';
59 $filterEntry = substr( $filterEntry, 2 );
61 $regex .= preg_quote( $filterEntry, '!' ) . '!Si';
66 * Make an array to be used for calls to DatabaseBase::buildLike(), which
67 * will match the specified string. There are several kinds of filter entry:
68 * *.domain.com - Produces http://com.domain.%, matches domain.com
70 * domain.com - Produces http://com.domain./%, matches domain.com
71 * or domain.com/ but not www.domain.com
72 * *.domain.com/x - Produces http://com.domain.%/x%, matches
74 * domain.com/x - Produces http://com.domain./x%, matches
75 * domain.com/xy but not www.domain.com/xy
77 * Asterisks in any other location are considered invalid.
79 * @param $filterEntry String: domainparts
80 * @param $prot String: protocol
81 * @return Array to be passed to DatabaseBase::buildLike() or false on error
83 public static function makeLikeArray( $filterEntry , $prot = 'http://' ) {
84 $db = wfGetDB( DB_MASTER
);
85 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
87 $filterEntry = substr( $filterEntry, 2 );
88 if ( $filterEntry == '' ) {
89 // We don't want to make a clause that will match everything,
90 // that could be dangerous
96 // No stray asterisks, that could cause confusion
97 // It's not simple or efficient to handle it properly so we don't
99 if ( strpos( $filterEntry, '*' ) !== false ) {
102 $slash = strpos( $filterEntry, '/' );
103 if ( $slash !== false ) {
104 $path = substr( $filterEntry, $slash );
105 $host = substr( $filterEntry, 0, $slash );
108 $host = $filterEntry;
110 // Reverse the labels in the hostname, convert to lower case
111 // For emails reverse domainpart only
112 if ( $prot == 'mailto:' && strpos($host, '@') ) {
113 // complete email adress
114 $mailparts = explode( '@', $host );
115 $domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
116 $host = $domainpart . '@' . $mailparts[0];
117 $like = array( "$prot$host", $db->anyString() );
118 } elseif ( $prot == 'mailto:' ) {
119 // domainpart of email adress only. do not add '.'
120 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
121 $like = array( "$prot$host", $db->anyString() );
123 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
124 if ( substr( $host, -1, 1 ) !== '.' ) {
127 $like = array( "$prot$host" );
130 $like[] = $db->anyString();
132 if ( !$subdomains ||
$path !== '/' ) {
134 $like[] = $db->anyString();
141 * Filters an array returned by makeLikeArray(), removing everything past first pattern placeholder.
143 * @param $arr array: array to filter
144 * @return array filtered array
146 public static function keepOneWildcard( $arr ) {
147 if( !is_array( $arr ) ) {
151 foreach( $arr as $key => $value ) {
152 if ( $value instanceof LikeMatch
) {
153 return array_slice( $arr, 0, $key +
1 );