Debian Codegraph @

classes/Report/OverallReport.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2024-2025 Aleksey Andreev (liuch)
6 *
7 * Available at:
8 * https://github.com/liuch/dmarc-srg
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * =========================
23 *
24 * This file contains OverallReport class
25 *
26 * @category API
27 * @package DmarcSrg
28 * @author Aleksey Andreev (liuch)
29 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
30 */
31
32 namespace Liuch\DmarcSrg\Report;
33
34 use Liuch\DmarcSrg\Common;
35 use Liuch\DmarcSrg\TextTable;
36
37 /**
38 * This class is for generating an overall report on accumulated data
39 */
40 class OverallReport
41 {
42 private $rows = [];
43
44 /**
45 * Adds a report row
46 *
47 * @param array $data Report data array
48 *
49 * @return void
50 */
51 public function appendData(array $data): void
52 {
53 $this->rows[] = $data;
54 }
55
56 /**
57 * Removes data for the specified domain from the report
58 *
59 * @param \Liuch\DmarcSrg\Domains\Domain $domain Domain to remove data for
60 *
61 * @return void
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 * Returns the report data as an array
77 *
78 * @return array
79 */
80 public function toArray(): array
81 {
82 return $this->rows;
83 }
84
85 /**
86 * Returns the report as an array of text strings
87 *
88 * @return array
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 * Returns the report as an array of html strings
124 *
125 * @return array
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 * Returns the report as an array of markdown strings with proper table formatting
193 *
194 * @return array
195 */
196 public function markdown(): array
197 {
198 $res = [ '# Overall by domains' ];
199 $res[] = '';
200
201 // Generate Markdown table
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 * Escapes special Markdown characters that could break table formatting
235 *
236 * @param string $text Text to escape
237 *
238 * @return string
239 */
240 private function escapeMarkdown(string $text): string
241 {
242 // Escape special Markdown characters that could break table formatting
243 $text = str_replace('\\', '\\\\', $text); // Backslash first
244 $text = str_replace('|', '\\|', $text); // Pipe (critical for tables)
245
246 return $text;
247 }
248
249 /**
250 * Returns the report data in CSV format
251 *
252 * @return string
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 }