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