From 3bff5e6545a5a128adf61760d381a3208705d8ba Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Sun, 30 Oct 2005 11:24:43 +0000 Subject: [PATCH 1/1] * Rewrote the url protocol thing to not suck --- includes/GlobalFunctions.php | 16 ++++++++++++++++ includes/Parser.php | 11 +++++------ includes/Sanitizer.php | 3 +-- includes/Skin.php | 3 +-- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 9513fa3a23..78dc8c4ddb 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -1498,4 +1498,20 @@ function wfEmptyMsg( $msg, $wfMsgOut ) { function in_string( $needle, $str ) { return strpos( $str, $needle ) !== false; } + +/** + * Returns a regular expression of url protocols + * + * @return string + */ +function wfUrlProtocols() { + global $wgUrlProtocols; + + $x = array(); + foreach ($wgUrlProtocols as $protocol) + $x[] = preg_quote( $protocol, '/' ); + + return implode( '|', $x ); +} + ?> diff --git a/includes/Parser.php b/includes/Parser.php index 4cc0c7d14e..bb74782b6a 100644 --- a/includes/Parser.php +++ b/includes/Parser.php @@ -54,7 +54,7 @@ define( 'EXT_LINK_URL_CLASS', '[^]<>"\\x00-\\x20\\x7F]' ); define( 'EXT_LINK_TEXT_CLASS', '[^\]\\x00-\\x1F\\x7F]' ); define( 'EXT_IMAGE_FNAME_CLASS', '[A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF]' ); define( 'EXT_IMAGE_EXTENSIONS', 'gif|png|jpg|jpeg' ); -define( 'EXT_LINK_BRACKETED', '/\[(\b('.$wgUrlProtocols.')'.EXT_LINK_URL_CLASS.'+) *('.EXT_LINK_TEXT_CLASS.'*?)\]/S' ); +define( 'EXT_LINK_BRACKETED', '/\[(\b(' . wfUrlProtocols() . ')'.EXT_LINK_URL_CLASS.'+) *('.EXT_LINK_TEXT_CLASS.'*?)\]/S' ); define( 'EXT_IMAGE_REGEX', '/^('.HTTP_PROTOCOLS.')'. # Protocol '('.EXT_LINK_URL_CLASS.'+)\\/'. # Hostname and path @@ -1121,12 +1121,11 @@ class Parser * @access private */ function replaceFreeExternalLinks( $text ) { - global $wgUrlProtocols; global $wgContLang; $fname = 'Parser::replaceFreeExternalLinks'; wfProfileIn( $fname ); - $bits = preg_split( '/(\b(?:'.$wgUrlProtocols.'))/S', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); + $bits = preg_split( '/(\b(?:' . wfUrlProtocols() . '))/S', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); $s = array_shift( $bits ); $i = 0; @@ -1208,7 +1207,7 @@ class Parser * @access private */ function replaceInternalLinks( $s ) { - global $wgContLang, $wgLinkCache, $wgUrlProtocols; + global $wgContLang, $wgLinkCache; static $fname = 'Parser::replaceInternalLinks' ; wfProfileIn( $fname ); @@ -1310,7 +1309,7 @@ class Parser # Don't allow internal links to pages containing # PROTO: where PROTO is a valid URL protocol; these # should be external links. - if (preg_match('/^(\b(?:'.$wgUrlProtocols.'))/', $m[1])) { + if (preg_match('/^(\b(?:' . wfUrlProtocols() . '))/', $m[1])) { $s .= $prefix . '[[' . $line ; continue; } @@ -1406,7 +1405,7 @@ class Parser $text = $this->replaceInternalLinks($text); # cloak any absolute URLs inside the image markup, so replaceExternalLinks() won't touch them - $s .= $prefix . preg_replace("/\b($wgUrlProtocols)/", UNIQ_PREFIX."NOPARSE$1", $this->makeImage( $nt, $text) ) . $trail; + $s .= $prefix . preg_replace( "/\b(" . wfUrlProtocols() . ')/', UNIQ_PREFIX."NOPARSE$1", $this->makeImage( $nt, $text) ) . $trail; $wgLinkCache->addImageLinkObj( $nt ); wfProfileOut( "$fname-image" ); diff --git a/includes/Sanitizer.php b/includes/Sanitizer.php index 4e478c6c75..ef92e28db3 100644 --- a/includes/Sanitizer.php +++ b/includes/Sanitizer.php @@ -525,7 +525,6 @@ class Sanitizer { * @todo Check for unique id attribute :P */ function fixTagAttributes( $text, $element ) { - global $wgUrlProtocols; if( trim( $text ) == '' ) { return ''; } @@ -585,7 +584,7 @@ class Sanitizer { # Stupid hack $value = preg_replace_callback( - '/(' . $wgUrlProtocols . ')/', + '/(' . wfUrlProtocols() . ')/', array( 'Sanitizer', 'armorLinksCallback' ), $value ); diff --git a/includes/Skin.php b/includes/Skin.php index 9e03676986..0fb968eccd 100644 --- a/includes/Skin.php +++ b/includes/Skin.php @@ -1272,8 +1272,7 @@ END; # If url string starts with http, consider as external URL, else # internal /*static*/ function makeInternalOrExternalUrl( $name ) { - global $wgUrlProtocols; - if ( preg_match( '/^(?:' . $wgUrlProtocols . ')/', $name ) ) { + if ( preg_match( '/^(?:' . wfUrlProtocols() . ')/', $name ) ) { return $name; } else { return $this->makeUrl( $name ); -- 2.20.1