1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ _ ___ ____ ____ ____ |
5
|
|
|
* / \ | |__ _ _ ___ ___|_ _| _ \| _ \| __ ) |
6
|
|
|
* / _ \ | '_ \| | | / __|/ _ \| || |_) | | | | _ \ |
7
|
|
|
* / ___ \| |_) | |_| \__ \ __/| || __/| |_| | |_) | |
8
|
|
|
* /_/ \_\_.__/ \__,_|___/\___|___|_| |____/|____/ |
9
|
|
|
* |
10
|
|
|
* This file is part of Kristuff\AbsuseIPDB. |
11
|
|
|
* |
12
|
|
|
* (c) Kristuff <[email protected]> |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
* |
17
|
|
|
* @version 0.9.8 |
18
|
|
|
* @copyright 2020-2021 Kristuff |
19
|
|
|
*/ |
20
|
|
|
namespace Kristuff\AbuseIPDB; |
21
|
|
|
|
22
|
|
|
use Kristuff\Mishell\Console; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class ShellUtils |
26
|
|
|
* |
27
|
|
|
* Absract base class for main cli program |
28
|
|
|
*/ |
29
|
|
|
abstract class ShellUtils |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* helper functions |
33
|
|
|
*/ |
34
|
|
|
use UtilsTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Prints title action banner |
38
|
|
|
* |
39
|
|
|
* @access protected |
40
|
|
|
* @static |
41
|
|
|
* @param array $arguments |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
protected static function printTitle(string $title) |
46
|
|
|
{ |
47
|
|
|
Console::log(); |
48
|
|
|
Console::log($title); |
49
|
|
|
Console::log(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Print temp message during api request |
54
|
|
|
* |
55
|
|
|
* @access protected |
56
|
|
|
* @static |
57
|
|
|
* @param array $arguments |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
protected static function printTempMessage() |
62
|
|
|
{ |
63
|
|
|
Console::reLog(Console::text(' ? ', 'green') . Console::text('waiting for api response', 'white') . Console::text(' ... ', 'green')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Clear the temp message set during api request |
68
|
|
|
* |
69
|
|
|
* @access protected |
70
|
|
|
* @static |
71
|
|
|
* @param array $arguments |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
protected static function clearTempMessage() |
76
|
|
|
{ |
77
|
|
|
// long blank string to overwrite previous message |
78
|
|
|
Console::reLog(' '); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Print app banner |
83
|
|
|
* |
84
|
|
|
* @access protected |
85
|
|
|
* @static |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
protected static function printBanner() |
90
|
|
|
{ |
91
|
|
|
Console::log(); |
92
|
|
|
Console::log( Console::text(' Kristuff\AbuseIPDB ', 'darkgray') . Console::text(' ' . AbuseIPDBClient::VERSION . ' ', 'white', 'blue')); |
93
|
|
|
Console::log(Console::text(' Made with ', 'darkgray') . Console::text('♥', 'red') . Console::text(' in France', 'darkgray')); |
94
|
|
|
Console::log(' © 2020-2021 Kristuff', 'darkgray'); |
95
|
|
|
Console::log(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Print footer |
100
|
|
|
* |
101
|
|
|
* @access protected |
102
|
|
|
* @static |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
protected static function printFooter(string $requestTime = '') |
107
|
|
|
{ |
108
|
|
|
if (!empty($requestTime)){ |
109
|
|
|
$date_utc = new \DateTime("now", new \DateTimeZone("UTC")); |
110
|
|
|
Console::log( |
111
|
|
|
Console::text(' Request time: ', 'darkgray') . Console::text($requestTime . 's', 'lightgray'). |
112
|
|
|
Console::text(' | UTC time: ', 'darkgray') . Console::text($date_utc->format('Y-m-d H:i:s'), 'lightgray') |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
Console::log(Console::text(' ------------------------------------------------------------------------------------------------------', 'darkgray')); |
116
|
|
|
Console::log( |
117
|
|
|
Console::text(' Kristuff\AbuseIPDB ', 'darkgray') . |
118
|
|
|
Console::text(AbuseIPDBClient::VERSION, 'lightgray') . |
119
|
|
|
Console::text(' | Made with ', 'darkgray') . |
120
|
|
|
Console::text('♥', 'red') . |
121
|
|
|
Console::text(' in France | © 2020-2021 Kristuff (https://github.com/kristuff)', 'darkgray') |
122
|
|
|
); |
123
|
|
|
Console::log(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Prints/gets a result value |
128
|
|
|
* |
129
|
|
|
* @access protected |
130
|
|
|
* @static |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
protected static function printResult($text, $value, string $foregroundColor = 'lightred', string $backgroundColor = '', bool $print = true) |
135
|
|
|
{ |
136
|
|
|
// do not print null/blank values |
137
|
|
|
if (isset($value)){ |
138
|
|
|
$line = Console::text($text, 'white') . Console::text($value, $foregroundColor, $backgroundColor); |
139
|
|
|
if ($print){ |
140
|
|
|
Console::log($line); |
141
|
|
|
} |
142
|
|
|
return $line; |
143
|
|
|
} |
144
|
|
|
return ''; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Prints score badge |
149
|
|
|
* |
150
|
|
|
* @access protected |
151
|
|
|
* @static |
152
|
|
|
* @param string $text |
153
|
|
|
* @param int $score |
154
|
|
|
* @param string $textColor |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
protected static function getScoreBadge(int $score, string $padding = ' ') |
159
|
|
|
{ |
160
|
|
|
$scoreforegroundColor = 'white'; |
161
|
|
|
$scoreBackgroundColor = 'green'; |
162
|
|
|
|
163
|
|
|
if (intval($score) > 0 ){ |
164
|
|
|
$scoreforegroundColor = 'black'; |
165
|
|
|
$scoreBackgroundColor = 'yellow'; |
166
|
|
|
} |
167
|
|
|
if (intval($score) > 50 ){ |
168
|
|
|
$scoreforegroundColor = 'white'; |
169
|
|
|
$scoreBackgroundColor = 'red'; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$badge = str_pad($score, 3, ' ',STR_PAD_LEFT); |
173
|
|
|
return Console::text($padding.$badge.$padding, $scoreforegroundColor, $scoreBackgroundColor); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Check and print errors in API response |
178
|
|
|
* |
179
|
|
|
* @access protected |
180
|
|
|
* @static |
181
|
|
|
* @param object $response |
182
|
|
|
* @param bool $checkForEmpty |
183
|
|
|
* |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
|
|
protected static function printErrors($response, bool $checkForEmpty = true) |
187
|
|
|
{ |
188
|
|
|
if (isset($response) && isset($response->errors)){ |
189
|
|
|
|
190
|
|
|
// top error badge |
191
|
|
|
Console::log(' ' . Console::text(' ERROR ','white', 'red')); |
192
|
|
|
|
193
|
|
|
$num = 0; |
194
|
|
|
// errors is an array, could have more than one error.. |
195
|
|
|
foreach ($response->errors as $err){ |
196
|
|
|
$num++; |
197
|
|
|
|
198
|
|
|
Console::log(Console::text(' ✗', 'red') . self::printResult(' Number: ', $num, 'lightyellow','', false)); |
199
|
|
|
self::printResult(' Status: ', $err->status ?? null, 'lightyellow',''); |
200
|
|
|
|
201
|
|
|
if (!empty($err->source) && !empty($err->source->parameter)){ |
202
|
|
|
self::printResult(' Parameter: ', $err->source->parameter, 'lightyellow'); |
203
|
|
|
} |
204
|
|
|
self::printResult(' Title: ', $err->title ?? null, 'lightyellow'); |
205
|
|
|
self::printResult(' Detail: ', $err->detail ?? null, 'lightyellow'); |
206
|
|
|
|
207
|
|
|
// separate errors |
208
|
|
|
if (count($response->errors) > 1){ |
209
|
|
|
Console::log(' ---'); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
} |
213
|
|
|
Console::log(); |
214
|
|
|
return true; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
// check for empty response ? |
218
|
|
|
if ( $checkForEmpty && ( empty($response) || empty($response->data)) ){ |
219
|
|
|
self::error('An unexpected error occurred.'); |
220
|
|
|
return true; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return false; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Print a single error |
228
|
|
|
* |
229
|
|
|
* @access protected |
230
|
|
|
* @static |
231
|
|
|
* @param string $error The error message |
232
|
|
|
* |
233
|
|
|
* @return void |
234
|
|
|
*/ |
235
|
|
|
protected static function error($error) |
236
|
|
|
{ |
237
|
|
|
// ✗ |
238
|
|
|
Console::log(' ' . Console::text(' ERROR ','white', 'red')); |
239
|
|
|
Console::log( |
240
|
|
|
Console::text(' ✗', 'red') . |
241
|
|
|
Console::text(' Detail: ', 'white') . |
242
|
|
|
Console::text($error, 'lightyellow') . |
243
|
|
|
Console::text('', 'white') |
244
|
|
|
); |
245
|
|
|
Console::log(); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* helper to validate a condition or exit with an error |
250
|
|
|
* |
251
|
|
|
* @access protected |
252
|
|
|
* @static |
253
|
|
|
* @param bool $condition The condition to evaluate |
254
|
|
|
* @param string $message Error message |
255
|
|
|
* @param bool $print True to print error. Default is true |
256
|
|
|
* |
257
|
|
|
* @return bool |
258
|
|
|
*/ |
259
|
|
|
protected static function validate(bool $condition, string $message, bool $print = true) |
260
|
|
|
{ |
261
|
|
|
if ( !$condition ){ |
262
|
|
|
if ($print) { |
263
|
|
|
Console::log(); |
264
|
|
|
self::error($message); |
265
|
|
|
self::printFooter(); |
266
|
|
|
} |
267
|
|
|
exit(1); |
|
|
|
|
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Get numeric parameter and exit on error |
273
|
|
|
* |
274
|
|
|
* @access protected |
275
|
|
|
* @static |
276
|
|
|
* @param array $arguments |
277
|
|
|
* @param string $shortArg The short argument name |
278
|
|
|
* @param string $longArg The long argument name |
279
|
|
|
* @param int $defaultValue |
280
|
|
|
* |
281
|
|
|
* @return int |
282
|
|
|
*/ |
283
|
|
|
protected static function getNumericParameter(array $arguments, string $shortArg, string $longArg, int $defaultValue) |
284
|
|
|
{ |
285
|
|
|
if (self::inArguments($arguments,$shortArg, $longArg)){ |
286
|
|
|
$val = self::getArgumentValue($arguments,$shortArg, $longArg); |
287
|
|
|
|
288
|
|
|
if (!is_numeric($val)){ |
289
|
|
|
self::error("Invalid parameter: $longArg must be a numeric value."); |
290
|
|
|
self::printFooter(); |
291
|
|
|
exit(1); |
|
|
|
|
292
|
|
|
} |
293
|
|
|
return intval($val); |
294
|
|
|
} |
295
|
|
|
return $defaultValue; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
} |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.