Debian Codegraph @

classes/Database/Common/CommonHostMapper.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) 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 the CommonHostMapper
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\Database\Common;
33
34 use Liuch\DmarcSrg\DateTime;
35 use Liuch\DmarcSrg\Database\HostMapperInterface;
36 use Liuch\DmarcSrg\Exception\DatabaseFatalException;
37
38 /**
39 * Universal implementation of HostMapper class
40 */
41 class CommonHostMapper implements HostMapperInterface
42 {
43 /** @var \Liuch\DmarcSrg\Database\DatabaseConnector */
44 private $connector = null;
45
46 /**
47 * The constructor
48 *
49 * @param \Liuch\DmarcSrg\Database\DatabaseConnector $connector DatabaseConnector instance of the current database
50 */
51 public function __construct(object $connector)
52 {
53 $this->connector = $connector;
54 }
55
56 /**
57 * Return an array with statistics
58 *
59 * @param array $data Array with filter parameters
60 * @param int $user_id User ID
61 *
62 * @return array
63 */
64 public function statistics(array &$data, int $user_id): array
65 {
66 $res = [];
67 if (!$user_id) {
68 $user_domains1 = '';
69 $user_domains2 = '';
70 } else {
71 $user_domains1 = ' INNER JOIN ' . $this->connector->tablePrefix('userdomains')
72 . ' AS ud ON rp.domain_id = ud.domain_id';
73 $user_domains2 = ' AND user_id = ' . $user_id;
74 }
75 $db = $this->connector->dbh();
76 $db->beginTransaction();
77 try {
78 $st = $db->prepare(
79 'SELECT COUNT(*), SUM(rc) FROM (SELECT COUNT(report_id), SUM(rcount) AS rc FROM '
80 . $this->connector->tablePrefix('rptrecords') . ' AS rr INNER JOIN '
81 . $this->connector->tablePrefix('reports') . ' AS rp ON rr.report_id = rp.id'
82 . $user_domains1 . ' WHERE rr.ip = ?' . $user_domains2 . ' GROUP BY report_id) as t'
83 );
84 $st->bindValue(1, inet_pton($data['ip']), \PDO::PARAM_STR);
85 $st->execute();
86 $row = $st->fetch(\PDO::FETCH_NUM);
87 $res['reports'] = intval($row[0]);
88 $res['messages'] = intval($row[1]);
89 $st->closeCursor();
90 $st = $db->prepare(
91 'SELECT rp.id, begin_time FROM ' . $this->connector->tablePrefix('rptrecords')
92 . ' AS rr INNER JOIN ' . $this->connector->tablePrefix('reports')
93 . ' AS rp ON rr.report_id = rp.id' . $user_domains1
94 . ' WHERE rr.ip = ?' . $user_domains2 . ' GROUP BY report_id ORDER BY begin_time DESC LIMIT 2'
95 );
96 $st->bindValue(1, inet_pton($data['ip']), \PDO::PARAM_STR);
97 $st->execute();
98 $last_report = [];
99 while ($row = $st->fetch(\PDO::FETCH_NUM)) {
100 $last_report[] = new DateTime($row[1]);
101 }
102 $res['last_report'] = $last_report;
103 $st->closeCursor();
104 $db->commit();
105 } catch (\PDOException $e) {
106 $db->rollBack();
107 throw new DatabaseFatalException('Failed to get host data', -1, $e);
108 }
109 return $res;
110 }
111 }