/
mime_parser.php
318 lines (283 loc) · 9.1 KB
/
mime_parser.php
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
// CubicleSoft PHP MIME Parser class.
// (C) 2016 CubicleSoft. All Rights Reserved.
// Load dependencies.
if (!class_exists("UTF8", false)) require_once str_replace("\\", "/", dirname(__FILE__)) . "/utf8.php";
class MIMEParser
{
// Reduce dependencies. Duplicates code though.
private static function ReplaceNewlines($replacewith, $data)
{
$data = str_replace("\r\n", "\n", $data);
$data = str_replace("\r", "\n", $data);
$data = str_replace("\n", $replacewith, $data);
return $data;
}
// RFC1341 is a hacky workaround to allow 8-bit over 7-bit transport.
// Also known as "Quoted Printable".
public static function ConvertFromRFC1341($data)
{
$result = "";
$data = self::ReplaceNewlines("\r\n", $data);
$data = explode("\r\n", $data);
$y = count($data);
for ($x = 0; $x < $y; $x++)
{
$currline = $data[$x];
do
{
$pos = strpos($currline, "=");
if ($pos !== false)
{
if ($pos == strlen($currline) - 1)
{
$result .= substr($currline, 0, $pos);
$x++;
$currline = $data[$x];
}
else if ($pos <= strlen($currline) - 3)
{
$result .= substr($currline, 0, $pos) . chr(hexdec(substr($currline, $pos + 1, 2)));
$currline = substr($currline, $pos + 3);
}
else
{
$x++;
$currline = $data[$x];
}
}
else
{
$result .= $currline;
$currline = "";
}
} while ($currline != "");
$result .= "\r\n";
}
$result = rtrim($result) . "\r\n";
return $result;
}
// RFC1342 is a hacky workaround to encode headers in e-mails.
// This function decodes RFC1342 to UTF-8.
public static function ConvertFromRFC1342($data)
{
$pos = strpos($data, "=?");
$pos2 = ($pos !== false ? strpos($data, "?", (int)$pos + 2) : false);
$pos3 = ($pos2 !== false ? strpos($data, "?", (int)$pos2 + 1) : false);
$pos4 = ($pos3 !== false ? strpos($data, "?=", (int)$pos3 + 1) : false);
while ($pos !== false && $pos2 !== false && $pos3 !== false && $pos4 !== false)
{
$encoding = strtoupper(substr($data, $pos + 2, $pos2 - $pos - 2));
$type = strtoupper(substr($data, $pos2 + 1, $pos3 - $pos2 - 1));
$data2 = substr($data, $pos3 + 1, $pos4 - $pos3 - 1);
if ($type != "B" && $type != "Q") $data2 = "";
else
{
if ($type == "B") $data2 = base64_decode($data2);
else $data2 = self::ConvertFromRFC1341($data2);
$data3 = self::ConvertCharset($data2, $encoding, "UTF-8");
if ($data3 !== false) $data2 = $data3;
$data2 = UTF8::MakeValid(trim($data2));
}
$data = substr($data, 0, $pos) . $data2 . substr($data, $pos4 + 2);
$pos = strpos($data, "=?");
$pos2 = ($pos !== false ? strpos($data, "?", (int)$pos + 2) : false);
$pos3 = ($pos2 !== false ? strpos($data, "?", (int)$pos2 + 1) : false);
$pos4 = ($pos3 !== false ? strpos($data, "?=", (int)$pos3 + 1) : false);
}
return $data;
}
// Convert between character sets (mostly UTF-8, ISO-8859-1, and ASCII) using PHP's functions.
// Returns false on failure.
public static function ConvertCharset($data, $incharset, $outcharset)
{
$result = false;
$incharset = strtoupper($incharset);
$outcharset = strtoupper($outcharset);
if ($incharset == $outcharset) return $data;
if ($incharset == "UTF-8") $data = UTF8::MakeValid($data);
if (function_exists("iconv"))
{
// Try transliteration, regular, and then ignore in that order.
$result = iconv($incharset, $outcharset . "//TRANSLIT", $data);
if ($result === false) $result = iconv($incharset, $outcharset, $data);
if ($result === false) $result = iconv($incharset, $outcharset . "//IGNORE", $data);
}
if ($result === false && function_exists("mb_convert_encoding"))
{
$result = @mb_convert_encoding($data, $outcharset, $incharset);
if ($data != "" && $result == "") $result = false;
}
if ($result === false)
{
if ($incharset == "ISO-8859-1" && $outcharset == "UTF-8") $result = utf8_encode($data);
else if ($incharset == "UTF-8" && $outcharset == "ISO-8859-1") $result = utf8_decode($data);
if ($data != "" && $result == "") $result = false;
}
return $result;
}
public static function ExplodeHeader($data)
{
$result = array();
$data = trim($data);
while ($data != "")
{
// Extract name/value pair.
$pos = strpos($data, "=");
$pos2 = strpos($data, ";");
if (($pos !== false && $pos2 === false) || ($pos !== false && $pos2 !== false && $pos < $pos2))
{
$name = trim(substr($data, 0, $pos));
$data = trim(substr($data, $pos + 1));
if (ord($data[0]) == ord("\""))
{
$pos = strpos($data, "\"", 1);
if ($pos !== false)
{
$value = substr($data, 1, $pos - 1);
$data = trim(substr($data, $pos + 1));
$pos = strpos($data, ";");
if ($pos !== false) $data = substr($data, $pos + 1);
else $data = "";
}
else
{
$value = $data;
$data = "";
}
}
else
{
$pos = strpos($data, ";");
if ($pos !== false)
{
$value = trim(substr($data, 0, $pos));
$data = substr($data, $pos + 1);
}
else
{
$value = $data;
$data = "";
}
}
}
else if ($pos2 !== false)
{
$name = "";
$value = trim(substr($data, 0, $pos2));
$data = substr($data, $pos2 + 1);
}
else
{
$name = "";
$value = $data;
$data = "";
}
if ($name != "" || $value != "") $result[strtolower($name)] = $value;
$data = trim($data);
}
return $result;
}
public static function Parse($data, $depth = 0)
{
$result = array();
if ($depth == 10) return $result;
// Extract headers.
$space = ord(" ");
$tab = ord("\t");
$data = self::ReplaceNewlines("\r\n", $data);
$data = explode("\r\n", $data);
$y = count($data);
$lastheader = "";
for ($x = 0; $x < $y; $x++)
{
$currline = rtrim($data[$x]);
if ($currline == "") break;
$TempChr = ord($currline[0]);
if ($TempChr == $space || $TempChr == $tab)
{
if ($lastheader != "") $result["headers"][$lastheader][count($result["headers"][$lastheader]) - 1] .= " " . self::ConvertFromRFC1342(ltrim($currline));
}
else
{
$pos = strpos($currline, ":");
if ($pos !== false)
{
$lastheader = strtolower(substr($currline, 0, $pos));
if (!isset($result["headers"][$lastheader])) $result["headers"][$lastheader] = array();
$result["headers"][$lastheader][] = self::ConvertFromRFC1342(ltrim(substr($currline, $pos + 1)));
}
}
}
// Extract body.
$data = implode("\r\n", array_slice($data, $x + 1));
if (isset($result["headers"]["content-transfer-encoding"]))
{
$encoding = self::ExplodeHeader($result["headers"]["content-transfer-encoding"][0]);
if (isset($encoding[""]))
{
if ($encoding[""] == "base64") $data = base64_decode(preg_replace("/\\s/", "", $data));
else if ($encoding[""] == "quoted-printable") $data = self::ConvertFromRFC1341($data);
}
}
// Process body for more MIME content.
if (!isset($result["headers"]["content-type"]))
{
$result["body"] = UTF8::MakeValid($data);
$result["mime"] = array();
}
else
{
$contenttype = self::ExplodeHeader($result["headers"]["content-type"][0]);
if (array_key_exists("charset", $contenttype))
{
$data2 = self::ConvertCharset($data, $contenttype["charset"], "UTF-8");
if ($data2 !== false) $data = $data2;
$data = UTF8::MakeValid($data);
}
if (!isset($contenttype["boundary"]))
{
$result["body"] = $data;
$result["mime"] = array();
}
else
{
$pos = strpos($data, "--" . $contenttype["boundary"]);
if ($pos !== false && !$pos) $data = "\r\n" . $data;
$data = explode("\r\n--" . $contenttype["boundary"], $data);
$result["body"] = UTF8::MakeValid($data[0]);
$result["mime"] = array();
$y = count($data);
for ($x = 1; $x < $y; $x++)
{
if (substr($data[$x], 0, 2) != "--") $result["mime"][$x - 1] = self::Parse(ltrim($data[$x]), $depth + 1);
else break;
}
}
}
return $result;
}
public static function ExtractContent($message, $depth = 0)
{
$result = array();
if ($depth == 10) return $result;
if (!$depth) $result["text/plain"] = $message["body"];
if (!isset($message["headers"]["content-type"])) $result["text/plain"] = $message["body"];
else
{
$contenttype = self::ExplodeHeader($message["headers"]["content-type"][0]);
if (!isset($contenttype[""])) $result["text/plain"] = $message["body"];
else if (strtolower($contenttype[""]) == "text/plain") $result["text/plain"] = $message["body"];
else if (strtolower($contenttype[""]) == "text/html") $result["text/html"] = $message["body"];
$y = count($message["mime"]);
for ($x = 0; $x < $y; $x++)
{
$data = self::ExtractContent($message["mime"][$x], $depth + 1);
if (isset($data["text/plain"])) $result["text/plain"] = $data["text/plain"];
if (isset($data["text/html"])) $result["text/html"] = $data["text/html"];
}
}
return $result;
}
}
?>