Navigation | Overlay |
---|---|
t Navigate files | h Toggle hits |
y Change url to tip of branch | m Toggle misses |
b / v Jump to prev/next hit line | p Toggle partial |
z / x Jump to prev/next missed or partial line | 1..9 Toggle flags |
shift + o Open current page in GitHub | a Toggle all on |
/ or ? Show keyboard shortcuts dialog | c Toggle context lines or commits |
1 |
<?php
|
|
2 |
/**
|
|
3 |
* This file is part of the Shieldon package.
|
|
4 |
*
|
|
5 |
* (c) Terry L. <contact@terryl.in>
|
|
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 <contact@terryl.in>
|
|
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 |
/**
|
|
33 |
* The sandbox for SMTP.
|
|
34 |
*/
|
|
35 |
class Smtp |
|
36 |
{
|
|
37 |
/**
|
|
38 |
* Invoker.
|
|
39 |
*
|
|
40 |
* @param array $args The arguments.
|
|
41 |
*
|
|
42 |
* @return bool
|
|
43 |
*/
|
|
44 | 3 |
public function __invoke(array $args): bool |
45 |
{
|
|
46 | 3 |
return $this->sandbox($args[0], $args[1]); |
47 |
}
|
|
48 |
|
|
49 |
/**
|
|
50 |
* Test SMTP.
|
|
51 |
*
|
|
52 |
* @param array $getParams The GET params passed from tryMessenger method.
|
|
53 |
* @param array $message The message title and body.
|
|
54 |
*
|
|
55 |
* @return bool
|
|
56 |
*/
|
|
57 | 3 |
private function sandbox($getParams, $message) |
58 |
{
|
|
59 | 3 |
$type = ''; |
60 | 3 |
$host = ''; |
61 | 3 |
$user = ''; |
62 | 3 |
$pass = ''; |
63 | 3 |
$port = ''; |
64 | 3 |
$sender = ''; |
65 | 3 |
$recipients = ''; |
66 |
|
|
67 |
$params = [ |
|
68 | 3 |
'type', |
69 |
'host', |
|
70 |
'user', |
|
71 |
'pass', |
|
72 |
'port', |
|
73 |
'sender', |
|
74 |
'recipients', |
|
75 |
];
|
|
76 |
|
|
77 | 3 |
foreach ($params as $param) { |
78 | 3 |
${$param} = $getParams[$param] ?? ''; |
79 |
|
|
80 | 3 |
if (empty(${$param})) { |
81 |
// @codeCoverageIgnoreStart
|
|
82 |
return false; |
|
83 |
// @codeCoverageIgnoreEnd
|
|
84 |
}
|
|
85 |
}
|
|
86 |
|
|
87 | 3 |
if (!$this->checkHost($host)) { |
88 | 3 |
return false; |
89 |
}
|
|
90 |
|
|
91 | 3 |
if (in_array($type, ['ssl', 'tls'])) { |
92 | 3 |
$host = $type . '://' . $host; |
93 |
}
|
|
94 |
|
|
95 | 3 |
$recipients = str_replace("\r", '|', $recipients); |
96 | 3 |
$recipients = str_replace("\n", '|', $recipients); |
97 | 3 |
$recipients = explode('|', $recipients); |
98 |
|
|
99 | 3 |
$messenger = new SmtpTest($user, $pass, $host, (int) $port); |
100 |
|
|
101 | 3 |
foreach ($recipients as $recipient) { |
102 | 3 |
if (filter_var($recipient, FILTER_VALIDATE_EMAIL)) { |
103 | 3 |
$messenger->addRecipient($recipient); |
104 |
}
|
|
105 |
}
|
|
106 |
|
|
107 | 3 |
if (filter_var($sender, FILTER_VALIDATE_EMAIL)) { |
108 | 3 |
$messenger->addSender($sender); |
109 |
}
|
|
110 |
|
|
111 | 3 |
$messenger->setSubject($message['title']); |
112 |
|
|
113 |
// @codeCoverageIgnoreStart
|
|
114 |
try { |
|
115 |
if ($messenger->send($message['body'])) { |
|
116 |
return true; |
|
117 |
}
|
|
118 |
} catch (Exception $e) { |
|
119 |
return false; |
|
120 |
}
|
|
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 | 3 |
private function checkHost(string $host): bool |
132 |
{
|
|
133 |
if ( |
|
134 | 3 |
!filter_var($host, FILTER_VALIDATE_IP) && |
135 | 3 |
!filter_var($host, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) |
136 |
) { |
|
137 | 3 |
return false; |
138 |
}
|
|
139 | 3 |
return true; |
140 |
}
|
|
141 |
}
|
Read our documentation on viewing source code .