Debian Codegraph @

classes/Hosts/Host.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 class Host
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\Hosts;
33
34 use Liuch\DmarcSrg\Core;
35 use Liuch\DmarcSrg\Exception\SoftException;
36 use Liuch\DmarcSrg\Exception\LogicException;
37 use Liuch\DmarcSrg\Exception\RuntimeException;
38
39 /**
40 * This class is designed for storing and manipulating hosts data and utilites.
41 */
42 class Host
43 {
44 private $db = null;
45 private $rip = null;
46 private $rdns = null;
47 private $data = [
48 'ip' => null
49 ];
50
51 /**
52 * The constructor
53 *
54 * @param string $ip IP address
55 * @param \Liuch\DmarcSrg\Database\DatabaseController $db The database controller
56 *
57 * @return void
58 */
59 public function __construct(string $ip, $db = null)
60 {
61 $this->db = $db ?? Core::instance()->database();
62 if (empty($ip)) {
63 throw new LogicException('Incorrect host data');
64 }
65 if (!filter_var(
66 $ip,
67 FILTER_VALIDATE_IP,
68 FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE | FILTER_FLAG_NO_PRIV_RANGE
69 )) {
70 throw new SoftException('Incorrect IP address');
71 }
72 $this->data['ip'] = $ip;
73 }
74
75 /**
76 * Returns brief information
77 *
78 * @param array &$fields List of fields
79 *
80 * @return array
81 */
82 public function information(array &$fields): array
83 {
84 $res = [];
85 $sts = null;
86 $usr = Core::instance()->getCurrentUser();
87 foreach ($fields as $fld) {
88 switch ($fld) {
89 case 'main.rdns':
90 $res[] = [ $fld, $this->rdnsName() ];
91 break;
92 case 'main.rip':
93 $res[] = [ $fld, $this->checkReverseIp() ];
94 break;
95 default:
96 if (!$sts && substr($fld, 0, 6) === 'stats.') {
97 $sts = $this->statistics($usr);
98 }
99 break;
100 }
101 }
102 if ($sts) {
103 foreach ([ 'reports', 'messages', 'last_report' ] as $fld) {
104 $lfld = 'stats.' . $fld;
105 if (in_array($lfld, $fields)) {
106 $res[] = [ $lfld, $sts[$fld] ];
107 }
108 }
109 }
110 return $res;
111 }
112
113 /**
114 * Returns the reverse DNS hostname or an empty string if lookup fails
115 *
116 * @return string
117 */
118 private function rdnsName(): string
119 {
120 if (is_null($this->rdns)) {
121 $rdns = gethostbyaddr($this->data['ip']);
122 if ($rdns === false) {
123 throw new RuntimeException('Failed to get the reverse DNS hostname');
124 }
125 if ($rdns === $this->data['ip']) {
126 $rdns = '';
127 }
128 $this->rdns = $rdns;
129 }
130 return $this->rdns;
131 }
132
133 /**
134 * Checks if the IP address resolved from the reverse DNS hostname matches the source IP address
135 *
136 * @return bool
137 */
138 private function checkReverseIP(): bool
139 {
140 if (!is_null($this->rip)) {
141 return $this->rip;
142 }
143
144 $rname = $this->rdnsName();
145 if (empty($rname)) {
146 return false;
147 }
148
149 if (filter_var($this->data['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
150 $type = DNS_A;
151 } else {
152 $type = DNS_AAAA;
153 }
154 $ip = inet_ntop(inet_pton($this->data['ip']));
155 $ip_res = dns_get_record($rname, $type);
156 if ($ip_res === false) {
157 throw new RuntimeException('Failed to resolve IP address');
158 }
159 $this->rip = false;
160 foreach ($ip_res as &$rec) {
161 $r = ($type === DNS_A) ? $rec['ip'] : $rec['ipv6'];
162 if ($ip === $r) {
163 $this->rip = true;
164 break;
165 }
166 }
167 unset($rec);
168 return $this->rip;
169 }
170
171 /*
172 * Returns an array with statistics of the host
173 *
174 * @param \Liuch\DmarcSrg\Users\User $user User for whom to get statistic
175 *
176 * @return array
177 */
178 private function statistics($user): array
179 {
180 return $this->db->getMapper('host')->statistics($this->data, $user->id());
181 }
182 }