From:             vl dot homutov at gmail dot com
Operating system: Linux
PHP version:      5.4.7
Package:          *XML functions
Bug Type:         Bug
Bug description:External DTDs are not processed

Description:
------------
PHP's xml_parse() ignores external DTD specified in the
XML file and thus can't parse the file if it has
unknown entities (defined in the DTD mentioned).


Test script:
---------------
#!/usr/bin/php
<?php

$xml_ext_dtd=<<<EOXML
<?xml version="1.0"?>
<!DOCTYPE mytag SYSTEM "./mytag.dtd">
<mytag><elem>one</elem><elem>two</elem><elem>&custom;</elem>/mytag>
EOXML;

$xml_int_dtd=<<<EOXML
<?xml version="1.0"?>
<!DOCTYPE mytag
[
<!ENTITY custom SYSTEM "file.txt">
]>
<mytag><elem>one</elem><elem>two</elem><elem>&custom;</elem>/mytag>
EOXML;

function externalEntityHandler($parser, $name, $base, $systemId,
$publicId)
{
        echo "PROCESS EXTERNAL REFERENCE(file=$systemId)\n";
        return true;
}

function characterDataHandler($parser, $data)
{
        echo "CDATA found: '$data'\n";
}

function xerr($parser)
{
        $out = "XML parser error:";
        $out.=xml_error_string(xml_get_error_code($parser));
        $out.="\n";
        return $out;
}

echo "This works OK - parse xml1:\n$xml_int_dtd\n";
echo "---------------------------------------\n";
$xml_parser = xml_parser_create();
xml_set_character_data_handler($xml_parser, "characterDataHandler");
xml_set_external_entity_ref_handler($xml_parser, "externalEntityHandler");
xml_parse($xml_parser, $xml_int_dtd) or die(xerr($xml_parser));

echo "\nThis FAILS - parse xml2:\n$xml_ext_dtd\n";
echo "---------------------------------------\n";
$xml_parser = xml_parser_create();
xml_set_character_data_handler($xml_parser, "characterDataHandler");
xml_set_external_entity_ref_handler($xml_parser, "externalEntityHandler");
$rv = xml_parse($xml_parser, $xml_ext_dtd);
if (!$rv) echo xerr($xml_parser);

echo "file 'mytag.dtd' is:\n".file_get_contents("./mytag.dtd");

?>

Expected result:
----------------
This works OK - parse xml1:
<?xml version="1.0"?>
<!DOCTYPE mytag
[
<!ENTITY custom SYSTEM "file.txt">
]>
<mytag><elem>one</elem><elem>two</elem><elem>&custom;</elem>/mytag>
---------------------------------------
CDATA found: 'one'
CDATA found: 'two'
PROCESS EXTERNAL REFERENCE(file=file.txt)


Actual result:
--------------
This FAILS - parse xml2:
<?xml version="1.0"?>
<!DOCTYPE mytag SYSTEM "./mytag.dtd">
<mytag><elem>one</elem><elem>two</elem><elem>&custom;</elem>/mytag>
---------------------------------------
CDATA found: 'one'
CDATA found: 'two'
XML parser error:Undeclared entity warning
file 'mytag.dtd' is:
<!ENTITY custom SYSTEM "file.txt">


-- 
Edit bug report at https://bugs.php.net/bug.php?id=63189&edit=1
-- 
Try a snapshot (PHP 5.4):   
https://bugs.php.net/fix.php?id=63189&r=trysnapshot54
Try a snapshot (PHP 5.3):   
https://bugs.php.net/fix.php?id=63189&r=trysnapshot53
Try a snapshot (trunk):     
https://bugs.php.net/fix.php?id=63189&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=63189&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=63189&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=63189&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=63189&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=63189&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=63189&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=63189&r=notwrong
Not enough info:            
https://bugs.php.net/fix.php?id=63189&r=notenoughinfo
Submitted twice:            
https://bugs.php.net/fix.php?id=63189&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=63189&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=63189&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=63189&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=63189&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=63189&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=63189&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=63189&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=63189&r=mysqlcfg

Reply via email to