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