Passed
Push — master ( 79bb7f...0baf9e )
by Andreas
24:25
created

org_openpsa_mail_message::_process_attachments()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 8.7414

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
c 2
b 0
f 0
nc 4
nop 1
dl 0
loc 10
ccs 2
cts 6
cp 0.3333
crap 8.7414
rs 10
1
<?php
2
/**
3
 * @package org.openpsa.mail
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
use Symfony\Component\Mime\Email;
10
use Symfony\Component\Mime\Part\DataPart;
11
use Symfony\Component\Mime\MimeTypes;
12
13
/**
14
 * Wrapper class for emails
15
 *
16
 * @package org.openpsa.mail
17
 */
18
class org_openpsa_mail_message
19
{
20
    private $_to;
21
22
    private $_encoding;
23
24
    private $_headers;
25
26
    private $_body;
27
    private $_html_body;
28
29
    /**
30
     * @var Email
31
     */
32
    private $_message;
33
34 8
    public function __construct($to, array $headers, string $encoding)
35
    {
36 8
        $this->_to = $to;
37 8
        $this->_headers = $headers;
38 8
        $this->_encoding = $encoding;
39
40 8
        $this->_message = new Email;
41
    }
42
43 8
    public function get_recipients()
44
    {
45 8
        return $this->_to;
46
    }
47
48
    public function get_message() : Email
49
    {
50
        // set headers
51
        $msg_headers = $this->_message->getHeaders();
52
53
        foreach ($this->get_headers() as $name => $value) {
54
            if (is_string($value) && in_array(strtolower($name), ["from", "to", "cc", "bcc", "reply-to"])) {
55
                $value = [$value];
56
            }
57
            $msg_headers->addHeader($name, $value);
58
        }
59
60
        // somehow we need to set the body after the headers...
61
        if (!empty($this->_html_body)) {
62
            $this->_message->html($this->_html_body);
63
        }
64
        $this->_message->text($this->_body);
65
66
        return $this->_message;
67
    }
68
69
    public function set_header_field(string $name, $value)
70
    {
71
        $this->_headers[$name] = $value;
72
    }
73
74 8
    public function get_headers() : array
75
    {
76 8
        reset($this->_headers);
77 8
        foreach ($this->_headers as $header => $value) {
78 8
            if (is_string($value)) {
79 8
                $this->_headers[$header] = trim($value);
80
            }
81
        }
82
83 8
        return $this->_headers;
84
    }
85
86 8
    public function get_body()
87
    {
88 8
        return $this->_body;
89
    }
90
91 6
    public function set_body($body)
92
    {
93 6
        $this->_body = $body;
94 6
        $this->_html_body = null;
95
    }
96
97 2
    public function set_html_body(string $body, string $altBody, array $attachments, bool $do_image_embedding)
98
    {
99 2
        $this->_body = $altBody;
100 2
        $this->_html_body = $body;
101
102
        // adjust html body
103 2
        if ($do_image_embedding) {
104 1
            $this->_embed_images();
105
        }
106
107
        // process attachments
108 2
        $this->_process_attachments($attachments);
109
    }
110
111 1
    private function _embed_images()
112
    {
113
        // anything with SRC = "" something in it (images etc)
114 1
        $regExp_src = "/(src|background)=([\"'�])(((https?|ftp):\/\/)?(.*?))\\2/i";
115 1
        preg_match_all($regExp_src, $this->_html_body, $matches_src);
116 1
        debug_print_r("matches_src:", $matches_src);
117
118 1
        $matches = [
119 1
            "whole" => $matches_src[0],
120 1
            "uri" => $matches_src[3],
121 1
            "proto" => $matches_src[4],
122 1
            "location" => $matches_src[6]
123
        ];
124
125 1
        foreach ($matches["whole"] as $key => $match) {
126
            $location = $matches["location"][$key];
127
            // uri is fully qualified
128
            if ($matches['proto'][$key]) {
129
                $uri = $matches["uri"][$key];
130
            }
131
            // uri is absolute
132
            elseif (str_starts_with($location, '/')) {
133
                $uri = midcom::get()->get_host_name() . $location;
134
            } else {
135
                debug_add('No usable uri found, skipping embed', MIDCOM_LOG_WARN);
136
                continue;
137
            }
138
139
            // replace src by embedded image
140
            $name = basename($uri);
141
            $mimetype = null;
142
            if ($ext = pathinfo($name, PATHINFO_EXTENSION)) {
143
                $mimetype = (new MimeTypes)->getMimeTypes($ext)[0] ?? null;
0 ignored issues
show
Bug introduced by
It seems like $ext can also be of type array; however, parameter $ext of Symfony\Component\Mime\MimeTypes::getMimeTypes() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

143
                $mimetype = (new MimeTypes)->getMimeTypes(/** @scrutinizer ignore-type */ $ext)[0] ?? null;
Loading history...
144
            }
145
146
            $part = (new DataPart(fopen($uri, 'r'), $name, $mimetype))
147
                ->asInline();
148
            $this->_message->attachPart($part);
149
150
            $new_html = str_replace($location, 'cid:' . $part->getContentId(), $match);
151
            $this->_html_body = str_replace($match, $new_html, $this->_html_body);
152
        }
153
    }
154
155 2
    private function _process_attachments(array $attachments)
156
    {
157 2
        foreach ($attachments as $att) {
158
            // we got a file path
159
            if (!empty($att['file'])) {
160
                $this->_message->attachFromPath($att['file'], $att['name'] ?? basename($att['file']), $att['mimetype'] ?? null);
161
            }
162
            // we got the contents (bytes)
163
            elseif (!empty($att['content'])) {
164
                $this->_message->attach($att['content'], $att['name'], $att['mimetype'] ?? null);
165
            }
166
        }
167
    }
168
}
169