Debian Codegraph @

public/domains.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) 2021-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 the domains information
25 *
26 * HTTP GET query:
27 * when the header 'Accept' is 'application/json':
28 * It returns a list of the domains or data for the domain specified in the parameter domain.
29 * otherwise:
30 * it returns the content of the template.html file
31 * HTTP POST query:
32 * Inserts or updates data for specified domain. The data must be in json format with the following fields:
33 * `fqdn` string FQDN of the domain.
34 * `action` string Must be one of the following values: `add`, `update`, `delete`.
35 * If the value is `delete`, all the fields below will be ignored.
36 * `active` boolean Whether reports for the domain will be accepted or not.
37 * `description` string Description of the domain.
38 * Example:
39 * { "fqnd": "example.com", "action": "update", "active": true, "description": "My description" }
40 * Other HTTP methods:
41 * it returns an error
42 *
43 * All the data is in json format.
44 *
45 * @category Web
46 * @package DmarcSrg
47 * @author Aleksey Andreev (liuch)
48 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
49 */
50
51 namespace Liuch\DmarcSrg;
52
53 use Liuch\DmarcSrg\Users\User;
54 use Liuch\DmarcSrg\Domains\Domain;
55 use Liuch\DmarcSrg\Domains\DomainList;
56 use Liuch\DmarcSrg\Exception\SoftException;
57 use Liuch\DmarcSrg\Exception\RuntimeException;
58
59 require realpath(__DIR__ . '/..') . '/init.php';
60
61 if (Core::isJson()) {
62 try {
63 if (Core::requestMethod() == 'GET') {
64 $core = Core::instance();
65 $core->auth()->isAllowed(User::LEVEL_USER);
66
67 $user = $core->getCurrentUser();
68 if (isset($_GET['domain'])) {
69 $fqdn = trim($_GET['domain']);
70 if (empty($fqdn)) {
71 $core->auth()->isAllowed(User::LEVEL_MANAGER);
72 $res = [];
73 if ($user->id() !== 0 && $core->config('users/domain_verification', 'none') === 'dns') {
74 $res['verification'] = 'dns';
75 $res['verification_data'] = 'dmarcsrg-verification=' . $user->verificationString();
76 }
77 } else {
78 $domain = new Domain($_GET['domain']);
79 $domain->isAssigned($user, true);
80 $res = $domain->toArray();
81 }
82 Core::sendJson($res);
83 return;
84 }
85
86 $res = (new DomainList($user))->getList();
87 $list = array_map(function ($domain) {
88 return $domain->toArray();
89 }, $res['domains']);
90
91 Core::sendJson([
92 'domains' => $list,
93 'more' => $res['more']
94 ]);
95 return;
96 } elseif (Core::requestMethod() == 'POST') {
97 $core = Core::instance();
98 Core::instance()->auth()->isAllowed(User::LEVEL_MANAGER);
99
100 $data = Core::getJsonData();
101 if ($data) {
102 $domain = new Domain([
103 'fqdn' => $data['fqdn'] ?? null,
104 'active' => $data['active'] ?? null,
105 'description' => $data['description'] ?? null
106 ]);
107 $user = Core::instance()->getCurrentUser();
108 $action = $data['action'] ?? '';
109 switch ($action) {
110 case 'add':
111 if ($domain->isAssigned($user)) {
112 throw new SoftException('The domain already exists');
113 }
114 if ($user->id() !== 0) {
115 $method = $core->config('users/domain_verification', 'none');
116 if ($method !== 'none') {
117 $domain->verifyOwnership($user->verificationString(), $method);
118 }
119 }
120 if (!$domain->exists()) {
121 $domain->save();
122 }
123 $domain->assignUser($user);
124 break;
125 case 'update':
126 $domain->isAssigned($user, true);
127 $domain->save();
128 break;
129 case 'delete':
130 if ($user->id() === 0) {
131 $domain->delete($data['force'] ?? false);
132 } else {
133 $domain->unassignUser($user);
134 }
135 unset($domain);
136 break;
137 default:
138 throw new SoftException('Unknown action. Valid values are "add", "update", "delete".');
139 }
140
141 $res = [
142 'error_code' => 0,
143 'message' => 'Successfully'
144 ];
145 if (isset($domain)) {
146 $res['domain'] = $domain->toArray();
147 }
148 Core::sendJson($res);
149 return;
150 }
151 }
152 } catch (RuntimeException $e) {
153 Core::sendJson(ErrorHandler::exceptionResult($e));
154 return;
155 }
156 } elseif (Core::requestMethod() == 'GET') {
157 Core::instance()->sendHtml();
158 return;
159 }
160
161 Core::sendBad();