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