getMessage()); } $fhuh = fopen($altsparename, "w"); fwrite($fhuh, $xxx); fclose($fhuh); $forcxml_lines = file($altsparename); $suffix .= " for xml url " . $forcxml_realpath; if ($forcdtd_realpath == null) $forcdtd_realpath = $altdtdname; $where = "1"; } else { $httppos = strpos($forcxml_realpath, "file:"); if ($httppos !== false && $httppos == 0) { $altdtdname = str_replace(".xml", ".dtd", $forcxml_realpath); $xxx = ""; try { $xxx = file_get_contents($forcxml_realpath); $forcxmlrealpath = $forcxml_realpath; } catch (Exception $ex) { $xxx = ""; $thingsfound .= (" " . $ex->getMessage()); } $fhuh = fopen($altsparename, "w"); fwrite($fhuh, $xxx); fclose($fhuh); $suffix .= " for xml url " . $forcxml_realpath; $forcxml_lines = file($altsparename); if ($forcdtd_realpath == null) $forcdtd_realpath = $altdtdname; $where = "2"; } else { $fhuh = fopen($altsparename, "w"); fwrite($fhuh, $forcxml_realpath); fclose($fhuh); $forcxml_lines = file($altsparename); $where = "3"; } } } else { $suffix .= " for xml file " . $forcxml_realpath; $altdtdname = str_replace(".xml", ".dtd", $forcxml_realpath); try { $forcxml_lines = file($forcxml_realpath); if (count($forcxml_lines) == 0) { $thingsfound .= " access to " . $forcxml_realpath . " denied"; } else { $forcxmlrealpath = $forcxml_realpath; } } catch (Exception $ex) { $thingsfound .= (" " . $ex->getMessage()); } $where = "4"; } if ($forcdtd_realpath != null) { if (!file_exists($forcdtd_realpath)) { $httppos = strpos(str_replace("https", "http", $forcdtd_realpath), "http:"); if ($httppos !== false && $httppos == 0) { $xxx = ""; try { $xxx = file_get_contents($forcdtd_realpath); } catch (Exception $ex) { $xxx = ""; $thingsfound .= (" " . $ex->getMessage()); } $fhuh = fopen($altsparename, "w"); fwrite($fhuh, $xxx); fclose($fhuh); $forcdtd_lines = file($altsparename); $where .= "1"; $suffix .= " and dtd url " . $forcdtd_realpath; } else { $httppos = strpos($forcdtd_realpath, "file:"); if ($httppos !== false && $httppos == 0) { $xxx = ""; try { $xxx = file_get_contents($forcdtd_realpath); } catch (Exception $ex) { $xxx = ""; $thingsfound .= (" " . $ex->getMessage()); } $fhuh = fopen($altsparename, "w"); fwrite($fhuh, $xxx); fclose($fhuh); $forcdtd_lines = file($altsparename); $suffix .= " and dtd url " . $forcdtd_realpath; $where .= "2"; } else { $fhuh = fopen($altdtdname, "w"); fwrite($fhuh, $forcdtd_realpath); fclose($fhuh); $forcdtd_realpath = $altdtdname; $forcdtd_lines = file($forcdtd_realpath); $where .= "3"; } } } else { $suffix .= " and dtd file " . $forcdtd_realpath; try { $forcdtd_lines = file($forcdtd_realpath); if (count($forcdtd_lines) == 0) $thingsfound .= " access to " . $forcdtd_realpath . " denied"; } catch (Exception $ex) { $thingsfound .= (" " . $ex->getMessage()); } $where .= "4"; } } $xmldoc = new DOMDocument; $forcx = "huh"; libxml_use_internal_errors(true); if ($forcdtd_realpath) { // Inject DTD inside DOCTYPE line: //$forcdtd_lines = file($forcdtd_realpath); $forcnew_lines = array(); foreach ($forcxml_lines as $forcx) { if ($validdata == "") $validdata = "Show xml
";
// Assume DOCTYPE format used:
if (preg_match('/DOCTYPE/', $forcx)) {
$forcy = preg_replace('/SYSTEM "(.*)"/', " [\n" . implode("\n", $forcdtd_lines) . "\n]", $forcx);
$forcnew_lines[] = $forcy;
$validdata .= str_replace(">", ">", str_replace("<", "<", $forcy)) . "
";
} else {
$forcnew_lines[] = $forcx;
$validdata .= str_replace(">", ">", str_replace("<", "<", $forcx)) . "
";
}
}
$xmldoc->loadXML(implode("\n", $forcnew_lines));
} else {
$xmldoc->loadXML(implode("\n", $forcxml_lines));
}
// Enable user error handling
libxml_use_internal_errors(true);
if (@$xmldoc->validate() && $forcx != "huh") {
if ($validdata != "") $validdata .= "
";
$forcerrors = libxml_get_errors();
//foreach ($forcerrors as $forcerror) {
// print_r($forcerror, true);
//}
$fhuh = fopen($altsparename, "w");
fwrite($fhuh, $validdata);
fclose($fhuh);
$forcxmllines = file($altsparename);
foreach ($forcerrors as $forcerror) {
echo str_replace("\n", "
", display_xml_error($forcerror, $forcxmllines, $forcxmlrealpath));
}
echo "
Try call like ./xmldtdvalidate.php?inxml=test.xml&indtd=test.dtd
"; echo "