MediaWiki
master
|
00001 <?php 00034 class LinkFilter { 00035 00043 static function matchEntry( Content $content, $filterEntry ) { 00044 if ( !( $content instanceof TextContent ) ) { 00045 //TODO: handle other types of content too. 00046 // Maybe create ContentHandler::matchFilter( LinkFilter ). 00047 // Think about a common base class for LinkFilter and MagicWord. 00048 return 0; 00049 } 00050 00051 $text = $content->getNativeData(); 00052 00053 $regex = LinkFilter::makeRegex( $filterEntry ); 00054 return preg_match( $regex, $text ); 00055 } 00056 00064 private static function makeRegex( $filterEntry ) { 00065 $regex = '!http://'; 00066 if ( substr( $filterEntry, 0, 2 ) == '*.' ) { 00067 $regex .= '(?:[A-Za-z0-9.-]+\.|)'; 00068 $filterEntry = substr( $filterEntry, 2 ); 00069 } 00070 $regex .= preg_quote( $filterEntry, '!' ) . '!Si'; 00071 return $regex; 00072 } 00073 00092 public static function makeLikeArray( $filterEntry , $prot = 'http://' ) { 00093 $db = wfGetDB( DB_MASTER ); 00094 if ( substr( $filterEntry, 0, 2 ) == '*.' ) { 00095 $subdomains = true; 00096 $filterEntry = substr( $filterEntry, 2 ); 00097 if ( $filterEntry == '' ) { 00098 // We don't want to make a clause that will match everything, 00099 // that could be dangerous 00100 return false; 00101 } 00102 } else { 00103 $subdomains = false; 00104 } 00105 // No stray asterisks, that could cause confusion 00106 // It's not simple or efficient to handle it properly so we don't 00107 // handle it at all. 00108 if ( strpos( $filterEntry, '*' ) !== false ) { 00109 return false; 00110 } 00111 $slash = strpos( $filterEntry, '/' ); 00112 if ( $slash !== false ) { 00113 $path = substr( $filterEntry, $slash ); 00114 $host = substr( $filterEntry, 0, $slash ); 00115 } else { 00116 $path = '/'; 00117 $host = $filterEntry; 00118 } 00119 // Reverse the labels in the hostname, convert to lower case 00120 // For emails reverse domainpart only 00121 if ( $prot == 'mailto:' && strpos($host, '@') ) { 00122 // complete email adress 00123 $mailparts = explode( '@', $host ); 00124 $domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) ); 00125 $host = $domainpart . '@' . $mailparts[0]; 00126 $like = array( "$prot$host", $db->anyString() ); 00127 } elseif ( $prot == 'mailto:' ) { 00128 // domainpart of email adress only. do not add '.' 00129 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) ); 00130 $like = array( "$prot$host", $db->anyString() ); 00131 } else { 00132 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) ); 00133 if ( substr( $host, -1, 1 ) !== '.' ) { 00134 $host .= '.'; 00135 } 00136 $like = array( "$prot$host" ); 00137 00138 if ( $subdomains ) { 00139 $like[] = $db->anyString(); 00140 } 00141 if ( !$subdomains || $path !== '/' ) { 00142 $like[] = $path; 00143 $like[] = $db->anyString(); 00144 } 00145 } 00146 return $like; 00147 } 00148 00155 public static function keepOneWildcard( $arr ) { 00156 if( !is_array( $arr ) ) { 00157 return $arr; 00158 } 00159 00160 foreach( $arr as $key => $value ) { 00161 if ( $value instanceof LikeMatch ) { 00162 return array_slice( $arr, 0, $key + 1 ); 00163 } 00164 } 00165 00166 return $arr; 00167 } 00168 }