-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanCommandOutputHelper.php
More file actions
193 lines (160 loc) · 6.02 KB
/
ScanCommandOutputHelper.php
File metadata and controls
193 lines (160 loc) · 6.02 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
declare(strict_types=1);
namespace Gared\EtherScan\Console;
use DateTimeImmutable;
use DateTimeInterface;
use Gared\EtherScan\Service\Scanner\Health\HealthResponseException;
use Gared\EtherScan\Service\ScannerServiceCallbackInterface;
use GuzzleHttp\Exception\GuzzleException;
use JsonException;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;
class ScanCommandOutputHelper implements ScannerServiceCallbackInterface
{
public function __construct(
private readonly SymfonyStyle $symfonyStyle,
private readonly OutputInterface $output,
) {
}
public function onScanApiStart(string $baseUrl): void
{
$this->symfonyStyle->title('Starting scan of api: ' . $baseUrl);
}
public function onScanApiResponse(ResponseInterface $response): void
{
// TODO: Implement onScanApiResponse() method.
}
public function onScanApiException(GuzzleException|JsonException $e): void
{
$this->symfonyStyle->error($e->getMessage());
}
public function onScanApiRevision(?string $revision): void
{
if ($revision === null) {
$this->symfonyStyle->info('No revision in server header');
return;
}
$this->symfonyStyle->info('Revision in server header: ' . $revision);
}
public function onScanApiRevisionCommit(array $commit): void
{
$this->symfonyStyle->info('commit date: ' . $commit['commit']['committer']['date']);
}
public function onScanApiVersion(string $apiVersion): void
{
$text = 'api version: ' . $apiVersion;
$this->symfonyStyle->info($text);
}
public function onScanPluginsList(array $plugins): void
{
if (count($plugins) === 0) {
$this->symfonyStyle->info('No plugins found');
return;
}
$this->symfonyStyle->writeln('Plugins:');
$pluginData = [];
foreach ($plugins as $pluginName => $plugin) {
$version = $plugin['package']['version'] ?? null;
$pluginData[] = $pluginName . ($version === null ? '' : '@' . $version);
}
sort($pluginData);
$this->symfonyStyle->listing($pluginData);
}
public function onStatsResult(array $data): void
{
if (isset($data['httpStartTime'])) {
$startTime = new DateTimeImmutable('@' . ($data['httpStartTime'] / 1000));
$this->symfonyStyle->info('Server running since: ' . $startTime->format(DateTimeInterface::RFC3339));
}
if (isset($data['ueberdb_writesFailed']) && $data['ueberdb_writesFailed'] > 0) {
$this->symfonyStyle->error('Database writes failed: ' . $data['ueberdb_writesFailed']);
}
if (isset($data['ueberdb_readsFailed']) && $data['ueberdb_readsFailed'] > 0) {
$this->symfonyStyle->error('Database reads failed: ' . $data['ueberdb_readsFailed']);
}
$this->output->writeln('Stats: ' . print_r($data, true), OutputInterface::VERBOSITY_DEBUG);
}
public function onStatsException(GuzzleException|JsonException $e): void
{
$this->symfonyStyle->error($e->getMessage());
}
public function onHealthResult(array $data): void
{
if (isset($data['status']) && $data['status'] === 'pass') {
$this->symfonyStyle->success('Server is healthy');
return;
}
$this->symfonyStyle->error('Health: ' . print_r($data, true));
}
public function onHealthException(HealthResponseException $e): void
{
$this->symfonyStyle->error($e->getMessage());
}
public function onScanAdminStart(): void
{
$this->symfonyStyle->title('Starting scan of admin area...');
}
public function onScanAdminResult(string $user, string $password, bool $result): void
{
if ($result) {
$this->symfonyStyle->error('Admin area is accessible with ' . $user . ' / ' . $password);
return;
}
$this->symfonyStyle->success('Admin area is not accessible with ' . $user . ' / ' . $password);
}
public function onScanPadStart(): void
{
$this->symfonyStyle->title('Starting scan of a pad...');
}
public function onScanPadException(Throwable $e): void
{
$this->symfonyStyle->error($e->getMessage());
}
public function onScanPadSuccess(): void
{
$this->symfonyStyle->success('Pads are publicly accessible');
}
public function onConnectedTransport(bool $usedWebsocket): void
{
if ($usedWebsocket) {
$this->symfonyStyle->success('Websocket is supported');
} else {
$this->symfonyStyle->error('Websocket is not supported');
}
}
public function onVersionResult(?string $minVersion, ?string $maxVersion): void
{
if ($maxVersion === null && $minVersion === null) {
$this->symfonyStyle->info('Could not determine version');
return;
}
if ($maxVersion === null) {
$this->symfonyStyle->info('Version greater than ' . $minVersion);
return;
}
if (version_compare($maxVersion, '2.0.0', '<')) {
$this->symfonyStyle->error('You have an old version of etherpad! Please update to 2.0.0 or newer!');
}
if ($minVersion === null) {
$this->symfonyStyle->info('Version less than ' . $maxVersion);
return;
}
if ($minVersion === $maxVersion) {
$this->symfonyStyle->info('Version is ' . $maxVersion);
return;
}
$this->symfonyStyle->info('Version between ' . $minVersion . ' and ' . $maxVersion);
}
public function onClientVars(string $version, array $data): void
{
$this->symfonyStyle->info('Package version: ' . $version);
}
public function getConsoleLogger(): ?LoggerInterface
{
return new ConsoleLogger($this->output);
}
}