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