| 1 |
<?php |
| 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 |
namespace Liuch\DmarcSrg\Report; |
| 33 |
|
| 34 |
use Liuch\DmarcSrg\Common; |
| 35 |
use Liuch\DmarcSrg\TextTable; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class OverallReport |
| 41 |
{ |
| 42 |
private $rows = []; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function appendData(array $data): void |
| 52 |
{ |
| 53 |
$this->rows[] = $data; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function removeRow($domain): void |
| 64 |
{ |
| 65 |
$fqdn = $domain->fqdn(); |
| 66 |
foreach ($this->rows as $i => &$row) { |
| 67 |
if ($row['fqdn'] === $fqdn) { |
| 68 |
unset($this->rows[$i]); |
| 69 |
break; |
| 70 |
} |
| 71 |
} |
| 72 |
unset($row); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function toArray(): array |
| 81 |
{ |
| 82 |
return $this->rows; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function text(): array |
| 91 |
{ |
| 92 |
$res = [ '# Overall by domains', '' ]; |
| 93 |
|
| 94 |
$table = new TextTable([ '', 'Emails', 'SPF only', 'DKIM only', 'Not aligned', 'Quar+Rej' ]); |
| 95 |
$table->setMinColumnsWidth([ 15, 6, 8, 9, 11, 8 ])->setBorders('', '', '') |
| 96 |
->setColumnAlignment(4, 'right')->setColumnAlignment(5, 'right'); |
| 97 |
foreach ($this->rows as &$row) { |
| 98 |
$total = $row['total']; |
| 99 |
$d_aln = $row['dkim_aligned']; |
| 100 |
$s_aln = $row['spf_aligned']; |
| 101 |
$q_dis = $row['quarantined']; |
| 102 |
$r_dis = $row['rejected']; |
| 103 |
$n_aln = $total - $row['dkim_spf_aligned'] - $d_aln - $s_aln; |
| 104 |
if ($q_dis || $r_dis) { |
| 105 |
$s_dis = Common::num2percent($q_dis + $r_dis, $total, false) . "({$q_dis}+{$r_dis})"; |
| 106 |
} else { |
| 107 |
$s_dis = '0'; |
| 108 |
} |
| 109 |
$table->appendRow([ |
| 110 |
$row['fqdn'], $total, $s_aln, $d_aln, Common::num2percent($n_aln, $total, true), $s_dis |
| 111 |
]); |
| 112 |
} |
| 113 |
unset($row); |
| 114 |
foreach ($table->toArray() as $line) { |
| 115 |
$res[] = " $line"; |
| 116 |
} |
| 117 |
$res[] = ''; |
| 118 |
|
| 119 |
return $res; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public function html(): array |
| 128 |
{ |
| 129 |
$h2a = 'style="margin:15px 0 5px;"'; |
| 130 |
$t2a = 'style="border-collapse:collapse;border-spacing:0;"'; |
| 131 |
$c1a = 'style="font-style:italic;"'; |
| 132 |
$d3s = 'border:1px solid #888;'; |
| 133 |
$d4s = 'text-align:right;'; |
| 134 |
$d5s = 'padding:.3em;'; |
| 135 |
$rs2 = 'rowspan="2"'; |
| 136 |
$cs2 = 'colspan="2"'; |
| 137 |
|
| 138 |
$get_color = function (string $name, int $num) { |
| 139 |
$cn = ''; |
| 140 |
if ($num > 0) { |
| 141 |
switch ($name) { |
| 142 |
case 'red': |
| 143 |
$cn = 'f00'; |
| 144 |
break; |
| 145 |
case 'green': |
| 146 |
$cn = '080'; |
| 147 |
break; |
| 148 |
} |
| 149 |
} |
| 150 |
return empty($cn) ? '' : "color:#{$cn};"; |
| 151 |
}; |
| 152 |
|
| 153 |
$r_cnt = count($this->rows); |
| 154 |
$res[] = "<h2 {$h2a}>Overall by domains</h2>"; |
| 155 |
$res[] = "<table {$t2a}>"; |
| 156 |
$res[] = " <caption {$c1a}>Total records: {$r_cnt}</caption>"; |
| 157 |
$res[] = ' <thead>'; |
| 158 |
$style = "style=\"{$d3s}{$d5s}\""; |
| 159 |
$res[] = " <tr><th {$rs2} {$style}>Name</th><th {$rs2} {$style}>Emails</th>" . |
| 160 |
"<th {$cs2} {$style}>Partial aligned</th><th {$rs2} {$style}>Not aligned</th>" . |
| 161 |
"<th {$cs2} {$style}>Disposition</th></tr>"; |
| 162 |
$res[] = "<th {$style}>SPF only</th><th {$style}>DKIM only</th>" . |
| 163 |
"<th {$style}>quar+rej</th><th {$style}>fail rate</th></tr>"; |
| 164 |
$res[] = ' </thead>'; |
| 165 |
$res[] = ' <tbody>'; |
| 166 |
$style = "style=\"{$d3s}{$d5s}"; |
| 167 |
foreach ($this->rows as &$row) { |
| 168 |
$name = htmlspecialchars(trim($row['fqdn'])); |
| 169 |
$total = $row['total']; |
| 170 |
$f_aln = $row['dkim_spf_aligned']; |
| 171 |
$d_aln = $row['dkim_aligned']; |
| 172 |
$s_aln = $row['spf_aligned']; |
| 173 |
$n_aln = $total - $f_aln - $d_aln - $s_aln; |
| 174 |
$q_dis = $row['quarantined']; |
| 175 |
$r_dis = $row['rejected']; |
| 176 |
$s_dis = ($q_dis || $r_dis) ? "{$q_dis}+{$r_dis}" : '0'; |
| 177 |
$res[] = " <tr><td {$style}\">{$name}</td><td {$style}{$d4s}\">{$total}</td>" . |
| 178 |
"<td {$style}{$d4s}\">{$s_aln}</td><td {$style}{$d4s}\">{$d_aln}</td>" . |
| 179 |
"<td {$style}{$d4s}{$get_color('red', $n_aln)}\">{$n_aln}</td>" . |
| 180 |
"<td {$style}{$d4s}{$get_color('red', $q_dis + $r_dis)}\">{$s_dis}</td>" . |
| 181 |
"<td {$style}{$d4s}\">" . Common::num2percent($q_dis + $r_dis, $total, false) . |
| 182 |
'</td></tr>'; |
| 183 |
} |
| 184 |
unset($row); |
| 185 |
$res[] = ' </tbody>'; |
| 186 |
$res[] = '</table>'; |
| 187 |
|
| 188 |
return $res; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public function markdown(): array |
| 197 |
{ |
| 198 |
$res = [ '# Overall by domains' ]; |
| 199 |
$res[] = ''; |
| 200 |
|
| 201 |
|
| 202 |
$res[] = '| Domain | Emails | SPF only | DKIM only | Not aligned | Quar+Rej |'; |
| 203 |
$res[] = '|--------|--------|----------|-----------|-------------|----------|'; |
| 204 |
|
| 205 |
foreach ($this->rows as &$row) { |
| 206 |
$total = $row['total']; |
| 207 |
$d_aln = $row['dkim_aligned']; |
| 208 |
$s_aln = $row['spf_aligned']; |
| 209 |
$q_dis = $row['quarantined']; |
| 210 |
$r_dis = $row['rejected']; |
| 211 |
$n_aln = $total - $row['dkim_spf_aligned'] - $d_aln - $s_aln; |
| 212 |
if ($q_dis || $r_dis) { |
| 213 |
$s_dis = Common::num2percent($q_dis + $r_dis, $total, false) . "({$q_dis}+{$r_dis})"; |
| 214 |
} else { |
| 215 |
$s_dis = '0'; |
| 216 |
} |
| 217 |
|
| 218 |
$res[] = sprintf('| %s | %s | %s | %s | %s | %s |', |
| 219 |
$this->escapeMarkdown(trim($row['fqdn'])), |
| 220 |
$total, |
| 221 |
$s_aln, |
| 222 |
$d_aln, |
| 223 |
$this->escapeMarkdown(Common::num2percent($n_aln, $total, true)), |
| 224 |
$this->escapeMarkdown($s_dis) |
| 225 |
); |
| 226 |
} |
| 227 |
unset($row); |
| 228 |
$res[] = ''; |
| 229 |
|
| 230 |
return $res; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
private function escapeMarkdown(string $text): string |
| 241 |
{ |
| 242 |
|
| 243 |
$text = str_replace('\\', '\\\\', $text); |
| 244 |
$text = str_replace('|', '\\|', $text); |
| 245 |
|
| 246 |
return $text; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public function csv(): string |
| 255 |
{ |
| 256 |
$res = [ 'Overall by domains' ]; |
| 257 |
$res[] = ''; |
| 258 |
$res[] = [ '', 'Emails', 'SPF only', 'DKIM only', 'Not aligned', 'Quar+Rej' ]; |
| 259 |
foreach ($this->rows as &$row) { |
| 260 |
$total = $row['total']; |
| 261 |
$d_aln = $row['dkim_aligned']; |
| 262 |
$s_aln = $row['spf_aligned']; |
| 263 |
$q_dis = $row['quarantined']; |
| 264 |
$r_dis = $row['rejected']; |
| 265 |
$n_aln = $total - $row['dkim_spf_aligned'] - $d_aln - $s_aln; |
| 266 |
if ($q_dis || $r_dis) { |
| 267 |
$s_dis = Common::num2percent($q_dis + $r_dis, $total, false) . "({$q_dis}+{$r_dis})"; |
| 268 |
} else { |
| 269 |
$s_dis = '0'; |
| 270 |
} |
| 271 |
$res[] = [ |
| 272 |
trim($row['fqdn']), $total, $s_aln, $d_aln, Common::num2percent($n_aln, $total, true), $s_dis |
| 273 |
]; |
| 274 |
} |
| 275 |
unset($row); |
| 276 |
return Common::arrayToCSV($res); |
| 277 |
} |
| 278 |
} |