Just fixing a bug I ran across where attachments was an empty array.
[squirrelmail.git] / functions / imap_messages.php
CommitLineData
59177427 1<?php
7350889b 2
35586184 3/**
4 * imap_messages.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 implements functions that manipulate messages
10 *
11 * $Id$
12 */
052e0c26 13
3411d4ec 14/* Copies specified messages to specified folder */
35586184 15function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
1c72b151 16 $read = sqimap_run_command ($imap_stream, "COPY $start:$end \"$mailbox\"", true, $response, $message);
97f7ddf2 17}
18
3411d4ec 19/* Deletes specified messages and moves them to trash if possible */
97f7ddf2 20function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) {
21 global $move_to_trash, $trash_folder, $auto_expunge;
22
23 if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) {
24 sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
97f7ddf2 25 }
3411d4ec 26 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
97f7ddf2 27}
28
3411d4ec 29/* Sets the specified messages with specified flag */
97f7ddf2 30function sqimap_messages_flag ($imap_stream, $start, $end, $flag) {
1c72b151 31 $read = sqimap_run_command ($imap_stream, "STORE $start:$end +FLAGS (\\$flag)", true, $response, $message);
97f7ddf2 32}
33
3411d4ec 34/* Remove specified flag from specified messages */
97f7ddf2 35function sqimap_messages_remove_flag ($imap_stream, $start, $end, $flag) {
1c72b151 36 $read = sqimap_run_command ($imap_stream, "STORE $start:$end -FLAGS (\\$flag)", true, $response, $message);
97f7ddf2 37}
38
3411d4ec 39/* Returns some general header information -- FROM, DATE, and SUBJECT */
97f7ddf2 40class small_header {
41 var $from = '', $subject = '', $date = '', $to = '',
42 $priority = 0, $message_id = 0, $cc = '';
43}
44
45function sqimap_get_small_header ($imap_stream, $id, $sent) {
46 $res = sqimap_get_small_header_list($imap_stream, array($id), $sent);
47 return $res[0];
48}
3411d4ec 49
50/*
51 * Sort the message list and crunch to be as small as possible
97f7ddf2 52 * (overflow could happen, so make it small if possible)
53 */
54function sqimap_message_list_squisher($messages_array) {
55 if( !is_array( $messages_array ) ) {
56 return;
57 }
58 sort($messages_array, SORT_NUMERIC);
59 $msgs_str = '';
60 while ($messages_array) {
61 $start = array_shift($messages_array);
62 $end = $start;
63 while (isset($messages_array[0]) && $messages_array[0] == $end + 1) {
64 $end = array_shift($messages_array);
65 }
66 if ($msgs_str != '') {
67 $msgs_str .= ',';
68 }
69 $msgs_str .= $start;
70 if ($start != $end) {
71 $msgs_str .= ':' . $end;
72 }
73 }
3411d4ec 74
97f7ddf2 75 return $msgs_str;
3411d4ec 76}
97f7ddf2 77
67c2f5ea 78/* returns the references header lines */
79function get_reference_header ($imap_stream, $message) {
80 $responses = array ();
81 $sid = sqimap_session_id();
82 $results = array();
83 $references = "";
84 $query = "$sid FETCH $message BODY.PEEK[HEADER.FIELDS (References)]\r\n";
85 fputs ($imap_stream, $query);
86 $responses = sqimap_read_data_list($imap_stream, $sid, true, $responses, $message);
87 if (!eregi("^\\* ([0-9]+) FETCH", $responses[0][0], $regs)) {
88 $responses = array ();
89 }
90 return $responses;
91}
92
aa0da530 93
94/* get sort order from server and
95 * return it as the $id array for
96 * mailbox_display
97 */
98
99function sqimap_get_sort_order ($imap_stream, $sort) {
ba80e62b 100 global $default_charset, $thread_sort_messages,
101 $internal_date_sort, $server_sort_array,
102 $sent_folder, $mailbox;
103
60a3e687 104 if (session_is_registered('server_sort_array')) {
105 session_unregister('server_sort_array');
106 }
aa0da530 107 $sid = sqimap_session_id();
108 $sort_on = array();
109 $reverse = 0;
110 $server_sort_array = array();
111 $sort_test = array();
112 $sort_query = '';
113 $sort_on = array (0=> 'DATE',
114 1=> 'DATE',
115 2=> 'FROM',
116 3=> 'FROM',
117 4=> 'SUBJECT',
118 5=> 'SUBJECT',
119 6=> 'DATE');
120 if ($internal_date_sort == true) {
121 $sort_on[0] = 'ARRIVAL';
122 $sort_on[1] = 'ARRIVAL';
123 }
ba80e62b 124 if ($sent_folder == $mailbox) {
125 $sort_on[2] = 'TO';
126 $sort_on[3] = 'TO';
127 }
aa0da530 128 if (!empty($sort_on[$sort])) {
129 $sort_query = "$sid SORT ($sort_on[$sort]) ".strtoupper($default_charset)." ALL\r\n";
130 fputs($imap_stream, $sort_query);
26eca02e 131 $sort_test = sqimap_read_data($imap_stream, $sid, false, $response, $message);
aa0da530 132 }
133 if (preg_match("/^\* SORT (.+)$/", $sort_test[0], $regs)) {
134 $server_sort_array = preg_split("/ /", trim($regs[1]));
135 }
136 if ($sort == 0 || $sort == 2 || $sort == 4) {
137 $server_sort_array = array_reverse($server_sort_array);
138 }
26eca02e 139 if (!preg_match("/OK/", $response)) {
140 $server_sort_array = 'no';
141 }
60a3e687 142 session_register('server_sort_array');
aa0da530 143 return $server_sort_array;
144}
145
7c612fdd 146/* returns an indent array for printMessageinfo()
ba80e62b 147 this represents the amount of indent needed (value)
148 for this message number (key)
7c612fdd 149*/
150
151function get_parent_level ($imap_stream) {
152 global $sort_by_ref, $default_charset, $thread_new;
153 $parent = "";
154 $child = "";
155 for ($i=0;$i<count($thread_new);$i++) {
156 $thread_new[$i] = preg_replace("/\s\(/", "(", $thread_new[$i]);
157 $thread_new[$i] = preg_replace("/(\d+)/", "$1|", $thread_new[$i]);
158 $thread_new[$i] = preg_split("/\|/", $thread_new[$i], -1, PREG_SPLIT_NO_EMPTY);
159 }
160 $indent_array = array();
161 if (!$thread_new) {
162 $thread_new = array();
163 }
164 for ($i=0;$i<count($thread_new);$i++) {
165 if (isset($thread_new[$i][0])) {
166 if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) {
167 $parent = $regs[1];
168 }
169 }
170 $indent_array[$parent] = 0;
171 $indent = 0;
172 $go = 'stop';
173 $spaces = array ();
174 $l = 0;
175 for ($k=1;$k<(count($thread_new[$i]))-1;$k++) {
176 $chars = count_chars($thread_new[$i][$k], 1);
177 if (isset($chars['40']) && isset($chars['41'])) {
178 $l--;
179 }
180 if (isset($chars['40'])) { // (
181 $indent = $indent + $chars[40];
182 $go = 'start';
183 $l++;
184 }
185 if (isset($chars['41'])) { // )
186 if ($go == 'start') {
187 if (!isset($spaces[$l])) {
188 $spaces[$l] = 0;
189 }
190 $indent = $indent - $spaces[$l];
191 $indent = $indent - $chars[41] ;
192 $go = 'stop';
193 $l--;
194 }
195 else {
196 $indent = $indent - $chars[41];
197 }
198 }
199 if (isset($chars['32'])) { // space
200 $indent = $indent + $chars[32];
201 if ($go == 'start') {
202 if (!isset($spaces[$l])) {
203 $spaces[$l] = 0;
204 }
205 $spaces[$l] = $spaces[$l] + $chars[32];
206 }
207 }
208 if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) {
209 $child = $regs[1];
210 }
211 $indent_array[$child] = abs($indent);
212 }
213 }
214 return $indent_array;
215}
216
217
218/* returns an array with each element as a string
219 representing one message thread as returned by
220 the IMAP server
221*/
222
223function get_thread_sort ($imap_stream) {
60a3e687 224 global $thread_new, $sort_by_ref, $default_charset, $server_sort_array;
7c612fdd 225
60a3e687 226 if (session_is_registered('thread_new')) {
7c612fdd 227 session_unregister('thread_new');
228 }
60a3e687 229 if (session_is_registered('server_sort_array')) {
230 session_unregister('server_srot_array');
231 }
7c612fdd 232 $sid = sqimap_session_id();
233 $thread_temp = array ();
234 if ($sort_by_ref == 1) {
235 $sort_type = 'REFERENCES';
236 }
237 else {
238 $sort_type = 'ORDEREDSUBJECT';
239 }
aa0da530 240 $thread_query = "$sid THREAD $sort_type ".strtoupper($default_charset)." ALL\r\n";
7c612fdd 241 fputs($imap_stream, $thread_query);
26eca02e 242 $thread_test = sqimap_read_data($imap_stream, $sid, false, $response, $message);
7c612fdd 243 if (preg_match("/^\* THREAD (.+)$/", $thread_test[0], $regs)) {
244 $thread_list = trim($regs[1]);
245 }
246 else {
247 $thread_list = "";
248 }
26eca02e 249 if (!preg_match("/OK/", $response)) {
250 $server_sort_array = 'no';
251 return $server_sort_array;
252 }
7c612fdd 253 $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
254 $char_count = count($thread_temp);
255 $counter = 0;
256 $thread_new = array();
257 $k = 0;
258 $thread_new[0] = "";
259 for ($i=0;$i<$char_count;$i++) {
260 if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
261 $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
262 }
263 elseif ($thread_temp[$i] == '(') {
264 $thread_new[$k] .= $thread_temp[$i];
265 $counter++;
266 }
267 elseif ($thread_temp[$i] == ')') {
268 if ($counter > 1) {
269 $thread_new[$k] .= $thread_temp[$i];
270 $counter = $counter - 1;
271 }
272 else {
273 $thread_new[$k] .= $thread_temp[$i];
274 $k++;
275 $thread_new[$k] = "";
276 $counter = $counter - 1;
277 }
278 }
279 }
280 session_register('$thread_new');
281 $thread_new = array_reverse($thread_new);
282 $thread_list = implode(" ", $thread_new);
283 $thread_list = str_replace("(", " ", $thread_list);
284 $thread_list = str_replace(")", " ", $thread_list);
285 $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
60a3e687 286 $server_sort_array = $thread_list;
287 session_register('server_sort_array');
7c612fdd 288 return $thread_list;
289}
290
291
292
97f7ddf2 293function sqimap_get_small_header_list ($imap_stream, $msg_list, $issent) {
9735cf27 294 global $squirrelmail_language, $color, $data_dir, $username;
97f7ddf2 295
296 /* Get the small headers for each message in $msg_list */
297 $sid = sqimap_session_id();
298 $maxmsg = sizeof($msg_list);
299 $msgs_str = sqimap_message_list_squisher($msg_list);
300 $results = array();
301 $read_list = array();
302 $sizes_list = array();
3411d4ec 303 /*
97f7ddf2 304 * We need to return the data in the same order as the caller supplied
305 * in $msg_list, but IMAP servers are free to return responses in
306 * whatever order they wish... So we need to re-sort manually
307 */
308 for ($i = 0; $i < sizeof($msg_list); $i++) {
309 $id2index[$msg_list[$i]] = $i;
310 }
311
67c2f5ea 312 $query = "$sid FETCH $msgs_str BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority Content-Type In-Reply-To)]\r\n";
97f7ddf2 313 fputs ($imap_stream, $query);
314 $readin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
315
316 foreach ($readin_list as $r) {
317 if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
91f68e94 318 set_up_language($squirrelmail_language);
0fbbda83 319 echo '<br><b><font color=$color[2]>' .
97f7ddf2 320 _("ERROR : Could not complete request.") .
321 '</b><br>' .
322 _("Unknown response from IMAP server: ") . ' 1.' .
323 $r[0] . "</font><br>\n";
97f7ddf2 324 } else if (! isset($id2index[$regs[1]]) || !count($id2index[$regs[1]])) {
325 set_up_language($squirrelmail_language);
326 echo '<br><b><font color=$color[2]>' .
327 _("ERROR : Could not complete request.") .
328 '</b><br>' .
329 _("Unknown message number in reply from server: ") .
330 $regs[1] . "</font><br>\n";
97f7ddf2 331 } else {
332 $read_list[$id2index[$regs[1]]] = $r;
333 }
334 }
335 arsort($read_list);
3411d4ec 336
97f7ddf2 337 $query = "$sid FETCH $msgs_str RFC822.SIZE\r\n";
338 fputs ($imap_stream, $query);
339 $sizesin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
3411d4ec 340
97f7ddf2 341 foreach ($sizesin_list as $r) {
342 if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
91f68e94 343 set_up_language($squirrelmail_language);
344 echo "<br><b><font color=$color[2]>\n";
345 echo _("ERROR : Could not complete request.");
346 echo "</b><br>\n";
0fbbda83 347 echo _("Unknown response from IMAP server: ") . ' 2.';
91f68e94 348 echo $r[0] . "</font><br>\n";
349 exit;
97f7ddf2 350 }
351 if (!count($id2index[$regs[1]])) {
91f68e94 352 set_up_language($squirrelmail_language);
353 echo "<br><b><font color=$color[2]>\n";
354 echo _("ERROR : Could not complete request.");
355 echo "</b><br>\n";
356 echo _("Unknown messagenumber in reply from server: ");
357 echo $regs[1] . "</font><br>\n";
358 exit;
97f7ddf2 359 }
360 $sizes_list[$id2index[$regs[1]]] = $r;
361 }
362 arsort($sizes_list);
3411d4ec 363
97f7ddf2 364 for ($msgi = 0; $msgi < $maxmsg; $msgi++) {
365 $subject = _("(no subject)");
366 $from = _("Unknown Sender");
367 $priority = 0;
368 $messageid = "<>";
369 $cc = "";
370 $to = "";
371 $date = "";
372 $type[0] = "";
373 $type[1] = "";
67c2f5ea 374 $inrepto = "";
97f7ddf2 375 $read = $read_list[$msgi];
376
ec91fad4 377 $prevline = false;
3411d4ec 378 foreach ($read as $read_part) {
ec91fad4 379 //unfold multi-line headers
380 while ($prevline && strspn($read_part, "\t ") > 0) {
11505466 381 $read_part = substr($prevline, 0, -2) . ' ' . ltrim($read_part);
ec91fad4 382 }
383 $prevline = $read_part;
3411d4ec 384 if (eregi ("^to:(.*)$", $read_part, $regs)) {
97f7ddf2 385 $to = $regs[1];
3411d4ec 386 } else if (eregi ("^from:(.*)$", $read_part, $regs)) {
97f7ddf2 387 $from = $regs[1];
3411d4ec 388 } else if (eregi ("^x-priority:(.*)$", $read_part, $regs)) {
97f7ddf2 389 $priority = trim($regs[1]);
3411d4ec 390 } else if (eregi ("^message-id:(.*)$", $read_part, $regs)) {
97f7ddf2 391 $messageid = trim($regs[1]);
3411d4ec 392 } else if (eregi ("^cc:(.*)$", $read_part, $regs)) {
97f7ddf2 393 $cc = $regs[1];
3411d4ec 394 } else if (eregi ("^date:(.*)$", $read_part, $regs)) {
97f7ddf2 395 $date = $regs[1];
3411d4ec 396 } else if (eregi ("^subject:(.*)$", $read_part, $regs)) {
97f7ddf2 397 $subject = htmlspecialchars(trim($regs[1]));
398 if ($subject == "") {
399 $subject = _("(no subject)");
400 }
3411d4ec 401 } else if (eregi ("^content-type:(.*)$", $read_part, $regs)) {
97f7ddf2 402 $type = strtolower(trim($regs[1]));
403 if ($pos = strpos($type, ";")) {
404 $type = substr($type, 0, $pos);
405 }
406 $type = explode("/", $type);
3411d4ec 407 if (!isset($type[1])) {
97f7ddf2 408 $type[1] = '';
409 }
b288e71e 410 } else if (eregi ("^in-reply-to:(.*)$", $read_part, $regs)) {
67c2f5ea 411 $inrepto = trim($regs[1]);
91f68e94 412 }
97f7ddf2 413 }
3411d4ec 414 $internaldate = getPref($data_dir, $username, 'internal_date_sort');
9735cf27 415 if (trim($date) == "" || $internaldate) {
0fbbda83 416 fputs($imap_stream, "$sid FETCH $msg_list[$msgi] INTERNALDATE\r\n");
417 $readdate = sqimap_read_data($imap_stream, $sid, true, $response, $message);
91f68e94 418 if (eregi(".*INTERNALDATE \"(.*)\".*", $readdate[0], $regs)) {
3411d4ec 419 $date_list = explode(' ', trim($regs[1]));
420 $date_list[0] = str_replace("-", ' ', $date_list[0]);
421 $date = implode(' ', $date_list);
91f68e94 422 }
97f7ddf2 423 }
424 eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
425 $size = $regs[1];
3411d4ec 426
97f7ddf2 427 $header = new small_header;
3411d4ec 428 if ($issent) {
f7b1b3b1 429 $header->from = (trim($to) != '' ? $to : '(' ._("No To Address") . ')');
97f7ddf2 430 } else {
91f68e94 431 $header->from = $from;
97f7ddf2 432 }
433
434 $header->date = $date;
435 $header->subject = $subject;
436 $header->to = $to;
437 $header->priority = $priority;
438 $header->message_id = $messageid;
439 $header->cc = $cc;
440 $header->size = $size;
441 $header->type0 = $type[0];
442 $header->type1 = $type[1];
67c2f5ea 443 $header->inrepto = $inrepto;
97f7ddf2 444 $result[] = $header;
445 }
446 return $result;
447}
448
3411d4ec 449/* Returns the flags for the specified messages */
97f7ddf2 450function sqimap_get_flags ($imap_stream, $i) {
1c72b151 451 $read = sqimap_run_command ($imap_stream, "FETCH $i:$i FLAGS", true, $response, $message);
3411d4ec 452 if (ereg('FLAGS(.*)', $read[0], $regs)) {
0f8a1ce9 453 return explode(' ', trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
97f7ddf2 454 }
3411d4ec 455 return array('None');
97f7ddf2 456}
457
458function sqimap_get_flags_list ($imap_stream, $msg_list) {
459 $msgs_str = sqimap_message_list_squisher($msg_list);
460 for ($i = 0; $i < sizeof($msg_list); $i++) {
461 $id2index[$msg_list[$i]] = $i;
462 }
1c72b151 463 $result_list = sqimap_run_command_list ($imap_stream, "FETCH $msgs_str FLAGS", true, $response, $message);
97f7ddf2 464 $result_flags = array();
465
466 for ($i = 0; $i < sizeof($result_list); $i++) {
3411d4ec 467 if (eregi('^\* ([0-9]+).*FETCH.*FLAGS(.*)', $result_list[$i][0], $regs)
97f7ddf2 468 && isset($id2index[$regs[1]]) && count($id2index[$regs[1]])) {
91f68e94 469 $result_flags[$id2index[$regs[1]]] = explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[2])));
97f7ddf2 470 } else {
91f68e94 471 set_up_language($squirrelmail_language);
49c17806 472 echo "<br><b><font color=$color[2]>\n" .
473 _("ERROR : Could not complete request.") .
474 "</b><br>\n" .
475 _("Unknown response from IMAP server: ") .
476 $result_list[$i][0] . "</font><br>\n";
91f68e94 477 exit;
97f7ddf2 478 }
479 }
480 arsort($result_flags);
481 return $result_flags;
482}
483
3411d4ec 484/*
485 * Returns a message array with all the information about a message.
486 * See the documentation folder for more information about this array.
487 */
97f7ddf2 488function sqimap_get_message ($imap_stream, $id, $mailbox) {
489 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
490 return sqimap_get_message_body($imap_stream, $header);
491}
492
3411d4ec 493/* Wrapper function that reformats the header information. */
97f7ddf2 494function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
1c72b151 495 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[HEADER]", true, $response, $message);
97f7ddf2 496 $header = sqimap_get_header($imap_stream, $read);
497 $header->id = $id;
498 $header->mailbox = $mailbox;
97f7ddf2 499 return $header;
500}
501
3411d4ec 502/* Wrapper function that reformats the entity header information. */
43786d5c 503function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
504 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.HEADER]", true, $response, $message);
505 $header = sqimap_get_header($imap_stream, $read);
506 $header->id = $id;
507 $header->mailbox = $mailbox;
508 return $header;
509}
510
511
3411d4ec 512/* Wrapper function that returns entity headers for use by decodeMime */
97f7ddf2 513/*
514function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
515 $header = sqimap_get_header($imap_stream, $read);
516 $type0 = $header["TYPE0"];
517 $type1 = $header["TYPE1"];
518 $bound = $header["BOUNDARY"];
519 $encoding = $header["ENCODING"];
520 $charset = $header["CHARSET"];
521 $filename = $header["FILENAME"];
522}
523*/
3411d4ec 524/* Queries the IMAP server and gets all header information. */
97f7ddf2 525function sqimap_get_header ($imap_stream, $read) {
526 global $where, $what;
527
528 $hdr = new msg_header();
529 $i = 0;
3411d4ec 530
97f7ddf2 531 /* Set up some defaults */
532 $hdr->type0 = "text";
533 $hdr->type1 = "plain";
534 $hdr->charset = "us-ascii";
535
536 while ($i < count($read)) {
ec91fad4 537 //unfold multi-line headers
538 while ($i + 1 < count($read) && strspn($read[$i + 1], "\t ") > 0) {
11505466 539 $read[$i + 1] = substr($read[$i], 0, -2) . ' ' . ltrim($read[$i + 1]);
ec91fad4 540 array_splice($read, $i, 1);
541 }
542
97f7ddf2 543 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
8beafbbc 544 $hdr->mime = true;
052e0c26 545 $i++;
97f7ddf2 546 }
3411d4ec 547 /* ENCODING TYPE */
97f7ddf2 548 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
8beafbbc 549 $hdr->encoding = strtolower(trim(substr($read[$i], 26)));
052e0c26 550 $i++;
97f7ddf2 551 }
3411d4ec 552 /* CONTENT-TYPE */
553 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
052e0c26 554 $cont = strtolower(trim(substr($read[$i], 13)));
97f7ddf2 555 if (strpos($cont, ";")) {
556 $cont = substr($cont, 0, strpos($cont, ";"));
557 }
052e0c26 558
052e0c26 559 if (strpos($cont, "/")) {
97f7ddf2 560 $hdr->type0 = substr($cont, 0, strpos($cont, "/"));
561 $hdr->type1 = substr($cont, strpos($cont, "/")+1);
052e0c26 562 } else {
97f7ddf2 563 $hdr->type0 = $cont;
052e0c26 564 }
565
052e0c26 566 $line = $read[$i];
567 $i++;
568 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
97f7ddf2 569 str_replace("\n", "", $line);
570 str_replace("\n", "", $read[$i]);
571 $line = "$line $read[$i]";
572 $i++;
052e0c26 573 }
574
3411d4ec 575 /* Detect the boundary of a multipart message */
97f7ddf2 576 if (eregi('boundary="([^"]+)"', $line, $regs)) {
577 $hdr->boundary = $regs[1];
052e0c26 578 }
579
3411d4ec 580 /* Detect the charset */
052e0c26 581 if (strpos(strtolower(trim($line)), "charset=")) {
97f7ddf2 582 $pos = strpos($line, "charset=") + 8;
583 $charset = trim($line);
584 if (strpos($line, ";", $pos) > 0) {
585 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
586 } else {
587 $charset = substr($charset, $pos);
588 }
589 $charset = str_replace("\"", "", $charset);
590 $hdr->charset = $charset;
052e0c26 591 } else {
97f7ddf2 592 $hdr->charset = "us-ascii";
052e0c26 593 }
97f7ddf2 594 }
97f7ddf2 595 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
3411d4ec 596 /* Add better content-disposition support */
052e0c26 597 $line = $read[$i];
598 $i++;
599 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
97f7ddf2 600 str_replace("\n", "", $line);
601 str_replace("\n", "", $read[$i]);
602 $line = "$line $read[$i]";
603 $i++;
052e0c26 604 }
605
3411d4ec 606 /* Detects filename if any */
052e0c26 607 if (strpos(strtolower(trim($line)), "filename=")) {
97f7ddf2 608 $pos = strpos($line, "filename=") + 9;
609 $name = trim($line);
610 if (strpos($line, " ", $pos) > 0) {
3411d4ec 611 $name = substr($name, $pos, strpos($line, " ", $pos));
97f7ddf2 612 } else {
3411d4ec 613 $name = substr($name, $pos);
97f7ddf2 614 }
615 $name = str_replace("\"", "", $name);
616 $hdr->filename = $name;
052e0c26 617 }
97f7ddf2 618 }
3411d4ec 619 /* REPLY-TO */
97f7ddf2 620 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
8beafbbc 621 $hdr->replyto = trim(substr($read[$i], 9, strlen($read[$i])));
052e0c26 622 $i++;
97f7ddf2 623 }
3411d4ec 624 /* FROM */
97f7ddf2 625 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
8beafbbc 626 $hdr->from = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
97f7ddf2 627 if (! isset($hdr->replyto) || $hdr->replyto == "") {
628 $hdr->replyto = $hdr->from;
629 }
052e0c26 630 $i++;
97f7ddf2 631 }
3411d4ec 632 /* DATE */
97f7ddf2 633 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
052e0c26 634 $d = substr($read[$i], 5);
635 $d = trim($d);
a5227f0e 636 $d = strtr($d, array(' ' => ' '));
146e0c45 637 $d = explode(' ', $d);
8beafbbc 638 $hdr->date = getTimeStamp($d);
052e0c26 639 $i++;
97f7ddf2 640 }
3411d4ec 641 /* SUBJECT */
97f7ddf2 642 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
8beafbbc 643 $hdr->subject = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
53828ec5 644 if (strlen(Chop($hdr->subject)) == 0) {
8beafbbc 645 $hdr->subject = _("(no subject)");
508c06bf 646 }
3411d4ec 647 /*
53828ec5 648 if ($where == 'SUBJECT') {
97f7ddf2 649 $hdr->subject = $what;
650 // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
53828ec5 651 }
652 */
052e0c26 653 $i++;
97f7ddf2 654 }
3411d4ec 655 /* CC */
97f7ddf2 656 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
052e0c26 657 $pos = 0;
8beafbbc 658 $hdr->cc[$pos] = trim(substr($read[$i], 4));
052e0c26 659 $i++;
11728a4d 660 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
97f7ddf2 661 $pos++;
662 $hdr->cc[$pos] = trim($read[$i]);
663 $i++;
052e0c26 664 }
97f7ddf2 665 }
3411d4ec 666 /* BCC */
97f7ddf2 667 else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
853b543d 668 $pos = 0;
669 $hdr->bcc[$pos] = trim(substr($read[$i], 5));
670 $i++;
671 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
97f7ddf2 672 $pos++;
673 $hdr->bcc[$pos] = trim($read[$i]);
674 $i++;
853b543d 675 }
97f7ddf2 676 }
3411d4ec 677 /* TO */
97f7ddf2 678 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
052e0c26 679 $pos = 0;
8beafbbc 680 $hdr->to[$pos] = trim(substr($read[$i], 4));
052e0c26 681 $i++;
11728a4d 682 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
97f7ddf2 683 $pos++;
684 $hdr->to[$pos] = trim($read[$i]);
685 $i++;
052e0c26 686 }
97f7ddf2 687 }
3411d4ec 688 /* MESSAGE ID */
97f7ddf2 689 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
8beafbbc 690 $hdr->message_id = trim(substr($read[$i], 11));
052e0c26 691 $i++;
97f7ddf2 692 }
3411d4ec 693 /* ERROR CORRECTION */
97f7ddf2 694 else if (substr($read[$i], 0, 1) == ")") {
695 if (strlen(trim($hdr->subject)) == 0) {
8beafbbc 696 $hdr->subject = _("(no subject)");
97f7ddf2 697 }
698 if (strlen(trim($hdr->from)) == 0) {
8beafbbc 699 $hdr->from = _("(unknown sender)");
97f7ddf2 700 }
701 if (strlen(trim($hdr->date)) == 0) {
8beafbbc 702 $hdr->date = time();
97f7ddf2 703 }
052e0c26 704 $i++;
97f7ddf2 705 }
3411d4ec 706 /* X-PRIORITY */
97f7ddf2 707 else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
020abcf3 708 $hdr->priority = trim(substr($read[$i], 11));
709 $i++;
97f7ddf2 710 }
711 else {
052e0c26 712 $i++;
97f7ddf2 713 }
714 }
715 return $hdr;
716}
052e0c26 717
3411d4ec 718/* Returns the body of a message. */
97f7ddf2 719function sqimap_get_message_body ($imap_stream, &$header) {
720 $id = $header->id;
721 return decodeMime($imap_stream, $header);
722}
8beafbbc 723
052e0c26 724?>