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