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