Passed
Push — 2.x ( c080f0...5968ae )
by Terry
02:07
created

Smtp   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 83
rs 10
c 2
b 0
f 0
wmc 17

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 3 1
A checkHost() 0 9 3
C sandbox() 0 43 13
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 as Messenger;
16
17
/**
18
 * The sandbox for SMTP.
19
 */
20
class Smtp
21
{
22
    /**
23
     * Invoker.
24
     *
25
     * @param array $args
26
     *
27
     * @return bool
28
     */
29
    public function __invoke(array $args): bool
30
    {
31
        return $this->sandbox($args[0], $args[1]);
32
    }
33
34
    /**
35
     * Test SMTP.
36
     *
37
     * @param array $getParams The GET params passed from tryMessenger method.
38
     * @param array $message   The message title and body.
39
     *
40
     * @return bool
41
     */
42
    private function sandbox($getParams, $message)
43
    {
44
        $type = $getParams['type'] ?? '';
45
        $host = $getParams['host'] ?? '';
46
        $user = $getParams['user'] ?? '';
47
        $pass = $getParams['pass'] ?? '';
48
        $port = $getParams['port'] ?? '';
49
50
        $sender = $getParams['sender'] ?? '';
51
        $recipients = $getParams['recipients'] ?? '';
52
53
        if (!$this->checkHost($host) || !is_numeric($port) || empty($user) || empty($pass)) {
54
            return false;
55
        }
56
57
        if ('ssl' === $type || 'tls' === $type) {
58
            $host = $type . '://' . $host;
59
        }
60
61
        if (!empty($sender) && $recipients) {
62
            $recipients = str_replace("\r", '|', $recipients);
63
            $recipients = str_replace("\n", '|', $recipients);
64
            $recipients = explode('|', $recipients);
65
66
            $messenger = new Messenger\Smtp($user, $pass, $host, (int) $port);
67
68
            foreach($recipients as $recipient) {
69
                if (filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
70
                    $messenger->addRecipient($recipient);
71
                }
72
            }
73
74
            if (filter_var($sender, FILTER_VALIDATE_EMAIL)) {
75
                $messenger->addSender($sender);
76
            }
77
78
            $messenger->setSubject($message['title']);
79
80
            if ($messenger->send($message['body'])) {
81
                return true;
82
            }
83
        }
84
        return false;
85
    }
86
87
    /**
88
     * Check the SMTP host.
89
     *
90
     * @param string $host The IP address or server domain name.
91
     *
92
     * @return bool
93
     */
94
    private function checkHost(string $host): bool
95
    {
96
        if (
97
            !filter_var($host, FILTER_VALIDATE_IP) && 
98
            !filter_var($host, FILTER_VALIDATE_DOMAIN)
99
        ) {
100
            return false;
101
        }
102
        return true;
103
    }
104
}