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\AbuseIPDB\ApiHandler; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class ShellUtils |
27
|
|
|
* |
28
|
|
|
* Abstract base class for main cli program |
29
|
|
|
*/ |
30
|
|
|
abstract class ShellUtils |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* helper functions |
34
|
|
|
*/ |
35
|
|
|
use UtilsTrait; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
const OUTPUT_JSON = 'json'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
const OUTPUT_DEFAULT = 'default'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
const OUTPUT_PLAINTEXT = 'plaintext'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string $outputFormat |
54
|
|
|
*/ |
55
|
|
|
protected static $outputFormat = self::OUTPUT_DEFAULT; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Gets whether current output is default |
59
|
|
|
*/ |
60
|
|
|
protected static function isDefaultOuput(): bool |
61
|
|
|
{ |
62
|
|
|
return self::$outputFormat === self::OUTPUT_DEFAULT; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Prints title action banner |
67
|
|
|
* |
68
|
|
|
* @access protected |
69
|
|
|
* @static |
70
|
|
|
* @param string $title |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
protected static function printTitle(string $title): void |
75
|
|
|
{ |
76
|
|
|
if (self::isDefaultOuput()) { |
77
|
|
|
Console::log(); |
78
|
|
|
Console::log($title); |
79
|
|
|
Console::log(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Print temp message during api request |
85
|
|
|
* |
86
|
|
|
* @access protected |
87
|
|
|
* @static |
88
|
|
|
* @param array $arguments |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
protected static function printTempMessage(): void |
93
|
|
|
{ |
94
|
|
|
if (self::isDefaultOuput()) { |
95
|
|
|
Console::reLog(Console::text(' ? ', 'green') . Console::text('waiting for api response', 'white') . Console::text(' ... ', 'green')); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Clear the temp message set during api request |
101
|
|
|
* |
102
|
|
|
* @access protected |
103
|
|
|
* @static |
104
|
|
|
* @param array $arguments |
105
|
|
|
* |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
protected static function clearTempMessage(): void |
109
|
|
|
{ |
110
|
|
|
if (self::isDefaultOuput()) { |
111
|
|
|
// long blank string to overwrite previous message |
112
|
|
|
Console::reLog(' '); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Print to banner |
118
|
|
|
* |
119
|
|
|
* @access protected |
120
|
|
|
* @static |
121
|
|
|
* @param array $arguments |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
protected static function printLogo(): void |
126
|
|
|
{ |
127
|
|
|
if (self::isDefaultOuput()) { |
128
|
|
|
//Console::log(" _ _ _ __ __ ", "darkgray"); |
129
|
|
|
//Console::log(" | |___ _(_)__| |_ _ _ / _|/ _| ", "darkgray"); |
130
|
|
|
//Console::log(" | / / '_| (_-< _| || | _| _| ", "darkgray"); |
131
|
|
|
//Console::log(" |_\_\_| |_/__/\__|\_,_|_| |_| ", "darkgray"); |
132
|
|
|
Console::log(" _ ___ ___ ___ ___ ", "darkgray"); |
133
|
|
|
Console::log(" __ _| |__ _ _ ___ ___|_ _| _ \ \| _ ) ", "darkgray"); |
134
|
|
|
Console::log(" / _` | '_ \ || (_-</ -_)| || _/ |) | _ \ ", "darkgray"); |
135
|
|
|
Console::log(" \__,_|_.__/\_,_/__/\___|___|_| |___/|___/ ", "darkgray"); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Print version |
141
|
|
|
* |
142
|
|
|
* @access protected |
143
|
|
|
* @static |
144
|
|
|
* @param array $arguments |
145
|
|
|
* |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
|
|
protected static function printVersion(): void |
149
|
|
|
{ |
150
|
|
|
self::printLogo(); |
151
|
|
|
|
152
|
|
|
Console::log(); |
153
|
|
|
Console::log(Console::text(' Kristuff/AbuseIPDB Client version: ', 'darkgray') . Console::text(AbuseIPDBClient::VERSION, 'lightgray')); |
154
|
|
|
Console::log(Console::text(' Kristuff/AbuseIPDB Core version: ', 'darkgray') . Console::text(ApiHandler::VERSION, 'lightgray')); |
155
|
|
|
Console::log(Console::text(' --------------------------------------------------', 'darkgray')); |
156
|
|
|
Console::log(Console::text(' Released under the MIT licence', 'darkgray')); |
157
|
|
|
Console::log(Console::text(' Made with ', 'darkgray') . Console::text('♥', 'red') . Console::text(' in France', 'darkgray')); |
158
|
|
|
Console::log( |
159
|
|
|
Console::text(' © 2020-2021 Kristuff (', 'darkgray'). |
160
|
|
|
Console::text('https://github.com/kristuff', 'darkgray', 'underlined'). |
161
|
|
|
Console::text(')', 'darkgray') |
162
|
|
|
); |
163
|
|
|
Console::log(Console::text(' --------------------------------------------------', 'darkgray')); |
164
|
|
|
Console::log(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Print app banner |
169
|
|
|
* |
170
|
|
|
* @access protected |
171
|
|
|
* @static |
172
|
|
|
* |
173
|
|
|
* @return void |
174
|
|
|
*/ |
175
|
|
|
protected static function printBanner(): void |
176
|
|
|
{ |
177
|
|
|
if (self::isDefaultOuput()) { |
178
|
|
|
Console::log(); |
179
|
|
|
Console::log( Console::text(' Kristuff/AbuseIPDB-client ', 'darkgray') . Console::text(' ' . AbuseIPDBClient::VERSION . ' ', 'white', 'blue')); |
180
|
|
|
Console::log(Console::text(' Made with ', 'darkgray') . Console::text('♥', 'red') . Console::text(' in France | © 2020-2021 Kristuff', 'darkgray')); |
181
|
|
|
Console::log(); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Print footer |
187
|
|
|
* |
188
|
|
|
* @access protected |
189
|
|
|
* @static |
190
|
|
|
* |
191
|
|
|
* @return void |
192
|
|
|
*/ |
193
|
|
|
protected static function printFooter($requestTime = ''): void |
194
|
|
|
{ |
195
|
|
|
if (self::isDefaultOuput()) { |
196
|
|
|
if (!empty($requestTime)){ |
197
|
|
|
$date_utc = new \DateTime("now", new \DateTimeZone("UTC")); |
198
|
|
|
Console::log( |
199
|
|
|
Console::text(' Request time: ', 'darkgray') . Console::text($requestTime . 's', 'lightgray'). |
200
|
|
|
Console::text(' | UTC time: ', 'darkgray') . Console::text($date_utc->format('Y-m-d H:i:s'), 'lightgray') |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
Console::log(Console::text(' ------------------------------------------------------------------------------------------------------', 'darkgray')); |
204
|
|
|
Console::log( |
205
|
|
|
Console::text(' Kristuff\AbuseIPDB ', 'darkgray') . |
206
|
|
|
Console::text(AbuseIPDBClient::VERSION, 'lightgray') . |
207
|
|
|
Console::text(' | Made with ', 'darkgray') . |
208
|
|
|
Console::text('♥', 'red') . |
209
|
|
|
Console::text(' in France | © 2020-2021 Kristuff (https://github.com/kristuff)', 'darkgray') |
210
|
|
|
); |
211
|
|
|
Console::log(); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Prints/gets a result value |
217
|
|
|
* |
218
|
|
|
* @access protected |
219
|
|
|
* @static |
220
|
|
|
* |
221
|
|
|
* @return string |
222
|
|
|
*/ |
223
|
|
|
protected static function printResult($text, $value, string $foregroundColor = 'lightred', string $backgroundColor = '', bool $print = true): string |
224
|
|
|
{ |
225
|
|
|
// do not print null/blank values |
226
|
|
|
if (isset($value)){ |
227
|
|
|
$line = Console::text($text, 'white') . Console::text($value, $foregroundColor, $backgroundColor); |
228
|
|
|
if ($print && self::isDefaultOuput()){ |
229
|
|
|
Console::log($line); |
230
|
|
|
} |
231
|
|
|
return $line; |
232
|
|
|
} |
233
|
|
|
return ''; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Prints score badge |
238
|
|
|
* |
239
|
|
|
* @access protected |
240
|
|
|
* @static |
241
|
|
|
* @param string $text |
242
|
|
|
* @param int $score |
243
|
|
|
* @param string $textColor |
244
|
|
|
* |
245
|
|
|
* @return string |
246
|
|
|
*/ |
247
|
|
|
protected static function getScoreBadge(int $score, string $padding = ' '): string |
248
|
|
|
{ |
249
|
|
|
$scoreforegroundColor = 'white'; |
250
|
|
|
$scoreBackgroundColor = 'green'; |
251
|
|
|
|
252
|
|
|
if (intval($score) > 0 ){ |
253
|
|
|
$scoreforegroundColor = 'black'; |
254
|
|
|
$scoreBackgroundColor = 'yellow'; |
255
|
|
|
} |
256
|
|
|
if (intval($score) > 50 ){ |
257
|
|
|
$scoreforegroundColor = 'white'; |
258
|
|
|
$scoreBackgroundColor = 'red'; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$badge = str_pad($score, 3, ' ',STR_PAD_LEFT); |
262
|
|
|
return Console::text($padding.$badge.$padding, $scoreforegroundColor, $scoreBackgroundColor); |
263
|
|
|
} |
264
|
|
|
} |