Debian Codegraph @

classes/Settings/SettingsList.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 file contains the class SettingsList
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\Settings;
33
34 use Liuch\DmarcSrg\Core;
35 use Liuch\DmarcSrg\Exception\SoftException;
36 use Liuch\DmarcSrg\Exception\RuntimeException;
37
38 /**
39 * This class is designed to work with the list of the settings
40 */
41 class SettingsList
42 {
43 public const ORDER_ASCENT = 0;
44 public const ORDER_DESCENT = 1;
45
46 private $core = null;
47 private $order = self::ORDER_ASCENT;
48
49 /**
50 * The constructor
51 *
52 * @param Core|null $core Instance of the Core class
53 */
54 public function __construct($core = null)
55 {
56 $this->core = $core ?? Core::instance();
57 }
58
59 /**
60 * Returns a list of the settings
61 *
62 * It returns a list of the settings that are marked public.
63 * The value is taken from the database, if any, or the default.
64 *
65 * @return array Array with instances of Setting class
66 */
67 public function getList(): array
68 {
69 $list = [];
70 $user = $this->core->getCurrentUser();
71 $db_map = $this->core->database()->getMapper('setting')->list($user->id());
72 foreach (static::$schema as $name => &$sch_data) {
73 if ($sch_data['public'] ?? false) {
74 $value = $db_map[$name] ?? $sch_data['default'];
75 switch ($sch_data['type']) {
76 case 'select':
77 $list[] = new SettingStringSelect([
78 'name' => $name,
79 'user' => $user,
80 'value' => $value
81 ], true, $this->core);
82 break;
83 case 'integer':
84 $list[] = new SettingInteger([
85 'name' => $name,
86 'user' => $user,
87 'value' => intval($value)
88 ], true, $this->core);
89 break;
90 case 'string':
91 $list[] = new SettingString([
92 'name' => $name,
93 'user' => $user,
94 'value' => $value
95 ], true, $this->core);
96 break;
97 }
98 }
99 }
100 unset($sch_data);
101
102 $dir = $this->order == self::ORDER_ASCENT ? 1 : -1;
103 usort($list, static function ($a, $b) use ($dir) {
104 return ($a->name() <=> $b->name()) * $dir;
105 });
106
107 return [
108 'list' => $list,
109 'more' => false
110 ];
111 }
112
113 /**
114 * Sets the sorting direction for the list
115 *
116 * @param int $direction The sorting direction. ORDER_ASCENT or ORDER_DESCENT must be used here.
117 *
118 * @return SettingsList $this
119 */
120 public function setOrder(int $direction)
121 {
122 if ($direction !== self::ORDER_DESCENT) {
123 $direction = self::ORDER_ASCENT;
124 }
125 $this->order = $direction;
126 return $this;
127 }
128
129 /**
130 * Throws an exception if there is no setting with name $name
131 *
132 * @param string $name Setting name to check
133 *
134 * @return void
135 */
136 public static function checkName($name): void
137 {
138 if (!isset(self::$schema[$name])) {
139 throw new SoftException('Unknown setting name: ' . $name);
140 }
141 }
142
143 /**
144 * Returns an instance of the Setting class by its name
145 *
146 * It returns an instance of the Setting class but only if it is marked public.
147 *
148 * @param string $name Setting name
149 * @param int|null $user User or null for the current user
150 *
151 * @return Setting
152 */
153 public static function getSettingByName(string $name, $user = null)
154 {
155 self::checkName($name);
156 if (!(self::$schema[$name]['public'] ?? false)) {
157 throw new SoftException('Attempt to access an internal variable');
158 }
159
160 $data = [
161 'name' => $name,
162 'user' => is_null($user) ? Core::instance()->getCurrentUser() : $user
163 ];
164
165 switch (self::$schema[$name]['type']) {
166 case 'string':
167 return new SettingString($data);
168 case 'select':
169 return new SettingStringSelect($data);
170 case 'integer':
171 return new SettingInteger($data);
172 default:
173 throw new RuntimeException('Unknown setting type');
174 }
175 }
176
177 /**
178 * List of the possible setting items that must be returned in getList method, their types and other data
179 */
180 public static $schema = [
181 'version' => [
182 'type' => 'string',
183 'default' => ''
184 ],
185 'status.emails-for-last-n-days' => [
186 'type' => 'integer',
187 'public' => true,
188 'minimum' => 1,
189 'maximum' => 365,
190 'default' => '30'
191 ],
192 'status.emails-filter-when-list-filtered' => [
193 'type' => 'select',
194 'public' => true,
195 'options' => [ 'yes', 'no' ],
196 'default' => 'yes'
197 ],
198 'report-view.sort-records-by' => [
199 'type' => 'select',
200 'public' => true,
201 'options' => [ 'ip,ascent', 'ip,descent', 'message-count,ascent', 'message-count,descent' ],
202 'default' => 'message-count,descent'
203 ],
204 'report-view.filter.initial-value' => [
205 'type' => 'select',
206 'public' => true,
207 'options' => [ 'none', 'from-list', 'last-value', 'last-value-tab' ],
208 'default' => 'last-value-tab'
209 ],
210 'log-view.sort-list-by' => [
211 'type' => 'select',
212 'public' => true,
213 'options' => [ 'event-time,ascent', 'event-time,descent' ],
214 'default' => 'event-time,ascent'
215 ],
216 'ui.datetime.offset' => [
217 'type' => 'select',
218 'public' => true,
219 'options' => [ 'auto', 'utc', 'local' ],
220 'default' => 'auto'
221 ],
222 'ui.ipv4.url' => [
223 'type' => 'string',
224 'public' => true,
225 'default' => '!https://who.is/whois-ip/ip-address/{$ip}'
226 ],
227 'ui.ipv6.url' => [
228 'type' => 'string',
229 'public' => true,
230 'default' => '!https://who.is/whois-ip/ip-address/{$ip}'
231 ]
232 ];
233 }