newmail...
[squirrelmail.git] / functions / mime.php
CommitLineData
59177427 1<?php
2ba13803 2
35586184 3/**
4 * mime.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains the functions necessary to detect and decode MIME
10 * messages.
11 *
12 * $Id$
13 */
b74ba498 14
35586184 15require_once('../functions/imap.php');
16require_once('../functions/attachment_common.php');
8beafbbc 17
35586184 18/** Setting up the objects that have the structure for the message **/
19class msg_header {
20 /** msg_header contains generic variables for values that **/
21 /** could be in a header. **/
b74ba498 22
88cb1b4d 23 var $type0 = '', $type1 = '', $boundary = '', $charset = '',
24 $encoding = '', $size = 0, $to = array(), $from = '', $date = '',
25 $cc = array(), $bcc = array(), $reply_to = '', $subject = '',
26 $id = 0, $mailbox = '', $description = '', $filename = '',
27 $entity_id = 0, $message_id = 0, $name = '', $priority = 3;
35586184 28}
b74ba498 29
451f74a2 30class message {
31 /** message is the object that contains messages. It is a recursive
32 object in that through the $entities variable, it can contain
33 more objects of type message. See documentation in mime.txt for
34 a better description of how this works.
35 **/
77b88425 36 var $header = '', $entities = array();
37
451f74a2 38 function addEntity ($msg) {
39 $this->entities[] = $msg;
40 }
41}
8beafbbc 42
451f74a2 43/* --------------------------------------------------------------------------------- */
44/* MIME DECODING */
45/* --------------------------------------------------------------------------------- */
b74ba498 46
451f74a2 47/* This function gets the structure of a message and stores it in the "message" class.
48 * It will return this object for use with all relevant header information and
49 * fully parsed into the standard "message" object format.
50 */
51function mime_structure ($imap_stream, $header) {
52
53 sqimap_messages_flag ($imap_stream, $header->id, $header->id, 'Seen');
54 $ssid = sqimap_session_id();
55 $lsid = strlen( $ssid );
56 $id = $header->id;
57 fputs ($imap_stream, "$ssid FETCH $id BODYSTRUCTURE\r\n");
58 //
59 // This should use sqimap_read_data instead of reading it itself
60 //
77b88425 61 $read = fgets ($imap_stream, 9216);
451f74a2 62 $bodystructure = '';
63 while ( substr($read, 0, $lsid) <> $ssid &&
64 !feof( $imap_stream ) ) {
65 $bodystructure .= $read;
77b88425 66 $read = fgets ($imap_stream, 9216);
451f74a2 67 }
68 $read = $bodystructure;
77b88425 69
451f74a2 70 // isolate the body structure and remove beginning and end parenthesis
71 $read = trim(substr ($read, strpos(strtolower($read), 'bodystructure') + 13));
72 $read = trim(substr ($read, 0, -1));
73 $end = mime_match_parenthesis(0, $read);
74 while ($end == strlen($read)-1) {
75 $read = trim(substr ($read, 0, -1));
76 $read = trim(substr ($read, 1));
77 $end = mime_match_parenthesis(0, $read);
78 }
77b88425 79
451f74a2 80 $msg = mime_parse_structure ($read, 0);
81 $msg->header = $header;
77b88425 82
451f74a2 83 return( $msg );
84}
b74ba498 85
451f74a2 86/* this starts the parsing of a particular structure. It is called recursively,
87 * so it can be passed different structures. It returns an object of type
88 * $message.
89 * First, it checks to see if it is a multipart message. If it is, then it
90 * handles that as it sees is necessary. If it is just a regular entity,
91 * then it parses it and adds the necessary header information (by calling out
92 * to mime_get_elements()
93 */
94function mime_parse_structure ($structure, $ent_id) {
95
96 $msg = new message();
97 if ($structure{0} == '(') {
98 $ent_id = mime_new_element_level($ent_id);
99 $start = $end = -1;
100 do {
101 $start = $end+1;
102 $end = mime_match_parenthesis ($start, $structure);
103
104 $element = substr($structure, $start+1, ($end - $start)-1);
105 $ent_id = mime_increment_id ($ent_id);
106 $newmsg = mime_parse_structure ($element, $ent_id);
107 $msg->addEntity ($newmsg);
108 } while ($structure{$end+1} == '(');
109 } else {
110 // parse the elements
111 $msg = mime_get_element ($structure, $msg, $ent_id);
112 }
113 return $msg;
114}
e4a256af 115
451f74a2 116/* Increments the element ID. An element id can look like any of
117 * the following: 1, 1.2, 4.3.2.4.1, etc. This function increments
118 * the last number of the element id, changing 1.2 to 1.3.
119 */
120function mime_increment_id ($id) {
121
77b88425 122 if (strpos($id, '.')) {
123 $first = substr($id, 0, strrpos($id, '.'));
124 $last = substr($id, strrpos($id, '.')+1);
451f74a2 125 $last++;
77b88425 126 $new = $first . '.' .$last;
451f74a2 127 } else {
128 $new = $id + 1;
129 }
77b88425 130
451f74a2 131 return $new;
132}
133
134/*
135 * See comment for mime_increment_id().
136 * This adds another level on to the entity_id changing 1.3 to 1.3.0
137 * NOTE: 1.3.0 is not a valid element ID. It MUST be incremented
138 * before it can be used. I left it this way so as not to have
139 * to make a special case if it is the first entity_id. It
140 * always increments it, and that works fine.
141 */
142function mime_new_element_level ($id) {
143
77b88425 144 if (!$id) {
145 $id = 0;
146 } else {
147 $id = $id . '.0';
148 }
451f74a2 149
77b88425 150 return( $id );
451f74a2 151}
152
153function mime_get_element (&$structure, $msg, $ent_id) {
154
155 $elem_num = 1;
156 $msg->header = new msg_header();
157 $msg->header->entity_id = $ent_id;
158 $properties = array();
159
160 while (strlen($structure) > 0) {
161 $structure = trim($structure);
162 $char = $structure{0};
163
164 if (strtolower(substr($structure, 0, 3)) == 'nil') {
165 $text = '';
166 $structure = substr($structure, 3);
167 } else if ($char == '"') {
168 // loop through until we find the matching quote, and return that as a string
169 $pos = 1;
170 $text = '';
171 while ( ($char = $structure{$pos} ) <> '"' && $pos < strlen($structure)) {
172 $text .= $char;
173 $pos++;
174 }
175 $structure = substr($structure, strlen($text) + 2);
176 } else if ($char == '(') {
177 // comment me
178 $end = mime_match_parenthesis (0, $structure);
179 $sub = substr($structure, 1, $end-1);
180 $properties = mime_get_props($properties, $sub);
181 $structure = substr($structure, strlen($sub) + 2);
182 } else {
183 // loop through until we find a space or an end parenthesis
184 $pos = 0;
185 $char = $structure{$pos};
186 $text = '';
187 while ($char != ' ' && $char != ')' && $pos < strlen($structure)) {
188 $text .= $char;
189 $pos++;
190 $char = $structure{$pos};
191 }
192 $structure = substr($structure, strlen($text));
193 }
194
195 // This is where all the text parts get put into the header
196 switch ($elem_num) {
197 case 1:
198 $msg->header->type0 = strtolower($text);
199 break;
200 case 2:
201 $msg->header->type1 = strtolower($text);
202 break;
203 case 4: // Id
204 // Invisimail enclose images with <>
205 $msg->header->id = str_replace( '<', '', str_replace( '>', '', $text ) );
206 break;
207 case 5:
208 $msg->header->description = $text;
209 break;
210 case 6:
211 $msg->header->encoding = strtolower($text);
212 break;
213 case 7:
214 $msg->header->size = $text;
215 break;
216 default:
217 if ($msg->header->type0 == 'text' && $elem_num == 8) {
218 // This is a plain text message, so lets get the number of lines
219 // that it contains.
220 $msg->header->num_lines = $text;
221
222 } else if ($msg->header->type0 == 'message' && $msg->header->type1 == 'rfc822' && $elem_num == 8) {
223 // This is an encapsulated message, so lets start all over again and
224 // parse this message adding it on to the existing one.
225 $structure = trim($structure);
226 if ( $structure{0} == '(' ) {
227 $e = mime_match_parenthesis (0, $structure);
228 $structure = substr($structure, 0, $e);
229 $structure = substr($structure, 1);
230 $m = mime_parse_structure($structure, $msg->header->entity_id);
231
232 // the following conditional is there to correct a bug that wasn't
233 // incrementing the entity IDs correctly because of the special case
234 // that message/rfc822 is. This fixes it fine.
235 if (substr($structure, 1, 1) != '(')
236 $m->header->entity_id = mime_increment_id(mime_new_element_level($ent_id));
237
238 // Now we'll go through and reformat the results.
239 if ($m->entities) {
240 for ($i=0; $i < count($m->entities); $i++) {
241 $msg->addEntity($m->entities[$i]);
242 }
243 } else {
244 $msg->addEntity($m);
245 }
246 $structure = "";
247 }
248 }
249 break;
250 }
251 $elem_num++;
252 $text = "";
253 }
254 // loop through the additional properties and put those in the various headers
255 if ($msg->header->type0 != 'message') {
256 for ($i=0; $i < count($properties); $i++) {
257 $msg->header->{$properties[$i]['name']} = $properties[$i]['value'];
258 }
259 }
260
261 return $msg;
262}
263
264/*
265 * I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't
266 * figure out how to do this part, so I decided to go to bed. I woke up
267 * in the morning and had a flash of insight. I went to the white-board
268 * and scribbled it out, then spent a bit programming it, and this is the
269 * result. Nothing complicated, but I think my brain was fried yesterday.
270 * Funny how that happens some times.
271 *
272 * This gets properties in a nested parenthesisized list. For example,
273 * this would get passed something like: ("attachment" ("filename" "luke.tar.gz"))
274 * This returns an array called $props with all paired up properties.
275 * It ignores the "attachment" for now, maybe that should change later
276 * down the road. In this case, what is returned is:
277 * $props[0]["name"] = "filename";
278 * $props[0]["value"] = "luke.tar.gz";
279 */
280function mime_get_props ($props, $structure) {
281
282 while (strlen($structure) > 0) {
283 $structure = trim($structure);
284 $char = $structure{0};
285
286 if ($char == '"') {
287 $pos = 1;
288 $tmp = '';
289 while ( ( $char = $structure{$pos} ) != '"' &&
290 $pos < strlen($structure)) {
291 $tmp .= $char;
292 $pos++;
293 }
294 $structure = trim(substr($structure, strlen($tmp) + 2));
295 $char = $structure{0};
296
297 if ($char == '"') {
298 $pos = 1;
299 $value = '';
300 while ( ( $char = $structure{$pos} ) != '"' &&
301 $pos < strlen($structure) ) {
302 $value .= $char;
303 $pos++;
304 }
305 $structure = trim(substr($structure, strlen($tmp) + 2));
306
307 $k = count($props);
308 $props[$k]['name'] = strtolower($tmp);
309 $props[$k]['value'] = $value;
310 } else if ($char == '(') {
311 $end = mime_match_parenthesis (0, $structure);
312 $sub = substr($structure, 1, $end-1);
313 if (! isset($props))
314 $props = array();
315 $props = mime_get_props($props, $sub);
316 $structure = substr($structure, strlen($sub) + 2);
317 }
318 return $props;
319 } else if ($char == '(') {
320 $end = mime_match_parenthesis (0, $structure);
321 $sub = substr($structure, 1, $end-1);
322 $props = mime_get_props($props, $sub);
323 $structure = substr($structure, strlen($sub) + 2);
324 return $props;
325 } else {
326 return $props;
327 }
328 }
329}
330
331/*
332 * Matches parenthesis. It will return the position of the matching
333 * parenthesis in $structure. For instance, if $structure was:
334 * ("text" "plain" ("val1name", "1") nil ... )
335 * x x
336 * then this would return 42 to match up those two.
337 */
338function mime_match_parenthesis ($pos, $structure) {
339
340 $j = strlen( $structure );
341
342 // ignore all extra characters
343 // If inside of a string, skip string -- Boundary IDs and other
344 // things can have ) in them.
345 if ( $structure{$pos} != '(' ) {
346 return( $j );
347 }
348
349 while ( $pos < $j ) {
350 $pos++;
351 if ($structure{$pos} == ')') {
8beafbbc 352 return $pos;
451f74a2 353 } elseif ($structure{$pos} == '"') {
b74ba498 354 $pos++;
451f74a2 355 while ( $structure{$pos} != '"' &&
356 $pos < $j ) {
357 if (substr($structure, $pos, 2) == '\\"') {
b74ba498 358 $pos++;
451f74a2 359 } elseif (substr($structure, $pos, 2) == '\\\\') {
b74ba498 360 $pos++;
451f74a2 361 }
b74ba498 362 $pos++;
5ffe5a7e 363 }
451f74a2 364 } elseif ( $structure{$pos} == '(' ) {
8beafbbc 365 $pos = mime_match_parenthesis ($pos, $structure);
451f74a2 366 }
367 }
368 echo _("Error decoding mime structure. Report this as a bug!") . '<br>';
369 return( $pos );
370}
371
346817d4 372function mime_fetch_body($imap_stream, $id, $ent_id ) {
09a4bde3 373
374 /*
375 * do a bit of error correction. If we couldn't find the entity id, just guess
376 * that it is the first one. That is usually the case anyway.
377 */
378 if (!$ent_id) {
451f74a2 379 $ent_id = 1;
09a4bde3 380 }
346817d4 381
6ab1bd9e 382 $cmd = "FETCH $id BODY[$ent_id]";
346817d4 383 $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message);
77b88425 384
385 do {
386 $topline = array_shift( $data );
387 } while( $topline && $topline == '*' && !preg_match( '/\\* [0-9] FETCH.*/i', $topline )) ;
451f74a2 388 $wholemessage = implode('', $data);
389 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
77b88425 390
451f74a2 391 $ret = substr( $wholemessage, 0, $regs[1] );
392 /*
393 There is some information in the content info header that could be important
394 in order to parse html messages. Let's get them here.
395 */
396 if ( $ret{0} == '<' ) {
1c72b151 397 $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message);
e5ea9327 398 /* BASE within HTML documents is illegal (see w3 spec)
399* $base = '';
400* $k = 10;
401* foreach( $data as $d ) {
402* if ( substr( $d, 0, 13 ) == 'Content-Base:' ) {
403* $j = strlen( $d );
404* $i = 13;
405* $base = '';
406* while ( $i < $j &&
407* ( !isNoSep( $d{$i} ) || $d{$i} == '"' ) )
408* $i++;
409* while ( $i < $j ) {
410* if ( isNoSep( $d{$i} ) )
411* $base .= $d{$i};
412* $i++;
413* }
414* $k = 0;
415* } elseif ( $k == 1 && !isnosep( $d{0} ) ) {
416* $base .= substr( $d, 1 );
417* }
418* $k++;
419* }
420* if ( $base <> '' ) {
421* $ret = "<base href=\"$base\">" . $ret;
422* }
423* */
451f74a2 424 }
425 } else if (ereg('"([^"]*)"', $topline, $regs)) {
426 $ret = $regs[1];
427 } else {
428 global $where, $what, $mailbox, $passed_id, $startMessage;
e5ea9327 429 $par = 'mailbox=' . urlencode($mailbox) . "&amp;passed_id=$passed_id";
451f74a2 430 if (isset($where) && isset($what)) {
e5ea9327 431 $par .= '&amp;where='. urlencode($where) . "&amp;what=" . urlencode($what);
a3daaaf3 432 } else {
e5ea9327 433 $par .= "&amp;startMessage=$startMessage&amp;show_more=0";
451f74a2 434 }
e5ea9327 435 $par .= '&amp;response=' . urlencode($response) .
436 '&amp;message=' . urlencode($message).
437 '&amp;topline=' . urlencode($topline);
a019eeb8 438
346817d4 439 echo '<tt><br>' .
440 '<table width="80%"><tr>' .
441 '<tr><td colspan=2>' .
451f74a2 442 _("Body retrieval error. The reason for this is most probably that the message is malformed. Please help us making future versions better by submitting this message to the developers knowledgebase!") .
346817d4 443 " <A HREF=\"../src/retrievalerror.php?$par\"><br>" .
444 _("Submit message") . '</A><BR>&nbsp;' .
445 '</td></tr>' .
446 '<td><b>' . _("Command:") . "</td><td>$cmd</td></tr>" .
447 '<td><b>' . _("Response:") . "</td><td>$response</td></tr>" .
448 '<td><b>' . _("Message:") . "</td><td>$message</td></tr>" .
449 '<td><b>' . _("FETCH line:") . "</td><td>$topline</td></tr>" .
450 "</table><BR></tt></font><hr>";
451
1c72b151 452 $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message);
451f74a2 453 array_shift($data);
454 $wholemessage = implode('', $data);
a019eeb8 455
346817d4 456 $ret = $wholemessage;
a3daaaf3 457 }
451f74a2 458 return( $ret );
459}
d4467150 460
451f74a2 461function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
462 // do a bit of error correction. If we couldn't find the entity id, just guess
463 // that it is the first one. That is usually the case anyway.
464 if (!$ent_id) {
465 $ent_id = 1;
466 }
467 $sid = sqimap_session_id();
468 // Don't kill the connection if the browser is over a dialup
469 // and it would take over 30 seconds to download it.
b7206e1d 470
471