Debian Codegraph @

public/settings.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 is used to manage the settings via the web interface
25 *
26 * HTTP GET query:
27 * When the header 'Accept' is 'application/json':
28 * It returns the setting specified in the GET parameter `name`.
29 * Otherwise it returns a list of the settings. The request may have the following GET parameters:
30 * `direction` string The sort direction. Can be one of the following values: `ascent`, `descent'.
31 * The default value is `ascent`. The list will be sorted by Setting name.
32 * The data will be returned in json format.
33 * Otherwise:
34 * It returns the content of the template.html file.
35 *
36 * HTTP POST query:
37 * Updates data for the specified setting. Data must be in json format with the following fields:
38 * `name` string Name of the setting.
39 * `action` string Must be `update`.
40 * `value` string|int Value to update.
41 * Example:
42 * { "name": "web.report-view.sort-records-by", "value": "ip", "action": "update" }
43 *
44 * Other HTTP methods:
45 * It returns an error.
46 *
47 * @category Web
48 * @package DmarcSrg
49 * @author Aleksey Andreev (liuch)
50 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
51 */
52
53 namespace Liuch\DmarcSrg;
54
55 use Liuch\DmarcSrg\Users\User;
56 use Liuch\DmarcSrg\Settings\SettingsList;
57 use Liuch\DmarcSrg\Exception\SoftException;
58 use Liuch\DmarcSrg\Exception\RuntimeException;
59
60 require realpath(__DIR__ . '/..') . '/init.php';
61
62 if (Core::isJson()) {
63 try {
64 Core::instance()->auth()->isAllowed(User::LEVEL_USER);
65
66 if (Core::requestMethod() == 'GET') {
67 if (isset($_GET['name'])) {
68 Core::sendJson(SettingsList::getSettingByName($_GET['name'])->toArray());
69 return;
70 }
71
72 $dir = $_GET['direction'] ?? 'ascent';
73 $ndir = $dir === 'ascent' ? SettingsList::ORDER_ASCENT : SettingsList::ORDER_DESCENT;
74 $res = (new SettingsList())->setOrder($ndir)->getList();
75 $list = array_map(function ($setting) {
76 return $setting->toArray();
77 }, $res['list']);
78
79 Core::sendJson([
80 'settings' => $list,
81 'more' => $res['more']
82 ]);
83 return;
84 }
85
86 if (Core::requestMethod() == 'POST') {
87 $data = Core::getJsonData();
88 if ($data) {
89 $sett = SettingsList::getSettingByName($data['name'] ?? '');
90 $action = $data['action'] ?? '';
91 switch ($action) {
92 case 'update':
93 $sett->setValue($data['value']);
94 $sett->save();
95 Core::sendJson([
96 'error_code' => 0,
97 'message' => 'Successfully updated'
98 ]);
99 break;
100 default:
101 throw new SoftException('Unknown action. The only valid value is "update".');
102 }
103 return;
104 }
105 }
106 } catch (RuntimeException $e) {
107 Core::sendJson(ErrorHandler::exceptionResult($e));
108 return;
109 }
110 } elseif (Core::requestMethod() == 'GET') {
111 Core::instance()->sendHtml();
112 return;
113 }
114
115 Core::sendBad();