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