| 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 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * Trait CheckBlock | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | trait CheckBlockTrait | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * Prints IP detail  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      *  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @access protected | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @static | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * @param object    $response | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      *  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     protected static function printCheckBlockDetail(object $response): void | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         self::printResult(Console::pad('   Network Address:', 23), $response->data->networkAddress, 'lightyellow'); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         self::printResult(Console::pad('   Netmask:', 23), $response->data->netmask, 'lightyellow'); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         self::printResult(Console::pad('   Min Address:', 23), $response->data->minAddress, 'lightyellow'); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         self::printResult(Console::pad('   Max Address:', 23), $response->data->maxAddress, 'lightyellow'); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         self::printResult(Console::pad('   Possible Hosts:', 23), $response->data->numPossibleHosts, 'lightyellow'); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         self::printResult(Console::pad('   Address SpaceDesc:', 23), $response->data->addressSpaceDesc, 'lightyellow'); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     }    | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * Prints reported IP  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      *  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * @access protected | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * @static | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * @param object    $response | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * @param int       $maxAge | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * @param int       $limit | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      *  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     protected static function printCheckBlockReportedIP(object $response, int $maxAge, int $limit): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $nbReports = isset($response->data->reportedAddress) ? count($response->data->reportedAddress) : 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         if ($nbReports > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             self::printResult(Console::pad('   Reported addresses:', 23), $nbReports, 'lightyellow'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $numberDiplayedReports = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             foreach ($response->data->reportedAddress as $report){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 $score = empty($report->abuseConfidenceScore) ? 0 : $report->abuseConfidenceScore; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 $defaultColor = self::getScoreColor($score); // color based on score | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |        | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 $line  = Console::text('   →', $defaultColor); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 $line .= self::printResult(' IP: ', $report->ipAddress, $defaultColor, '', false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 $line .= self::printResult(' Country: ', $report->countryCode , $defaultColor, '', false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 $line .= Console::text(' | Confidence score: ', 'white'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 $line .= self::getScoreBadge($score); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 $line .= self::printResult(' Total reports: ', $report->numReports, $defaultColor, '', false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 $line .= self::printResult(' Last reported at: ', self::getDate($report->mostRecentReport), $defaultColor, '', false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 Console::log($line); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |        | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 // counter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 $numberDiplayedReports++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |        | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 if ($numberDiplayedReports === $limit || $numberDiplayedReports === $nbReports) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                     $line  = Console::text('      (', 'white'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                     $line .= Console::text($numberDiplayedReports, 'lightyellow'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                     $line .= Console::text('/', 'white'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                     $line .= Console::text($nbReports, 'lightyellow'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                     $line .= Console::text($numberDiplayedReports > 1 ? ' IPs displayed)': ' IP displayed)', 'white'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                     Console::log($line); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |        | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             // no reports | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $day = $maxAge > 1 ? 'in last '. $maxAge . ' days': ' today'; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  |             Console::log( Console::text('    ✓', 'green') . Console::text(' No IP reported ' . $day)); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  |     }    | 
            
                                                        
            
                                    
            
            
                | 100 |  |  | } |