diff options
author | TechSneeze <dave@techsneeze.com> | 2017-10-02 21:34:42 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-02 21:34:42 +0300 |
commit | 5d97612e00bcce48d446fa85e19b392d8433376f (patch) | |
tree | b712058b4327e5008b88b7721b670bc269cd4d87 | |
parent | 4b8d4d7465393d9b3620a6ea0fae9dddd13e130c (diff) | |
parent | a7472936187071a45d415f719d286432b86f74d4 (diff) |
Merge pull request #15 from lwt-pressy/master
a bit fancy stuff and make domain selectable
-rw-r--r-- | default.css | 4 | ||||
-rw-r--r-- | dmarcts-report-viewer.php | 73 |
2 files changed, 62 insertions, 15 deletions
diff --git a/default.css b/default.css index fad73a9..0b3ebea 100644 --- a/default.css +++ b/default.css @@ -19,7 +19,9 @@ table.reportlist thead { table.reportlist tbody tr:first-child td {
padding-top: 10px;
}
-
+table.reportlist tr.sum {
+ border-top: 1px solid grey;
+}
.reportdesc {
diff --git a/dmarcts-report-viewer.php b/dmarcts-report-viewer.php index 4d08900..ef88fe2 100644 --- a/dmarcts-report-viewer.php +++ b/dmarcts-report-viewer.php @@ -36,11 +36,11 @@ function format_date($date, $format) { return $answer; }; -function tmpl_reportList($allowed_reports, $host_lookup = 1) { +function tmpl_reportList($allowed_reports, $host_lookup = 1,$dom_select = '') { $reportlist[] = ""; $reportlist[] = "<!-- Start of report list -->"; - $reportlist[] = "<h1>DMARC Reports</h1>"; + $reportlist[] = "<h1>DMARC Reports" . ($dom_select == '' ? '' : " for " . htmlentities($dom_select)) . "</h1>"; $reportlist[] = "<table class='reportlist'>"; $reportlist[] = " <thead>"; $reportlist[] = " <tr>"; @@ -54,6 +54,7 @@ function tmpl_reportList($allowed_reports, $host_lookup = 1) { $reportlist[] = " </thead>"; $reportlist[] = " <tbody>"; + $reportsum = 0; foreach ($allowed_reports[BySerial] as $row) { $row = array_map('htmlspecialchars', $row); @@ -63,10 +64,12 @@ function tmpl_reportList($allowed_reports, $host_lookup = 1) { $reportlist[] = " <td class='right'>". format_date($row['maxdate'], $date_output_format). "</td>"; $reportlist[] = " <td class='center'>". $row['domain']. "</td>"; $reportlist[] = " <td class='center'>". $row['org']. "</td>"; - $reportlist[] = " <td class='center'><a href='?report=" . $row['serial'] . ( $host_lookup ? "&hostlookup=1" : "&hostlookup=0" ) . "#rpt". $row['serial'] . "'>". $row['reportid']. "</a></td>"; - $reportlist[] = " <td class='center'>". $row['rcount']. "</td>"; + $reportlist[] = " <td class='center'><a href='?report=" . $row['serial'] . ( $host_lookup ? "&hostlookup=1" : "&hostlookup=0" ) . ($dom_select == '' ? '' : "&d=$dom_select") . "#rpt". $row['serial'] . "'>". $row['reportid']. "</a></td>"; + $reportlist[] = " <td class='center'>". number_format($row['rcount']+0,0). "</td>"; $reportlist[] = " </tr>"; + $reportsum += $row['rcount']; } + $reportlist[] = "<tr class='sum'><td></td><td></td><td></td><td></td><td class='right'>Sum:</td><td class='center'>".number_format($reportsum,0)."</td></tr>"; $reportlist[] = " </tbody>"; $reportlist[] = "</table>"; @@ -86,12 +89,13 @@ function tmpl_reportData($reportnumber, $allowed_reports, $host_lookup = 1) { $reportdata[] = ""; $reportdata[] = "<!-- Start of report rata -->"; + $reportsum = 0; if (isset($allowed_reports[BySerial][$reportnumber])) { $row = $allowed_reports[BySerial][$reportnumber]; $row = array_map('htmlspecialchars', $row); $reportdata[] = "<a id='rpt".$reportnumber."'></a>"; - $reportdata[] = "<div class='center reportdesc'><p> Report from ".$row['org']." for ".$row['domain']."<br>(". format_date($row['mindate'], "r" ). " - ".format_date($row['maxdate'], "r" ).")<br> Policies: adkim=" . $row[policy_adkim] . ", aspf=" . $row[policy_aspf] . ", p=" . $row[policy_p] . ", sp=" . $row[policy_sp] . ", pct=" . $row[policy_pct] . "</p></div>"; + $reportdata[] = "<div class='center reportdesc'><p> Report from ".$row['org']." for ".$row['domain']."<br>(". format_date($row['mindate'], "r" ). " - ".format_date($row['maxdate'], "r" ).")<br> Policies: adkim=" . $row['policy_adkim'] . ", aspf=" . $row['policy_aspf'] . ", p=" . $row['policy_p'] . ", sp=" . $row['policy_sp'] . ", pct=" . $row['policy_pct'] . "</p></div>"; } else { return "Unknown report number!"; } @@ -151,7 +155,10 @@ function tmpl_reportData($reportnumber, $allowed_reports, $host_lookup = 1) { $reportdata[] = " <td>". $row['spfdomain']. "</td>"; $reportdata[] = " <td>". $row['spfresult']. "</td>"; $reportdata[] = " </tr>"; + + $reportsum += $row['rcount']; } + $reportdata[] = "<tr><td></td><td></td><td>$reportsum</td><td></td><td></td><td></td><td></td><td></td><td></td></tr>"; $reportdata[] = " </tbody>"; $reportdata[] = "</table>"; @@ -162,10 +169,13 @@ function tmpl_reportData($reportnumber, $allowed_reports, $host_lookup = 1) { return implode("\n ",$reportdata); } -function tmpl_page ($body, $reportid, $host_lookup = 1) { +function tmpl_page ($body, $reportid, $host_lookup = 1, $dom_select, $domains = array() ) { $html = array(); - $url_switch = ( $reportid ? "?report=$reportid&hostlookup=" : "?hostlookup=" ) - . ($host_lookup ? "0" : "1" ); + $url_hswitch = ( $reportid ? "?report=$reportid&hostlookup=" : "?hostlookup=" ) + . ($host_lookup ? "0" : "1" ) + . (isset($dom_select) && $dom_select <> "" ? "&d=$dom_select" : "" ) + ; + $url_dswitch = "?hostlookup=" . ($host_lookup ? "1" : "0" ); // drop selected report on domain switch $html[] = "<!DOCTYPE html>"; $html[] = "<html>"; @@ -175,7 +185,14 @@ function tmpl_page ($body, $reportid, $host_lookup = 1) { $html[] = " </head>"; $html[] = " <body>"; - $html[] = " <div class='options'>Hostname Lookup is " . ($host_lookup ? "on" : "off" ) . " [<a href=\"$url_switch\">" . ($host_lookup ? "off" : "on" ) . "</a>]</div>"; + $html[] = " <div class='options'>Hostname Lookup is " . ($host_lookup ? "on" : "off" ) . " [<a href=\"$url_hswitch\">" . ($host_lookup ? "off" : "on" ) . "</a>]</div>"; + if ( count( $domains ) > 1 ) { + $html[] = "<div class='options'>Domains: "; + foreach( $domains as $d) { + $html[] = "[<a href=\"$url_dswitch&d=$d\">" . $d . "</a>] "; + } + $html[] = "<a href=\"$url_dswitch\">[all]</a></div>"; + } $html[] = $body; @@ -194,7 +211,7 @@ function tmpl_page ($body, $reportid, $host_lookup = 1) { // The file is expected to be in the same folder as this script, and it // must exist. include "dmarcts-report-viewer-config.php"; - +$dom_select= ''; if(isset($_GET['report']) && is_numeric($_GET['report'])){ $reportid=$_GET['report']+0; @@ -210,6 +227,13 @@ if(isset($_GET['hostlookup']) && is_numeric($_GET['hostlookup'])){ }else{ die('Invalid hostlookup flag'); } +if(isset($_GET['d'])){ + $dom_select=$_GET['d']; +}elseif(!isset($_GET['d'])){ + $dom_select= ''; +}else{ + die('Invalid domain'); +} // Make a MySQL Connection using mysqli @@ -225,10 +249,29 @@ define("BySerial", 1); define("ByDomain", 2); define("ByOrganisation", 3); +// get all domains reported +$sql="SELECT DISTINCT domain FROM `report` ORDER BY domain"; +$domains= array(); +$query = $mysqli->query($sql) or die("Query failed: ".$mysqli->error." (Error #" .$mysqli->errno.")"); +while($row = $query->fetch_assoc()) { + $domains[] = $row['domain']; +} +if( $dom_select <> '' && array_search($dom_select, $domains) === FALSE ) { + echo "Error: invalid domain " . htmlentities($dom_select) . " \n"; + exit; +} + + // Get allowed reports and cache them - using serial as key $allowed_reports = array(); + # Include the rcount via left join, so we do not have to make an sql query for every single report. -$sql = "SELECT report.* , sum(rptrecord.rcount) as rcount FROM `report` LEFT Join rptrecord on report.serial = rptrecord.serial group by serial order by mindate"; +$where = ''; +if( $dom_select <> '' ) { + $where = "WHERE domain='" . $mysqli->real_escape_string($dom_select) . "'"; +} +$sql = "SELECT report.* , sum(rptrecord.rcount) AS rcount FROM `report` LEFT JOIN rptrecord ON report.serial = rptrecord.serial $where GROUP BY serial ORDER BY mindate,maxdate,org"; + $query = $mysqli->query($sql) or die("Query failed: ".$mysqli->error." (Error #" .$mysqli->errno.")"); while($row = $query->fetch_assoc()) { //todo: check ACL if this row is allowed @@ -236,16 +279,18 @@ while($row = $query->fetch_assoc()) { //add data by serial $allowed_reports[BySerial][$row['serial']] = $row; //make a list of serials by domain and by organisation - $allowed_reports[ByDomain][$row['domain']][] = $row['serial']; - $allowed_reports[ByOrganisation][$row['org']][] = $row['serial']; + //$allowed_reports[ByDomain][$row['domain']][] = $row['serial']; + //$allowed_reports[ByOrganisation][$row['org']][] = $row['serial']; } } // Generate Page with report list and report data (if a report is selected). echo tmpl_page( "" - .tmpl_reportList($allowed_reports, $hostlookup) + .tmpl_reportList($allowed_reports, $hostlookup, $dom_select) .tmpl_reportData($reportid, $allowed_reports, $hostlookup ) , $reportid , $hostlookup + , $dom_select + , $domains ); ?> |