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