Passed
Branch dev (c721e8)
by Kris
02:10
created

AbuseIPDBClient::clearIP()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 40
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 28
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 40
rs 9.1608
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.10
18
 * @copyright  2020-2021 Kristuff
19
 */
20
namespace Kristuff\AbuseIPDB;
21
22
use Kristuff\AbuseIPDB\SilentApiHandler;
0 ignored issues
show
Bug introduced by
The type Kristuff\AbuseIPDB\SilentApiHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

613
                self::printCheckReports($check, $maxAge, /** @scrutinizer ignore-type */ $verbose);
Loading history...
614
                self::printCheckLastReports($check, $verbose, $maxReportsNumber);
615
                Console::log();
616
                self::printFooter($time);
617
                break;
618
619
            case self::OUTPUT_PLAINTEXT:
620
                echo ($check->data->abuseConfidenceScore ?? 0) . PHP_EOL;
621
                break;
622
623
        }
624
    }
625
}