From c8f13d82becfae3f9a554ddec46ce4a9bacde74d Mon Sep 17 00:00:00 2001 From: jnew-gh Date: Mon, 3 May 2021 20:35:44 -0400 Subject: Add xml highlighting support Re-code formatXML() function to accommodate xml highlighting --- dmarcts-report-viewer-report-data.php | 71 +++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 12 deletions(-) diff --git a/dmarcts-report-viewer-report-data.php b/dmarcts-report-viewer-report-data.php index 1d5e4da..9bc6b6e 100644 --- a/dmarcts-report-viewer-report-data.php +++ b/dmarcts-report-viewer-report-data.php @@ -55,11 +55,11 @@ function tmpl_reportData($reportnumber, $reports, $host_lookup = 1) { if (isset($reports[$reportnumber])) { $row = $reports[$reportnumber]; - $row['raw_xml'] = formatXML($row['raw_xml']); - $row = array_map('htmlspecialchars', $row); + + $row['raw_xml'] = formatXML($row['raw_xml'], $reportnumber); $reportdata[] = "
"; - $reportdata[] = "
Report from ".$row['org']." for ".$row['domain']."
(". format_date($row['mindate'], "r" ). " - ".format_date($row['maxdate'], "r" ).")
Policies: adkim=" . $row['policy_adkim'] . ", aspf=" . $row['policy_aspf'] . ", p=" . $row['policy_p'] . ", sp=" . $row['policy_sp'] . ", pct=" . $row['policy_pct'] . "
"; + $reportdata[] = "
Report from ".$row['org']." for ".$row['domain']."
(". format_date($row['mindate'], "r" ). " - ".format_date($row['maxdate'], "r" ).")
Policies: adkim=" . $row['policy_adkim'] . ", aspf=" . $row['policy_aspf'] . ", p=" . $row['policy_p'] . ", sp=" . $row['policy_sp'] . ", pct=" . $row['policy_pct'] . "
"; $reportdata[] = "
Show Raw Report XML
"; @@ -70,7 +70,7 @@ function tmpl_reportData($reportnumber, $reports, $host_lookup = 1) { } $reportdata[] = ""; $reportdata[] = "
"; @@ -146,7 +146,7 @@ ORDER BY /* escape html characters after exploring binary values, which will be messed up */ $row = array_map('htmlspecialchars', $row); - $reportdata[] = " "; + $reportdata[] = " "; $reportdata[] = " ". $ip. ""; if ( $host_lookup ) { $reportdata[] = " ". gethostbyaddr($ip). ""; @@ -179,19 +179,66 @@ ORDER BY return implode("\n ",$reportdata); } -function formatXML($xml) { +function formatXML($raw_xml, $reportnumber) { - $dom = new DOMDocument(); + global $mysqli; - // Initial block (must before load xml string) + $out = ""; + $html = ""; + + $sql = " + SELECT + MIN(id) AS id_min, + MAX(id) AS id_max + FROM + rptrecord + WHERE + serial = $reportnumber; + "; + + $query = $mysqli->query($sql) or die("Query failed: ".$mysqli->error." (Error #" .$mysqli->errno.")"); + + while($row = $query->fetch_assoc()) { + $id_min = $row['id_min']; + $id_max = $row['id_max']; + } + + $dom = new DOMDocument(); $dom->preserveWhiteSpace = false; $dom->formatOutput = true; - // End initial block + $dom->loadXML($raw_xml); + + // These next few lines adding and (as well as the lines adding the closing 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: and + $html = "
" . htmlspecialchars($out) . "
"; + + $out = $dom->saveXML($dom->getElementsByTagName("report_metadata")[0]); + $out = htmlspecialchars($out); + + $html .= "
" . $out . "
"; + + $records = $dom->getElementsByTagName("record"); + $i = 0; + // $i++; + foreach ( $records as $record) { + $out = $dom->saveXML($dom->getElementsByTagName("record")[$i]); + $out = htmlspecialchars($out); + $html .= "
";
+		$html .= $out;
+		$html .= "
"; + $id_min++; + $i++; + } - $dom->loadXML($xml); - $out = $dom->saveXML(); + $out = $xml_arr[sizeof($xml_arr)-2]; + $out = htmlspecialchars($out); + $html .= "
" . $out . "
"; - return $out; + return $html; } //#################################################################### -- cgit v1.2.3