| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace EndelWar\Spammer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class Validator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** @var InputInterface $input */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     private $input; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     public function __construct(InputInterface $input) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         $this->input = $input; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function validateInput(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $validInput = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $validInput['smtpServerIp'] = $this->input->getOption('server'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $this->validateInputServerIP($validInput['smtpServerIp']); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $validInput['smtpServerPort'] = $this->input->getOption('port'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->validateInputServerPort($validInput['smtpServerPort']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $validInput['corpusPath'] = $this->validateInputCorpusPath($this->input->getOption('set-corpus-path')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $validInput['count'] = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $validInput['locale'] = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         if (false === $validInput['corpusPath']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             $validInput['count'] = (int)$this->input->getOption('count'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             $this->validateInputCount($validInput['count']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             $validInput['locale'] = $this->input->getOption('locale'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $validInput['to'] = $this->validateInputToFrom($this->input->getOption('to')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $validInput['from'] = $this->validateInputToFrom($this->input->getOption('from')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if (false !== $validInput['corpusPath'] && (false !== $validInput['count'] || false !== $validInput['locale'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             throw new \InvalidArgumentException('Cannot set both corpus path and count or locale'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         return $validInput; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param string $ip | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     private function validateInputServerIP($ip) | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         if (!filter_var($ip, FILTER_VALIDATE_IP)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             throw new \InvalidArgumentException('server option is not a valid IP'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param int $port | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     private function validateInputServerPort($port) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if (!is_numeric($port) || ($port < 0 || $port > 65535)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             throw new \InvalidArgumentException('server port must be a number between 0 and 65536'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param int $count | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     private function validateInputCount($count) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         if ($count < 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             throw new \InvalidArgumentException('count must be equal or greater than 1 (you want to send email, right?)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param $string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     private function validateInputToFrom($string): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         if (null === $string) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             return ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $string = strtolower($string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         if (strpos($string, '@') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             if (filter_var($string, FILTER_VALIDATE_EMAIL)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 return $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } elseif ($this->isValidDomain($string)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             return $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         throw new \InvalidArgumentException('To and from must be a valid email address or a FQDN'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @param $domain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @return bool|mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     private function isValidDomain($domain) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $domain = strtolower($domain); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $regex = "/^((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}$/"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         return preg_match($regex, $domain); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @return bool|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     private function validateInputCorpusPath($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         if (null === $path) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $path = realpath($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         if (!is_dir($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             throw new \InvalidArgumentException('Set a valid directory as corpus path'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         return $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 138 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 139 |  |  |  |