]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/LinkFilter.php
MediaWiki 1.11.0-scripts
[autoinstalls/mediawiki.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          */
53          public static function makeLike( $filterEntry , $prot = 'http://' ) {
54                 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
55                         $subdomains = true;
56                         $filterEntry = substr( $filterEntry, 2 );
57                         if ( $filterEntry == '' ) {
58                                 // We don't want to make a clause that will match everything,
59                                 // that could be dangerous
60                                 return false;
61                         }
62                 } else {
63                         $subdomains = false;
64                 }
65                 // No stray asterisks, that could cause confusion
66                 // It's not simple or efficient to handle it properly so we don't
67                 // handle it at all.
68                 if ( strpos( $filterEntry, '*' ) !== false ) {
69                         return false;
70                 }
71                 $slash = strpos( $filterEntry, '/' );
72                 if ( $slash !== false ) {
73                         $path = substr( $filterEntry, $slash );
74                         $host = substr( $filterEntry, 0, $slash );
75                 } else {
76                         $path = '/';
77                         $host = $filterEntry;
78                 }
79                 // Reverse the labels in the hostname, convert to lower case
80                 // For emails reverse domainpart only
81                 if ( $prot == 'mailto:' && strpos($host, '@') ) {
82                         // complete email adress 
83                         $mailparts = explode( '@', $host );
84                         $domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
85                         $host = $domainpart . '@' . $mailparts[0];
86                         $like = "$prot$host%";
87                 } elseif ( $prot == 'mailto:' ) {
88                         // domainpart of email adress only. do not add '.'
89                         $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );   
90                         $like = "$prot$host%";                  
91                 } else {
92                         $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );   
93                         if ( substr( $host, -1, 1 ) !== '.' ) {
94                                 $host .= '.';
95                         }
96                         $like = "$prot$host";
97
98                         if ( $subdomains ) {
99                                 $like .= '%';
100                         }
101                         if ( !$subdomains || $path !== '/' ) {
102                                 $like .= $path . '%';
103                         }
104                 }
105                 return $like;
106         }
107 }
108