Move remaining LoadBalancer classes to Rdbms
[mediawiki.git] / includes / LinkFilter.php
blob2f5055871acfc037acbbd0be8dc1a652dacf20ea
1 <?php
2 /**
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
20 * @file
22 use Wikimedia\Rdbms\LikeMatch;
24 /**
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
30 * list of domains.
32 * Another cool thing to do would be a web interface for fast spam removal.
34 class LinkFilter {
36 /**
37 * Check whether $content contains a link to $filterEntry
39 * @param Content $content Content to check
40 * @param string $filterEntry Domainparts, see makeRegex() for more details
41 * @return int 0 if no match or 1 if there's at least one match
43 static function matchEntry( Content $content, $filterEntry ) {
44 if ( !( $content instanceof TextContent ) ) {
45 // TODO: handle other types of content too.
46 // Maybe create ContentHandler::matchFilter( LinkFilter ).
47 // Think about a common base class for LinkFilter and MagicWord.
48 return 0;
51 $text = $content->getNativeData();
53 $regex = LinkFilter::makeRegex( $filterEntry );
54 return preg_match( $regex, $text );
57 /**
58 * Builds a regex pattern for $filterEntry.
60 * @param string $filterEntry URL, if it begins with "*.", it'll be
61 * replaced to match any subdomain
62 * @return string Regex pattern, for preg_match()
64 private static function makeRegex( $filterEntry ) {
65 $regex = '!http://';
66 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
67 $regex .= '(?:[A-Za-z0-9.-]+\.|)';
68 $filterEntry = substr( $filterEntry, 2 );
70 $regex .= preg_quote( $filterEntry, '!' ) . '!Si';
71 return $regex;
74 /**
75 * Make an array to be used for calls to Database::buildLike(), which
76 * will match the specified string. There are several kinds of filter entry:
77 * *.domain.com - Produces http://com.domain.%, matches domain.com
78 * and www.domain.com
79 * domain.com - Produces http://com.domain./%, matches domain.com
80 * or domain.com/ but not www.domain.com
81 * *.domain.com/x - Produces http://com.domain.%/x%, matches
82 * www.domain.com/xy
83 * domain.com/x - Produces http://com.domain./x%, matches
84 * domain.com/xy but not www.domain.com/xy
86 * Asterisks in any other location are considered invalid.
88 * This function does the same as wfMakeUrlIndexes(), except it also takes care
89 * of adding wildcards
91 * @param string $filterEntry Domainparts
92 * @param string $protocol Protocol (default http://)
93 * @return array|bool Array to be passed to Database::buildLike() or false on error
95 public static function makeLikeArray( $filterEntry, $protocol = 'http://' ) {
96 $db = wfGetDB( DB_REPLICA );
98 $target = $protocol . $filterEntry;
99 $bits = wfParseUrl( $target );
101 if ( $bits == false ) {
102 // Unknown protocol?
103 return false;
106 if ( substr( $bits['host'], 0, 2 ) == '*.' ) {
107 $subdomains = true;
108 $bits['host'] = substr( $bits['host'], 2 );
109 if ( $bits['host'] == '' ) {
110 // We don't want to make a clause that will match everything,
111 // that could be dangerous
112 return false;
114 } else {
115 $subdomains = false;
118 // Reverse the labels in the hostname, convert to lower case
119 // For emails reverse domainpart only
120 if ( $bits['scheme'] === 'mailto' && strpos( $bits['host'], '@' ) ) {
121 // complete email address
122 $mailparts = explode( '@', $bits['host'] );
123 $domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
124 $bits['host'] = $domainpart . '@' . $mailparts[0];
125 } elseif ( $bits['scheme'] === 'mailto' ) {
126 // domainpart of email address only, do not add '.'
127 $bits['host'] = strtolower( implode( '.', array_reverse( explode( '.', $bits['host'] ) ) ) );
128 } else {
129 $bits['host'] = strtolower( implode( '.', array_reverse( explode( '.', $bits['host'] ) ) ) );
130 if ( substr( $bits['host'], -1, 1 ) !== '.' ) {
131 $bits['host'] .= '.';
135 $like[] = $bits['scheme'] . $bits['delimiter'] . $bits['host'];
137 if ( $subdomains ) {
138 $like[] = $db->anyString();
141 if ( isset( $bits['port'] ) ) {
142 $like[] = ':' . $bits['port'];
144 if ( isset( $bits['path'] ) ) {
145 $like[] = $bits['path'];
146 } elseif ( !$subdomains ) {
147 $like[] = '/';
149 if ( isset( $bits['query'] ) ) {
150 $like[] = '?' . $bits['query'];
152 if ( isset( $bits['fragment'] ) ) {
153 $like[] = '#' . $bits['fragment'];
156 // Check for stray asterisks: asterisk only allowed at the start of the domain
157 foreach ( $like as $likepart ) {
158 if ( !( $likepart instanceof LikeMatch ) && strpos( $likepart, '*' ) !== false ) {
159 return false;
163 if ( !( $like[count( $like ) - 1] instanceof LikeMatch ) ) {
164 // Add wildcard at the end if there isn't one already
165 $like[] = $db->anyString();
168 return $like;
172 * Filters an array returned by makeLikeArray(), removing everything past first
173 * pattern placeholder.
175 * @param array $arr Array to filter
176 * @return array Filtered array
178 public static function keepOneWildcard( $arr ) {
179 if ( !is_array( $arr ) ) {
180 return $arr;
183 foreach ( $arr as $key => $value ) {
184 if ( $value instanceof LikeMatch ) {
185 return array_slice( $arr, 0, $key + 1 );
189 return $arr;