1 | <?php |
||
12 | class SpammerCommand extends Command |
||
13 | { |
||
14 | 32 | protected function configure() |
|
35 | |||
36 | /** |
||
37 | * @param InputInterface $input |
||
38 | * @param OutputInterface $output |
||
39 | * @return integer |
||
40 | * @throws \InvalidArgumentException |
||
41 | */ |
||
42 | 31 | protected function execute(InputInterface $input, OutputInterface $output) |
|
43 | { |
||
44 | 31 | $validInput = $this->validateInput($input); |
|
45 | |||
46 | 12 | $style = new OutputFormatterStyle('green', null, array('bold')); |
|
47 | 12 | $output->getFormatter()->setStyle('bold', $style); |
|
48 | 12 | $output->writeln('<comment>Spammer starting up</comment>'); |
|
49 | 12 | $message = '<info>Sending </info><bold>' . $validInput['count'] . '</bold>' . |
|
50 | 12 | '<info> email to server </info><bold>' . $validInput['smtpServerIp'] . '</bold>' . |
|
51 | 12 | '<info>:</info><bold>' . $validInput['smtpServerPort'] . '</bold>'; |
|
52 | 12 | if ($validInput['from'] !== '') { |
|
53 | 3 | $message .= '<info> from </info><bold>' . $validInput['from'] . '</bold>'; |
|
54 | } |
||
55 | 12 | if ($validInput['to'] !== '') { |
|
56 | 3 | $message .= '<info> to </info><bold>' . $validInput['to'] . '</bold>'; |
|
57 | } |
||
58 | 12 | $output->write($message); |
|
59 | 12 | $output->writeln('<info> using locale </info><bold>' . $validInput['locale'] . '</bold>'); |
|
60 | |||
61 | 12 | $faker = Faker\Factory::create($validInput['locale']); |
|
62 | 12 | $faker->seed(mt_rand()); |
|
63 | |||
64 | 12 | $transport = \Swift_SmtpTransport::newInstance()->setHost($validInput['smtpServerIp'])->setPort( |
|
65 | 12 | $validInput['smtpServerPort'] |
|
66 | ); |
||
67 | 12 | $mailer = \Swift_Mailer::newInstance($transport); |
|
68 | |||
69 | 12 | $numSent = 0; |
|
70 | 12 | for ($i = 0; $i < $validInput['count']; $i++) { |
|
71 | 12 | $emaiText = $faker->realText(mt_rand(200, 1000)); |
|
72 | 12 | $emailSubject = implode(' ', $faker->words(mt_rand(3, 7))); |
|
73 | 12 | $message = \Swift_Message::newInstance($emailSubject); |
|
74 | |||
75 | 12 | $from = $this->getFromTo($faker, $validInput['from']); |
|
76 | 12 | $message->setFrom($from); |
|
77 | |||
78 | 12 | $to = $this->getFromTo($faker, $validInput['to']); |
|
79 | 12 | $message->setTo($to); |
|
80 | |||
81 | 12 | $message->setBody($emaiText, 'text/plain'); |
|
82 | 12 | $message->addPart('<p>' . $emaiText . '</p>', 'text/html'); |
|
83 | |||
84 | 12 | $output->writeln('Sending email nr. ' . ($i + 1) . ': ' . key($from) . ' => ' . key($to)); |
|
85 | try { |
||
86 | 12 | $numSent += $mailer->send($message); |
|
87 | 1 | } catch (\Swift_TransportException $swe) { |
|
88 | 1 | $output->writeln('<error>' . $swe->getMessage() . '</error>'); |
|
89 | |||
90 | 1 | return 1; |
|
91 | } |
||
92 | } |
||
93 | |||
94 | 11 | $output->writeln('Sent ' . $numSent . ' messages'); |
|
95 | |||
96 | 11 | unset($faker); |
|
97 | |||
98 | 11 | return 0; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param $faker |
||
103 | * @param $validInputFromTo |
||
104 | * @return array |
||
105 | */ |
||
106 | 12 | private function getFromTo($faker, $validInputFromTo) |
|
107 | { |
||
108 | // generate fake address and name if null |
||
109 | 12 | if ($validInputFromTo === '') { |
|
110 | 11 | return [$faker->safeEmail => $faker->name]; |
|
111 | } |
||
112 | |||
113 | // use user submitted email if is email |
||
114 | 5 | if (filter_var($validInputFromTo, FILTER_VALIDATE_EMAIL)) { |
|
115 | 3 | return [$validInputFromTo => $faker->name]; |
|
116 | } |
||
117 | |||
118 | // get a random username and attach it to domain supplied by user |
||
119 | 2 | $user = strstr($faker->safeEmail, '@', true); |
|
120 | |||
121 | 2 | return [$user . '@' . $validInputFromTo => $faker->name]; |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * @param InputInterface $input |
||
126 | * @throws \InvalidArgumentException |
||
127 | * @return array |
||
128 | */ |
||
129 | 31 | protected function validateInput(InputInterface $input) |
|
149 | |||
150 | /** |
||
151 | * @param string $ip |
||
152 | * @throws \InvalidArgumentException |
||
153 | */ |
||
154 | 31 | private function validateInputServerIP($ip) |
|
160 | |||
161 | /** |
||
162 | * @param int $port |
||
163 | * @throws \InvalidArgumentException |
||
164 | */ |
||
165 | 29 | private function validateInputServerPort($port) |
|
171 | |||
172 | /** |
||
173 | * @param int $count |
||
174 | * @throws \InvalidArgumentException |
||
175 | */ |
||
176 | 26 | private function validateInputCount($count) |
|
182 | |||
183 | /** |
||
184 | * @param $string |
||
185 | * @return string |
||
186 | * @throws \InvalidArgumentException |
||
187 | */ |
||
188 | 25 | private function validateInputToFrom($string) |
|
207 | |||
208 | /** |
||
209 | * @param $domain |
||
210 | * @return bool|mixed |
||
211 | */ |
||
212 | 8 | private function isValidDomain($domain) |
|
219 | } |
||
220 |