00001 <?php
00002
00013 class LinkFilter {
00014
00022 static function matchEntry( $text, $filterEntry ) {
00023 $regex = LinkFilter::makeRegex( $filterEntry );
00024 return preg_match( $regex, $text );
00025 }
00026
00034 private static function makeRegex( $filterEntry ) {
00035 $regex = '!http://';
00036 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
00037 $regex .= '(?:[A-Za-z0-9.-]+\.|)';
00038 $filterEntry = substr( $filterEntry, 2 );
00039 }
00040 $regex .= preg_quote( $filterEntry, '!' ) . '!Si';
00041 return $regex;
00042 }
00043
00063 public static function makeLike( $filterEntry , $prot = 'http://' ) {
00064 wfDeprecated( __METHOD__ );
00065
00066 $like = self::makeLikeArray( $filterEntry , $prot );
00067 if ( !$like ) {
00068 return false;
00069 }
00070 $dbw = wfGetDB( DB_MASTER );
00071 $s = $dbw->buildLike( $like );
00072 $m = false;
00073 if ( preg_match( "/^ *LIKE '(.*)' *$/", $s, $m ) ) {
00074 return $m[1];
00075 } else {
00076 throw new MWException( __METHOD__.': this DBMS is not supported by this function.' );
00077 }
00078 }
00079
00098 public static function makeLikeArray( $filterEntry , $prot = 'http://' ) {
00099 $db = wfGetDB( DB_MASTER );
00100 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
00101 $subdomains = true;
00102 $filterEntry = substr( $filterEntry, 2 );
00103 if ( $filterEntry == '' ) {
00104
00105
00106 return false;
00107 }
00108 } else {
00109 $subdomains = false;
00110 }
00111
00112
00113
00114 if ( strpos( $filterEntry, '*' ) !== false ) {
00115 return false;
00116 }
00117 $slash = strpos( $filterEntry, '/' );
00118 if ( $slash !== false ) {
00119 $path = substr( $filterEntry, $slash );
00120 $host = substr( $filterEntry, 0, $slash );
00121 } else {
00122 $path = '/';
00123 $host = $filterEntry;
00124 }
00125
00126
00127 if ( $prot == 'mailto:' && strpos($host, '@') ) {
00128
00129 $mailparts = explode( '@', $host );
00130 $domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
00131 $host = $domainpart . '@' . $mailparts[0];
00132 $like = array( "$prot$host", $db->anyString() );
00133 } elseif ( $prot == 'mailto:' ) {
00134
00135 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
00136 $like = array( "$prot$host", $db->anyString() );
00137 } else {
00138 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
00139 if ( substr( $host, -1, 1 ) !== '.' ) {
00140 $host .= '.';
00141 }
00142 $like = array( "$prot$host" );
00143
00144 if ( $subdomains ) {
00145 $like[] = $db->anyString();
00146 }
00147 if ( !$subdomains || $path !== '/' ) {
00148 $like[] = $path;
00149 $like[] = $db->anyString();
00150 }
00151 }
00152 return $like;
00153 }
00154
00161 public static function keepOneWildcard( $arr ) {
00162 if( !is_array( $arr ) ) {
00163 return $arr;
00164 }
00165
00166 foreach( $arr as $key => $value ) {
00167 if ( $value instanceof LikeMatch ) {
00168 return array_slice( $arr, 0, $key + 1 );
00169 }
00170 }
00171
00172 return $arr;
00173 }
00174 }