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