|
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\Writer; |
|
8
|
|
|
|
|
9
|
|
|
use ZBateson\MailMimeParser\Message; |
|
10
|
|
|
use ZBateson\MailMimeParser\Message\MimePart; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Writes out a message in a mail-compliant format. |
|
14
|
|
|
* |
|
15
|
|
|
* Provides a way of writing out a ZBateson\MailMimeParser\Message object to a |
|
16
|
|
|
* resource handle. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Zaahid Bateson |
|
19
|
|
|
*/ |
|
20
|
|
|
class MessageWriter extends MimePartWriter |
|
21
|
|
|
{ |
|
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Writes out a mime boundary to the passed $handle optionally writing out a |
|
24
|
|
|
* number of empty lines before it. |
|
25
|
|
|
* |
|
26
|
|
|
* @param resource $handle |
|
27
|
|
|
* @param string $boundary |
|
28
|
|
|
* @param int $numLinesBefore |
|
29
|
|
|
* @param bool $isEnd |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function writeBoundary($handle, $boundary, $numLinesBefore, $isEnd) |
|
32
|
|
|
{ |
|
33
|
|
|
if ($numLinesBefore > 0) { |
|
34
|
|
|
fwrite($handle, str_repeat("\r\n", $numLinesBefore)); |
|
35
|
|
|
} |
|
36
|
|
|
fwrite($handle, '--'); |
|
37
|
|
|
fwrite($handle, $boundary); |
|
38
|
|
|
if ($isEnd) { |
|
39
|
|
|
fwrite($handle, "--\r\n"); |
|
40
|
|
|
} else { |
|
41
|
|
|
fwrite($handle, "\r\n"); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Writes out headers and content for the passed MimePart, then loops over |
|
47
|
|
|
* its child parts calling recursiveWriteParts on each part. |
|
48
|
|
|
* |
|
49
|
|
|
* @param MimePart $part the current part to write out |
|
50
|
|
|
* @param resource $handle the handle to write out to |
|
51
|
|
|
* @return bool true if the part had children (and ended with writing a |
|
52
|
|
|
* boundary) |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function recursiveWriteParts(MimePart $part, $handle) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->writePartHeadersTo($part, $handle); |
|
57
|
|
|
$this->writePartContentTo($part, $handle); |
|
58
|
|
|
$ended = false; |
|
59
|
|
|
$boundary = $part->getHeaderParameter('Content-Type', 'boundary'); |
|
60
|
|
|
foreach ($part->getChildParts() as $i => $child) { |
|
61
|
|
|
if ($boundary !== null) { |
|
62
|
|
|
$numLines = ($i !== 0 && !$ended) ? 2 : (int) $ended; |
|
63
|
|
|
$this->writeBoundary($handle, $boundary, $numLines, false); |
|
64
|
|
|
} |
|
65
|
|
|
$ended = $this->recursiveWriteParts($child, $handle); |
|
66
|
|
|
} |
|
67
|
|
|
if ($boundary !== null) { |
|
68
|
|
|
$this->writeBoundary($handle, $boundary, ($ended) ? 1 : 2, true); |
|
69
|
|
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Saves the message as a MIME message to the passed resource handle. |
|
76
|
|
|
* |
|
77
|
|
|
* @param Message $message |
|
78
|
|
|
* @param resource $handle |
|
79
|
|
|
*/ |
|
80
|
|
|
public function writeMessageTo(Message $message, $handle) |
|
81
|
|
|
{ |
|
82
|
|
|
if ($message->isMime()) { |
|
83
|
|
|
$this->recursiveWriteParts($message, $handle); |
|
84
|
|
|
} else { |
|
85
|
|
|
$this->writePartHeadersTo($message, $handle); |
|
86
|
|
|
foreach ($message->getChildParts() as $i => $child) { |
|
87
|
|
|
if ($i !== 0) { |
|
88
|
|
|
fwrite($handle, "\r\n\r\n"); |
|
89
|
|
|
} |
|
90
|
|
|
$this->writePartContentTo($child, $handle); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Returns the content part of a signed message for a signature to be |
|
97
|
|
|
* calculated on the message. |
|
98
|
|
|
* |
|
99
|
|
|
* @param Message $message |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getSignableBody(Message $message) |
|
103
|
|
|
{ |
|
104
|
|
|
if (!$message->isMime()) { |
|
105
|
|
|
return null; |
|
106
|
|
|
} |
|
107
|
|
|
$handle = fopen('php://temp', 'r+'); |
|
108
|
|
|
$ended = $this->recursiveWriteParts($message->getChild(0), $handle); |
|
|
|
|
|
|
109
|
|
|
rewind($handle); |
|
110
|
|
|
$str = stream_get_contents($handle); |
|
111
|
|
|
fclose($handle); |
|
112
|
|
|
if (!$ended) { |
|
113
|
|
|
$str .= "\r\n"; |
|
114
|
|
|
} |
|
115
|
|
|
return $str; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|