Fixup for r57989: removed artifacts of the previous version of my patch, improved...
[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
57 $like = self::makeLikeArray( $filterEntry , $prot );
58 if ( !$like ) {
59 return false;
60 }
61 $dbw = wfGetDB( DB_MASTER );
62
63 return $dbw->buildLike( $like );
64 }
65
66 /**
67 * Make an array to be used for calls to Database::buildLike(), which will match the specified
68 * string. There are several kinds of filter entry:
69 * *.domain.com - Produces http://com.domain.%, matches domain.com
70 * and www.domain.com
71 * domain.com - Produces http://com.domain./%, matches domain.com
72 * or domain.com/ but not www.domain.com
73 * *.domain.com/x - Produces http://com.domain.%/x%, matches
74 * www.domain.com/xy
75 * domain.com/x - Produces http://com.domain./x%, matches
76 * domain.com/xy but not www.domain.com/xy
77 *
78 * Asterisks in any other location are considered invalid.
79 *
80 * @static
81 * @param $filterEntry String: domainparts
82 * @param $prot String: protocol
83 */
84 public static function makeLikeArray( $filterEntry , $prot = 'http://' ) {
85 $db = wfGetDB( DB_MASTER );
86 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
87 $subdomains = true;
88 $filterEntry = substr( $filterEntry, 2 );
89 if ( $filterEntry == '' ) {
90 // We don't want to make a clause that will match everything,
91 // that could be dangerous
92 return false;
93 }
94 } else {
95 $subdomains = false;
96 }
97 // No stray asterisks, that could cause confusion
98 // It's not simple or efficient to handle it properly so we don't
99 // handle it at all.
100 if ( strpos( $filterEntry, '*' ) !== false ) {
101 return false;
102 }
103 $slash = strpos( $filterEntry, '/' );
104 if ( $slash !== false ) {
105 $path = substr( $filterEntry, $slash );
106 $host = substr( $filterEntry, 0, $slash );
107 } else {
108 $path = '/';
109 $host = $filterEntry;
110 }
111 // Reverse the labels in the hostname, convert to lower case
112 // For emails reverse domainpart only
113 if ( $prot == 'mailto:' && strpos($host, '@') ) {
114 // complete email adress
115 $mailparts = explode( '@', $host );
116 $domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
117 $host = $domainpart . '@' . $mailparts[0];
118 $like = array( "$prot$host", $db->anyString() );
119 } elseif ( $prot == 'mailto:' ) {
120 // domainpart of email adress only. do not add '.'
121 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
122 $like = array( "$prot$host", $db->anyString() );
123 } else {
124 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
125 if ( substr( $host, -1, 1 ) !== '.' ) {
126 $host .= '.';
127 }
128 $like = array( "$prot$host" );
129
130 if ( $subdomains ) {
131 $like[] = $db->anyString();
132 }
133 if ( !$subdomains || $path !== '/' ) {
134 $like[] = $path;
135 $like[] = $db->anyString();
136 }
137 }
138 return $like;
139 }
140
141 /**
142 * Filters an array returned by makeLikeArray(), removing everything past first pattern placeholder.
143 * @static
144 * @param $arr array: array to filter
145 * @return filtered array
146 */
147 public static function keepOneWildcard( $arr ) {
148 if( !is_array( $arr ) ) {
149 return $arr;
150 }
151
152 foreach( $arr as $key => $value ) {
153 if ( $value instanceof LikeMatch ) {
154 return array_slice( $arr, 0, $key + 1 );
155 }
156 }
157
158 return $arr;
159 }
160 }