Debian Codegraph @

public/hosts.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 script returns information about a host by its IP address
25 *
26 * HTTP GET query:
27 * when the header 'Accept' is 'application/json':
28 * It returns the host information defined by the passed parameters:
29 * `host` string Host IP address
30 * `fields` string Comma-separated list of fields to get information for.
31 * Possible value are `main`, `stats`.
32 * otherwise:
33 * It returns an error.
34 * Other HTTP methods:
35 * It returns an error.
36 *
37 * @category Web
38 * @package DmarcSrg
39 * @author Aleksey Andreev (liuch)
40 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
41 */
42
43 namespace Liuch\DmarcSrg;
44
45 use Liuch\DmarcSrg\Hosts\Host;
46 use Liuch\DmarcSrg\Users\User;
47 use Liuch\DmarcSrg\Plugins\PluginManager;
48 use Liuch\DmarcSrg\Exception\SoftException;
49 use Liuch\DmarcSrg\Exception\RuntimeException;
50
51 require realpath(__DIR__ . '/..') . '/init.php';
52
53 if (Core::isJson()) {
54 try {
55 $core = Core::instance();
56
57 if (Core::requestMethod() == 'GET') {
58 $core->auth()->isAllowed(User::LEVEL_USER);
59
60 if (isset($_GET['host']) && isset($_GET['fields'])) {
61 $host = new Host($_GET['host']);
62 $fields = explode(',', trim($_GET['fields']));
63 if (!count($fields) || count(array_diff(
64 $fields,
65 [ 'main.rdns', 'main.rip', 'stats.reports', 'stats.messages', 'stats.last_report' ]
66 ))) {
67 throw new SoftException('Incorrect field list');
68 }
69
70 $res = [];
71 if (count($fields)) {
72 $data = [ 'ip' => $host, 'fields' => $fields ];
73 PluginManager::dispatchEvent('Host', 'hostInformationStart', $data);
74 if (isset($data['fields'])) {
75 $data['result'] = $host->information($data['fields']);
76 PluginManager::dispatchEvent('Host', 'hostInformationFinish', $data);
77 $res['data'] = $data['result'] ?? [];
78 if (isset($data['dictionary'])) {
79 $res['dictionary'] = $data['dictionary'];
80 }
81 }
82 }
83
84 Core::sendJson($res);
85 return;
86 }
87
88 Core::sendJson([ 'error_code' => -1, 'message' => 'Bad request' ]);
89 }
90 } catch (RuntimeException $e) {
91 Core::sendJson(ErrorHandler::exceptionResult($e));
92 }
93 return;
94 }
95
96 Core::sendBad();