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