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