1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the ZBateson\MailMimeParser project. |
4
|
|
|
* |
5
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
6
|
|
|
*/ |
7
|
|
|
namespace ZBateson\MailMimeParser\Stream; |
8
|
|
|
|
9
|
|
|
use ZBateson\MailMimeParser\MailMimeParser; |
10
|
|
|
use ZBateson\MailMimeParser\Message\Part\MessagePart; |
11
|
|
|
use ZBateson\MailMimeParser\Message\Part\MimePart; |
12
|
|
|
use ZBateson\MailMimeParser\Message\Part\ParentHeaderPart; |
13
|
|
|
use ZBateson\MailMimeParser\Stream\StreamFactory; |
14
|
|
|
use Psr\Http\Message\StreamInterface; |
15
|
|
|
use GuzzleHttp\Psr7\StreamDecoratorTrait; |
16
|
|
|
use GuzzleHttp\Psr7\AppendStream; |
17
|
|
|
use GuzzleHttp\Psr7; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Writes a MimePart to a resource handle. |
21
|
|
|
* |
22
|
|
|
* The class is responsible for writing out the headers and content of a |
23
|
|
|
* MimePart to an output stream buffer, taking care of encoding and filtering. |
24
|
|
|
* |
25
|
|
|
* @author Zaahid Bateson |
26
|
|
|
*/ |
27
|
|
|
class MessagePartStream implements StreamInterface |
28
|
|
|
{ |
29
|
|
|
use StreamDecoratorTrait; |
30
|
|
|
|
31
|
|
|
protected $streamFactory; |
32
|
|
|
protected $part; |
33
|
|
|
|
34
|
|
|
public function __construct(StreamFactory $sdf, MessagePart $part) |
35
|
|
|
{ |
36
|
|
|
$this->streamFactory = $sdf; |
37
|
|
|
$this->part = $part; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Sets up a mailmimeparser-encode stream filter on the content resource |
42
|
|
|
* handle of the passed MimePart if applicable and returns a reference to |
43
|
|
|
* the filter. |
44
|
|
|
* |
45
|
|
|
* @param MimePart $part |
46
|
|
|
* @return StreamInterface a reference to the appended stream filter or null |
47
|
|
|
*/ |
48
|
|
|
private function getCharsetDecoratorForStream(MessagePart $part, StreamInterface $stream) |
49
|
|
|
{ |
50
|
|
|
$charset = $part->getCharset(); |
51
|
|
|
if (!empty($charset)) { |
52
|
|
|
$decorator = $this->streamFactory->newCharsetStream( |
53
|
|
|
$stream, |
54
|
|
|
$charset, |
55
|
|
|
MailMimeParser::DEFAULT_CHARSET |
56
|
|
|
); |
57
|
|
|
return $decorator; |
58
|
|
|
} |
59
|
|
|
return $stream; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Appends a stream filter on the passed MimePart's content resource handle |
64
|
|
|
* based on the type of encoding for the passed part. |
65
|
|
|
* |
66
|
|
|
* @param MimePart $part |
67
|
|
|
* @param resource $handle |
68
|
|
|
* @param StreamLeftover $leftovers |
|
|
|
|
69
|
|
|
* @return StreamInterface the stream filter |
70
|
|
|
*/ |
71
|
|
|
private function getTransferEncodingDecoratorForStream( |
72
|
|
|
MessagePart $part, |
73
|
|
|
StreamInterface $stream |
74
|
|
|
) { |
75
|
|
|
$encoding = $part->getContentTransferEncoding(); |
76
|
|
|
$decorator = null; |
77
|
|
|
switch ($encoding) { |
78
|
|
|
case 'quoted-printable': |
79
|
|
|
$decorator = $this->streamFactory->newQuotedPrintableStream($stream); |
80
|
|
|
break; |
81
|
|
|
case 'base64': |
82
|
|
|
$decorator = $this->streamFactory->newBase64Stream($stream); |
83
|
|
|
break; |
84
|
|
|
case 'x-uuencode': |
85
|
|
|
$decorator = $this->streamFactory->newUUStream($stream); |
86
|
|
|
$decorator->setFilename($part->getFilename()); |
87
|
|
|
break; |
88
|
|
|
default: |
89
|
|
|
return $stream; |
90
|
|
|
} |
91
|
|
|
return $decorator; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Writes out the content portion of the mime part based on the headers that |
96
|
|
|
* are set on the part, taking care of character/content-transfer encoding. |
97
|
|
|
* |
98
|
|
|
* @param MessagePart $part |
99
|
|
|
* @param StreamInterface $stream |
100
|
|
|
*/ |
101
|
|
|
public function writePartContentTo(MessagePart $part, StreamInterface $stream) |
102
|
|
|
{ |
103
|
|
|
$contentStream = $part->getContentStream(); |
104
|
|
|
if ($contentStream !== null) { |
105
|
|
|
$copyStream = $this->streamFactory->newNonClosingStream($stream); |
106
|
|
|
$es = $this->getTransferEncodingDecoratorForStream( |
107
|
|
|
$part, |
108
|
|
|
$copyStream |
109
|
|
|
); |
110
|
|
|
$cs = $this->getCharsetDecoratorForStream($part, $es); |
111
|
|
|
Psr7\copy_to_stream($contentStream, $cs); |
|
|
|
|
112
|
|
|
$cs->close(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function getBoundaryAndChildStreams(ParentHeaderPart $part) |
117
|
|
|
{ |
118
|
|
|
$streams = []; |
119
|
|
|
$boundary = $part->getHeaderParameter('Content-Type', 'boundary'); |
120
|
|
|
foreach ($part->getChildParts() as $child) { |
121
|
|
|
if ($boundary !== null) { |
122
|
|
|
$streams[] = Psr7\stream_for("\r\n--$boundary\r\n"); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
$streams[] = $child->getStream(); |
125
|
|
|
} |
126
|
|
|
if ($boundary !== null) { |
|
|
|
|
127
|
|
|
$streams[] = Psr7\stream_for("\r\n--$boundary--\r\n"); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function getStreamsArray() |
132
|
|
|
{ |
133
|
|
|
$content = Psr7\stream_for('php://temp'); |
|
|
|
|
134
|
|
|
$this->writePartContentTo($this->part, $content); |
135
|
|
|
$content->rewind(); |
136
|
|
|
$streams = [ new HeaderStream($this->part), $content ]; |
137
|
|
|
|
138
|
|
|
if ($this->part instanceof ParentHeaderPart) { |
139
|
|
|
$streams = array_merge($streams, $this->getBoundaryAndChildStreams($this->part)); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $streams; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Creates the underlying stream lazily when required. |
147
|
|
|
* |
148
|
|
|
* @return StreamInterface |
149
|
|
|
*/ |
150
|
|
|
protected function createStream() |
151
|
|
|
{ |
152
|
|
|
return new AppendStream($this->getStreamsArray()); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths