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