<?php
-
/**
* (X)HTML sanitizer for MediaWiki
*
'zwj' => 8205,
'zwnj' => 8204 );
+/** @package MediaWiki */
class Sanitizer {
/**
* Cleans up HTML, removes dangerous tags and attributes, and
$htmlsingle = array(
'br', 'hr', 'li', 'dt', 'dd'
);
+ $htmlsingleonly = array( # Elements that cannot have close tags
+ 'br', 'hr'
+ );
$htmlnest = array( # Tags that can be nested--??
'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
'dl', 'font', 'big', 'small', 'sub', 'sup', 'span'
$tagstack = array(); $tablestack = array();
foreach ( $bits as $x ) {
$prev = error_reporting( E_ALL & ~( E_NOTICE | E_WARNING ) );
- preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
+ preg_match( '/^(\\/?)(\\w+)([^>]*?)(\\/{0,1}>)([^<]*)$/',
$x, $regs );
list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
error_reporting( $prev );
# Check our stack
if ( $slash ) {
# Closing a tag...
- if ( ! in_array( $t, $htmlsingle ) &&
+ if( in_array( $t, $htmlsingleonly ) ) {
+ $badtag = 1;
+ } elseif( !in_array( $t, $htmlsingle ) &&
( $ot = @array_pop( $tagstack ) ) != $t ) {
@array_push( $tagstack, $ot );
$badtag = 1;
} else if ( in_array( $t, $tagstack ) &&
! in_array ( $t , $htmlnest ) ) {
$badtag = 1 ;
+ } elseif( in_array( $t, $htmlsingleonly ) ) {
+ # Hack to force empty tag for uncloseable elements
+ $brace = '/>';
} else if ( ! in_array( $t, $htmlsingle ) ) {
if ( $t == 'table' ) {
array_push( $tablestack, $tagstack );
}
if ( ! $badtag ) {
$rest = str_replace( '>', '>', $rest );
- $text .= "<$slash$t$newparams$brace$rest";
+ $close = ( $brace == '/>' ) ? ' /' : '';
+ $text .= "<$slash$t$newparams$close>$rest";
continue;
}
}
} else {
# this might be possible using tidy itself
foreach ( $bits as $x ) {
- preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
+ preg_match( '/^(\\/?)(\\w+)([^>]*?)(\\/{0,1}>)([^<]*)$/',
$x, $regs );
@list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
* @todo Check for unique id attribute :P
*/
function fixTagAttributes( $text, $element ) {
+ global $wgUrlProtocols;
if( trim( $text ) == '' ) {
return '';
}
'RFC' => 'RFC',
'PMID' => 'PMID',
) );
- $value = preg_replace(
- '/(' . URL_PROTOCOLS . '):/',
- '\\1:', $value );
- if( !isset( $attribs[$attribute] ) ) {
- $attribs[$attribute] = "$attribute=\"$value\"";
- }
+ # Stupid hack
+ $value = preg_replace_callback(
+ '/(' . $wgUrlProtocols . ')/',
+ array( 'Sanitizer', 'armorLinksCallback' ),
+ $value );
+
+ // If this attribute was previously set, override it.
+ // Output should only have one attribute of each name.
+ $attribs[$attribute] = "$attribute=\"$value\"";
}
if( empty( $attribs ) ) {
return '';
}
}
+ /**
+ * Regex replace callback for armoring links against further processing.
+ * @param array $matches
+ * @return string
+ * @access private
+ */
+ function armorLinksCallback( $matches ) {
+ return str_replace( ':', ':', $matches[1] );
+ }
+
/**
* Return an associative array of attribute names and values from
* a partial tag string. Attribute names are forces to lowercase,
}
function decCharReference( $codepoint ) {
- $point = IntVal( $codepoint );
+ $point = intval( $codepoint );
if( Sanitizer::validateCodepoint( $point ) ) {
return sprintf( '&#%d;', $point );
} else {