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\Message\Part\ParentHeaderPart; |
10
|
|
|
use ZBateson\MailMimeParser\Message\Part\MessagePart; |
11
|
|
|
use Psr\Http\Message\StreamInterface; |
12
|
|
|
use GuzzleHttp\Psr7\StreamDecoratorTrait; |
13
|
|
|
use GuzzleHttp\Psr7; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Psr7 stream decorator implementation providing a readable stream for a part's |
17
|
|
|
* headers. |
18
|
|
|
* |
19
|
|
|
* HeaderStream is only used by a MimePart parent. It can accept any |
20
|
|
|
* MessagePart - for non-MimeParts, only type headers are generated based on |
21
|
|
|
* available information. |
22
|
|
|
* |
23
|
|
|
* @author Zaahid Bateson |
24
|
|
|
*/ |
25
|
|
|
class HeaderStream implements StreamInterface |
26
|
|
|
{ |
27
|
|
|
use StreamDecoratorTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var MessagePart the part to read from. |
31
|
|
|
*/ |
32
|
|
|
protected $part; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param MessagePart $part |
36
|
|
|
*/ |
37
|
2 |
|
public function __construct(MessagePart $part) |
38
|
|
|
{ |
39
|
2 |
|
$this->part = $part; |
40
|
2 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns a header array for the current part. |
44
|
|
|
* |
45
|
|
|
* If the part is not a MimePart, Content-Type, Content-Disposition and |
46
|
|
|
* Content-Transfer-Encoding headers are generated manually. |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
2 |
|
private function getPartHeadersArray() |
51
|
|
|
{ |
52
|
2 |
|
if ($this->part instanceof ParentHeaderPart) { |
53
|
1 |
|
return $this->part->getRawHeaders(); |
54
|
1 |
|
} elseif ($this->part->getParent() !== null && $this->part->getParent()->isMime()) { |
55
|
|
|
return [ |
56
|
1 |
|
[ 'Content-Type', $this->part->getContentType() ], |
57
|
1 |
|
[ 'Content-Disposition', $this->part->getContentDisposition() ], |
58
|
1 |
|
[ 'Content-Transfer-Encoding', $this->part->getContentTransferEncoding() ] |
59
|
|
|
]; |
60
|
|
|
} |
61
|
1 |
|
return []; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Writes out headers for $this->part and follows them with an empty line. |
66
|
|
|
* |
67
|
|
|
* @param StreamInterface $stream |
68
|
|
|
*/ |
69
|
2 |
|
public function writePartHeadersTo(StreamInterface $stream) |
70
|
|
|
{ |
71
|
2 |
|
$headers = $this->getPartHeadersArray($this->part); |
|
|
|
|
72
|
2 |
|
foreach ($headers 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
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.