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