diff options
author | TechSneeze <dave@techsneeze.com> | 2021-06-18 21:22:15 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-18 21:22:15 +0300 |
commit | 287a3638e88cb8116b475bcd8553b77f1a1ccfc6 (patch) | |
tree | f15aef5f4f3084a6670d227273687527d29a575a | |
parent | 608f9fe3de9af929bb535498ca79f27e5c3b7367 (diff) | |
parent | 622815e12794304a244adfde51bac9548f897b42 (diff) |
Merge pull request #74 from mwander/xmlreport
Improved robustness and fixed bugs in formatXML()
-rw-r--r-- | dmarcts-report-viewer-report-data.php | 56 |
1 files changed, 29 insertions, 27 deletions
diff --git a/dmarcts-report-viewer-report-data.php b/dmarcts-report-viewer-report-data.php index aa04bc7..5aae830 100644 --- a/dmarcts-report-viewer-report-data.php +++ b/dmarcts-report-viewer-report-data.php @@ -208,35 +208,37 @@ function formatXML($raw_xml, $reportnumber) { $dom->formatOutput = true; $dom->loadXML($raw_xml); - // These next few lines adding <?xml version=\"1.0\" encoding=\"UTF-8\" > and <feedback> (as well as the lines adding the closing </feedback> tag) are are very risky because they assume that the first two lines and the last line of the raw_xml are weel-formed - // Hopefully not too risky as the raw_xml has already gone through the dmarcts-parser routine that looks for bad XML. - // If someone can code a proper way to get those lines, it would be appreciated. - $xml_arr = explode(PHP_EOL,$raw_xml); - $out = $xml_arr[0] . "\n" . $xml_arr[1]; - // Should return first 2 lines of xml: <?xml version=\"1.0\" encoding=\"UTF-8\"> and <feedback> - $html = "<pre><code class='xml'>" . htmlspecialchars($out) . "</code></pre>"; - - $out = $dom->saveXML($dom->getElementsByTagName("report_metadata")[0]); - $out = htmlspecialchars($out); - - $html .= "<div id='report_metadata' onmouseover='highlight(this);' onmouseout='unhighlight(this);' onclick='pin(this)'><pre><code class='xml'>" . $out . "</code></pre></div>"; - - $records = $dom->getElementsByTagName("record"); - $i = 0; - // $i++; - foreach ( $records as $record) { - $out = $dom->saveXML($dom->getElementsByTagName("record")[$i]); - $out = htmlspecialchars($out); - $html .= "<div id='record$id_min' onmouseover='highlight(this);' onmouseout='unhighlight(this);' onclick='pin(this)'><pre><code class='xml'>"; - $html .= $out; - $html .= "</code></pre></div>"; - $id_min++; - $i++; + // Extract and print <?xml ...> from raw_xml, if it matches the regex pattern. + if (preg_match("/<\?xml([^?>]*)\?>/", $raw_xml, $matches)) { + $html .= "<pre><code class='xml'>" . htmlspecialchars($matches[0]) . "</code></pre>"; + } + + // Extract and print root <feedback> from raw_xml. + $root = $dom->firstChild; + if (preg_match("/<". $root->localName ."([^>]*)>/", $raw_xml, $matches)) { + $html .= "<pre><code class='xml'>" . htmlspecialchars($matches[0]) . "</code></pre>"; + } + + // Print all child nodes + foreach ($root->childNodes as $element) { + $out = $dom->saveXML($element); + $out = htmlspecialchars($out); + + $elementName = $element->localName; + + // If element is a 'record', append database id to unique HTML id + if ($elementName === "record") { + $elementName .= $id_min; + $id_min++; + } + + $html .= "<div id='". $elementName ."' onmouseover='highlight(this);' onmouseout='unhighlight(this);' onclick='pin(this)'><pre><code class='xml'>" . $out . "</code></pre></div>"; } - $out = $xml_arr[sizeof($xml_arr)-2]; - $out = htmlspecialchars($out); - $html .= "<pre><code class='xml'>" . $out . "</code></pre>"; + // Extract closing </feedback> from raw_xml. + if (preg_match("/<\/". $root->localName .">/", $raw_xml, $matches)) { + $html .= "<pre><code class='xml'>" . htmlspecialchars($matches[0]) . "</code></pre>"; + } return $html; } |