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