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