| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 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 |
|
| 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 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function __construct($core = null) |
| 55 |
{ |
| 56 |
$this->core = $core ?? Core::instance(); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 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 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 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 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 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 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 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 |
|
| 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 |
} |