1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Shieldon package. |
4
|
|
|
* |
5
|
|
|
* (c) Terry L. <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Shieldon\Firewall\Panel\Sandbox; |
14
|
|
|
|
15
|
|
|
use Shieldon\Messenger\Smtp as SmtpTest; |
16
|
|
|
use function explode; |
17
|
|
|
use function filter_var; |
18
|
|
|
use function str_replace; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The sandbox for SMTP. |
23
|
|
|
*/ |
24
|
|
|
class Smtp |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Invoker. |
28
|
|
|
* |
29
|
|
|
* @param array $args |
30
|
|
|
* |
31
|
|
|
* @return bool |
32
|
|
|
*/ |
33
|
|
|
public function __invoke(array $args): bool |
34
|
|
|
{ |
35
|
|
|
return $this->sandbox($args[0], $args[1]); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Test SMTP. |
40
|
|
|
* |
41
|
|
|
* @param array $getParams The GET params passed from tryMessenger method. |
42
|
|
|
* @param array $message The message title and body. |
43
|
|
|
* |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
private function sandbox($getParams, $message) |
47
|
|
|
{ |
48
|
|
|
$params = [ |
49
|
|
|
'type', |
50
|
|
|
'host', |
51
|
|
|
'user', |
52
|
|
|
'pass', |
53
|
|
|
'port', |
54
|
|
|
'sender', |
55
|
|
|
'recipients', |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
foreach ($params as $param) { |
59
|
|
|
${$param} = $getParams[$param] ?? ''; |
60
|
|
|
|
61
|
|
|
if (empty(${$param})) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!$this->checkHost($host)) { |
|
|
|
|
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ('ssl' === $type || 'tls' === $type) { |
|
|
|
|
71
|
|
|
$host = $type . '://' . $host; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$recipients = str_replace("\r", '|', $recipients); |
|
|
|
|
75
|
|
|
$recipients = str_replace("\n", '|', $recipients); |
76
|
|
|
$recipients = explode('|', $recipients); |
77
|
|
|
|
78
|
|
|
$messenger = new SmtpTest($user, $pass, $host, (int) $port); |
|
|
|
|
79
|
|
|
|
80
|
|
|
foreach ($recipients as $recipient) { |
81
|
|
|
if (filter_var($recipient, FILTER_VALIDATE_EMAIL)) { |
82
|
|
|
$messenger->addRecipient($recipient); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (filter_var($sender, FILTER_VALIDATE_EMAIL)) { |
|
|
|
|
87
|
|
|
$messenger->addSender($sender); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$messenger->setSubject($message['title']); |
91
|
|
|
|
92
|
|
|
if ($messenger->send($message['body'])) { |
93
|
|
|
return true; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Check the SMTP host. |
101
|
|
|
* |
102
|
|
|
* @param string $host The IP address or server domain name. |
103
|
|
|
* |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
private function checkHost(string $host): bool |
107
|
|
|
{ |
108
|
|
|
if ( |
109
|
|
|
!filter_var($host, FILTER_VALIDATE_IP) && |
110
|
|
|
!filter_var($host, FILTER_VALIDATE_DOMAIN) |
111
|
|
|
) { |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
} |