00001 <?php
00002
00013 class LinkFilter {
00017 static function matchEntry( $text, $filterEntry ) {
00018 $regex = LinkFilter::makeRegex( $filterEntry );
00019 return preg_match( $regex, $text );
00020 }
00021
00025 private static function makeRegex( $filterEntry ) {
00026 $regex = '!http://';
00027 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
00028 $regex .= '(?:[A-Za-z0-9.-]+\.|)';
00029 $filterEntry = substr( $filterEntry, 2 );
00030 }
00031 $regex .= preg_quote( $filterEntry, '!' ) . '!Si';
00032 return $regex;
00033 }
00034
00053 public static function makeLike( $filterEntry , $prot = 'http://' ) {
00054 $db = wfGetDB( DB_MASTER );
00055 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
00056 $subdomains = true;
00057 $filterEntry = substr( $filterEntry, 2 );
00058 if ( $filterEntry == '' ) {
00059
00060
00061 return false;
00062 }
00063 } else {
00064 $subdomains = false;
00065 }
00066
00067
00068
00069 if ( strpos( $filterEntry, '*' ) !== false ) {
00070 return false;
00071 }
00072 $slash = strpos( $filterEntry, '/' );
00073 if ( $slash !== false ) {
00074 $path = substr( $filterEntry, $slash );
00075 $host = substr( $filterEntry, 0, $slash );
00076 } else {
00077 $path = '/';
00078 $host = $filterEntry;
00079 }
00080
00081
00082 if ( $prot == 'mailto:' && strpos($host, '@') ) {
00083
00084 $mailparts = explode( '@', $host );
00085 $domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
00086 $host = $domainpart . '@' . $mailparts[0];
00087 $like = $db->escapeLike( "$prot$host" ) . "%";
00088 } elseif ( $prot == 'mailto:' ) {
00089
00090 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
00091 $like = $db->escapeLike( "$prot$host" ) . "%";
00092 } else {
00093 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
00094 if ( substr( $host, -1, 1 ) !== '.' ) {
00095 $host .= '.';
00096 }
00097 $like = $db->escapeLike( "$prot$host" );
00098
00099 if ( $subdomains ) {
00100 $like .= '%';
00101 }
00102 if ( !$subdomains || $path !== '/' ) {
00103 $like .= $db->escapeLike( $path ) . '%';
00104 }
00105 }
00106 return $like;
00107 }
00108 }