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 Shieldon\Messenger\Mail as MailTest;
26
use function explode;
27
use function filter_var;
28
use function function_exists;
29
use function str_replace;
30

31
/**
32
 * The sandbox for PHP native mail.
33
 */
34
class NativePhpMail
35
{
36
    /**
37
     * Invoker.
38
     *
39
     * @param array $args The arguments.
40
     *
41
     * @return bool
42
     */
43 3
    public function __invoke(array $args): bool
44
    {
45 3
        return $this->sandbox($args[0], $args[1]);
46
    }
47

48
    /**
49
     * Test PHP native mail.
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 3
    private function sandbox($getParams, $message)
57
    {
58 3
        $sender = $getParams['sender'] ?? '';
59 3
        $recipients = $getParams['recipients'] ?? '';
60

61 3
        if (!empty($sender) && !empty($recipients)) {
62 3
            $recipients = str_replace("\r", '|', $recipients);
63 3
            $recipients = str_replace("\n", '|', $recipients);
64 3
            $recipients = explode('|', $recipients);
65

66 3
            $messenger = new MailTest();
67

68 3
            foreach ($recipients as $recipient) {
69 3
                if (filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
70 3
                    $messenger->addRecipient($recipient);
71
                }
72
            }
73

74 3
            if (filter_var($sender, FILTER_VALIDATE_EMAIL)) {
75 3
                $messenger->addSender($sender);
76
            }
77

78 3
            $messenger->setSubject($message['title']);
79

80 3
            if (!defined('PHP_UNIT_TEST') && $messenger->send($message['body'])) {
81
                // @codeCoverageIgnoreStart
82
                return true;
83
                // @codeCoverageIgnoreEnd
84
            }
85
        }
86

87
        // @codeCoverageIgnoreStart
88
        return false;
89
        // @codeCoverageIgnoreEnd
90
    }
91
}

Read our documentation on viewing source code .

Loading