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
|
|
|
* php version 7.1.0 |
11
|
|
|
* |
12
|
|
|
* @category Web-security |
13
|
|
|
* @package Shieldon |
14
|
|
|
* @author Terry Lin <[email protected]> |
15
|
|
|
* @copyright 2019 terrylinooo |
16
|
|
|
* @license https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT |
17
|
|
|
* @link https://github.com/terrylinooo/shieldon |
18
|
|
|
* @see https://shieldon.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
namespace Shieldon\Firewall\Panel\Sandbox; |
24
|
|
|
|
25
|
|
|
use Exception; |
26
|
|
|
use Shieldon\Messenger\Smtp as SmtpTest; |
27
|
|
|
use function explode; |
28
|
|
|
use function filter_var; |
29
|
|
|
use function str_replace; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The sandbox for SMTP. |
33
|
|
|
*/ |
34
|
|
|
class Smtp |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* Invoker. |
38
|
|
|
* |
39
|
|
|
* @param array $args The arguments. |
40
|
|
|
* |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
|
|
public function __invoke(array $args): bool |
44
|
2 |
|
{ |
45
|
|
|
return $this->sandbox($args[0], $args[1]); |
46
|
2 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Test SMTP. |
50
|
|
|
* |
51
|
|
|
* @param array $getParams The GET params passed from tryMessenger method. |
52
|
|
|
* @param array $message The message title and body. |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
private function sandbox($getParams, $message): bool |
57
|
2 |
|
{ |
58
|
|
|
$type = ''; |
59
|
2 |
|
$host = ''; |
60
|
2 |
|
$user = ''; |
61
|
2 |
|
$pass = ''; |
62
|
2 |
|
$port = ''; |
63
|
2 |
|
$sender = ''; |
64
|
2 |
|
$recipients = ''; |
65
|
2 |
|
|
66
|
|
|
$params = [ |
67
|
2 |
|
'type', |
68
|
2 |
|
'host', |
69
|
2 |
|
'user', |
70
|
2 |
|
'pass', |
71
|
2 |
|
'port', |
72
|
2 |
|
'sender', |
73
|
2 |
|
'recipients', |
74
|
2 |
|
]; |
75
|
2 |
|
|
76
|
|
|
foreach ($params as $param) { |
77
|
2 |
|
${$param} = $getParams[$param] ?? ''; |
78
|
2 |
|
|
79
|
|
|
if (empty(${$param})) { |
80
|
2 |
|
// @codeCoverageIgnoreStart |
81
|
|
|
return false; |
82
|
|
|
// @codeCoverageIgnoreEnd |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (!$this->checkHost($host)) { |
87
|
2 |
|
return false; |
88
|
1 |
|
} |
89
|
|
|
|
90
|
|
|
if (in_array($type, ['ssl', 'tls'])) { |
91
|
1 |
|
$host = $type . '://' . $host; |
92
|
1 |
|
} |
93
|
|
|
|
94
|
|
|
$recipients = str_replace("\r", '|', $recipients); |
95
|
1 |
|
$recipients = str_replace("\n", '|', $recipients); |
96
|
1 |
|
$recipients = explode('|', $recipients); |
97
|
1 |
|
|
98
|
|
|
$messenger = new SmtpTest($user, $pass, $host, (int) $port); |
99
|
1 |
|
|
100
|
|
|
foreach ($recipients as $recipient) { |
101
|
1 |
|
if (filter_var($recipient, FILTER_VALIDATE_EMAIL)) { |
102
|
1 |
|
$messenger->addRecipient($recipient); |
103
|
1 |
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (filter_var($sender, FILTER_VALIDATE_EMAIL)) { |
107
|
1 |
|
$messenger->addSender($sender); |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
$messenger->setSubject($message['title']); |
111
|
1 |
|
|
112
|
|
|
// @codeCoverageIgnoreStart |
113
|
|
|
try { |
114
|
|
|
if ($messenger->send($message['body'])) { |
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
} catch (Exception $e) { |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
// @codeCoverageIgnoreEnd |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Check the SMTP host. |
126
|
|
|
* |
127
|
|
|
* @param string $host The IP address or server domain name. |
128
|
|
|
* |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
2 |
|
private function checkHost(string $host): bool |
132
|
|
|
{ |
133
|
|
|
if (!filter_var($host, FILTER_VALIDATE_IP) && |
134
|
2 |
|
!filter_var($host, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) |
135
|
2 |
|
) { |
136
|
|
|
return false; |
137
|
1 |
|
} |
138
|
|
|
return true; |
139
|
1 |
|
} |
140
|
|
|
} |
141
|
|
|
|