Debian Codegraph @

classes/Report/SummaryReport.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) 2022-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 SummaryReport 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\DateTime;
36 use Liuch\DmarcSrg\TextTable;
37 use Liuch\DmarcSrg\Statistics;
38 use Liuch\DmarcSrg\Exception\SoftException;
39 use Liuch\DmarcSrg\Exception\LogicException;
40
41 /**
42 * This class is for generating summary data for the specified period and domain
43 */
44 class SummaryReport
45 {
46 private const LAST_WEEK = -1;
47 private const LAST_MONTH = -2;
48 private const DATE_RANGE = -3;
49
50 private $period = 0;
51 private $offset = 0;
52 private $range = null;
53 private $domain = null;
54 private $overall = [];
55 private $stat = [];
56 private $subject = '';
57
58 /**
59 * Constructor
60 *
61 * @param string $period The period for which the report is created
62 * Must me one of the following values: `lastweek`, `lastmonth`, and `lastndays:N`
63 * where N is the number of days the report is created for
64 * @param int $offset Range offset
65 */
66 public function __construct(string $period, int $offset = 0)
67 {
68 switch ($period) {
69 case 'lastweek':
70 $period = self::LAST_WEEK;
71 $subject = ' weekly digest';
72 break;
73 case 'lastmonth':
74 $period = self::LAST_MONTH;
75 $subject = ' monthly digest';
76 break;
77 default:
78 $ndays = 0;
79 $av = explode(':', $period);
80 if (count($av) === 2) {
81 switch ($av[0]) {
82 case 'lastndays':
83 $ndays = intval($av[1]);
84 if ($ndays <= 0) {
85 throw new SoftException('The parameter "days" has an incorrect value');
86 }
87 $period = $ndays;
88 $subject = sprintf(' %d day%s digest', $ndays, ($ndays > 1 ? 's' : ''));
89 break;
90 case 'range':
91 $range = explode('-', $av[1]);
92 if (count($range) !== 2) {
93 throw new SoftException('The parameter "range" must contain two dates');
94 }
95 $range = array_map(function ($r) {
96 $cnt = 0;
97 $sd = preg_replace('/^(\d{4})(\d{2})(\d{2})$/', '\1-\2-\3', $r, -1, $cnt);
98 if (!$cnt) {
99 throw new SoftException('The parameter "range" has an incorrect value');
100 }
101 return new DateTime($sd);
102 }, $range);
103 if ($range[0] > $range[1]) {
104 throw new SoftException('Incorrect date range');
105 }
106 $period = self::DATE_RANGE;
107 $subject = ' report ' . Common::rangeToString($range);
108 $range[1]->modify('next day');
109 $this->range = $range;
110 break;
111 }
112 }
113 break;
114 }
115 if (empty($subject)) {
116 throw new SoftException('The parameter "period" has an incorrect value');
117 }
118 if ($offset < 0) {
119 throw new SoftException('The parameter "offset" has an incorrect value');
120 }
121 $this->period = $period;
122 $this->offset = $offset;
123 $this->subject = "DMARC{$subject}";
124 }
125
126 /**
127 * Binds a domain to the report
128 *
129 * @param \Liuch\DmarcSrg\Domains\Domain $domain The domain for which the report is created
130 *
131 * @return self
132 */
133 public function setDomain($domain)
134 {
135 $this->domain = $domain;
136 $this->stat = [];
137 return $this;
138 }
139
140 /**
141 * Checks if the report is empty
142 *
143 * @return bool
144 */
145 public function isEmpty(): bool
146 {
147 return ($this->getData('summary')['emails']['total'] === 0);
148 }
149
150 /**
151 * Binds a report section
152 *
153 * @param mixed $section Report section
154 *
155 * @return self
156 */
157 public function bindSection($section)
158 {
159 if ($section instanceof OverallReport) {
160 $this->overall[] = $section;
161 }
162 return $this;
163 }
164
165 /**
166 * Returns the report data as an array
167 *
168 * @return array
169 */
170 public function toArray(): array
171 {
172 $range = $this->getData('range');
173 return [
174 'date_range' => [ 'begin' => $range[0], 'end' => $range[1] ],
175 'summary' => $this->getData('summary'),
176 'sources' => $this->getData('ips'),
177 'organizations' => $this->getData('organizations')
178 ];
179 }
180
181 /**
182 * Returns the subject string. It is used in email messages.
183 *
184 * @return string
185 */
186 public function subject(): string
187 {
188 return $this->subject;
189 }
190
191 /**
192 * Returns the report as an array of text strings
193 *
194 * @return array
195 */
196 public function text(): array
197 {
198 $rdata = $this->reportData();
199
200 $res = [ '# Domain: ' . $this->domain->fqdn() ];
201 $res[] = ' Range: ' . $rdata['range'];
202 $res[] = '';
203
204 $res[] = '## Summary';
205 $total = $rdata['summary']['total'];
206 $res[] = sprintf(' Total: %d', $total);
207 $res[] = sprintf(' Fully aligned: %s', Common::num2percent($rdata['summary']['f_aligned'], $total, true));
208 $res[] = sprintf(' Partial aligned: %s', Common::num2percent($rdata['summary']['p_aligned'], $total, true));
209 $res[] = sprintf(' Not aligned: %s', Common::num2percent($rdata['summary']['n_aligned'], $total, true));
210 $res[] = sprintf(' Quarantined: %s', Common::num2percent($rdata['summary']['quarantined'], $total, true));
211 $res[] = sprintf(' Rejected: %s', Common::num2percent($rdata['summary']['rejected'], $total, true));
212 $res[] = '';
213
214 if (count($rdata['sources']) > 0) {
215 $res[] = '## Sources';
216 $table = new TextTable([ '', 'Total', 'SPF only', 'DKIM only', 'Not aligned', 'Quar+Rej' ]);
217 $table->setMinColumnsWidth([ 15, 5, 8, 9, 11, 8 ])->setBorders('', '', '')
218 ->setColumnAlignment(4, 'right')->setColumnAlignment(5, 'right');
219 foreach ($rdata['sources'] as &$it) {
220 $total = $it['emails'];
221 $f_aln = $it['dkim_spf_aligned'];
222 $d_aln = $it['dkim_aligned'];
223 $s_aln = $it['spf_aligned'];
224 $n_aln = $total - $f_aln - $d_aln - $s_aln;
225 $q_dis = $it['quarantined'];
226 $r_dis = $it['rejected'];
227 if ($q_dis || $r_dis) {
228 $s_dis = Common::num2percent($q_dis + $r_dis, $total, false) . "({$q_dis}+{$r_dis})";
229 } else {
230 $s_dis = '0';
231 }
232 $table->appendRow([
233 $it['ip'], $total, $s_aln, $d_aln, Common::num2percent($n_aln, $total, true), $s_dis
234 ]);
235 }
236 unset($it);
237 $res = array_merge($res, $table->toArray());
238 $res[] = '';
239 }
240
241 if (count($rdata['organizations']) > 0) {
242 $res[] = '## Reporting organizations';
243 $table = new TextTable([ '', 'Reports', 'Emails', 'SPF only', 'DKIM only', 'Not aligned', 'Quar+Rej' ]);
244 $table->setMinColumnsWidth([ 15, 7, 6, 8, 9, 11, 8 ])->setBorders('', '', '')
245 ->setColumnAlignment(5, 'right')->setColumnAlignment(6, 'right');
246 foreach ($rdata['organizations'] as &$it) {
247 $total = $it['emails'];
248 $f_aln = $it['dkim_spf_aligned'];
249 $d_aln = $it['dkim_aligned'];
250 $s_aln = $it['spf_aligned'];
251 $n_aln = $total - $f_aln - $d_aln - $s_aln;
252 $q_dis = $it['quarantined'];
253 $r_dis = $it['rejected'];
254 if ($q_dis || $r_dis) {
255 $s_dis = Common::num2percent($q_dis + $r_dis, $total, false) . "({$q_dis}+{$r_dis})";
256 } else {
257 $s_dis = '0';
258 }
259 $table->appendRow([
260 $it['name'], $it['reports'], $total, $s_aln, $d_aln,
261 Common::num2percent($n_aln, $total, true), $s_dis
262 ]);
263 }
264 unset($it);
265 $res = array_merge($res, $table->toArray());
266 $res[] = '';
267 }
268
269 return $res;
270 }
271
272 /**
273 * Escapes special characters for Markdown output
274 *
275 * @param string $text The text to escape
276 *
277 * @return string
278 */
279 private function escapeMarkdown(string $text): string
280 {
281 // Escape special Markdown characters that could break table formatting
282 $text = str_replace('\\', '\\\\', $text); // Backslash first
283 $text = str_replace('|', '\\|', $text); // Pipe (critical for tables)
284
285 // Only escape formatting characters if they appear in contexts that could be problematic
286 // For table cells, we mainly need to escape pipes
287
288 return $text;
289 }
290
291 /**
292 * Returns the report as an array of markdown strings with proper table formatting
293 *
294 * @return array
295 */
296 public function markdown(): array
297 {
298 $rdata = $this->reportData();
299
300 $res = [ '# Domain: ' . $this->domain->fqdn() ];
301 $res[] = '';
302 $res[] = '**Range:** ' . $rdata['range'];
303 $res[] = '';
304
305 $res[] = '## Summary';
306 $res[] = '';
307 $total = $rdata['summary']['total'];
308 $res[] = sprintf('- **Total:** %d', $total);
309 $res[] = sprintf('- **Fully aligned:** %s', Common::num2percent($rdata['summary']['f_aligned'], $total, true));
310 $res[] = sprintf('- **Partial aligned:** %s', Common::num2percent($rdata['summary']['p_aligned'], $total, true));
311 $res[] = sprintf('- **Not aligned:** %s', Common::num2percent($rdata['summary']['n_aligned'], $total, true));
312 $res[] = sprintf('- **Quarantined:** %s', Common::num2percent($rdata['summary']['quarantined'], $total, true));
313 $res[] = sprintf('- **Rejected:** %s', Common::num2percent($rdata['summary']['rejected'], $total, true));
314 $res[] = '';
315
316 if (count($rdata['sources']) > 0) {
317 $res[] = '## Sources';
318 $res[] = '';
319
320 // Generate Markdown table
321 $res[] = '| IP Address | Total | SPF only | DKIM only | Not aligned | Quar+Rej |';
322 $res[] = '|------------|-------|----------|-----------|-------------|----------|';
323
324 foreach ($rdata['sources'] as &$it) {
325 $total = $it['emails'];
326 $f_aln = $it['dkim_spf_aligned'];
327 $d_aln = $it['dkim_aligned'];
328 $s_aln = $it['spf_aligned'];
329 $n_aln = $total - $f_aln - $d_aln - $s_aln;
330 $q_dis = $it['quarantined'];
331 $r_dis = $it['rejected'];
332 if ($q_dis || $r_dis) {
333 $s_dis = Common::num2percent($q_dis + $r_dis, $total, false) . "({$q_dis}+{$r_dis})";
334 } else {
335 $s_dis = '0';
336 }
337
338 $res[] = sprintf('| %s | %s | %s | %s | %s | %s |',
339 $this->escapeMarkdown($it['ip']),
340 $total,
341 $s_aln,
342 $d_aln,
343 $this->escapeMarkdown(Common::num2percent($n_aln, $total, true)),
344 $this->escapeMarkdown($s_dis)
345 );
346 }
347 unset($it);
348 $res[] = '';
349 }
350
351 if (count($rdata['organizations']) > 0) {
352 $res[] = '## Reporting organizations';
353 $res[] = '';
354
355 // Generate Markdown table
356 $res[] = '| Organization | Reports | Emails | SPF only | DKIM only | Not aligned | Quar+Rej |';
357 $res[] = '|--------------|---------|--------|----------|-----------|-------------|----------|';
358
359 foreach ($rdata['organizations'] as &$it) {
360 $total = $it['emails'];
361 $f_aln = $it['dkim_spf_aligned'];
362 $d_aln = $it['dkim_aligned'];
363 $s_aln = $it['spf_aligned'];
364 $n_aln = $total - $f_aln - $d_aln - $s_aln;
365 $q_dis = $it['quarantined'];
366 $r_dis = $it['rejected'];
367 if ($q_dis || $r_dis) {
368 $s_dis = Common::num2percent($q_dis + $r_dis, $total, false) . "({$q_dis}+{$r_dis})";
369 } else {
370 $s_dis = '0';
371 }
372
373 $res[] = sprintf('| %s | %s | %s | %s | %s | %s | %s |',
374 $this->escapeMarkdown(trim($it['name'])),
375 $it['reports'],
376 $total,
377 $s_aln,
378 $d_aln,
379 $this->escapeMarkdown(Common::num2percent($n_aln, $total, true)),
380 $this->escapeMarkdown($s_dis)
381 );
382 }
383 unset($it);
384 $res[] = '';
385 }
386
387 return $res;
388 }
389
390 /**
391 * Returns the report as an array of html strings
392 *
393 * @return array
394 */
395 public function html(): array
396 {
397 $h2a = 'style="margin:15px 0 5px;"';
398 $t2a = 'style="border-collapse:collapse;border-spacing:0;"';
399 $c1a = 'style="font-style:italic;"';
400 $d1s = 'padding-left:1em;';
401 $d2s = 'min-width:4em;';
402 $d3s = 'border:1px solid #888;';
403 $d4s = 'text-align:right;';
404 $d5s = 'padding:.3em;';
405 $rs2 = 'rowspan="2"';
406 $cs2 = 'colspan="2"';
407
408 $get_color = function (string $name, int $num) {
409 $cn = '';
410 if ($num > 0) {
411 switch ($name) {
412 case 'red':
413 $cn = 'f00';
414 break;
415 case 'green':
416 $cn = '080';
417 break;
418 }
419 }
420 return empty($cn) ? '' : "color:#{$cn};";
421 };
422
423 $rdata = $this->reportData();
424 $res = [];
425 $res[] = "<h2 {$h2a}>Domain: " . htmlspecialchars($this->domain->fqdn()) . '</h2>';
426 $res[] = '<p style="margin:0;">Range: ' . htmlspecialchars($rdata['range']) . '</p>';
427
428 $res[] = "<h3 {$h2a}>Summary</h3>";
429 $res[] = '<table>';
430 $total = $rdata['summary']['total'];
431 $res[] = " <tr><td>Total: </td><td style=\"{$d1s}\">{$total}</td></tr>";
432 foreach ([
433 [ 'Fully aligned', $rdata['summary']['f_aligned'], 'green' ],
434 [ 'Partial aligned', $rdata['summary']['p_aligned'], '' ],
435 [ 'Not aligned', $rdata['summary']['n_aligned'], 'red' ],
436 [ 'Quarantined', $rdata['summary']['quarantined'], 'red' ],
437 [ 'Rejected', $rdata['summary']['rejected'], 'red' ]
438 ] as &$rd) {
439 $color = $get_color($rd[2], $rd[1]);
440 $s_data = Common::num2percent($rd[1], $total, true);
441 $res[] = " <tr><td>{$rd[0]}: </td><td style=\"{$d1s}{$color}\">{$s_data}</td></tr>";
442 }
443 unset($rd);
444 $res[] = '</table>';
445
446 $s_cnt = count($rdata['sources']);
447 if ($s_cnt > 0) {
448 $res[] = "<h3 {$h2a}>Sources</h3>";
449 $res[] = "<table {$t2a}>";
450 $res[] = " <caption {$c1a}>Total records: {$s_cnt}</caption>";
451 $res[] = ' <thead>';
452 $style = "style=\"{$d3s}{$d5s}\"";
453 $res[] = " <tr><th {$rs2} {$style}>IP address</th><th {$rs2} {$style}>Email volume</th>" .
454 "<th {$cs2} {$style}>Partial aligned</th><th {$rs2} {$style}>Not aligned</th>" .
455 "<th {$cs2} {$style}>Disposition</th></tr>";
456 $style = "style=\"{$d2s}{$d3s}{$d5s}\"";
457 $res[] = " <tr><th {$style}>SPF only</th><th {$style}>DKIM only</th>" .
458 "<th {$style}>quar+rej</th><th {$style}>fail rate</th></tr>";
459 $res[] = ' </thead>';
460 $res[] = ' <tbody>';
461 $style = "style=\"{$d3s}{$d5s}";
462 foreach ($rdata['sources'] as &$row) {
463 $ip = htmlspecialchars($row['ip']);
464 $total = $row['emails'];
465 $f_aln = $row['dkim_spf_aligned'];
466 $d_aln = $row['dkim_aligned'];
467 $s_aln = $row['spf_aligned'];
468 $n_aln = $total - $f_aln - $d_aln - $s_aln;
469 $q_dis = $row['quarantined'];
470 $r_dis = $row['rejected'];
471 $s_dis = ($q_dis || $r_dis) ? "{$q_dis}+{$r_dis}" : '0';
472 $res[] = " <tr><td {$style}\">{$ip}</td><td {$style}{$d4s}\">{$total}</td>" .
473 "<td {$style}{$d4s}\">{$s_aln}</td><td {$style}{$d4s}\">{$d_aln}</td>" .
474 "<td {$style}{$d4s}{$get_color('red', $n_aln)}\">{$n_aln}</td>" .
475 "<td {$style}{$d4s}{$get_color('red', $q_dis + $r_dis)}\">{$s_dis}</td>" .
476 "<td {$style}{$d4s}\">" . Common::num2percent($q_dis + $r_dis, $total, false) .
477 '</td></tr>';
478 }
479 unset($row);
480 $res[] = ' </tbody>';
481 $res[] = '</table>';
482 }
483
484 $o_cnt = count($rdata['organizations']);
485 if ($o_cnt) {
486 $res[] = "<h3 {$h2a}>Organizations</h3>";
487 $res[] = "<table {$t2a}>";
488 $res[] = " <caption {$c1a}>Total records: {$o_cnt}</caption>";
489 $res[] = ' <thead>';
490 $style = "style=\"{$d3s}{$d5s}\"";
491 $res[] = " <tr><th {$rs2} {$style}>Name</th><th {$cs2} {$style}>Volume</th>" .
492 "<th {$cs2} {$style}>Partial aligned</th><th {$rs2} {$style}>Not aligned</th>" .
493 "<th {$cs2} {$style}>Disposition</th></tr>";
494 $res[] = " <tr><th {$style}>reports</th><th {$style}>emails</th>" .
495 "<th {$style}>SPF only</th><th {$style}>DKIM only</th>" .
496 "<th {$style}>quar+rej</th><th {$style}>fail rate</th></tr>";
497 $res[] = ' </thead>';
498 $res[] = ' <tbody>';
499 $style = "style=\"{$d3s}{$d5s}";
500 foreach ($rdata['organizations'] as &$row) {
501 $name = htmlspecialchars(trim($row['name']));
502 $total = $row['emails'];
503 $f_aln = $row['dkim_spf_aligned'];
504 $d_aln = $row['dkim_aligned'];
505 $s_aln = $row['spf_aligned'];
506 $n_aln = $total - $f_aln - $d_aln - $s_aln;
507 $q_dis = $row['quarantined'];
508 $r_dis = $row['rejected'];
509 $s_dis = ($q_dis || $r_dis) ? "{$q_dis}+{$r_dis}" : '0';
510 $res[] = " <tr><td {$style}\">{$name}</td>" .
511 "<td {$style}{$d4s}\">{$row['reports']}</td><td {$style}{$d4s}\">{$total}</td>" .
512 "<td {$style}{$d4s}\">{$s_aln}</td><td {$style}{$d4s}\">{$d_aln}</td>" .
513 "<td {$style}{$d4s}{$get_color('red', $n_aln)}\">{$n_aln}</td>" .
514 "<td {$style}{$d4s}{$get_color('red', $q_dis + $r_dis)}\">{$s_dis}</td>" .
515 "<td {$style}{$d4s}\">" . Common::num2percent($q_dis + $r_dis, $total, false) .
516 '</td></tr>';
517 }
518 unset($row);
519 $res[] = ' </tbody>';
520 $res[] = '</table>';
521 }
522
523 return $res;
524 }
525
526 /**
527 * Returns the report data in CSV format
528 *
529 * @return string
530 */
531 public function csv(): string
532 {
533 $rdata = $this->reportData();
534
535 $res = [];
536 $res[] = 'Domain: ' . $this->domain->fqdn();
537
538 $res[] = 'Range: ' . $rdata['range'];
539 $res[] = '';
540
541 $res[] = 'Summary';
542 $total = $rdata['summary']['total'];
543 $res[] = sprintf('Total: %d', $total);
544 $res[] = sprintf('Fully aligned: %s', Common::num2percent($rdata['summary']['f_aligned'], $total, true));
545 $res[] = sprintf('Partial aligned: %s', Common::num2percent($rdata['summary']['p_aligned'], $total, true));
546 $res[] = sprintf('Not aligned: %s', Common::num2percent($rdata['summary']['n_aligned'], $total, true));
547 $res[] = sprintf('Quarantined: %s', Common::num2percent($rdata['summary']['quarantined'], $total, true));
548 $res[] = sprintf('Rejected: %s', Common::num2percent($rdata['summary']['rejected'], $total, true));
549 $res[] = '';
550
551 if (count($rdata['sources']) > 0) {
552 $res[] = 'Sources';
553 $res[] = [ '', 'Total', 'SPF only', 'DKIM only', 'Not aligned', 'Quar+Rej' ];
554 foreach ($rdata['sources'] as &$it) {
555 $total = $it['emails'];
556 $f_aln = $it['dkim_spf_aligned'];
557 $d_aln = $it['dkim_aligned'];
558 $s_aln = $it['spf_aligned'];
559 $n_aln = $total - $f_aln - $d_aln - $s_aln;
560 $q_dis = $it['quarantined'];
561 $r_dis = $it['rejected'];
562 if ($q_dis || $r_dis) {
563 $s_dis = Common::num2percent($q_dis + $r_dis, $total, false) . "({$q_dis}+{$r_dis})";
564 } else {
565 $s_dis = '0';
566 }
567 $res[] = [ $it['ip'], $total, $s_aln, $d_aln, Common::num2percent($n_aln, $total, true), $s_dis ];
568 }
569 unset($it);
570 $res[] = '';
571 }
572
573 if (count($rdata['organizations']) > 0) {
574 $res[] = 'Organizations';
575 $res[] = [ '', 'Reports', 'Emails', 'SPF only', 'DKIM only', 'Not aligned', 'Quar+Rej' ];
576 foreach ($rdata['organizations'] as &$it) {
577 $total = $it['emails'];
578 $f_aln = $it['dkim_spf_aligned'];
579 $d_aln = $it['dkim_aligned'];
580 $s_aln = $it['spf_aligned'];
581 $n_aln = $total - $f_aln - $d_aln - $s_aln;
582 $q_dis = $it['quarantined'];
583 $r_dis = $it['rejected'];
584 if ($q_dis || $r_dis) {
585 $s_dis = Common::num2percent($q_dis + $r_dis, $total, false) . "({$q_dis}+{$r_dis})";
586 } else {
587 $s_dis = '0';
588 }
589 $res[] = [
590 trim($it['name']), $it['reports'], $total, $s_aln, $d_aln,
591 Common::num2percent($n_aln, $total, true), $s_dis
592 ];
593 }
594 unset($it);
595 $res[] = '';
596 }
597 return Common::arrayToCSV($res);
598 }
599
600 /**
601 * Caches and returns statistics for the specified section
602 *
603 * @param string $section Section name
604 *
605 * @return array
606 */
607 private function getData(string $section): array
608 {
609 if (!$this->domain) {
610 throw new LogicException('No one domain was specified');
611 }
612
613 $instance = $this->stat['instance'] ?? null;
614 if (!$instance) {
615 switch ($this->period) {
616 case self::LAST_WEEK:
617 $instance = Statistics::lastWeek($this->domain, $this->offset);
618 break;
619 case self::LAST_MONTH:
620 $instance = Statistics::lastMonth($this->domain, $this->offset);
621 break;
622 case self::DATE_RANGE:
623 $instance = Statistics::fromTo($this->domain, $this->range[0], $this->range[1]);
624 break;
625 default:
626 $instance = Statistics::lastNDays($this->domain, $this->period, $this->offset);
627 break;
628 }
629 $this->stat['instance'] = $instance;
630 foreach ($this->overall as $o) {
631 if ($o instanceof OverallReport) {
632 $o->appendData(
633 array_merge([ 'fqdn' => $this->domain->fqdn() ], $this->getData('summary')['emails'])
634 );
635 }
636 }
637 }
638 switch ($section) {
639 case 'range':
640 case 'summary':
641 case 'ips':
642 case 'organizations':
643 $res = $this->stat[$section] ?? null;
644 break;
645 default:
646 throw new LogicException('Unknown section name');
647 }
648 if (!$res) {
649 $res = $instance->$section();
650 $this->stat[$section] = &$res;
651 }
652 return $res;
653 }
654
655 /**
656 * Returns prepared data for the report
657 *
658 * @return array
659 */
660 private function reportData(): array
661 {
662 $rdata = [];
663 $rdata['range'] = Common::rangeToString($this->getData('range'));
664
665 $summ = $this->getData('summary');
666 $total = $summ['emails']['total'];
667 $f_aligned = $summ['emails']['dkim_spf_aligned'];
668 $p_aligned = $summ['emails']['dkim_aligned'] + $summ['emails']['spf_aligned'];
669 $n_aligned = $total - $f_aligned - $p_aligned;
670 $rdata['summary'] = [
671 'total' => $total,
672 'organizations' => $summ['organizations']
673 ];
674
675 $rdata['summary']['f_aligned'] = $f_aligned;
676 $rdata['summary']['p_aligned'] = $p_aligned;
677 $rdata['summary']['n_aligned'] = $n_aligned;
678 $rdata['summary']['rejected'] = $summ['emails']['rejected'];
679 $rdata['summary']['quarantined'] = $summ['emails']['quarantined'];
680
681 $rdata['sources'] = $this->getData('ips');
682
683 $rdata['organizations'] = $this->getData('organizations');
684
685 return $rdata;
686 }
687 }