|
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 ArrayIterator; |
|
10
|
|
|
use GuzzleHttp\Psr7; |
|
11
|
|
|
use GuzzleHttp\Psr7\StreamDecoratorTrait; |
|
12
|
|
|
use Psr\Http\Message\StreamInterface; |
|
13
|
|
|
use ZBateson\MailMimeParser\Message\Part\ParentHeaderPart; |
|
14
|
|
|
use ZBateson\MailMimeParser\Message\Part\MessagePart; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Psr7 stream decorator implementation providing a readable stream for a part's |
|
18
|
|
|
* headers. |
|
19
|
|
|
* |
|
20
|
|
|
* HeaderStream is only used by a MimePart parent. It can accept any |
|
21
|
|
|
* MessagePart - for non-MimeParts, only type headers are generated based on |
|
22
|
|
|
* available information. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Zaahid Bateson |
|
25
|
|
|
*/ |
|
26
|
|
|
class HeaderStream implements StreamInterface |
|
27
|
|
|
{ |
|
28
|
|
|
use StreamDecoratorTrait; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var MessagePart the part to read from. |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $part; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param MessagePart $part |
|
37
|
|
|
*/ |
|
38
|
2 |
|
public function __construct(MessagePart $part) |
|
39
|
|
|
{ |
|
40
|
2 |
|
$this->part = $part; |
|
41
|
2 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Returns a header array for the current part. |
|
45
|
|
|
* |
|
46
|
|
|
* If the part is not a MimePart, Content-Type, Content-Disposition and |
|
47
|
|
|
* Content-Transfer-Encoding headers are generated manually. |
|
48
|
|
|
* |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
2 |
|
private function getPartHeadersIterator() |
|
52
|
|
|
{ |
|
53
|
2 |
|
if ($this->part instanceof ParentHeaderPart) { |
|
54
|
1 |
|
return $this->part->getRawHeaderIterator(); |
|
55
|
1 |
|
} elseif ($this->part->getParent() !== null && $this->part->getParent()->isMime()) { |
|
56
|
1 |
|
return new ArrayIterator([ |
|
57
|
1 |
|
[ 'Content-Type', $this->part->getContentType() ], |
|
58
|
1 |
|
[ 'Content-Disposition', $this->part->getContentDisposition() ], |
|
59
|
1 |
|
[ 'Content-Transfer-Encoding', $this->part->getContentTransferEncoding() ] |
|
60
|
|
|
]); |
|
61
|
|
|
} |
|
62
|
1 |
|
return new ArrayIterator(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Writes out headers for $this->part and follows them with an empty line. |
|
67
|
|
|
* |
|
68
|
|
|
* @param StreamInterface $stream |
|
69
|
|
|
*/ |
|
70
|
2 |
|
public function writePartHeadersTo(StreamInterface $stream) |
|
71
|
|
|
{ |
|
72
|
2 |
|
foreach ($this->getPartHeadersIterator() as $header) { |
|
73
|
2 |
|
$stream->write("${header[0]}: ${header[1]}\r\n"); |
|
74
|
|
|
} |
|
75
|
2 |
|
$stream->write("\r\n"); |
|
76
|
2 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Creates the underlying stream lazily when required. |
|
80
|
|
|
* |
|
81
|
|
|
* @return StreamInterface |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function createStream() |
|
84
|
|
|
{ |
|
85
|
2 |
|
$stream = Psr7\stream_for(); |
|
|
|
|
|
|
86
|
2 |
|
$this->writePartHeadersTo($stream); |
|
87
|
2 |
|
$stream->rewind(); |
|
88
|
2 |
|
return $stream; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|