-
Notifications
You must be signed in to change notification settings - Fork 1
/
Decode.php
247 lines (226 loc) · 8.83 KB
/
Decode.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
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Mime
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* @namespace
*/
namespace Zend\Mime;
/**
* @uses \Zend\Mime\Exception\RuntimeException
* @uses \Zend\Mime\Mime
* @category Zend
* @package Zend_Mime
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Decode
{
/**
* Explode MIME multipart string into seperate parts
*
* Parts consist of the header and the body of each MIME part.
*
* @param string $body raw body of message
* @param string $boundary boundary as found in content-type
* @return array parts with content of each part, empty if no parts found
* @throws Exception\RuntimeException
*/
public static function splitMime($body, $boundary)
{
// TODO: we're ignoring \r for now - is this function fast enough and is it safe to asume noone needs \r?
$body = str_replace("\r", '', $body);
$start = 0;
$res = array();
// find every mime part limiter and cut out the
// string before it.
// the part before the first boundary string is discarded:
$p = strpos($body, '--' . $boundary . "\n", $start);
if ($p === false) {
// no parts found!
return array();
}
// position after first boundary line
$start = $p + 3 + strlen($boundary);
while (($p = strpos($body, '--' . $boundary . "\n", $start)) !== false) {
$res[] = substr($body, $start, $p-$start);
$start = $p + 3 + strlen($boundary);
}
// no more parts, find end boundary
$p = strpos($body, '--' . $boundary . '--', $start);
if ($p===false) {
throw new Exception\RuntimeException('Not a valid Mime Message: End Missing');
}
// the remaining part also needs to be parsed:
$res[] = substr($body, $start, $p-$start);
return $res;
}
/**
* decodes a mime encoded String and returns a
* struct of parts with header and body
*
* @param string $message raw message content
* @param string $boundary boundary as found in content-type
* @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND}
* @return array|null parts as array('header' => array(name => value), 'body' => content), null if no parts found
* @throws Exception\RuntimeException
*/
public static function splitMessageStruct($message, $boundary, $EOL = Mime::LINEEND)
{
$parts = self::splitMime($message, $boundary);
if (count($parts) <= 0) {
return null;
}
$result = array();
$headers = null; // "Declare" variable before the first usage "for reading"
$body = null; // "Declare" variable before the first usage "for reading"
foreach ($parts as $part) {
self::splitMessage($part, $headers, $body, $EOL);
$result[] = array('header' => $headers,
'body' => $body );
}
return $result;
}
/**
* split a message in header and body part, if no header or an
* invalid header is found $headers is empty
*
* The charset of the returned headers depend on your iconv settings.
*
* @param string $message raw message with header and optional content
* @param array $headers output param, array with headers as array(name => value)
* @param string $body output param, content of message
* @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND}
* @return null
*/
public static function splitMessage($message, &$headers, &$body, $EOL = Mime::LINEEND)
{
// check for valid header at first line
$firstline = strtok($message, "\n");
if (!preg_match('%^[^\s]+[^:]*:%', $firstline)) {
$headers = array();
// TODO: we're ignoring \r for now - is this function fast enough and is it safe to asume noone needs \r?
$body = str_replace(array("\r", "\n"), array('', $EOL), $message);
return;
}
// find an empty line between headers and body
// default is set new line
if (strpos($message, $EOL . $EOL)) {
list($headers, $body) = explode($EOL . $EOL, $message, 2);
// next is the standard new line
} else if ($EOL != "\r\n" && strpos($message, "\r\n\r\n")) {
list($headers, $body) = explode("\r\n\r\n", $message, 2);
// next is the other "standard" new line
} else if ($EOL != "\n" && strpos($message, "\n\n")) {
list($headers, $body) = explode("\n\n", $message, 2);
// at last resort find anything that looks like a new line
} else {
@list($headers, $body) = @preg_split("%([\r\n]+)\\1%U", $message, 2);
}
$headers = iconv_mime_decode_headers($headers, ICONV_MIME_DECODE_CONTINUE_ON_ERROR);
if ($headers === false) {
// an error occurs during the decoding
return;
}
// normalize header names
foreach ($headers as $name => $header) {
$lower = strtolower($name);
if ($lower == $name) {
continue;
}
unset($headers[$name]);
if (!isset($headers[$lower])) {
$headers[$lower] = $header;
continue;
}
if (is_array($headers[$lower])) {
$headers[$lower][] = $header;
continue;
}
$headers[$lower] = array($headers[$lower], $header);
}
}
/**
* split a content type in its different parts
*
* @param string $type content-type
* @param string $wantedPart the wanted part, else an array with all parts is returned
* @return string|array wanted part or all parts as array('type' => content-type, partname => value)
*/
public static function splitContentType($type, $wantedPart = null)
{
return self::splitHeaderField($type, $wantedPart, 'type');
}
/**
* split a header field like content type in its different parts
*
* @param string $type header field
* @param string $wantedPart the wanted part, else an array with all parts is returned
* @param string $firstName key name for the first part
* @return string|array wanted part or all parts as array($firstName => firstPart, partname => value)
* @throws Exception\RuntimeException
*/
public static function splitHeaderField($field, $wantedPart = null, $firstName = 0)
{
$wantedPart = strtolower($wantedPart);
$firstName = strtolower($firstName);
// special case - a bit optimized
if ($firstName === $wantedPart) {
$field = strtok($field, ';');
return $field[0] == '"' ? substr($field, 1, -1) : $field;
}
$field = $firstName . '=' . $field;
if (!preg_match_all('%([^=\s]+)\s*=\s*("[^"]+"|[^;]+)(;\s*|$)%', $field, $matches)) {
throw new Exception\RuntimeException('not a valid header field');
}
if ($wantedPart) {
foreach ($matches[1] as $key => $name) {
if (strcasecmp($name, $wantedPart)) {
continue;
}
if ($matches[2][$key][0] != '"') {
return $matches[2][$key];
}
return substr($matches[2][$key], 1, -1);
}
return null;
}
$split = array();
foreach ($matches[1] as $key => $name) {
$name = strtolower($name);
if ($matches[2][$key][0] == '"') {
$split[$name] = substr($matches[2][$key], 1, -1);
} else {
$split[$name] = $matches[2][$key];
}
}
return $split;
}
/**
* decode a quoted printable encoded string
*
* The charset of the returned string depends on your iconv settings.
*
* @param string encoded string
* @return string decoded string
*/
public static function decodeQuotedPrintable($string)
{
return iconv_mime_decode($string, ICONV_MIME_DECODE_CONTINUE_ON_ERROR);
}
}