859702b59e41cf7c30f337a658bac9de5167b3c9
[squirrelmail.git] / functions / imap_messages.php
1 <?php
2
3 /**
4 * imap_messages.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
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 */
13
14 /* Copies specified messages to specified folder */
15 function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
16 global $uid_support;
17 $read = sqimap_run_command ($imap_stream, "COPY $start:$end \"$mailbox\"", true, $response, $message, $uid_support);
18 }
19
20 /* Deletes specified messages and moves them to trash if possible */
21 function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) {
22 global $move_to_trash, $trash_folder, $auto_expunge, $uid_support;
23
24 if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) {
25 sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
26 }
27 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted", true);
28 }
29
30 /* Sets the specified messages with specified flag */
31 function sqimap_messages_flag ($imap_stream, $start, $end, $flag, $handle_errors) {
32 global $uid_support;
33 $read = sqimap_run_command ($imap_stream, "STORE $start:$end +FLAGS (\\$flag)", $handle_errors, $response, $message, $uid_support);
34 }
35
36 /* Remove specified flag from specified messages */
37 function sqimap_messages_remove_flag ($imap_stream, $start, $end, $flag, $handle_errors) {
38 global $uid_support;
39 $read = sqimap_run_command ($imap_stream, "STORE $start:$end -FLAGS (\\$flag)", $handle_errors, $response, $message, $uid_support);
40 }
41
42 /* Returns some general header information -- FROM, DATE, and SUBJECT */
43 class small_header {
44 var $from = '', $subject = '', $date = '', $to = '',
45 $priority = 0, $message_id = 0, $cc = '', $uid = '';
46 }
47
48 function sqimap_get_small_header ($imap_stream, $id, $sent) {
49 $res = sqimap_get_small_header_list($imap_stream, array($id), $sent);
50 return $res[0];
51 }
52
53 /*
54 * Sort the message list and crunch to be as small as possible
55 * (overflow could happen, so make it small if possible)
56 */
57 function sqimap_message_list_squisher($messages_array) {
58 if( !is_array( $messages_array ) ) {
59 return;
60 }
61
62 sort($messages_array, SORT_NUMERIC);
63 $msgs_str = '';
64 while ($messages_array) {
65 $start = array_shift($messages_array);
66 $end = $start;
67 while (isset($messages_array[0]) && $messages_array[0] == $end + 1) {
68 $end = array_shift($messages_array);
69 }
70 if ($msgs_str != '') {
71 $msgs_str .= ',';
72 }
73 $msgs_str .= $start;
74 if ($start != $end) {
75 $msgs_str .= ':' . $end;
76 }
77 }
78 return $msgs_str;
79 }
80
81 /* returns the references header lines */
82 function get_reference_header ($imap_stream, $message) {
83 global $uid_support;
84 $responses = array ();
85 $sid = sqimap_session_id($uid_support);
86 $results = array();
87 $references = "";
88 $query = "$sid FETCH $message BODY[HEADER.FIELDS (References)]\r\n";
89 fputs ($imap_stream, $query);
90 $responses = sqimap_read_data_list($imap_stream, $sid, true, $responses, $message);
91 if (!eregi("^\\* ([0-9]+) FETCH", $responses[0][0], $regs)) {
92 $responses = array ();
93 }
94 return $responses;
95 }
96
97
98 /* get sort order from server and
99 * return it as the $id array for
100 * mailbox_display
101 */
102
103 function sqimap_get_sort_order ($imap_stream, $sort, $mbxresponse) {
104 global $default_charset, $thread_sort_messages,
105 $internal_date_sort, $server_sort_array,
106 $sent_folder, $mailbox, $uid_support;
107
108 if (sqsession_is_registered('server_sort_array')) {
109 sqsession_unregister('server_sort_array');
110 }
111
112 $sid = sqimap_session_id($uid_support);
113 $sort_on = array();
114 $reverse = 0;
115 $server_sort_array = array();
116 $sort_test = array();
117 $sort_query = '';
118
119 if ($sort == 6) {
120 if ($uid_support) {
121 if (isset($mbxresponse['UIDNEXT']) && $mbxresponse['UIDNEXT']) {
122 $uidnext = $mbxresponse['UIDNEXT']-1;
123 } else {
124 $uidnext = '*';
125 }
126 $uid_query = "$sid SEARCH UID 1:$uidnext\r\n";
127 fputs($imap_stream, $uid_query);
128 $uids = sqimap_read_data($imap_stream, $sid, true ,$response, $message);
129 if (isset($uids[0])) {
130 if (preg_match("/^\* SEARCH (.+)$/", $uids[0], $regs)) {
131 $server_sort_array = preg_split("/ /", trim($regs[1]));
132 }
133 }
134 if (!preg_match("/OK/", $response)) {
135 $server_sort_array = 'no';
136 }
137 } else {
138 $qty = $mbxresponse['EXISTS'];
139 $server_sort_array = range(1, $qty);
140 }
141 sqsession_register($server_sort_array, 'server_sort_array');
142 return $server_sort_array;
143 }
144
145 $sort_on = array (0=> 'DATE',
146 1=> 'DATE',
147 2=> 'FROM',
148 3=> 'FROM',
149 4=> 'SUBJECT',
150 5=> 'SUBJECT');
151 if ($internal_date_sort == true) {
152 $sort_on[0] = 'ARRIVAL';
153 $sort_on[1] = 'ARRIVAL';
154 }
155 if ($sent_folder == $mailbox) {
156 $sort_on[2] = 'TO';
157 $sort_on[3] = 'TO';
158 }
159 if (!empty($sort_on[$sort])) {
160 $sort_query = "$sid SORT ($sort_on[$sort]) ".strtoupper($default_charset)." ALL\r\n";
161 fputs($imap_stream, $sort_query);
162 $sort_test = sqimap_read_data($imap_stream, $sid, true ,$response, $message);
163 }
164 if (isset($sort_test[0])) {
165 if (preg_match("/^\* SORT (.+)$/", $sort_test[0], $regs)) {
166 $server_sort_array = preg_split("/ /", trim($regs[1]));
167 }
168 }
169 if ($sort == 0 || $sort == 2 || $sort == 4) {
170 $server_sort_array = array_reverse($server_sort_array);
171 }
172 if (!preg_match("/OK/", $response)) {
173 $server_sort_array = 'no';
174 }
175 sqsession_register($server_sort_array, 'server_sort_array');
176 return $server_sort_array;
177 }
178
179
180 function sqimap_get_php_sort_order ($imap_stream, $mbxresponse) {
181 global $uid_support;
182
183 if (sqsession_is_registered('php_sort_array')) {
184 sqsession_unregister('php_sort_array');
185 }
186
187 $sid = sqimap_session_id($uid_support);
188 $php_sort_array = array();
189
190 if ($uid_support) {
191 if (isset($mbxresponse['UIDNEXT']) && $mbxresponse['UIDNEXT']) {
192 $uidnext = $mbxresponse['UIDNEXT']-1;
193 } else {
194 $uidnext = '*';
195 }
196 $uid_query = "$sid SEARCH UID 1:$uidnext\r\n";
197 fputs($imap_stream, $uid_query);
198 $uids = sqimap_read_data($imap_stream, $sid, true ,$response, $message);
199 if (isset($uids[0])) {
200 if (preg_match("/^\* SEARCH (.+)$/", $uids[0], $regs)) {
201 $php_sort_array = preg_split("/ /", trim($regs[1]));
202 }
203 }
204 if (!preg_match("/OK/", $response)) {
205 $php_sort_array = 'no';
206 }
207 } else {
208 $qty = $mbxresponse['EXISTS'];
209 $php_sort_array = range(1, $qty);
210 }
211 sqsession_register($php_sort_array, 'php_sort_array');
212 return $php_sort_array;
213 }
214
215
216 /* returns an indent array for printMessageinfo()
217 this represents the amount of indent needed (value)
218 for this message number (key)
219 */
220
221 function get_parent_level ($imap_stream) {
222 global $sort_by_ref, $default_charset, $thread_new;
223 $parent = "";
224 $child = "";
225 $cutoff = 0;
226
227 /* loop through the threads and take unwanted characters out
228 of the thread string then chop it up
229 */
230 for ($i=0;$i<count($thread_new);$i++) {
231 $thread_new[$i] = preg_replace("/\s\(/", "(", $thread_new[$i]);
232 $thread_new[$i] = preg_replace("/(\d+)/", "$1|", $thread_new[$i]);
233 $thread_new[$i] = preg_split("/\|/", $thread_new[$i], -1, PREG_SPLIT_NO_EMPTY);
234 }
235 $indent_array = array();
236 if (!$thread_new) {
237 $thread_new = array();
238 }
239 /* looping through the parts of one message thread */
240
241 for ($i=0;$i<count($thread_new);$i++) {
242 /* first grab the parent, it does not indent */
243
244 if (isset($thread_new[$i][0])) {
245 if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) {
246 $parent = $regs[1];
247 }
248 }
249 $indent_array[$parent] = 0;
250
251 /* now the children, checking each thread portion for
252 ),(, and space, adjusting the level and space values
253 to get the indent level
254 */
255 $level = 0;
256 $spaces = array();
257 $spaces_total = 0;
258 $indent = 0;
259 $fake = FALSE;
260 for ($k=1;$k<(count($thread_new[$i]))-1;$k++) {
261 $chars = count_chars($thread_new[$i][$k], 1);
262 if (isset($chars['40'])) { /* testing for ( */
263 $level = $level + $chars['40'];
264 }
265 if (isset($chars['41'])) { /* testing for ) */
266 $level = $level - $chars['41'];
267 $spaces[$level] = 0;
268 /* if we were faking lets stop, this portion
269 of the thread is over
270 */
271 if ($level == $cutoff) {
272 $fake = FALSE;
273 }
274 }
275 if (isset($chars['32'])) { /* testing for space */
276 if (!isset($spaces[$level])) {
277 $spaces[$level] = 0;
278 }
279 $spaces[$level] = $spaces[$level] + $chars['32'];
280 }
281 for ($x=0;$x<=$level;$x++) {
282 if (isset($spaces[$x])) {
283 $spaces_total = $spaces_total + $spaces[$x];
284 }
285 }
286 $indent = $level + $spaces_total;
287 /* must have run into a message that broke the thread
288 so we are adjusting for that portion
289 */
290 if ($fake == TRUE) {
291 $indent = $indent +1;
292 }
293 if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) {
294 $child = $regs[1];
295 }
296 /* the thread must be broken if $indent == 0
297 so indent the message once and start faking it
298 */
299 if ($indent == 0) {
300 $indent = 1;
301 $fake = TRUE;
302 $cutoff = $level;
303 }
304 /* dont need abs but if indent was negative
305 errors would occur
306 */
307 $indent_array[$child] = abs($indent);
308 $spaces_total = 0;
309 }
310 }
311 return $indent_array;
312 }
313
314
315 /* returns an array with each element as a string
316 representing one message thread as returned by
317 the IMAP server
318 */
319
320 function get_thread_sort ($imap_stream) {
321 global $thread_new, $sort_by_ref, $default_charset, $server_sort_array, $uid_support;
322 if (sqsession_is_registered('thread_new')) {
323 sqsession_unregister('thread_new');
324 }
325 if (sqsession_is_registered('server_sort_array')) {
326 sqsession_unregister('server_sort_array');
327 }
328 $sid = sqimap_session_id($uid_support);
329 $thread_temp = array ();
330 if ($sort_by_ref == 1) {
331 $sort_type = 'REFERENCES';
332 }
333 else {
334 $sort_type = 'ORDEREDSUBJECT';
335 }
336 $thread_query = "$sid THREAD $sort_type ".strtoupper($default_charset)." ALL\r\n";
337 fputs($imap_stream, $thread_query);
338 $thread_test = sqimap_read_data($imap_stream, $sid, false, $response, $message);
339 if (isset($thread_test[0])) {
340 if (preg_match("/^\* THREAD (.+)$/", $thread_test[0], $regs)) {
341 $thread_list = trim($regs[1]);
342 }
343 }
344 else {
345 $thread_list = "";
346 }
347 if (!preg_match("/OK/", $response)) {
348 $server_sort_array = 'no';
349 return $server_sort_array;
350 }
351 if (isset($thread_list)) {
352 $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
353 }
354 $char_count = count($thread_temp);
355 $counter = 0;
356 $thread_new = array();
357 $k = 0;
358 $thread_new[0] = "";
359 for ($i=0;$i<$char_count;$i++) {
360 if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
361 $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
362 }
363 elseif ($thread_temp[$i] == '(') {
364 $thread_new[$k] .= $thread_temp[$i];
365 $counter++;
366 }
367 elseif ($thread_temp[$i] == ')') {
368 if ($counter > 1) {
369 $thread_new[$k] .= $thread_temp[$i];
370 $counter = $counter - 1;
371 }
372 else {
373 $thread_new[$k] .= $thread_temp[$i];
374 $k++;
375 $thread_new[$k] = "";
376 $counter = $counter - 1;
377 }
378 }
379 }
380 sqsession_register($thread_new, 'thread_new');
381 $thread_new = array_reverse($thread_new);
382 $thread_list = implode(" ", $thread_new);
383 $thread_list = str_replace("(", " ", $thread_list);
384 $thread_list = str_replace(")", " ", $thread_list);
385 $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
386 $server_sort_array = $thread_list;
387 sqsession_register($server_sort_array, 'server_sort_array');
388 return $thread_list;
389 }
390
391 function elapsedTime($start) {
392 $stop = gettimeofday();
393 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec'];
394 return $timepassed;
395 }
396
397 function sqimap_get_small_header_list ($imap_stream, $msg_list) {
398 global $squirrelmail_language, $color, $data_dir, $username, $imap_server_type;
399 global $uid_support;
400
401 /* Get the small headers for each message in $msg_list */
402 $sid = sqimap_session_id($uid_support);
403
404 $maxmsg = sizeof($msg_list);
405 $msgs_str = sqimap_message_list_squisher($msg_list);
406 $results = array();
407 $read_list = array();
408 /*
409 * We need to return the data in the same order as the caller supplied
410 * in $msg_list, but IMAP servers are free to return responses in
411 * whatever order they wish... So we need to re-sort manually
412 */
413 for ($i = 0; $i < sizeof($msg_list); $i++) {
414 $id2index[$msg_list[$i]] = $i;
415 }
416
417 $internaldate = getPref($data_dir, $username, 'internal_date_sort');
418 if ($internaldate) {
419 $query = "$sid FETCH $msgs_str (FLAGS UID RFC822.SIZE INTERNALDATE BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject X-Priority Content-Type)])\r\n";
420 } else {
421 $query = "$sid FETCH $msgs_str (FLAGS UID RFC822.SIZE BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject X-Priority Content-Type)])\r\n";
422 }
423 fputs ($imap_stream, $query);
424 $readin_list = sqimap_read_data_list($imap_stream, $sid, false, $response, $message);
425 $i = 0;
426 foreach ($readin_list as $r) {
427 if (!$uid_support) {
428 if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH/iAU",$r[0], $regs)) {
429 set_up_language($squirrelmail_language);
430 echo '<br><b><font color=$color[2]>' .
431 _("ERROR : Could not complete request.") .
432 '</b><br>' .
433 _("Unknown response from IMAP server: ") . ' 1.' .
434 $r[0] . "</font><br>\n";
435 } else if (! isset($id2index[$regs[1]]) || !count($id2index[$regs[1]])) {
436 set_up_language($squirrelmail_language);
437 echo '<br><b><font color=$color[2]>' .
438 _("ERROR : Could not complete request.") .
439 '</b><br>' .
440 _("Unknown message number in reply from server: ") .
441 $regs[1] . "</font><br>\n";
442 } else {
443 $read_list[$id2index[$regs[1]]] = $r;
444 }
445 } else {
446 if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH.*UID\s+([0-9]+)\s+/iAU",$r[0], $regs)) {
447 set_up_language($squirrelmail_language);
448 echo '<br><b><font color=$color[2]>' .
449 _("ERROR : Could not complete request.") .
450 '</b><br>' .
451 _("Unknown response from IMAP server: ") . ' 1.' .
452 $r[0] . "</font><br>\n";
453 } else if (! isset($id2index[$regs[2]]) || !count($id2index[$regs[2]])) {
454 set_up_language($squirrelmail_language);
455 echo '<br><b><font color=$color[2]>' .
456 _("ERROR : Could not complete request.") .
457 '</b><br>' .
458 _("Unknown message number in reply from server: ") .
459 $regs[2] . "</font><br>\n";
460 } else {
461 $read_list[$id2index[$regs[2]]] = $r;
462 $unique_id = $regs[2];
463 }
464 }
465 }
466 arsort($read_list);
467
468 $patterns = array (
469 "/^To:(.*)\$/AUi",
470 "/^From:(.*)\$/AUi",
471 "/^X-Priority:(.*)\$/AUi",
472 "/^Cc:(.*)\$/AUi",
473 "/^Date:(.*)\$/AUi",
474 "/^Subject:(.*)\$/AUi",
475 "/^Content-Type:(.*)\$/AUi"
476 );
477 $regpattern = '';
478
479 for ($msgi = 0; $msgi < $maxmsg; $msgi++) {
480 $subject = _("(no subject)");
481 $from = _("Unknown Sender");
482 $priority = 0;
483 $messageid = "<>";
484 $cc = "";
485 $to = "";
486 $date = "";
487 $type[0] = "";
488 $type[1] = "";
489 $inrepto = "";
490 $flag_seen = false;
491 $flag_answered = false;
492 $flag_deleted = false;
493 $flag_flagged = false;
494 $read = $read_list[$msgi];
495 $prevline = false;
496
497 foreach ($read as $read_part) {
498 //unfold multi-line headers
499 if ($prevline && strpos($read_part, "\t ") === true) {
500 $read_part = substr($prevline, 0, -2) . preg_replace('/(\t\s+)/',' ',$read_part);
501 }
502 $prevline = $read_part;
503 if ($read_part{0} == '*') {
504 if ($internaldate) {
505 if (preg_match ("/^.+INTERNALDATE\s+\"(.+)\".+/iUA",$read_part, $reg)) {
506 $tmpdate = trim($reg[1]);
507 $tmpdate = str_replace(' ',' ',$tmpdate);
508 $tmpdate = explode(' ',$tmpdate);
509 $date = str_replace('-',' ',$tmpdate[0]) . " " .
510 $tmpdate[1] . " " .
511 $tmpdate[2];
512 }
513 }
514 if (preg_match ("/^.+RFC822.SIZE\s+(\d+).+/iA",$read_part, $reg)) {
515 $size = $reg[1];
516 }
517 if (preg_match("/^.+FLAGS\s+\((.*)\).+/iUA", $read_part, $regs)) {
518 $flags = explode(' ',trim($regs[1]));
519 foreach ($flags as $flag) {
520 $flag = strtolower($flag);
521 if ($flag == '\\seen') {
522 $flag_seen = true;
523 } else if ($flag == '\\answered') {
524 $flag_answered = true;
525 } else if ($flag == '\\deleted') {
526 $flag_deleted = true;
527 } else if ($flag == '\\flagged') {
528 $flag_flagged = true;
529 }
530 }
531 }
532 if (preg_match ("/^.+UID\s+(\d+).+/iA",$read_part, $reg)) {
533 $unique_id = $reg[1];
534 }
535 } else {
536 $firstchar = $read_part{0};
537 if ($firstchar == 'T') {
538 $regpattern = $patterns[0];
539 $id = 1;
540 } else if ($firstchar == 'F') {
541 $regpattern = $patterns[1];
542 $id = 2;
543 } else if ($firstchar == 'X') {
544 $regpattern = $patterns[2];
545 $id = 3;
546 } else if ($firstchar == 'C') {
547 if (strtolower($read_part{1}) == 'c') {
548 $regpattern = $patterns[3];
549 $id = 4;
550 } else if (strtolower($read_part{1}) == 'o') {
551 $regpattern = $patterns[6];
552 $id = 7;
553 }
554 } else if ($firstchar == 'D' && !$internaldate ) {
555 $regpattern = $patterns[4];
556 $id = 5;
557 } else if ($firstchar == 'S') {
558 $regpattern = $patterns[5];
559 $id = 6;
560 } else $regpattern = '';
561
562 if ($regpattern) {
563 if (preg_match ($regpattern, $read_part, $regs)) {
564 switch ($id) {
565 case 1:
566 $to = $regs[1];
567 break;
568 case 2:
569 $from = $regs[1];
570 break;
571 case 3:
572 $priority = $regs[1];
573 break;
574 case 4:
575 $cc = $regs[1];
576 break;
577 case 5:
578 $date = $regs[1];
579 break;
580 case 6:
581 $subject = htmlspecialchars(trim($regs[1]));
582 if ($subject == "") {
583 $subject = _("(no subject)");
584 }
585 break;
586 case 7:
587 $type = strtolower(trim($regs[1]));
588 if ($pos = strpos($type, ";")) {
589 $type = substr($type, 0, $pos);
590 }
591 $type = explode("/", $type);
592 if (!isset($type[1])) {
593 $type[1] = '';
594 }
595 break;
596 default:
597 break;
598 }
599 }
600 }
601 }
602
603 }
604
605 $header = new small_header;
606
607 if ($uid_support) {
608 $header->uid = $unique_id;
609 } else {
610 $header->uid = $msg_list[$msgi];
611 }
612 $header->date = $date;
613 $header->subject = $subject;
614 $header->to = $to;
615 $header->from = $from;
616 $header->priority = $priority;
617 $header->message_id = $messageid;
618 $header->cc = $cc;
619 $header->size = $size;
620 $header->type0 = $type[0];
621 $header->type1 = $type[1];
622 $header->flag_seen = $flag_seen;
623 $header->flag_answered = $flag_answered;
624 $header->flag_deleted = $flag_deleted;
625 $header->flag_flagged = $flag_flagged;
626 $header->inrepto = $inrepto;
627 $result[] = $header;
628 }
629 return $result;
630 }
631
632 function sqimap_get_headerfield($imap_stream, $field) {
633 $sid = sqimap_session_id(false);
634
635 $results = array();
636 $read_list = array();
637
638 $query = "$sid FETCH 1:* (UID BODY.PEEK[HEADER.FIELDS ($field)])\r\n";
639 fputs ($imap_stream, $query);
640 $readin_list = sqimap_read_data_list($imap_stream, $sid, false, $response, $message);
641 $i = 0;
642
643 foreach ($readin_list as $r) {
644 $r = implode('',$r);
645 /* first we unfold the header */
646 $r = str_replace(array("\r\n\t","\r\n\s"),array('',''),$r);
647 /*
648 * now we can make a new header array with each element representing
649 * a headerline
650 */
651 $r = explode("\r\n" , $r);
652 if (!$uid_support) {
653 if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH/iAU",$r[0], $regs)) {
654 set_up_language($squirrelmail_language);
655 echo '<br><b><font color=$color[2]>' .
656 _("ERROR : Could not complete request.") .
657 '</b><br>' .
658 _("Unknown response from IMAP server: ") . ' 1.' .
659 $r[0] . "</font><br>\n";
660 } else {
661 $id = $regs[1];
662 }
663 } else {
664 if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH.*UID\s+([0-9]+)\s+/iAU",$r[0], $regs)) {
665 set_up_language($squirrelmail_language);
666 echo '<br><b><font color=$color[2]>' .
667 _("ERROR : Could not complete request.") .
668 '</b><br>' .
669 _("Unknown response from IMAP server: ") . ' 1.' .
670 $r[0] . "</font><br>\n";
671 } else {
672 $id = $regs[2];
673 }
674 }
675 $field = $r[1];
676 $field = substr($field,strlen($field)+2);
677 $result[] = array($id,$field);
678 }
679 return $result;
680 }
681
682
683
684
685
686 /*
687 * Returns a message array with all the information about a message.
688 * See the documentation folder for more information about this array.
689 */
690 function sqimap_get_message ($imap_stream, $id, $mailbox) {
691 global $uid_support;
692
693 $flags = array();
694 $read = sqimap_run_command ($imap_stream, "FETCH $id (FLAGS BODYSTRUCTURE)", true, $response, $message, $uid_support);
695 if ($read) {
696 if (preg_match('/.+FLAGS\s\((.*)\)\s/AUi',$read[0],$regs)) {
697 if (trim($regs[1])) {
698 $flags = preg_split('/ /', $regs[1],-1,'PREG_SPLIT_NI_EMPTY');
699 }
700 }
701 } else {
702 echo "ERROR Yeah I know, not a very usefull errormessage (id = $id, mailbox = $mailbox sqimap_get_message)";
703 exit;
704 }
705 $bodystructure = implode('',$read);
706 $msg = mime_structure($bodystructure,$flags);
707 $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, $uid_support);
708 $rfc822_header = new Rfc822Header();
709 $rfc822_header->parseHeader($read);
710 $msg->rfc822_header = $rfc822_header;
711 return $msg;
712 }
713
714 /* Wrapper function that reformats the header information. */
715 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
716 global $uid_support;
717 $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, $uid_support);
718 $header = sqimap_get_header($imap_stream, $read);
719 $header->id = $id;
720 $header->mailbox = $mailbox;
721 return $header;
722 }
723
724 /* Wrapper function that reformats the entity header information. */
725 function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
726 global $uid_support;
727 $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent.HEADER]", true, $response, $message, $uid_support);
728 $header = sqimap_get_header($imap_stream, $read);
729 $header->id = $id;
730 $header->mailbox = $mailbox;
731 return $header;
732 }
733
734
735 /* Wrapper function that returns entity headers for use by decodeMime */
736 /*
737 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
738 $header = sqimap_get_header($imap_stream, $read);
739 $type0 = $header["TYPE0"];
740 $type1 = $header["TYPE1"];
741 $bound = $header["BOUNDARY"];
742 $encoding = $header["ENCODING"];
743 $charset = $header["CHARSET"];
744 $filename = $header["FILENAME"];
745 }
746
747 /* function to get the mime headers */
748 function sqimap_get_mime_ent_header ($imap_stream, $id, $mailbox, $ent) {
749 global $uid_support;
750 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.MIME]", true, $response, $message, $uid_support);
751 $header = sqimap_get_header($imap_stream, $read);
752 $header->id = $id;
753 $header->mailbox = $mailbox;
754 return $header;
755 }
756
757 /* Returns the body of a message. */
758 function sqimap_get_message_body ($imap_stream, &$header) {
759 // return decodeMime($imap_stream, $header->id);
760 }
761
762 ?>