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