Merge "Improve description of paths/urls in the INSTALL file."
[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
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 $filterEntry String: domainparts, see makeRegex() for more details
41 * @return Integer: 0 if no match or 1 if there's at least one match
42 */
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;
49 }
50
51 $text = $content->getNativeData();
52
53 $regex = LinkFilter::makeRegex( $filterEntry );
54 return preg_match( $regex, $text );
55 }
56
57 /**
58 * Builds a regex pattern for $filterEntry.
59 *
60 * @param $filterEntry String: URL, if it begins with "*.", it'll be
61 * replaced to match any subdomain
62 * @return String: regex pattern, for preg_match()
63 */
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 );
69 }
70 $regex .= preg_quote( $filterEntry, '!' ) . '!Si';
71 return $regex;
72 }
73
74 /**
75 * Make an array to be used for calls to DatabaseBase::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
85 *
86 * Asterisks in any other location are considered invalid.
87 *
88 * @param $filterEntry String: domainparts
89 * @param $prot String: protocol
90 * @return Array to be passed to DatabaseBase::buildLike() or false on error
91 */
92 public static function makeLikeArray( $filterEntry, $prot = 'http://' ) {
93 $db = wfGetDB( DB_MASTER );
94 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
95 $subdomains = true;
96 $filterEntry = substr( $filterEntry, 2 );
97 if ( $filterEntry == '' ) {
98 // We don't want to make a clause that will match everything,
99 // that could be dangerous
100 return false;
101 }
102 } else {
103 $subdomains = false;
104 }
105 // No stray asterisks, that could cause confusion
106 // It's not simple or efficient to handle it properly so we don't
107 // handle it at all.
108 if ( strpos( $filterEntry, '*' ) !== false ) {
109 return false;
110 }
111 $slash = strpos( $filterEntry, '/' );
112 if ( $slash !== false ) {
113 $path = substr( $filterEntry, $slash );
114 $host = substr( $filterEntry, 0, $slash );
115 } else {
116 $path = '/';
117 $host = $filterEntry;
118 }
119 // Reverse the labels in the hostname, convert to lower case
120 // For emails reverse domainpart only
121 if ( $prot == 'mailto:' && strpos( $host, '@' ) ) {
122 // complete email adress
123 $mailparts = explode( '@', $host );
124 $domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
125 $host = $domainpart . '@' . $mailparts[0];
126 $like = array( "$prot$host", $db->anyString() );
127 } elseif ( $prot == 'mailto:' ) {
128 // domainpart of email adress only. do not add '.'
129 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
130 $like = array( "$prot$host", $db->anyString() );
131 } else {
132 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
133 if ( substr( $host, -1, 1 ) !== '.' ) {
134 $host .= '.';
135 }
136 $like = array( "$prot$host" );
137
138 if ( $subdomains ) {
139 $like[] = $db->anyString();
140 }
141 if ( !$subdomains || $path !== '/' ) {
142 $like[] = $path;
143 $like[] = $db->anyString();
144 }
145 }
146 return $like;
147 }
148
149 /**
150 * Filters an array returned by makeLikeArray(), removing everything past first pattern placeholder.
151 *
152 * @param $arr array: array to filter
153 * @return array filtered array
154 */
155 public static function keepOneWildcard( $arr ) {
156 if( !is_array( $arr ) ) {
157 return $arr;
158 }
159
160 foreach( $arr as $key => $value ) {
161 if ( $value instanceof LikeMatch ) {
162 return array_slice( $arr, 0, $key + 1 );
163 }
164 }
165
166 return $arr;
167 }
168 }