-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEdit.php
More file actions
102 lines (83 loc) · 3.63 KB
/
Copy pathEdit.php
File metadata and controls
102 lines (83 loc) · 3.63 KB
1
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace BNETDocs\Controllers\Server;
use \BNETDocs\Libraries\Core\HttpCode;
use \BNETDocs\Libraries\Core\Router;
use \BNETDocs\Libraries\EventLog\Logger;
use \BNETDocs\Models\Server\Form as FormModel;
use \OutOfBoundsException;
class Edit extends \BNETDocs\Controllers\Base
{
public const S_DISABLED = ':no_entry: Disabled';
public const S_ONLINE = ':white_check_mark: Online';
public const S_OFFLINE = ':x: Offline';
public function __construct()
{
$this->model = new FormModel();
}
public function invoke(?array $args): bool
{
$this->model->server_edit = true;
if (!($this->model->active_user && $this->model->active_user->getOption(\BNETDocs\Libraries\User\User::OPTION_ACL_SERVER_MODIFY)))
{
$this->model->_responseCode = HttpCode::HTTP_FORBIDDEN;
$this->model->error = $this->model->active_user ? FormModel::ERROR_ACL_NOT_SET : FormModel::ERROR_NOT_LOGGED_IN;
return true;
}
$this->model->form_fields = Router::query();
$id = $this->model->form_fields['id'] ?? null;
if (!is_numeric($id))
{
$this->model->_responseCode = HttpCode::HTTP_BAD_REQUEST;
$this->model->error = FormModel::ERROR_INVALID_ID;
return true;
}
$id = (int) $id;
try { $this->model->server = new \BNETDocs\Libraries\Server\Server($id); }
catch (\UnexpectedValueException) { $this->model->server = null; }
if (!$this->model->server)
{
$this->model->_responseCode = HttpCode::HTTP_NOT_FOUND;
return true;
}
$this->model->_responseCode = HttpCode::HTTP_OK;
$this->model->server_types = \BNETDocs\Libraries\Server\Type::getAllServerTypes();
if (Router::requestMethod() == Router::METHOD_POST) $this->handlePost();
return true;
}
protected function handlePost(): void
{
$q = &$this->model->form_fields;
try { $this->model->server->setAddress($q['address'] ?? ''); }
catch (OutOfBoundsException) { $this->model->error = FormModel::ERROR_INVALID_ADDRESS; return; }
try { $this->model->server->setLabel(isset($q['label']) && !empty($q['label']) ? $q['label'] : null); }
catch (OutOfBoundsException) { $this->model->error = FormModel::ERROR_INVALID_LABEL; return; }
try { $this->model->server->setPort(isset($q['port']) ? (int) $q['port'] : 0); }
catch (OutOfBoundsException) { $this->model->error = FormModel::ERROR_INVALID_PORT; return; }
try { $this->model->server->setTypeId(isset($q['type']) ? (int) $q['type'] : 0); }
catch (OutOfBoundsException) { $this->model->error = FormModel::ERROR_INVALID_TYPE; return; }
$this->model->server->setDisabled((bool) ($this->model->form_fields['disabled'] ?? null));
$this->model->server->setOnline((bool) ($this->model->form_fields['online'] ?? null));
$this->model->error = $this->model->server->commit() ? false : FormModel::ERROR_INTERNAL;
if ($this->model->error === false)
{
$event = Logger::initEvent(
\BNETDocs\Libraries\EventLog\EventTypes::SERVER_EDITED,
$this->model->active_user,
getenv('REMOTE_ADDR'),
$this->model->server
);
if ($event->commit())
{
$embed = Logger::initDiscordEmbed($event, $this->model->server->getURI(), [
'Type' => $this->model->server->getType()->getLabel(),
'Label' => $this->model->server->getLabel(),
'Server' => $this->model->server->getAddress() . ':' . $this->model->server->getPort(),
'Status' => $this->model->server->isDisabled() ? self::S_DISABLED : (
$this->model->server->isOnline() ? self::S_ONLINE : self::S_OFFLINE
),
]);
Logger::logToDiscord($event, $embed);
}
}
}
}