-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.php
More file actions
230 lines (170 loc) · 5.61 KB
/
Message.php
File metadata and controls
230 lines (170 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
namespace Hfig\MAPI\Message;
use Hfig\MAPI\OLE\CompoundDocumentElement as Element;
use Hfig\MAPI\OLE\Guid\OleGuid;
use Hfig\MAPI\OLE\RTF;
use Hfig\MAPI\Item\Message as MessageItem;
use Hfig\MAPI\Property\PropertyStore;
use Hfig\MAPI\Property\PropertySet;
class Message extends MessageItem
{
const ATTACH_RX = '/^__attach_version1\.0_.*/';
const RECIP_RX = '/^__recip_version1\.0_.*/';
const VALID_RX = PropertyStore::VALID_RX + [
self::ATTACH_RX,
self::RECIP_RX
];
/** @var Element */
protected $obj;
/** @var PropertySet */
protected $properties;
/** @var Message */
protected $parent;
/** @var Attachment[] */
protected $attachments = [];
/** @var Recipient[] */
protected $recipients = [];
protected $bodyPlain;
protected $bodyRTF;
protected $bodyHTML;
public function __construct(Element $obj, Message $parent = null)
{
$this->obj = $obj;
$this->parent = $parent;
$this->properties = new PropertySet(
new PropertyStore($obj, ($parent) ? $parent->getNameId() : null)
);
$this->buildAttachments();
$this->buildRecipients();
}
protected function buildAttachments()
{
foreach ($this->obj->getChildren() as $child) {
if ($child->isDirectory() && preg_match(self::ATTACH_RX, $child->getName())) {
$attachment = new Attachment($child, $this);
if ($attachment->isValid()) {
$this->attachments[] = $attachment;
}
}
}
}
protected function buildRecipients()
{
foreach ($this->obj->getChildren() as $child) {
if ($child->isDirectory() && preg_match(self::RECIP_RX, $child->getName())) {
//echo 'Got child . ' . $child->getName() . "\n";
$recipient = new Recipient($child, $this);
$this->recipients[] = $recipient;
}
}
}
/** @return Attachment[] */
public function getAttachments()
{
return $this->attachments;
}
/** @return Recipient[] */
public function getRecipients()
{
return $this->recipients;
}
public function getRecipientsOfType($type)
{
$response = [];
foreach ($this->recipients as $r) {
if ($r->getType() == $type) {
$response[] = $r;
}
}
return $response;
}
public function getNameId()
{
return $this->properties->getStore()->getNameId();
}
public function getInternetMessageId(): ?string
{
return $this->properties['internet_message_id'] ?? null;
}
public function getBody()
{
if ($this->bodyPlain) return $this->bodyPlain;
if ($this->properties['body']) {
$this->bodyPlain = $this->properties['body'];
}
// parse from RTF
if (!$this->bodyPlain) {
//jstewmc/rtf
throw new \Exception('No Plain Text body. Convert from RTF not implemented');
}
return $this->bodyPlain;
}
public function getBodyRTF()
{
if ($this->bodyRTF) return $this->bodyRTF;
if ($this->properties['rtf_compressed']) {
$this->bodyRTF = RTF\CompressionCodec::decode($this->properties['rtf_compressed']);
}
return $this->bodyRTF;
}
public function getBodyHTML()
{
if ($this->bodyHTML) return $this->bodyHTML;
if ($this->properties['body_html']) {
$this->bodyHTML = $this->properties['body_html'];
if ($this->bodyHTML) {
$this->bodyHTML = trim($this->bodyHTML);
}
}
if (!$this->bodyHTML) {
if ($rtf = $this->getBodyRTF()) {
$this->bodyHTML = RTF\EmbeddedHTML::extract($rtf);
}
if (!$this->bodyHTML) {
//jstewmc/rtf
throw new \Exception('No HTML or Embedded RTF body. Convert from RTF not implemented');
}
}
return $this->bodyHTML;
}
public function getSender()
{
$senderName = $this->properties['sender_name'];
$senderAddr = $this->properties['sender_email_address'];
$senderType = $this->properties['sender_addrtype'];
$from = '';
if ($senderType == 'SMTP') {
$from = $senderAddr;
}
else {
$from = $this->properties['sender_smtp_address'] ??
$this->properties['sender_representing_smtp_address'] ??
// synthesise??
// for now settle on type:address eg X400:<dn>
sprintf('%s:%s', $senderType, $senderAddr);
}
if ($senderName) {
$from = sprintf('%s <%s>', $senderName, $from);
}
return $from;
}
public function getSendTime(): ?\DateTime
{
$sendTime = $this->properties['client_submit_time'];
if (!$sendTime) {
return null;
}
return \DateTime::createFromFormat('U',$sendTime);
}
public function properties(): PropertySet
{
return $this->properties;
}
public function __get($name)
{
if ($name == 'properties') {
return $this->properties;
}
return null;
}
}