60 return preg_match( $regex, $text );
73 private static function makeRegex( $filterEntry, $protocol ) {
74 $regex =
'!' . preg_quote( $protocol,
'!' );
75 if ( substr( $filterEntry, 0, 2 ) ==
'*.' ) {
76 $regex .=
'(?:[A-Za-z0-9.-]+\.|)';
77 $filterEntry = substr( $filterEntry, 2 );
79 $regex .= preg_quote( $filterEntry,
'!' ) .
'!Si';
89 return is_callable(
'idn_to_utf8' ) && defined(
'INTL_IDNA_VARIANT_UTS46' );
101 $host = rawurldecode( $host );
102 if ( $host !==
'' && self::supportsIDN() ) {
104 $tmp = idn_to_utf8( $host, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46 );
105 if ( $tmp !==
false ) {
109 $okChars =
'a-zA-Z0-9\\-._~!$&\'()*+,;=';
112 $okChars .=
'\x80-\xf4';
114 $host = preg_replace_callback(
115 '<[^' . $okChars .
']>',
117 return rawurlencode( $m[0] );
123 if ( preg_match(
'/^\[([0-9a-f:*]+)\]$/', rawurldecode( $host ), $m ) ) {
125 if ( IP::isValid( $ip ) ) {
126 return 'V6.' . implode(
'.', explode(
':', IP::sanitizeIP( $ip ) ) ) .
'.';
128 if ( substr( $ip, -2 ) ===
':*' ) {
129 $cutIp = substr( $ip, 0, -2 );
130 if ( IP::isValid(
"{$cutIp}::" ) ) {
132 $ct = count( explode(
':', $ip ) ) - 1;
134 implode(
'.', array_slice( explode(
':', IP::sanitizeIP(
"{$cutIp}::" ) ), 0, $ct ) ) .
137 if ( IP::isValid(
"{$cutIp}:1" ) ) {
140 substr( implode(
'.', explode(
':', IP::sanitizeIP(
"{$cutIp}:1" ) ) ), 0, -1 ) .
148 if ( substr( $host, -1 ) ===
'.' ) {
149 $host = substr( $host, 0, -1 );
153 $b =
'(?:0*25[0-5]|0*2[0-4][0-9]|0*1[0-9][0-9]|0*[0-9]?[0-9])';
154 if ( preg_match(
"/^(?:{$b}\.){3}{$b}$|^(?:{$b}\.){1,3}\*$/", $host ) ) {
155 return 'V4.' . implode(
'.', array_map(
function ( $v ) {
156 return $v ===
'*' ? $v : (int)$v;
157 }, explode(
'.', $host ) ) ) .
'.';
161 return implode(
'.', array_reverse( explode(
'.', $host ) ) ) .
'.';
186 if ( $bits[
'scheme'] ==
'mailto' ) {
187 $mailparts = explode(
'@', $bits[
'host'], 2 );
188 if ( count( $mailparts ) === 2 ) {
194 $bits[
'host'] = $domainpart .
'@' . $mailparts[0];
200 $index = $bits[
'scheme'] . $bits[
'delimiter'] . $bits[
'host'];
202 if ( isset( $bits[
'port'] ) ) {
203 $index .=
':' . $bits[
'port'];
205 if ( isset( $bits[
'path'] ) ) {
206 $index .= $bits[
'path'];
210 if ( isset( $bits[
'query'] ) ) {
211 $index .=
'?' . $bits[
'query'];
213 if ( isset( $bits[
'fragment'] ) ) {
214 $index .=
'#' . $bits[
'fragment'];
217 if ( $bits[
'scheme'] ==
'' ) {
218 return [
"http:$index",
"https:$index" ];
256 'protocol' =>
'http://',
257 'oneWildcard' =>
false,
264 if ( $like ===
false ) {
270 if ( $options[
'oneWildcard'] ) {
271 $like = $trimmedLike;
273 if ( $trimmedLike[count( $trimmedLike ) - 1] instanceof
LikeMatch ) {
274 array_pop( $trimmedLike );
276 $index = implode(
'', $trimmedLike );
278 $p = $options[
'prefix'];
282 $l = strlen( $index );
287 "{$p}_index_60" => substr( $index, 0, 60 ),
288 "{$p}_index" . $db->buildLike( $like ),
295 "{$p}_index_60" . $db->buildLike( $index, $db->anyString() ),
296 "{$p}_index" . $db->buildLike( $like ),
312 public static function makeLikeArray( $filterEntry, $protocol =
'http://' ) {
316 $target = $protocol . $filterEntry;
323 if ( $bits[
'scheme'] ===
'mailto' && strpos( $bits[
'host'],
'@' ) ) {
325 $mailparts = explode(
'@', $bits[
'host'], 2 );
327 if ( $mailparts[0] ===
'*' ) {
329 $bits[
'host'] = $domainpart .
'@';
331 $bits[
'host'] = $domainpart .
'@' . $mailparts[0];
336 if ( substr( $bits[
'host'], -3 ) ===
'.*.' ) {
338 $bits[
'host'] = substr( $bits[
'host'], 0, -2 );
342 $like[] = $bits[
'scheme'] . $bits[
'delimiter'] . $bits[
'host'];
345 $like[] = $db->anyString();
348 if ( isset( $bits[
'port'] ) ) {
349 $like[] =
':' . $bits[
'port'];
351 if ( isset( $bits[
'path'] ) ) {
352 $like[] = $bits[
'path'];
353 } elseif ( !$subdomains ) {
356 if ( isset( $bits[
'query'] ) ) {
357 $like[] =
'?' . $bits[
'query'];
359 if ( isset( $bits[
'fragment'] ) ) {
360 $like[] =
'#' . $bits[
'fragment'];
364 foreach ( $like as $likepart ) {
365 if ( !( $likepart instanceof
LikeMatch ) && strpos( $likepart,
'*' ) !==
false ) {
370 if ( !( $like[count( $like ) - 1] instanceof
LikeMatch ) ) {
372 $like[] = $db->anyString();
387 if ( !is_array( $arr ) ) {
391 foreach ( $arr as $key => $value ) {
393 return array_slice( $arr, 0, $key + 1 );
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Some functions to help implement an external link filter for spam control.
static makeRegex( $filterEntry, $protocol)
Builds a regex pattern for $filterEntry.
static makeIndexes( $url)
Converts a URL into a format for el_index.
static makeLikeArray( $filterEntry, $protocol='http://')
Make an array to be used for calls to Database::buildLike(), which will match the specified string.
static indexifyHost( $host)
Canonicalize a hostname for el_index.
static getQueryConditions( $filterEntry, array $options=[])
Return query conditions which will match the specified string.
static supportsIDN()
Indicate whether LinkFilter IDN support is available.
const VERSION
Increment this when makeIndexes output changes.
static matchEntry(Content $content, $filterEntry, $protocol='http://')
Check whether $content contains a link to $filterEntry.
static keepOneWildcard( $arr)
Filters an array returned by makeLikeArray(), removing everything past first pattern placeholder.
static isUtf8( $value)
Test whether a string is valid UTF-8.
Content object implementation for representing flat text.
Base interface for content objects.