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