Completed
Push — master ( 8cf48b...afece8 )
by Kris
13s queued 10s
created

AbuseIPDBClient::bulkReport()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 26
c 1
b 0
f 0
nc 10
nop 1
dl 0
loc 38
rs 8.8817
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 AbuseIPDB
27
 * 
28
 * The main cli program
29
 */
30
class AbuseIPDBClient extends AbstractClient
31
{
32
    /**
33
     * Helper methods
34
     */
35
    use CheckTrait, CheckBlockTrait, BulkReportTrait;
36
37
    /**
38
     * The entry point of our app 
39
     * 
40
     * @access public
41
     * @static
42
     * @param array     $arguments
43
     * @param string    $keyPath        The key file path
44
     * 
45
     * @return void
46
     */
47
    public static function start(array $arguments, string $keyPath): void
48
    {
49
        // required at least one valid argument
50
        self::$keyPath = $keyPath; 
51
        self::validate( !empty($arguments), 'No valid arguments given. Run abuseipdb --help to get help.');
52
        if (!self::parseCommand($arguments, $keyPath)) {
53
            self::error('Invalid arguments. Run abuseipdb --help to get help.');
54
            self::printFooter();
55
            Program::exit(1);
56
        }
57
        Program::exit(0);
58
    }
59
   
60
    /**
61
     * Register API key in a config file
62
     *  
63
     * @access protected
64
     * @static
65
     * 
66
     * @return bool
67
     */
68
    protected static function registerApiKey($arguments): void
69
    {
70
        self::printTitle(Console::text('  ► Register API key ', 'darkgray'));
71
        
72
        $key = self::getArgumentValue($arguments,'S', 'save-key');
73
        
74
        if (empty($key)){
75
            self::error('Null or invalid key argument.');
76
            self::printFooter();
77
            Program::exit(1);
78
        }
79
80
        $data = json_encode(['api_key' => $key]);
81
       
82
        if (file_put_contents(self::$keyPath, $data, LOCK_EX) === false){
83
            self::error('An error occurred when writing config file. Make sure to give the appropriate permissions to the config directory.');
84
            self::printFooter();
85
            Program::exit(1);
86
        }
87
        Console::log(Console::text('  ✓ ', 'green') . Console::text('Your config key file has been successfully created.', 'white'));
88
        Console::log();   
89
        self::printFooter();
90
        Program::exit();
91
    }
92
 
93
    /**
94
     * Prints the help
95
     * 
96
     * @access protected
97
     * @static
98
     * 
99
     * @return void
100
     */
101
    protected static function printHelp(): void
102
    {
103
        self::printBanner();
104
105
        Console::log(' ' . Console::text('SYNOPSIS:', 'white', 'underline')); 
106
        Console::log(' ' . Console::text('    abuseipdb -C ') . 
107
                           Console::text('IP', 'yellow') . 
108
                           Console::text(' [-d ') . 
109
                           Console::text('DAYS', 'yellow') . 
110
                           Console::text('] [-v] [-l ') . 
111
                           Console::text('LIMIT', 'yellow') . 
112
                           Console::text('] [-o ') . 
113
                           Console::text('FORMAT', 'yellow') . 
114
                           Console::text(']')); 
115
116
        Console::log(' ' . Console::text('    abuseipdb -K ') . 
117
                           Console::text('NETWORK', 'yellow') . 
118
                           Console::text(' [-d ') . 
119
                           Console::text('DAYS', 'yellow') . 
120
                           Console::text('] [-o ') . 
121
                           Console::text('FORMAT', 'yellow') . 
122
                           Console::text(']')); 
123
124
        Console::log(' ' . Console::text('    abuseipdb -R ') .
125
                           Console::text('IP', 'yellow') . ' -c ' .
126
                           Console::text('CATEGORIES', 'yellow') . ' -m ' .
127
                           Console::text('MESSAGE', 'yellow') .
128
                           Console::text(' [-o ') . 
129
                           Console::text('FORMAT', 'yellow') . 
130
                           Console::text(']')); 
131
132
        Console::log(' ' . Console::text('    abuseipdb -V ') .
133
                           Console::text('FILE', 'yellow') .
134
                           Console::text(' [-o ') . 
135
                           Console::text('FORMAT', 'yellow') . 
136
                           Console::text(']')); 
137
138
        Console::log(' ' . Console::text('    abuseipdb -E ') .
139
                           Console::text('IP', 'yellow').
140
                           Console::text(' [-o ') . 
141
                           Console::text('FORMAT', 'yellow') . 
142
                           Console::text(']')); 
143
                           
144
        Console::log(' ' . Console::text('    abuseipdb -B ') . 
145
                           Console::text('[-l ') . 
146
                           Console::text('LIMIT', 'yellow') . 
147
                           Console::text('] [-s ') . 
148
                           Console::text('SCORE', 'yellow') . 
149
                           Console::text('] [-o ') . 
150
                           Console::text('FORMAT', 'yellow') . 
151
                           Console::text(']')); 
152
153
        Console::log(' ' . Console::text('    abuseipdb -S ' .
154
                           Console::text('KEY', 'yellow')));
155
156
        Console::log(' ' . Console::text('    abuseipdb -L | -G | -h | --version'));
157
                           
158
        Console::log();    
159
        Console::log(' ' . Console::text('OPTIONS:', 'white', 'underline')); 
160
        Console::log();
161
        Console::log(Console::text('   -h, --help', 'white')); 
162
        Console::log('       Prints the current help. If given, all next arguments are ignored.', 'lightgray');
163
        Console::log();    
164
        Console::log(Console::text('   -G, --config', 'white')); 
165
        Console::log('       Prints the current config. If given, all next arguments are ignored.', 'lightgray');
166
        Console::log();    
167
        Console::log(Console::text('   -L, --list', 'white')); 
168
        Console::log('       Prints the list report categories. If given, all next arguments are ignored.', 'lightgray');
169
        Console::log();    
170
        Console::log(Console::text('   -C, --check ', 'white') . Console::text('IP', 'yellow', 'underline')); 
171
        Console::log('       Performs a check request for the given IP address. A valid IPv4 or IPv6 address is required.', 'lightgray');
172
        Console::log();    
173
        Console::log(Console::text('   -K, --check-block ', 'white') . Console::text('network', 'yellow', 'underline')); 
174
        Console::log('       Performs a check-block request for the given network. A valid subnet (v4 or v6) denoted with ', 'lightgray');
175
        Console::log('       CIDR notation is required.', 'lightgray');
176
        Console::log();    
177
        Console::log(Console::text('   -d, --days ', 'white') . Console::text('DAYS', 'yellow', 'underline')); 
178
        Console::log('       For a check or check-block request, defines the maxAgeDays. Min is 1, max is 365, default is 30.', 'lightgray');
179
        Console::log();    
180
        Console::log(Console::text('   -R, --report ', 'white') . Console::text('IP', 'yellow', 'underline')); 
181
        Console::log('       Performs a report request for the given IP address. A valid IPv4 or IPv6 address is required.', 'lightgray');
182
        Console::log();    
183
        Console::log(Console::text('   -V, --bulk-report ', 'white') . Console::text('FILE', 'yellow', 'underline')); 
184
        Console::log('       Performs a bulk-report request sending a csv file. A valid file name or full path is required.', 'lightgray');
185
        Console::log();    
186
        Console::log(Console::text('   -E, --clear ', 'white')); 
187
        Console::log('       Remove own reports for the given IP address. A valid IPv4 or IPv6 address is required.', 'lightgray');
188
        Console::log();
189
        Console::log(Console::text('   -c, --categories ', 'white') . Console::text('CATEGORIES', 'yellow', 'underline')); 
190
        Console::log('       For a report request, defines the report category(ies). Categories must be separate by a comma.', 'lightgray');
191
        Console::log('       Some categories cannot be used alone. A category can be represented by its shortname or by its', 'lightgray');
192
        Console::log(Console::text('       id. Use ','lightgray')  . Console::text('abuseipdb -L', 'white') . Console::text(' to print the categories list.','lightgray'));
193
        Console::log();    
194
        Console::log(Console::text('   -m, --message ', 'white') . Console::text('MESSAGE', 'yellow', 'underline')); 
195
        Console::log('       For a report request, defines the message to send with report. Message is required for all', 'lightgray');
196
        Console::log('       report requests.', 'lightgray');
197
        Console::log();
198
        Console::log(Console::text('   -B, --blacklist ', 'white')); 
199
        Console::log('       Performs a blacklist request. Default limit is 1000. This limit can ne changed with the', 'lightgray');
200
        Console::log('       ' . Console::text('--limit', 'white') . Console::text(' parameter. ', 'lightgray'));
201
        Console::log();    
202
        Console::log(Console::text('   -l, --limit ', 'white') . Console::text('LIMIT', 'yellow', 'underline')); 
203
        Console::log('       For a blacklist request, defines the limit.', 'lightgray');
204
        Console::log('       For a check request with verbose flag, sets the max number of last reports displayed. Default is 10', 'lightgray');
205
        Console::log('       For a check-block request, sets the max number of IPs displayed. Default is 0 (no limit).', 'lightgray');
206
        Console::log();    
207
        Console::log(Console::text('   -o, --output ', 'white') . Console::text('FORMAT', 'yellow', 'underline')); 
208
        Console::log('       Defines the output format for API requests. Default is a colorized report, possible formats are', 'lightgray');
209
        Console::log('       '. Console::text('json', 'yellow', 'underline') . ' or ' . Console::text('plaintext', 'yellow', 'underline') . '. Plaintext option prints partial response (blacklist: IPs list, ');
210
        Console::log('       check or report: confidence score only, check-block: reported IPs list with confidence score, ', 'lightgray');
211
        Console::log('       bulk-report: number of saved reports, clear: number of deleted reports).', 'lightgray');
212
        Console::log();    
213
        Console::log(Console::text('   -s, --score ', 'white')); 
214
        Console::log('       For a blacklist request, sets the confidence score minimum. The confidence minimum ', 'lightgray');
215
        Console::log('       must be between 25 and 100. This parameter is subscriber feature (not honored otherwise, allways 100).', 'lightgray');
216
        Console::log();    
217
        Console::log(Console::text('   -v, --verbose ', 'white')); 
218
        Console::log('       For a check request, display additional fields like the x last reports. This increases ', 'lightgray');
219
        Console::log(Console::text('       request time and response size. Max number of last reports displayed can be changed with the ', 'lightgray'));
220
        Console::log('       ' . Console::text('--limit', 'white') . Console::text(' parameter. ', 'lightgray'));
221
        Console::log();    
222
        Console::log(Console::text('   --version', 'white')); 
223
        Console::log('       Prints the current version. If given, all next arguments are ignored.', 'lightgray');
224
        Console::log(); 
225
        Console::log(Console::text('   -S, --save-key ', 'white') . Console::text('KEY', 'yellow', 'underline')); 
226
        Console::log('       Save the given API key in the configuration file. Required writing permissions on the config directory. ', 'lightgray');
227
        Console::log(); 
228
    }
229
230
    /**
231
     * Prints the current config and exit
232
     * 
233
     * @access protected
234
     * @static
235
     * 
236
     * @return void
237
     */
238
    protected static function printConfig(): void
239
    {
240
        $conf = self::$api->getConfig();
241
242
        self::printTitle(Console::text('  ► Current configuration ', 'darkgray'));
243
        
244
        Console::log(Console::text('  api_key:[', 'white') . Console::text($conf['apiKey'], 'green') . Console::text(']', 'white'));
245
        Console::log(Console::text('  self_ips:', 'white'));
246
        
247
        foreach ($conf['selfIps'] as $ip) {
248
            Console::log(Console::text('    [', 'white') . Console::text($ip, 'green') . Console::text(']', 'white'));   
249
        }
250
251
        Console::log();   
252
        self::printFooter();
253
    }
254
255
    /**
256
     * Prints the report categories list
257
     * 
258
     * @access protected
259
     * @static
260
     * 
261
     * @return void
262
     */
263
    protected static function printCategories(): void
264
    {
265
        self::printTitle(Console::text('  ► Report categories list ', 'darkgray'));
266
267
        $categories = ApiHandler::getCategories();
268
        $rowHeaders = [
269
            Console::text('ShortName',      'darkgray') => 15, 
270
            Console::text('Id',             'darkgray') => 2, 
271
            Console::text('Full name',      'darkgray') => 18,
272
            Console::text('Can be alone?',  'darkgray') => 15
273
        ];
274
        Console::$verticalSeparator = '  ';
275
        Console::$verticalInnerSeparator = '  ';
276
        Console::log(Console::tableRowSeparator($rowHeaders, 'darkgray'));
277
        Console::log(Console::tableRow($rowHeaders));      
278
        Console::log(Console::tableRowSeparator($rowHeaders), 'darkgray');
279
        
280
        foreach ($categories as $cat) {
281
            $id = Console::text($cat[1], 'white');
282
            $standalone = $cat[3] ? Console::text('✓', 'green') . Console::text(' true ', 'lightgray') : 
283
                                    Console::text('✗', 'red')   . Console::text(' false', 'darkgray');
284
            $shortName =  Console::text($cat[0], 'white');
285
            $fullName =   Console::text($cat[2], 'lightgray');
286
287
            Console::log(
288
                Console::TableRowStart().  
289
                Console::TableRowCell( $shortName , 15).  
290
                Console::TableRowCell( $id , 2, Console::ALIGN_CENTER).  
291
                Console::TableRowCell( $fullName , 18).  
292
                Console::TableRowCell( $standalone , 15,  Console::ALIGN_CENTER)  
293
            );
294
        }
295
        //Console::log(Console::tableRowSeparator($rowHeaders), 'darkgray');
296
        Console::log();
297
        self::printFooter();
298
    }
299
300
    /**
301
     * Perform a report request 
302
     * 
303
     * @access protected
304
     * @static
305
     * @param array $arguments
306
     * 
307
     * @return void
308
     */
309
    protected static function reportIP(array $arguments): void
310
    {
311
        $ip      = self::getArgumentValue($arguments,'R', 'report');
312
        $cats    = self::getArgumentValue($arguments,'c', 'categories');
313
        $message = self::getArgumentValue($arguments,'m', 'message');
314
        
315
        self::printTitle(Console::text('  ► Report IP: ', 'darkgray') . Console::text(escapeshellcmd($ip), 'white'));
316
        self::printTempMessage();
317
318
        // Peforms request 
319
        $timeStart = microtime(true);
320
        $report = self::$api->report($ip, $cats, $message)->getObject();     
321
        $timeEnd = microtime(true);
322
        $time = $timeEnd - $timeStart; // request time
323
        self::clearTempMessage();
324
        
325
        // check for errors / empty response
326
        if (self::hasErrors($report)){
327
            self::printFooter();
328
            Program::exit(1);
329
        }
330
               
331
        // ✓ Done: print reported IP and confidence score
332
        $score = empty($report->data->abuseConfidenceScore) ? 0 : $report->data->abuseConfidenceScore;
333
        $scoreColor = self::getScoreColor($score);
334
335
        switch (self::$outputFormat){
336
            case self::OUTPUT_JSON:
337
                echo json_encode($report, JSON_PRETTY_PRINT);
338
                break;
339
       
340
            case self::OUTPUT_DEFAULT:  
341
                Console::log(
342
                    Console::text('   ✓', 'green').Console::text(' IP: [', 'white') .
343
                    Console::text($ip, $scoreColor).Console::text('] successfully reported', 'white')
344
                );
345
                Console::log(Console::text('     Confidence score: ', 'white').self::getScoreBadge($score));
346
                Console::log();
347
                self::printFooter($time);
348
                break;
349
350
            case self::OUTPUT_PLAINTEXT:
351
                echo $score.PHP_EOL;
352
                break;
353
354
        }
355
    }
356
357
    /**
358
     * Perform a bulk-report request 
359
     * 
360
     * @access protected
361
     * @static
362
     * @param array $arguments
363
     * 
364
     * @return void
365
     */
366
    protected static function bulkReport(array $arguments): void
367
    {
368
        $fileName = self::getArgumentValue($arguments,'V', 'bulk-report');
369
370
        self::printTitle(Console::text('  ► Bulk report for file: ', 'darkgray') . Console::text(escapeshellcmd($fileName), 'white'));
371
        self::printTempMessage();
372
373
        // Peforms request 
374
        $timeStart = microtime(true);  
375
        $response = self::$api->bulkReport($fileName)->getObject();     
376
        $timeEnd = microtime(true);      
377
        $time = $timeEnd - $timeStart;  // request time
378
        self::clearTempMessage();
379
380
        // check for errors / empty response
381
        if (self::hasErrors($response)){
382
            self::printFooter();
383
            Program::exit(1);
384
        }
385
386
        // ✓ Done
387
        switch (self::$outputFormat){
388
            case self::OUTPUT_JSON:
389
                echo json_encode($response, JSON_PRETTY_PRINT);
390
                break;
391
        
392
            case self::OUTPUT_DEFAULT:  
393
                self::printBulkReportDetail($fileName);
394
                self::printBulkReportSavedReports($response);
395
                self::printBulkReportErrors($response);
396
                Console::log();
397
                self::printFooter($time);
398
                break;
399
400
            case self::OUTPUT_PLAINTEXT:
401
                $nbSavedReports = isset($response->data->savedReports) ? $response->data->savedReports : 0;
402
                echo $nbSavedReports . PHP_EOL;
403
                break;
404
405
        }
406
    }
407
408
    /**
409
     * Perform a clear-address request 
410
     * 
411
     * @access protected
412
     * @static
413
     * @param array $arguments
414
     * 
415
     * @return void
416
     */
417
    protected static function clearIP(array $arguments): void
418
    {
419
        $ip = self::getArgumentValue($arguments,'E', 'clear');
420
        self::printTitle(Console::text('  ► Clear reports for IP: ', 'darkgray') . Console::text(escapeshellcmd($ip), 'white'));
421
422
        // Peforms request 
423
        self::printTempMessage();
424
        $timeStart = microtime(true);
425
        $response = self::$api->clearAddress($ip)->getObject();     
426
        $timeEnd = microtime(true);
427
        $time = $timeEnd - $timeStart; // request time
428
        self::clearTempMessage();
429
430
        // check for errors / empty response
431
        if (self::hasErrors($response)){
432
            self::printFooter($time);
433
            Program::exit(1);
434
        }
435
        
436
        // ✓ Done: print deleted report number 
437
        switch (self::$outputFormat){
438
            case self::OUTPUT_JSON:
439
                echo json_encode($response, JSON_PRETTY_PRINT);
440
                break;
441
       
442
            case self::OUTPUT_DEFAULT:  
443
                Console::log(
444
                    Console::text('   ✓', 'green') . 
445
                    Console::text(' Successfull clear request for IP: [', 'white') .
446
                    Console::text($ip, 'lightyellow') .
447
                    Console::text(']', 'white')
448
                );
449
                self::printResult('     Deleted reports: ', $response->data->numReportsDeleted ?? 0, 'lightyellow');
450
                Console::log();
451
                self::printFooter($time);
452
                break;
453
454
            case self::OUTPUT_PLAINTEXT:
455
                echo ($response->data->numReportsDeleted ?? 0) . PHP_EOL;
456
                break;
457
458
        }
459
    }
460
461
    /**
462
     * Perform a blacklist request 
463
     * 
464
     * @access protected
465
     * @static
466
     * @param array $arguments
467
     * 
468
     * @return void
469
     */
470
    protected static function getBlacklist(array $arguments): void
471
    {
472
        self::printTitle(Console::text('  ► Get Blacklist ', 'darkgray'));
473
474
        $plainText  = self::$outputFormat === self::OUTPUT_PLAINTEXT; 
475
        $limit      = self::getNumericParameter($arguments,'l', 'limit', 1000);
476
        $scoreMin   = self::getNumericParameter($arguments,'s', 'score', 100);
477
        
478
        self::printTempMessage();
479
        
480
        // do request 
481
        $timeStart = microtime(true);
482
        $response = self::$api->blacklist($limit, $plainText, $scoreMin);
483
        $timeEnd = microtime(true);
484
        $time = $timeEnd - $timeStart; // request time
485
486
        self::clearTempMessage();
487
    
488
        // response could be json on error, while plaintext flag is set
489
        $decodedResponse = $response->getObject();
490
        if (self::hasErrors($decodedResponse, false)){
491
            self::printFooter($time);
492
            Program::exit(1);
493
        }
494
495
        // ✓ Done: print deleted report number 
496
        switch (self::$outputFormat){
497
            case self::OUTPUT_JSON:
498
                echo json_encode($response, JSON_PRETTY_PRINT);
499
                break;
500
       
501
            case self::OUTPUT_DEFAULT:  
502
                 // print list
503
                self::printResult('  List generated at: ', self::getDate($decodedResponse->meta->generatedAt), 'lightyellow', '');
504
                Console::log();
505
                foreach ($decodedResponse->data as $report){
506
                    $score = empty($report->abuseConfidenceScore) ? 0 : $report->abuseConfidenceScore;
507
                    $defaultColor = self::getScoreColor($score);
508
509
                    $line  = Console::text('    →', $defaultColor);
510
                    $line .= self::printResult(' IP: ', $report->ipAddress, $defaultColor, '', false);
511
                    $line .= self::printResult(' | Last reported at: ', self::getDate($report->lastReportedAt), $defaultColor, '', false);
512
                    $line .= Console::text(' | Confidence score: ', 'white');
513
                    $line .= self::getScoreBadge($score);
514
                    Console::log($line);
515
                }
516
517
                Console::log();
518
                self::printFooter($time);
519
                break;
520
521
            case self::OUTPUT_PLAINTEXT:
522
                // echo response "as is"
523
                Console::log($response->getPlaintext());
524
                break;
525
526
        }
527
    }
528
529
    /**
530
     * Perform a check-block request 
531
     * 
532
     * @access protected
533
     * @static
534
     * @param array $arguments
535
     * 
536
     * @return void
537
     */
538
    protected static function checkBlock(array $arguments): void
539
    {
540
        $network  = self::getArgumentValue($arguments,'K', 'check-block');
541
542
        self::printTitle(Console::text('  ► Check network: ', 'darkgray') . Console::text(escapeshellcmd($network), 'white') . Console::text('', 'darkgray'));
543
544
        $maxAge   = self::getNumericParameter($arguments, 'd', 'days', 30);
545
        $limit    = self::getNumericParameter($arguments,'l', 'limit', 0); // 0 mean no limit
546
547
        self::printTempMessage();
548
549
        $timeStart = microtime(true);                                       
550
        $check = self::$api->checkBlock($network, $maxAge)->getObject();
551
        $timeEnd = microtime(true);
552
        $time = $timeEnd - $timeStart; // request time
553
        self::clearTempMessage();
554
555
        // check for errors / empty response
556
        if (self::hasErrors($check)){
557
            self::printFooter($time);
558
            Program::exit(1);
559
        }
560
561
        switch (self::$outputFormat){
562
            case self::OUTPUT_JSON:
563
                echo json_encode($check, JSON_PRETTY_PRINT);
564
                break;
565
       
566
            case self::OUTPUT_DEFAULT:  
567
                self::printCheckBlockDetail($check);
568
                self::printCheckBlockReportedIP($check,$maxAge,$limit);
569
                Console::log();
570
                self::printFooter($time);
571
                break;
572
573
            case self::OUTPUT_PLAINTEXT:
574
                $nbReports = isset($check->data->reportedAddress) ? count($check->data->reportedAddress) : 0;
575
                if ($nbReports > 0) {
576
                    $numberDiplayedReports = 0;
577
                    foreach ($check->data->reportedAddress as $report){
578
                        echo ($report->ipAddress) . ' ' . $report->abuseConfidenceScore . PHP_EOL;
579
580
                        // counter
581
                        $numberDiplayedReports++;
582
                        if ($numberDiplayedReports === $limit) {
583
                            break;
584
                        }
585
                    }
586
                }
587
                break;
588
        }
589
    }
590
591
    /**
592
     * Perform a check request 
593
     * 
594
     * @access protected
595
     * @static
596
     * @param array $arguments
597
     * 
598
     * @return void
599
     */
600
    protected static function checkIP(array $arguments): void
601
    {
602
        $ip = self::getArgumentValue($arguments,'C', 'check');
603
        
604
        self::printTitle(Console::text('  ► Check IP: ', 'darkgray') . Console::text(escapeshellcmd($ip), 'white') . Console::text('', 'darkgray'));
605
        
606
        $verbose            = self::inArguments($arguments,'v', 'verbose');
607
        $maxAge             = self::getNumericParameter($arguments, 'd', 'days', 30);
608
        $maxReportsNumber   = self::getNumericParameter($arguments,'l', 'limit', 10);
609
        $ip                 = self::getArgumentValue($arguments,'C', 'check');
610
611
        self::printTempMessage();
612
        $timeStart = microtime(true);                                           
613
        $check = self::$api->check($ip, $maxAge, $verbose)->getObject();        
614
        $timeEnd = microtime(true);                                              
615
        $time = $timeEnd - $timeStart; // request time
616
        self::clearTempMessage();
617
618
        // check for errors / empty response
619
        if (self::hasErrors($check)){
620
            self::printFooter($time);
621
            Program::exit(1);
622
        }
623
624
        // score and data color (depending of abuseConfidenceScore)
625
        $score = empty($check->data->abuseConfidenceScore) ? 0 : $check->data->abuseConfidenceScore;
626
627
        switch (self::$outputFormat){
628
            case self::OUTPUT_JSON:
629
                echo json_encode($check, JSON_PRETTY_PRINT);
630
                break;
631
       
632
            case self::OUTPUT_DEFAULT:  
633
                $defaultColor = self::getScoreColor($score);
634
                self::printCheckScore($check);
635
                self::printCheckDetail($check, $defaultColor);
636
                self::printCheckReports($check, $maxAge, $defaultColor);
637
                if ($verbose){
638
                    self::printCheckLastReports($check, $maxReportsNumber);
639
                }
640
                Console::log();
641
                self::printFooter($time);
642
                break;
643
644
            case self::OUTPUT_PLAINTEXT:
645
                echo ($check->data->abuseConfidenceScore ?? 0) . PHP_EOL;
646
                break;
647
648
        }
649
    }
650
}