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
|
<?php
#
# forum-list bridge
# Copyright (C) 2010 Joel Uckelman
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
require_once(__DIR__ . '/Message.php');
abstract class EmailMessage implements Message {
protected $data;
protected $msg;
protected $parts;
public function __construct($input) {
$this->data = &$input;
# build the message structure
$this->msg = mailparse_msg_create();
mailparse_msg_parse($this->msg, $this->data);
# get the part data
$this->parts = array();
foreach (mailparse_msg_get_structure($this->msg) as $part_id) {
$part = mailparse_msg_get_part($this->msg, $part_id);
$this->parts[$part_id] = mailparse_msg_get_part_data($part);
}
}
public function __destruct() {
mailparse_msg_free($this->msg);
}
public function getPostId() {
return null;
}
protected function getHeader($name) {
return $this->getPartHeader($this->parts[1], $name);
}
protected function getPartHeaders(&$part_data) {
$headers = &$part_data['headers'];
return $headers;
}
protected function getPartHeader(&$part_data, $name) {
$headers = $this->getPartHeaders($part_data);
return array_key_exists($name, $headers) ? $headers[$name] : false;
}
public function getFrom() {
$from = mailparse_rfc822_parse_addresses($this->getHeader('from'));
return $from[0]['address'];
}
public function getSubject() {
return $this->getHeader('subject');
}
public function getMessageId() {
return $this->getHeader('message-id');
}
public function getInReplyTo() {
$irt = $this->getHeader('in-reply-to');
// Try to get a message id from the In-Reply-To header
foreach (mailparse_rfc822_parse_addresses($irt) as $part) {
if (isset($part['address']) && strlen($part['address']) > 0) {
return '<' . $part['address'] . '>';
}
}
// Ack, no message id, just return the raw header
return $irt;
}
public function getReferences() {
return $this->getHeader('references');
}
public function getParts() {
}
protected function getPartBody(&$part_data) {
$beg = $part_data['starting-pos-body'];
$end = $part_data['ending-pos-body'];
return substr($this->data, $beg, $end-$beg);
}
protected function decode($str, $encoding) {
if ($encoding == 'base64') {
return base64_decode($str);
}
else if ($encoding == 'quoted-printable') {
return quoted_printable_decode($str);
}
else {
return $str;
}
}
public function getFlattenedParts() {
$text = '';
$attachments = array();
$this->flatten_parts('1', $this->parts[1], $text, $attachments);
return array($text, $attachments);
}
protected function flatten_subparts($part_id, &$text, &$attachments) {
for ($i = 1, $child_id = "$part_id.$i";
array_key_exists($child_id, $this->parts);
++$i, $child_id = "$part_id.$i")
{
$child = $this->parts[$child_id];
$this->flatten_parts($child_id, $child, $text, $attachments);
}
}
protected function flatten_parts($part_id, &$part_data,
&$text, &$attachments) {
$type = $part_data['content-type'];
list($major, $minor) = split('/', $type, 2);
switch ($major) {
case 'multipart':
switch ($minor) {
case 'alternative':
# check alternatives for text/plain
$plain = false;
for ($i = 1, $child_id = "$part_id.$i";
array_key_exists($child_id, $this->parts);
++$i, $child_id = "$part_id.$i")
{
$child = $this->parts[$child_id];
$ctype = $child['content-type'];
if ($ctype == 'text/plain') {
# keep text/plain, chuck the rest
$this->flatten_parts($child_id, $child, $text, $attachments);
$plain = true;
break;
}
}
if (!$plain) {
# no text/plain, handle the subparts as attachments
$this->flatten_subparts($part_id, $text, $attachments);
}
break;
case 'mixed':
default:
# handle all subparts
$this->flatten_subparts($part_id, $text, $attachments);
break;
}
break;
case 'text':
# NB: We don't worry about text/html here because Mailman will have
# already stripped it.
# Text is appended to the main text.
$enc = $this->getPartHeader($part_data, 'content-transfer-encoding');
$body = $this->getPartBody($part_data);
$body = $this->decode($body, $enc);
$charset = $part_data['content-charset'];
if (strtoupper($charset) != 'UTF-8') {
if (mb_check_encoding($body, $charset)) {
$body = mb_convert_encoding($body, 'UTF-8', $charset);
}
else {
$body = mb_convert_encoding($body, 'UTF-8');
}
}
$text .= "$body\n";
break;
default:
# Everything else goes into phpBB as an attachment.
$enc = $this->getPartHeader($part_data, 'content-transfer-encoding');
$data = $this->getPartBody($part_data);
$data = $this->decode($data, $enc);
$disp = $part_data['content-disposition'];
if ($disp == 'attachment' || $disp == 'inline') {
$attachments[] = array(
'filename' => $part_data['disposition-filename'],
'mimetype' => $part_data['content-type'],
'comment' => $part_data['content-description'],
'data' => $data
);
}
}
}
}
?>
|