Return comment stuffs
[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 /**
16 * Check whether $text contains a link to $filterEntry
17 *
18 * @param $text String: text to check
19 * @param $filterEntry String: domainparts, see makeRegex() for more details
20 * @return Integer: 0 if no match or 1 if there's at least one match
21 */
22 static function matchEntry( $text, $filterEntry ) {
23 $regex = LinkFilter::makeRegex( $filterEntry );
24 return preg_match( $regex, $text );
25 }
26
27 /**
28 * Builds a regex pattern for $filterEntry.
29 *
30 * @param $filterEntry String: URL, if it begins with "*.", it'll be
31 * replaced to match any subdomain
32 * @return String: regex pattern, for preg_match()
33 */
34 private static function makeRegex( $filterEntry ) {
35 $regex = '!http://';
36 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
37 $regex .= '(?:[A-Za-z0-9.-]+\.|)';
38 $filterEntry = substr( $filterEntry, 2 );
39 }
40 $regex .= preg_quote( $filterEntry, '!' ) . '!Si';
41 return $regex;
42 }
43
44 /**
45 * Make an array to be used for calls to DatabaseBase::buildLike(), which
46 * will match the specified string. There are several kinds of filter entry:
47 * *.domain.com - Produces http://com.domain.%, matches domain.com
48 * and www.domain.com
49 * domain.com - Produces http://com.domain./%, matches domain.com
50 * or domain.com/ but not www.domain.com
51 * *.domain.com/x - Produces http://com.domain.%/x%, matches
52 * www.domain.com/xy
53 * domain.com/x - Produces http://com.domain./x%, matches
54 * domain.com/xy but not www.domain.com/xy
55 *
56 * Asterisks in any other location are considered invalid.
57 *
58 * @param $filterEntry String: domainparts
59 * @param $prot String: protocol
60 * @return Array to be passed to DatabaseBase::buildLike() or false on error
61 */
62 public static function makeLikeArray( $filterEntry , $prot = 'http://' ) {
63 $db = wfGetDB( DB_MASTER );
64 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
65 $subdomains = true;
66 $filterEntry = substr( $filterEntry, 2 );
67 if ( $filterEntry == '' ) {
68 // We don't want to make a clause that will match everything,
69 // that could be dangerous
70 return false;
71 }
72 } else {
73 $subdomains = false;
74 }
75 // No stray asterisks, that could cause confusion
76 // It's not simple or efficient to handle it properly so we don't
77 // handle it at all.
78 if ( strpos( $filterEntry, '*' ) !== false ) {
79 return false;
80 }
81 $slash = strpos( $filterEntry, '/' );
82 if ( $slash !== false ) {
83 $path = substr( $filterEntry, $slash );
84 $host = substr( $filterEntry, 0, $slash );
85 } else {
86 $path = '/';
87 $host = $filterEntry;
88 }
89 // Reverse the labels in the hostname, convert to lower case
90 // For emails reverse domainpart only
91 if ( $prot == 'mailto:' && strpos($host, '@') ) {
92 // complete email adress
93 $mailparts = explode( '@', $host );
94 $domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
95 $host = $domainpart . '@' . $mailparts[0];
96 $like = array( "$prot$host", $db->anyString() );
97 } elseif ( $prot == 'mailto:' ) {
98 // domainpart of email adress only. do not add '.'
99 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
100 $like = array( "$prot$host", $db->anyString() );
101 } else {
102 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
103 if ( substr( $host, -1, 1 ) !== '.' ) {
104 $host .= '.';
105 }
106 $like = array( "$prot$host" );
107
108 if ( $subdomains ) {
109 $like[] = $db->anyString();
110 }
111 if ( !$subdomains || $path !== '/' ) {
112 $like[] = $path;
113 $like[] = $db->anyString();
114 }
115 }
116 return $like;
117 }
118
119 /**
120 * Filters an array returned by makeLikeArray(), removing everything past first pattern placeholder.
121 *
122 * @param $arr array: array to filter
123 * @return filtered array
124 */
125 public static function keepOneWildcard( $arr ) {
126 if( !is_array( $arr ) ) {
127 return $arr;
128 }
129
130 foreach( $arr as $key => $value ) {
131 if ( $value instanceof LikeMatch ) {
132 return array_slice( $arr, 0, $key + 1 );
133 }
134 }
135
136 return $arr;
137 }
138 }