Skip to content

Instantly share code, notes, and snippets.

@goozak
Created March 11, 2016 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goozak/aa3f5bd5a146a51ddd75 to your computer and use it in GitHub Desktop.
Save goozak/aa3f5bd5a146a51ddd75 to your computer and use it in GitHub Desktop.
XMLReader bug
<?php
//Bug in PHP 5.5 and PHP 5.6 with XMLReader and specialy crafted XML
TestXML('https://www.dropbox.com/s/6uzaf0mqlp8znk9/XMLReaderGood.xml?dl=1');
echo '<hr>';
//This will report "Input is not proper UTF-8, indicate encoding !", which is incorrect
TestXML('https://www.dropbox.com/s/nkphfqbotxaogw7/XMLReaderBad.xml?dl=1');
function TestXML($file) {
$XR = new XMLReader;
$XR->open($file, null, LIBXML_NOBLANKS);
while (($lastRead = $XR->read()) && ($XR->name !== 'records')) { ; }
while (($lastRead = $XR->read()) && ($XR->name !== 'record')) { ; }
while ($lastRead) {
$xml = $XR->readOuterXML();
if ($xml === '') {
$err = '';
if ($e = libxml_get_last_error()) { $err = $e->message.' (line: '.$e->line.')'; }
$XR->close();
echo $file.' : Problem with file'.($err ? ' — '.$err : '').'.';
return;
}
while (($lastRead = $XR->next()) && ($XR->name !== 'record')) { ; }
}
$XR->close();
echo $file.' : Good!';
return;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment