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\Mishell\Program; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class ShellErrorHandler |
26
|
|
|
* |
27
|
|
|
* Abstract base class for main cli program |
28
|
|
|
*/ |
29
|
|
|
abstract class ShellErrorHandler extends ShellUtils |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Check and print errors in API response. |
33
|
|
|
* |
34
|
|
|
* @access protected |
35
|
|
|
* @static |
36
|
|
|
* @param object $response |
37
|
|
|
* @param bool $checkForEmpty |
38
|
|
|
* |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
|
|
protected static function hasErrors(?object $response = null, bool $checkForEmpty = true): bool |
42
|
|
|
{ |
43
|
|
|
return $checkForEmpty ? self::parseErrors($response) || self::checkForEmpty($response) : self::parseErrors($response); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Check and print errors in API response. |
48
|
|
|
* |
49
|
|
|
* @access protected |
50
|
|
|
* @static |
51
|
|
|
* @param object $response |
52
|
|
|
* @param bool $checkForEmpty |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
private static function parseErrors(?object $response = null): bool |
57
|
|
|
{ |
58
|
|
|
if (isset($response) && isset($response->errors)){ |
59
|
|
|
switch (self::$outputFormat){ |
60
|
|
|
case self::OUTPUT_DEFAULT: |
61
|
|
|
self::printFormattedErrors($response); |
62
|
|
|
break; |
63
|
|
|
|
64
|
|
|
case self::OUTPUT_PLAINTEXT: |
65
|
|
|
self::printPlainTextErrors($response); |
66
|
|
|
break; |
67
|
|
|
|
68
|
|
|
case self::OUTPUT_JSON: |
69
|
|
|
echo json_encode($response, JSON_PRETTY_PRINT); |
70
|
|
|
break; |
71
|
|
|
} |
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* |
79
|
|
|
* @access protected |
80
|
|
|
* @static |
81
|
|
|
* @param object $response |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
protected static function printFormattedErrors(object $response): void |
86
|
|
|
{ |
87
|
|
|
// top error badge |
88
|
|
|
Console::log(' ' . Console::text(' ERROR ','white', 'red')); |
89
|
|
|
|
90
|
|
|
$num = 0; |
91
|
|
|
// errors is an array, could have more than one error.. |
92
|
|
|
foreach ($response->errors as $err){ |
93
|
|
|
$num++; |
94
|
|
|
|
95
|
|
|
Console::log(Console::text(' ✗', 'red') . self::printResult(' Number: ', $num, 'lightyellow','', false)); |
96
|
|
|
self::printResult(' Status: ', $err->status ?? null, 'lightyellow',''); |
97
|
|
|
|
98
|
|
|
if (!empty($err->source) && !empty($err->source->parameter)){ |
99
|
|
|
self::printResult(' Parameter: ', $err->source->parameter, 'lightyellow'); |
100
|
|
|
} |
101
|
|
|
self::printResult(' Title: ', $err->title ?? null, 'lightyellow'); |
102
|
|
|
self::printResult(' Detail: ', $err->detail ?? null, 'lightyellow'); |
103
|
|
|
|
104
|
|
|
// separate errors |
105
|
|
|
if (count($response->errors) > 1){ |
106
|
|
|
Console::log(' ---'); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
Console::log(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Print a single error |
114
|
|
|
* |
115
|
|
|
* @access protected |
116
|
|
|
* @static |
117
|
|
|
* @param string $error The error message |
118
|
|
|
* |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
|
|
protected static function error(string $error): void |
122
|
|
|
{ |
123
|
|
|
if (self::isDefaultOuput()) { |
124
|
|
|
// ✗ |
125
|
|
|
Console::log(' ' . Console::text(' ERROR ','white', 'red')); |
126
|
|
|
Console::log( |
127
|
|
|
Console::text(' ✗', 'red') . |
128
|
|
|
Console::text(' Detail: ', 'white') . |
129
|
|
|
Console::text($error, 'lightyellow') . |
130
|
|
|
Console::text('', 'white') |
131
|
|
|
); |
132
|
|
|
Console::log(); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Helper to validate a condition or exit with an error |
138
|
|
|
* |
139
|
|
|
* @access protected |
140
|
|
|
* @static |
141
|
|
|
* @param bool $condition The condition to evaluate |
142
|
|
|
* @param string $message Error message |
143
|
|
|
* @param bool $print True to print error. Default is true |
144
|
|
|
* |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
|
|
protected static function validate(bool $condition, string $message, bool $print = true): void |
148
|
|
|
{ |
149
|
|
|
if ( !$condition ){ |
150
|
|
|
if ($print && self::isDefaultOuput()) { |
151
|
|
|
Console::log(); |
152
|
|
|
self::error($message); |
153
|
|
|
self::printFooter(); |
154
|
|
|
} |
155
|
|
|
Program::exit(1); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get numeric parameter and exit on error |
161
|
|
|
* |
162
|
|
|
* @access protected |
163
|
|
|
* @static |
164
|
|
|
* @param array $arguments |
165
|
|
|
* @param string $shortArg The short argument name |
166
|
|
|
* @param string $longArg The long argument name |
167
|
|
|
* @param int $defaultValue |
168
|
|
|
* |
169
|
|
|
* @return int |
170
|
|
|
*/ |
171
|
|
|
protected static function getNumericParameter(array $arguments, string $shortArg, string $longArg, int $defaultValue): int |
172
|
|
|
{ |
173
|
|
|
if (self::inArguments($arguments,$shortArg, $longArg)){ |
174
|
|
|
$val = self::getArgumentValue($arguments,$shortArg, $longArg); |
175
|
|
|
|
176
|
|
|
if (!is_numeric($val)){ |
177
|
|
|
self::error("Invalid parameter: $longArg must be a numeric value."); |
178
|
|
|
self::printFooter(); |
179
|
|
|
Program::exit(1); |
180
|
|
|
} |
181
|
|
|
return intval($val); |
182
|
|
|
} |
183
|
|
|
return $defaultValue; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Check and print errors in API response. Null response object is considered as no errors |
188
|
|
|
* |
189
|
|
|
* @access protected |
190
|
|
|
* @static |
191
|
|
|
* @param object $response |
192
|
|
|
* |
193
|
|
|
* @return void |
194
|
|
|
*/ |
195
|
|
|
protected static function printPlainTextErrors(object $response): void |
196
|
|
|
{ |
197
|
|
|
foreach ($response->errors as $err){ |
198
|
|
|
$text = 'Error: '; |
199
|
|
|
$text .= self::getErrorDetail($err, 'title'); |
200
|
|
|
$text .= self::getErrorDetail($err, 'statuts'); |
201
|
|
|
$text .= self::getErrorDetail($err, 'parameter', 'source'); |
202
|
|
|
$text .= self::getErrorDetail($err, 'detail'); |
203
|
|
|
$text .= PHP_EOL; |
204
|
|
|
echo $text; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get error property if exist |
210
|
|
|
* |
211
|
|
|
* @access protected |
212
|
|
|
* @static |
213
|
|
|
* @param object $error |
214
|
|
|
* @param string $field |
215
|
|
|
* @param string $parent |
216
|
|
|
* |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
|
|
private static function getErrorDetail(object $error, string $field, ?string $parent = null): string |
220
|
|
|
{ |
221
|
|
|
if (!empty($parent)){ |
222
|
|
|
return !empty($error->$parent) && !empty($error->$parent->$field) ? ' ' . $field . ': ' . $error->$parent->$field : ''; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return !empty($error->$field) ? ' ' . $field . ': ' . $error->$field : ''; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Check if response is empty |
230
|
|
|
* |
231
|
|
|
* @access protected |
232
|
|
|
* @static |
233
|
|
|
* @param object $response |
234
|
|
|
* |
235
|
|
|
* @return bool |
236
|
|
|
*/ |
237
|
|
|
protected static function checkForEmpty(?object $response = null): bool |
238
|
|
|
{ |
239
|
|
|
// check for empty response ? |
240
|
|
|
if ( empty($response) || empty($response->data) ){ |
241
|
|
|
self::error('An unexpected error occurred.'); |
242
|
|
|
return true; |
243
|
|
|
} |
244
|
|
|
return false; |
245
|
|
|
} |
246
|
|
|
} |