-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDelete.php
More file actions
118 lines (98 loc) · 4.3 KB
/
Copy pathDelete.php
File metadata and controls
118 lines (98 loc) · 4.3 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
namespace BNETDocs\Controllers\Packet;
use \BNETDocs\Libraries\Core\HttpCode;
use \BNETDocs\Libraries\Core\Router;
use \BNETDocs\Libraries\Discord\EmbedField as DiscordEmbedField;
use \BNETDocs\Libraries\EventLog\Logger;
use \BNETDocs\Models\Packet\Delete as DeleteModel;
class Delete extends \BNETDocs\Controllers\Base
{
public function __construct()
{
$this->model = new DeleteModel();
}
public function invoke(?array $args): bool
{
$this->model->acl_allowed = $this->model->active_user
&& $this->model->active_user->getOption(\BNETDocs\Libraries\User\User::OPTION_ACL_PACKET_DELETE);
if (!$this->model->acl_allowed)
{
$this->model->_responseCode = HttpCode::HTTP_UNAUTHORIZED;
$this->model->error = $this->model->active_user ? DeleteModel::ERROR_ACL_NOT_SET : DeleteModel::ERROR_NOT_LOGGED_IN;
return true;
}
$q = Router::query();
$this->model->packet_id = $q['id'] ?? null;
try { if (!is_null($this->model->packet_id)) $this->model->packet = new \BNETDocs\Libraries\Packet\Packet($this->model->packet_id); }
catch (\UnexpectedValueException) { $this->model->packet = null; }
if (!$this->model->packet)
{
$this->model->_responseCode = HttpCode::HTTP_NOT_FOUND;
$this->model->error = DeleteModel::ERROR_NOT_FOUND;
return true;
}
$this->model->label = $this->model->packet->getLabel();
if (Router::requestMethod() == Router::METHOD_POST) $this->tryDelete();
$this->model->_responseCode = $this->model->error ? HttpCode::HTTP_INTERNAL_SERVER_ERROR : HttpCode::HTTP_OK;
return true;
}
protected function tryDelete(): void
{
$this->model->error = $this->model->packet->deallocate() ? false : DeleteModel::ERROR_INTERNAL;
$event = Logger::initEvent(
\BNETDocs\Libraries\EventLog\EventTypes::PACKET_DELETED,
$this->model->active_user,
getenv('REMOTE_ADDR'),
[
'error' => $this->model->error,
'packet_id' => $this->model->packet_id,
'packet' => $this->model->packet,
]
);
if ($event->commit())
{
$packet = $this->model->packet;
$brief = $packet->getBrief(false);
$format = $packet->getFormat();
$remarks = $packet->getRemarks(false);
$offset = 13; // char count of code block, end-of-line, and ellipsis addons
if (\strlen($brief) - $offset > DiscordEmbedField::MAX_VALUE)
{
$brief = \substr($brief, 0, DiscordEmbedField::MAX_VALUE - $offset) . '…';
}
if (\strlen($format) - $offset > DiscordEmbedField::MAX_VALUE)
{
$format = \substr($format, 0, DiscordEmbedField::MAX_VALUE - $offset) . '…';
}
if (\strlen($remarks) - $offset > DiscordEmbedField::MAX_VALUE)
{
$remarks = \substr($remarks, 0, DiscordEmbedField::MAX_VALUE - $offset) . '…';
}
$used_by = '';
foreach ($packet->getUsedBy() as $product)
{
if (!empty($used_by)) $used_by .= ', ';
$used_by .= $product->getLabel();
}
if (empty($used_by)) $used_by = '*Unknown*';
$embed = Logger::initDiscordEmbed($event, $packet->getURI(), [
'Direction' => $packet->getDirectionLabel(),
'Id' => $packet->getPacketId(true),
'Name' => $packet->getName(),
'Brief' => !empty($brief) ? $brief : '*empty*',
'Deprecated' => $packet->isDeprecated() ? ':white_check_mark:' : ':x:',
'Draft' => !$packet->isPublished() ? ':white_check_mark:' : ':x:',
'Markdown' => $packet->isMarkdown() ? ':white_check_mark:' : ':x:',
'In research' => $packet->isInResearch() ? ':white_check_mark:' : ':x:',
'Application layer' => $packet->getApplicationLayer()->getLabel(),
'Transport layer' => $packet->getTransportLayer()->getTag(),
'Used by' => $used_by,
'Authored by' => !\is_null($packet->getUserId()) ? $packet->getUser()->getAsMarkdown() : '*Anonymous*',
'Deleted by' => $this->model->active_user->getAsMarkdown(),
]);
$embed->addField(new DiscordEmbedField('Format', '```' . \PHP_EOL . $format . \PHP_EOL . '```', false));
$embed->setDescription($packet->isMarkdown() ? $remarks : '```' . \PHP_EOL . $remarks . \PHP_EOL . '```');
Logger::logToDiscord($event, $embed);
}
}
}