(bug 20275) Fixed LIKE queries on SQLite backend
[lhc/web/wiklou.git] / includes / LinkFilter.php
1 <?php
2
3 /**
4 * Some functions to help implement an external link filter for spam control.
5 *
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
9 * list of domains.
10 *
11 * Another cool thing to do would be a web interface for fast spam removal.
12 */
13 class LinkFilter {
14 /**
15 * @static
16 */
17 static function matchEntry( $text, $filterEntry ) {
18 $regex = LinkFilter::makeRegex( $filterEntry );
19 return preg_match( $regex, $text );
20 }
21
22 /**
23 * @static
24 */
25 private static function makeRegex( $filterEntry ) {
26 $regex = '!http://';
27 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
28 $regex .= '(?:[A-Za-z0-9.-]+\.|)';
29 $filterEntry = substr( $filterEntry, 2 );
30 }
31 $regex .= preg_quote( $filterEntry, '!' ) . '!Si';
32 return $regex;
33 }
34
35 /**
36 * Make a string to go after an SQL LIKE, which will match the specified
37 * string. There are several kinds of filter entry:
38 * *.domain.com - Produces http://com.domain.%, matches domain.com
39 * and www.domain.com
40 * domain.com - Produces http://com.domain./%, matches domain.com
41 * or domain.com/ but not www.domain.com
42 * *.domain.com/x - Produces http://com.domain.%/x%, matches
43 * www.domain.com/xy
44 * domain.com/x - Produces http://com.domain./x%, matches
45 * domain.com/xy but not www.domain.com/xy
46 *
47 * Asterisks in any other location are considered invalid.
48 *
49 * @static
50 * @param $filterEntry String: domainparts
51 * @param $prot String: protocol
52 * @deprecated Use makeLikeArray() and pass result to Database::buildLike() instead
53 */
54 public static function makeLike( $filterEntry , $prot = 'http://' ) {
55 wfDeprecated( __METHOD__ );
56 $db = wfGetDB( DB_MASTER );
57 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
58 $subdomains = true;
59 $filterEntry = substr( $filterEntry, 2 );
60 if ( $filterEntry == '' ) {
61 // We don't want to make a clause that will match everything,
62 // that could be dangerous
63 return false;
64 }
65 } else {
66 $subdomains = false;
67 }
68 // No stray asterisks, that could cause confusion
69 // It's not simple or efficient to handle it properly so we don't
70 // handle it at all.
71 if ( strpos( $filterEntry, '*' ) !== false ) {
72 return false;
73 }
74 $slash = strpos( $filterEntry, '/' );
75 if ( $slash !== false ) {
76 $path = substr( $filterEntry, $slash );
77 $host = substr( $filterEntry, 0, $slash );
78 } else {
79 $path = '/';
80 $host = $filterEntry;
81 }
82 // Reverse the labels in the hostname, convert to lower case
83 // For emails reverse domainpart only
84 if ( $prot == 'mailto:' && strpos($host, '@') ) {
85 // complete email adress
86 $mailparts = explode( '@', $host );
87 $domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
88 $host = $domainpart . '@' . $mailparts[0];
89 $like = $db->escapeLike( "$prot$host" ) . "%";
90 } elseif ( $prot == 'mailto:' ) {
91 // domainpart of email adress only. do not add '.'
92 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
93 $like = $db->escapeLike( "$prot$host" ) . "%";
94 } else {
95 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
96 if ( substr( $host, -1, 1 ) !== '.' ) {
97 $host .= '.';
98 }
99 $like = $db->escapeLike( "$prot$host" );
100
101 if ( $subdomains ) {
102 $like .= '%';
103 }
104 if ( !$subdomains || $path !== '/' ) {
105 $like .= $db->escapeLike( $path ) . '%';
106 }
107 }
108 return $like;
109 }
110
111 /**
112 * Make an array to be used for calls to Database::like(), which will match the specified
113 * string. There are several kinds of filter entry:
114 * *.domain.com - Produces http://com.domain.%, matches domain.com
115 * and www.domain.com
116 * domain.com - Produces http://com.domain./%, matches domain.com
117 * or domain.com/ but not www.domain.com
118 * *.domain.com/x - Produces http://com.domain.%/x%, matches
119 * www.domain.com/xy
120 * domain.com/x - Produces http://com.domain./x%, matches
121 * domain.com/xy but not www.domain.com/xy
122 *
123 * Asterisks in any other location are considered invalid.
124 *
125 * @static
126 * @param $filterEntry String: domainparts
127 * @param $prot String: protocol
128 */
129 public static function makeLikeArray( $filterEntry , $prot = 'http://' ) {
130 $db = wfGetDB( DB_MASTER );
131 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
132 $subdomains = true;
133 $filterEntry = substr( $filterEntry, 2 );
134 if ( $filterEntry == '' ) {
135 // We don't want to make a clause that will match everything,
136 // that could be dangerous
137 return false;
138 }
139 } else {
140 $subdomains = false;
141 }
142 // No stray asterisks, that could cause confusion
143 // It's not simple or efficient to handle it properly so we don't
144 // handle it at all.
145 if ( strpos( $filterEntry, '*' ) !== false ) {
146 return false;
147 }
148 $slash = strpos( $filterEntry, '/' );
149 if ( $slash !== false ) {
150 $path = substr( $filterEntry, $slash );
151 $host = substr( $filterEntry, 0, $slash );
152 } else {
153 $path = '/';
154 $host = $filterEntry;
155 }
156 // Reverse the labels in the hostname, convert to lower case
157 // For emails reverse domainpart only
158 if ( $prot == 'mailto:' && strpos($host, '@') ) {
159 // complete email adress
160 $mailparts = explode( '@', $host );
161 $domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
162 $host = $domainpart . '@' . $mailparts[0];
163 $like = array( "$prot$host", $db->anyString() );
164 } elseif ( $prot == 'mailto:' ) {
165 // domainpart of email adress only. do not add '.'
166 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
167 $like = array( "$prot$host", $db->anyString() );
168 } else {
169 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
170 if ( substr( $host, -1, 1 ) !== '.' ) {
171 $host .= '.';
172 }
173 $like = array( "$prot$host" );
174
175 if ( $subdomains ) {
176 $like[] = $db->anyString();
177 }
178 if ( !$subdomains || $path !== '/' ) {
179 $like[] = $path;
180 $like[] = $db->anyString();
181 }
182 }
183 return $like;
184 }
185
186 /**
187 * Filters an array returned by makeLikeArray(), removing everything past first pattern placeholder.
188 * @static
189 * @param $arr array: array to filter
190 * @return filtered array
191 */
192 public static function keepOneWildcard( $arr ) {
193 if( !is_array( $arr ) ) {
194 return $arr;
195 }
196
197 foreach( $arr as $key => $value ) {
198 if ( $value instanceof LikeMatch ) {
199 return array_slice( $arr, 0, $key + 1 );
200 }
201 }
202
203 return $arr;
204 }
205 }