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