1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ ___ ___ ___ ___ |
5
|
|
|
* __ _| |__ _ _ ___ ___|_ _| _ \ \| _ ) |
6
|
|
|
* / _` | '_ \ || (_-</ -_)| || _/ |) | _ \ |
7
|
|
|
* \__,_|_.__/\_,_/__/\___|___|_| |___/|___/ |
8
|
|
|
* |
9
|
|
|
* This file is part of Kristuff\AbuseIPDB. |
10
|
|
|
* |
11
|
|
|
* (c) Kristuff <[email protected]> |
12
|
|
|
* |
13
|
|
|
* For the full copyright and license information, please view the LICENSE |
14
|
|
|
* file that was distributed with this source code. |
15
|
|
|
* |
16
|
|
|
* @version 0.9.20 |
17
|
|
|
* @copyright 2020-2022 Kristuff |
18
|
|
|
*/ |
19
|
|
|
namespace Kristuff\AbuseIPDB; |
20
|
|
|
|
21
|
|
|
use Kristuff\Mishell\Console; |
22
|
|
|
use Kristuff\AbuseIPDB\ApiHandler; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Trait Check |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
trait CheckTrait |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Print confidence score |
32
|
|
|
* |
33
|
|
|
* @access protected |
34
|
|
|
* @static |
35
|
|
|
* @param object $response |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
protected static function printCheckScore(object $response): void |
40
|
|
|
{ |
41
|
|
|
$score = empty($response->data->abuseConfidenceScore) ? 0 : $response->data->abuseConfidenceScore; |
42
|
|
|
$line = Console::text(Console::pad(' Confidence score:', 23), 'white') . self::getScoreBadge($score); |
43
|
|
|
Console::log($line); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Print check IP detail |
48
|
|
|
* |
49
|
|
|
* @access protected |
50
|
|
|
* @static |
51
|
|
|
* @param object $response |
52
|
|
|
* @param string $color |
53
|
|
|
* |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
|
protected static function printCheckDetail(object $response, string $color): void |
57
|
|
|
{ |
58
|
|
|
// self::printResult(' isPublic', $response->data->isPublic, $defaultColor); |
59
|
|
|
// self::printResult(' ipVersion', $response->data->ipVersion, $defaultColor); |
60
|
|
|
|
61
|
|
|
$line = self::printResult(Console::pad(' Whitelisted:', 23), $response->data->isWhitelisted ? 'true': 'false', $color, '', false); |
62
|
|
|
$line .= $response->data->isWhitelisted ? Console::text(' ★', 'green') : ''; |
63
|
|
|
Console::log($line); |
64
|
|
|
|
65
|
|
|
self::printResult(Console::pad(' Country code:', 23), $response->data->countryCode, $color); |
66
|
|
|
|
67
|
|
|
if (!empty($response->data->countryName)){ |
68
|
|
|
self::printResult(Console::pad(' Country name:', 23), $response->data->countryName, $color); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
self::printResult(Console::pad(' ISP:', 23), $response->data->isp, $color); |
72
|
|
|
|
73
|
|
|
if ($response->data->usageType){ |
74
|
|
|
$line = self::printResult(Console::pad(' Usage type:', 23), $response->data->usageType, $color, '', false); |
75
|
|
|
$line .= $response->data->usageType === 'Reserved' ? Console::text(' ◆', 'green') : ''; |
76
|
|
|
Console::log($line); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$hostames = implode(', ', array_filter($response->data->hostnames)) ?? null; |
80
|
|
|
if (!empty($hostames)){ |
81
|
|
|
self::printResult(Console::pad(' Hostname(s):', 23), $hostames, $color); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
self::printResult(Console::pad(' Domain:', 23), $response->data->domain, $color); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Print reports data |
89
|
|
|
* |
90
|
|
|
* @access protected |
91
|
|
|
* @static |
92
|
|
|
* @param object $response |
93
|
|
|
* @param int $maxAge |
94
|
|
|
* @param string $color |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
protected static function printCheckReports(object $response, int $maxAge, string $color): void |
99
|
|
|
{ |
100
|
|
|
$nbReport = $response->data->totalReports && is_numeric($response->data->totalReports) ? intval($response->data->totalReports) : 0; |
101
|
|
|
|
102
|
|
|
if ($nbReport > 0 ){ |
103
|
|
|
$line = self::printResult(Console::pad(' Total reports:', 23), $nbReport, $color, '', false); |
104
|
|
|
$line .= self::printResult(' from ', $response->data->numDistinctUsers, $color, '', false); |
105
|
|
|
$line .= Console::text($nbReport > 0 ? ' distinct users': ' user', 'white'); |
106
|
|
|
Console::log($line); |
107
|
|
|
|
108
|
|
|
} else { |
109
|
|
|
// no reports |
110
|
|
|
$period = $maxAge > 1 ? 'in last '. $maxAge . ' days': ' today'; |
111
|
|
|
if (empty($response->data->lastReportedAt)){ |
112
|
|
|
Console::log( Console::text(' ✓', 'green') . Console::text(' Not reported ' . $period)); |
113
|
|
|
|
114
|
|
|
} else { |
115
|
|
|
// in some case, we have no reports but lastReportedAt in not empty (when deleting reports for example) |
116
|
|
|
// maybe a bug or cache in api |
117
|
|
|
// In this case Set total 0 and let print lastReportedAt |
118
|
|
|
$line = self::printResult(Console::pad(' Total reports:', 23), $nbReport, $color, '', false); |
119
|
|
|
Console::log($line); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (!empty($response->data->lastReportedAt)){ |
124
|
|
|
self::printResult(Console::pad(' Last reported at:', 23), self::getDate($response->data->lastReportedAt), $color); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Print last reports data |
130
|
|
|
* |
131
|
|
|
* @access protected |
132
|
|
|
* @static |
133
|
|
|
* @param object $response |
134
|
|
|
* @param bool $verbose |
135
|
|
|
* @param int $maxReportsNumber |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
protected static function printCheckLastReports(object $response, int $maxReportsNumber): void |
140
|
|
|
{ |
141
|
|
|
$nbLastReports = isset($response->data->reports) ? count($response->data->reports) : 0; |
142
|
|
|
|
143
|
|
|
if ($nbLastReports > 0){ |
144
|
|
|
Console::log(' Last reports:', 'white'); |
145
|
|
|
$numberDiplayedReports = 0; |
146
|
|
|
$defaultColor = 'lightyellow'; // reset color for last reports |
147
|
|
|
|
148
|
|
|
foreach ($response->data->reports as $report){ |
149
|
|
|
self::printLastReport($report); |
150
|
|
|
$numberDiplayedReports++; |
151
|
|
|
|
152
|
|
|
if ($numberDiplayedReports === $maxReportsNumber || $numberDiplayedReports === $nbLastReports) { |
153
|
|
|
$line = Console::text(' (', 'white'); |
154
|
|
|
$line .= Console::text($numberDiplayedReports, $defaultColor); |
155
|
|
|
$line .= Console::text('/', 'white'); |
156
|
|
|
$line .= Console::text($nbLastReports, $defaultColor); |
157
|
|
|
$line .= Console::text($numberDiplayedReports > 1 ? ' reports displayed)': ' report displayed)', 'white'); |
158
|
|
|
Console::log($line); |
159
|
|
|
break; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Print single entry in last reports |
167
|
|
|
* |
168
|
|
|
* @access private |
169
|
|
|
* @static |
170
|
|
|
* @param object $report |
171
|
|
|
* |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
|
private static function printLastReport(object $report): void |
175
|
|
|
{ |
176
|
|
|
$categories = self::getLastReportsCategories($report); |
177
|
|
|
$defaultColor = 'lightyellow'; // reset color for last reports |
178
|
|
|
|
179
|
|
|
$line = Console::text(' →', $defaultColor); |
180
|
|
|
$line .= self::printResult(' reported at: ', self::getDate($report->reportedAt), $defaultColor, '', false); |
181
|
|
|
// $line .= self::printResult(' by user: ', $report->reporterId, $defaultColor, '', false); |
182
|
|
|
if (isset($report->reporterCountryCode) && isset($report->reporterCountryName)){ |
183
|
|
|
$line .= Console::text(' from: ', 'white'); |
184
|
|
|
$line .= self::printResult('', $report->reporterCountryCode, $defaultColor, '', false); |
185
|
|
|
$line .= Console::text(' - ', 'white'); |
186
|
|
|
$line .= self::printResult('', $report->reporterCountryName, $defaultColor, '', false); |
187
|
|
|
} |
188
|
|
|
$line .= Console::text(' with categor' . (count($categories) > 1 ? "ies: " : "y: "), 'white'); |
189
|
|
|
foreach ($categories as $key => $cat) { |
190
|
|
|
$line .= Console::text($key==0 ? '' : ',' , 'white') . Console::text($cat, $defaultColor); |
191
|
|
|
} |
192
|
|
|
Console::log($line); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Get last report categories array |
197
|
|
|
* |
198
|
|
|
* @access private |
199
|
|
|
* @static |
200
|
|
|
* @param object $report |
201
|
|
|
* |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
|
|
private static function getLastReportsCategories(object $report): array |
205
|
|
|
{ |
206
|
|
|
$categories = []; |
207
|
|
|
foreach (array_filter($report->categories) as $catId){ |
208
|
|
|
$cat = ApiHandler::getCategoryNameById(strval($catId)); |
209
|
|
|
if ($cat !== false) { |
210
|
|
|
$categories[] = $cat; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
return $categories; |
214
|
|
|
} |
215
|
|
|
} |