summaryrefslogtreecommitdiff
path: root/http/index.php
blob: 7950b67ce28b174e79978d5761d1163ba3be591a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php

$reports = array();

$errors = false;

foreach (glob('../reports/*') as $file) {
  $suffix = preg_replace('/^.+\.([^.]+)/', '\1', $file);
  switch ($suffix) {
    case 'gz':
      $content = shell_exec('zcat "' . $file . '"');
    break;
    case 'zip':
      $content = shell_exec('unzip -p "' . $file . '"');
    break;
    default:
      print 'Unknown suffix: ' . $suffix . "<br>\n";
      $errors = true;
      continue 2;
  }
  if (! $xml = simplexml_load_string($content)) {
    print "Failed parsing XML<br>\n";
    $errors = true;
    continue;
  }
  $reports[] = json_decode(json_encode((array)$xml),true);
}

if ($errors)
  die();

function compare_reports($a, $b) {
  if ($a['report_metadata']['date_range']['begin'] < $b['report_metadata']['date_range']['begin'])
    return 1;
  if ($a['report_metadata']['date_range']['begin'] > $b['report_metadata']['date_range']['begin'])
    return -1;
  return 0;
}

usort(
  $reports,
  'compare_reports'
);

function format_date($date) {
  return gmdate('Y-m-d', round($date/60/60/24)*60*60*24);
}
function result_color($result) {
  switch ($result) {
    case 'none':
      return 'c0c0c0';
    case 'pass':
      return 'a0ffa0';
    case 'fail':
      return 'ffa0a0';
    default:
      return 'ffffff';
  }
}
function host_color($host) {
  if ($host == gethostname())
    return 'a0ffa0';
  else
    return 'ffa0a0';
}

$columns = array(
  'Organization', 'Begin', 'End', 'Domain', 'IP (Host)', 'Disposition', 'DKIM', 'SPF', 'Count'
);

print "<html>\n";
print "  <body>\n";
print "    <table>\n";
print "      <tr>\n";
foreach ($columns as $title) {
  print "        <th>\n";
  print "          " . $title . "\n";
  print "        </th>\n";
}
print "      </tr>\n";
foreach ($reports as $report) {
  if (array_key_exists('row', $report['record']))
    $records = array($report['record']);
  else
    $records = $report['record'];
  foreach ($records as $record) {
    print "      <tr>\n";
    print "        <td>\n";
    print "          " . $report['report_metadata']['org_name'] . "\n";
    print "        </td>\n";
    print "        <td>\n";
    print "          " . format_date($report['report_metadata']['date_range']['begin']) . "\n";
    print "        </td>\n";
    print "        <td>\n";
    print "          " . format_date($report['report_metadata']['date_range']['end']) . "\n";
    print "        </td>\n";
    print "        <td>\n";
    print "          " . $report['policy_published']['domain'] . "\n";
    print "        </td>\n";
    $host = gethostbyaddr($record['row']['source_ip']);
    print "        <td bgcolor=\"" . host_color($host) . "\">\n";
    print "          " . $record['row']['source_ip'] . " (" . $host . ")\n";
    print "        </td>\n";
    foreach (array('disposition','dkim','spf') as $what) {
      print "        <td bgcolor=\"" . result_color($record['row']['policy_evaluated'][$what]) . "\">\n";
      print "          " . $record['row']['policy_evaluated'][$what] . "\n";
      print "        </td>\n";
    }
    print "        <td>\n";
    print "          " . $record['row']['count'] . "\n";
    print "        </td>\n";
    print "      </tr>\n";
  }
}
print "    </table>\n";
print "  </body>\n";
print "</html>";