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\Message\Helper; |
8
|
|
|
|
9
|
|
|
use ZBateson\MailMimeParser\Message; |
10
|
|
|
use ZBateson\MailMimeParser\Message\Part\Factory\MimePartFactory; |
11
|
|
|
use ZBateson\MailMimeParser\Message\Part\Factory\PartBuilderFactory; |
12
|
|
|
use ZBateson\MailMimeParser\Message\Part\Factory\UUEncodedPartFactory; |
13
|
|
|
use ZBateson\MailMimeParser\Message\PartFilter; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Provides routines to set or retrieve the signature part of a signed message. |
17
|
|
|
* |
18
|
|
|
* @author Zaahid Bateson |
19
|
|
|
*/ |
20
|
|
|
class PrivacyHelper extends AbstractHelper |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var GenericHelper a GenericHelper instance |
24
|
|
|
*/ |
25
|
|
|
private $genericHelper; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var MultipartHelper a MultipartHelper instance |
29
|
|
|
*/ |
30
|
|
|
private $multipartHelper; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param MimePartFactory $mimePartFactory |
34
|
|
|
* @param UUEncodedPartFactory $uuEncodedPartFactory |
35
|
|
|
* @param PartBuilderFactory $partBuilderFactory |
36
|
|
|
* @param GenericHelper $genericHelper |
37
|
|
|
* @param MultipartHelper $multipartHelper |
38
|
|
|
*/ |
39
|
|
|
public function __construct( |
40
|
|
|
MimePartFactory $mimePartFactory, |
41
|
|
|
UUEncodedPartFactory $uuEncodedPartFactory, |
42
|
|
|
PartBuilderFactory $partBuilderFactory, |
43
|
|
|
GenericHelper $genericHelper, |
44
|
|
|
MultipartHelper $multipartHelper |
45
|
|
|
) { |
46
|
|
|
parent::__construct($mimePartFactory, $uuEncodedPartFactory, $partBuilderFactory); |
47
|
|
|
$this->genericHelper = $genericHelper; |
48
|
|
|
$this->multipartHelper = $multipartHelper; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The passed message is set as multipart/signed, and a new part is created |
53
|
|
|
* below it with content headers, content and children copied from the |
54
|
|
|
* message. |
55
|
|
|
* |
56
|
|
|
* @param Message $message |
57
|
|
|
* @param string $micalg |
58
|
|
|
* @param string $protocol |
59
|
|
|
*/ |
60
|
|
|
public function setMessageAsMultipartSigned(Message $message, $micalg, $protocol) |
61
|
|
|
{ |
62
|
|
|
$this->multipartHelper->enforceMime($message); |
63
|
|
|
$messagePart = $this->partBuilderFactory->newPartBuilder($this->mimePartFactory)->createMessagePart(); |
64
|
|
|
$this->genericHelper->movePartContentAndChildren($message, $messagePart); |
65
|
|
|
$message->addChild($messagePart); |
66
|
|
|
|
67
|
|
|
$boundary = $this->multipartHelper->getUniqueBoundary('multipart/signed'); |
68
|
|
|
$message->setRawHeader( |
69
|
|
|
'Content-Type', |
70
|
|
|
"multipart/signed;\r\n\tboundary=\"$boundary\";\r\n\tmicalg=\"$micalg\"; protocol=\"$protocol\"" |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Sets the signature of the message to $body, creating a signature part if |
76
|
|
|
* one doesn't exist. |
77
|
|
|
* |
78
|
|
|
* @param Message $message |
79
|
|
|
* @param string $body |
80
|
|
|
*/ |
81
|
|
|
public function setSignature(Message $message, $body) |
82
|
|
|
{ |
83
|
|
|
$signedPart = $message->getSignaturePart(); |
84
|
|
|
if ($signedPart === null) { |
85
|
|
|
$signedPart = $this->partBuilderFactory->newPartBuilder($this->mimePartFactory)->createMessagePart(); |
86
|
|
|
$message->addChild($signedPart); |
87
|
|
|
} |
88
|
|
|
$signedPart->setRawHeader( |
|
|
|
|
89
|
|
|
'Content-Type', |
90
|
|
|
$message->getHeaderParameter('Content-Type', 'protocol') |
91
|
|
|
); |
92
|
|
|
$signedPart->setContent($body); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Loops over parts of the message and sets the content-transfer-encoding |
97
|
|
|
* header to quoted-printable for text/* mime parts, and to base64 |
98
|
|
|
* otherwise for parts that are '8bit' encoded. |
99
|
|
|
* |
100
|
|
|
* Used for multipart/signed messages which doesn't support 8bit transfer |
101
|
|
|
* encodings. |
102
|
|
|
* |
103
|
|
|
* @param Message $message |
104
|
|
|
*/ |
105
|
|
|
public function overwrite8bitContentEncoding(Message $message) |
106
|
|
|
{ |
107
|
|
|
$parts = $message->getAllParts(new PartFilter([ |
108
|
|
|
'headers' => [ PartFilter::FILTER_INCLUDE => [ |
109
|
|
|
'Content-Transfer-Encoding' => '8bit' |
110
|
|
|
] ] |
111
|
|
|
])); |
112
|
|
|
foreach ($parts as $part) { |
113
|
|
|
$contentType = strtolower($part->getHeaderValue('Content-Type', 'text/plain')); |
|
|
|
|
114
|
|
|
if ($contentType === 'text/plain' || $contentType === 'text/html') { |
115
|
|
|
$part->setRawHeader('Content-Transfer-Encoding', 'quoted-printable'); |
116
|
|
|
} else { |
117
|
|
|
$part->setRawHeader('Content-Transfer-Encoding', 'base64'); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Ensures a non-text part comes first in a signed multipart/alternative |
124
|
|
|
* message as some clients seem to prefer the first content part if the |
125
|
|
|
* client doesn't understand multipart/signed. |
126
|
|
|
* |
127
|
|
|
* @param Message $message |
128
|
|
|
*/ |
129
|
|
|
public function ensureHtmlPartFirstForSignedMessage(Message $message) |
130
|
|
|
{ |
131
|
|
|
$alt = $message->getPartByMimeType('multipart/alternative'); |
132
|
|
|
if ($alt !== null && $alt instanceof ParentPart) { |
|
|
|
|
133
|
|
|
$cont = $this->multipartHelper->getContentPartContainerFromAlternative('text/html', $alt); |
134
|
|
|
$children = $alt->getChildParts(); |
|
|
|
|
135
|
|
|
$pos = array_search($cont, $children, true); |
136
|
|
|
if ($pos !== false && $pos !== 0) { |
137
|
|
|
$alt->removePart($children[0]); |
|
|
|
|
138
|
|
|
$alt->addChild($children[0]); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|