diff --git a/includes/XmlTypeCheck.php b/includes/XmlTypeCheck.php index 2062101..761ba6a 100644 --- a/includes/XmlTypeCheck.php +++ b/includes/XmlTypeCheck.php @@ -53,10 +53,14 @@ class XmlTypeCheck { * @param array $options list of additional parsing options: * processing_instruction_handler: Callback for xml_set_processing_instruction_handler */ - function __construct( $file, $filterCallback=null, $options=array() ) { + function __construct( $file, $filterCallback=null, $options=array(), $isFile = true ) { $this->filterCallback = $filterCallback; $this->parserOptions = array_merge( $this->parserOptions, $options ); + if ( $isFile ) { $this->run( $file ); + } else { + $this->validateFromString( $file ); + } } /** @@ -110,6 +114,40 @@ class XmlTypeCheck { } /** + * Get an XML parser with the root element handler. + * @see XmlTypeCheck::rootElementOpen() + * @return resource a resource handle for the XML parser + */ + private function getParser() { + $parser = xml_parser_create_ns( 'UTF-8' ); + // case folding violates XML standard, turn it off + xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false ); + xml_set_element_handler( $parser, array( $this, 'rootElementOpen' ), false ); + if ( $this->parserOptions['processing_instruction_handler'] ) { + xml_set_processing_instruction_handler( + $parser, + array( $this, 'processingInstructionHandler' ) + ); + } + return $parser; + } + + /** + * + * @param string $string the XML-input-string to be checked. + */ + private function validateFromString( $string ) { + $parser = $this->getParser(); + $ret = xml_parse( $parser, $string, true ); + xml_parser_free( $parser ); + if ( $ret == 0 ) { + $this->wellFormed = false; + return; + } + $this->wellFormed = true; + } + + /** * @param $parser * @param $name * @param $attribs diff --git a/tests/phpunit/includes/upload/UploadTest.php b/tests/phpunit/includes/upload/UploadTest.php index ad1bc72..42b0a0b 100644 --- a/tests/phpunit/includes/upload/UploadTest.php +++ b/tests/phpunit/includes/upload/UploadTest.php @@ -368,8 +368,8 @@ class UploadTestHandler extends UploadBase { $check = new XmlTypeCheck( $svg, array( $this, 'checkSvgScriptCallback' ), - false, - array( 'processing_instruction_handler' => 'UploadBase::checkSvgPICallback' ) + array( 'processing_instruction_handler' => 'UploadBase::checkSvgPICallback' ), + false ); return array( $check->wellFormed, $check->filterMatch ); }