two more subpackage blocks
[squirrelmail.git] / functions / imap_messages.php
CommitLineData
59177427 1<?php
7350889b 2
35586184 3/**
4 * imap_messages.php
5 *
82d304a0 6 * Copyright (c) 1999-2004 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
d6c32258 10 * NOTE: Quite a few functions in this file are obsolete
35586184 11 *
eb19bc67 12 * @version $Id$
d6c32258 13 * @package squirrelmail
eb19bc67 14 * @subpackage imap
35586184 15 */
052e0c26 16
d6c32258 17/**
18 * Copies specified messages to specified folder
19 * @param int $imap_stream The resource ID for the IMAP connection
20 * @param string $start Beginning of range to copy
21 * @param string $end End of the range to copy
22 * @param string $mailbox Which box to copy to
23 * @deprecated This function is obsolete and should not be used
24 */
35586184 25function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
6201339c 26 $read = sqimap_run_command ($imap_stream, "COPY $start:$end " . sqimap_encode_mailbox_name($mailbox), true, $response, $message, TRUE);
97f7ddf2 27}
28
7c3e0802 29/**
30* copy a range of messages ($id) to another mailbox ($mailbox)
d6c32258 31* @param int $imap_stream The resource ID for the IMAP socket
32* @param string $id The list of messages to copy
33* @param string $mailbox The destination to copy to
34* @return void
7c3e0802 35*/
034fddf9 36function sqimap_msgs_list_copy ($imap_stream, $id, $mailbox) {
034fddf9 37 $msgs_id = sqimap_message_list_squisher($id);
6201339c 38 $read = sqimap_run_command ($imap_stream, "COPY $msgs_id " . sqimap_encode_mailbox_name($mailbox), true, $response, $message, TRUE);
7c3e0802 39}
40
41/**
42* move a range of messages ($id) to another mailbox. Deletes the originals.
d6c32258 43* @param int $imap_stream The resource ID for the IMAP socket
44* @param string $id The list of messages to move
45* @param string $mailbox The destination to move to
46* @return void
7c3e0802 47*/
48function sqimap_msgs_list_move ($imap_stream, $id, $mailbox) {
7c3e0802 49 $msgs_id = sqimap_message_list_squisher($id);
6201339c 50 $read = sqimap_run_command ($imap_stream, "COPY $msgs_id " . sqimap_encode_mailbox_name($mailbox), true, $response, $message, TRUE);
51 $read = sqimap_run_command ($imap_stream, "STORE $msgs_id +FLAGS (\\Deleted)", true, $response,$message, TRUE);
034fddf9 52}
53
54
d6c32258 55/**
56 * Deletes specified messages and moves them to trash if possible
57 * @deprecated This function is obsolete and should no longer be used
58 * @param int $imap_steam The resource ID for the IMAP connection
59 * @param string $start Start of range
60 * @param string $end End of range
61 * @param string $mailbox Mailbox messages are being deleted from
62 * @return void
63 */
abafb676 64function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox, $bypass_trash=false) {
6201339c 65 global $move_to_trash, $trash_folder, $auto_expunge;
97f7ddf2 66
abafb676 67 if (($move_to_trash == true) && ($bypass_trash != true) &&
68 (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) {
97f7ddf2 69 sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
97f7ddf2 70 }
d8a8203a 71 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted", true);
97f7ddf2 72}
73
6c540963 74function sqimap_msgs_list_delete ($imap_stream, $mailbox, $id, $bypass_trash=false) {
6201339c 75 global $move_to_trash, $trash_folder;
034fddf9 76 $msgs_id = sqimap_message_list_squisher($id);
abafb676 77 if (($move_to_trash == true) && ($bypass_trash != true) &&
78 (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder)) ) {
6201339c 79 $read = sqimap_run_command ($imap_stream, "COPY $msgs_id " . sqimap_encode_mailbox_name($trash_folder), true, $response, $message, TRUE);
034fddf9 80 }
6201339c 81 $read = sqimap_run_command ($imap_stream, "STORE $msgs_id +FLAGS (\\Deleted)", true, $response, $message, TRUE);
034fddf9 82}
83
84
d6c32258 85/**
86 * Sets the specified messages with specified flag
87 */
d8a8203a 88function sqimap_messages_flag ($imap_stream, $start, $end, $flag, $handle_errors) {
6201339c 89 $read = sqimap_run_command ($imap_stream, "STORE $start:$end +FLAGS (\\$flag)", $handle_errors, $response, $message, TRUE);
97f7ddf2 90}
91
034fddf9 92function sqimap_toggle_flag($imap_stream, $id, $flag, $set, $handle_errors) {
034fddf9 93 $msgs_id = sqimap_message_list_squisher($id);
94 $set_string = ($set ? '+' : '-');
6201339c 95 $read = sqimap_run_command ($imap_stream, "STORE $msgs_id ".$set_string."FLAGS ($flag)", $handle_errors, $response, $message, TRUE);
034fddf9 96}
97
48af4b64 98/** @deprecated */
97f7ddf2 99function sqimap_get_small_header ($imap_stream, $id, $sent) {
098ea084 100 $res = sqimap_get_small_header_list($imap_stream, $id, $sent);
97f7ddf2 101 return $res[0];
102}
3411d4ec 103
48af4b64 104/**
3411d4ec 105 * Sort the message list and crunch to be as small as possible
97f7ddf2 106 * (overflow could happen, so make it small if possible)
107 */
108function sqimap_message_list_squisher($messages_array) {
109 if( !is_array( $messages_array ) ) {
fe70ae27 110 return $messages_array;
97f7ddf2 111 }
2d34da11 112
97f7ddf2 113 sort($messages_array, SORT_NUMERIC);
114 $msgs_str = '';
115 while ($messages_array) {
116 $start = array_shift($messages_array);
117 $end = $start;
118 while (isset($messages_array[0]) && $messages_array[0] == $end + 1) {
119 $end = array_shift($messages_array);
120 }
121 if ($msgs_str != '') {
122 $msgs_str .= ',';
123 }
124 $msgs_str .= $start;
125 if ($start != $end) {
126 $msgs_str .= ':' . $end;
127 }
128 }
97f7ddf2 129 return $msgs_str;
3411d4ec 130}
97f7ddf2 131
48af4b64 132/**
133 * Get sort order from server and return it as the $id array for mailbox_display.
aa0da530 134 */
2d34da11 135function sqimap_get_sort_order ($imap_stream, $sort, $mbxresponse) {
ba80e62b 136 global $default_charset, $thread_sort_messages,
137 $internal_date_sort, $server_sort_array,
6201339c 138 $sent_folder, $mailbox;
2d34da11 139
d7c82551 140 if (sqsession_is_registered('server_sort_array')) {
9eb0fbd4 141 sqsession_unregister('server_sort_array');
60a3e687 142 }
2d34da11 143
aa0da530 144 $sort_on = array();
145 $reverse = 0;
146 $server_sort_array = array();
147 $sort_test = array();
148 $sort_query = '';
2d34da11 149
150 if ($sort == 6) {
6201339c 151 if (isset($mbxresponse['UIDNEXT']) && $mbxresponse['UIDNEXT']) {
152 $uidnext = $mbxresponse['UIDNEXT']-1;
2d34da11 153 } else {
6201339c 154 $uidnext = '*';
155 }
156 $query = "SEARCH UID 1:$uidnext";
157 $uids = sqimap_run_command ($imap_stream, $query, true, $response, $message, true);
158 if (isset($uids[0])) {
159 if (preg_match("/^\* SEARCH (.+)$/", $uids[0], $regs)) {
160 $server_sort_array = preg_split("/ /", trim($regs[1]));
161 }
162 }
163 if (!preg_match("/OK/", $response)) {
164 $server_sort_array = 'no';
cdca177a 165 }
770f0e03 166 $server_sort_array = array_reverse($server_sort_array);
9eb0fbd4 167 sqsession_register($server_sort_array, 'server_sort_array');
2d34da11 168 return $server_sort_array;
169 }
170
aa0da530 171 $sort_on = array (0=> 'DATE',
172 1=> 'DATE',
173 2=> 'FROM',
174 3=> 'FROM',
175 4=> 'SUBJECT',
d11ccd82 176 5=> 'SUBJECT');
aa0da530 177 if ($internal_date_sort == true) {
178 $sort_on[0] = 'ARRIVAL';
179 $sort_on[1] = 'ARRIVAL';
180 }
ba80e62b 181 if ($sent_folder == $mailbox) {
182 $sort_on[2] = 'TO';
183 $sort_on[3] = 'TO';
184 }
aa0da530 185 if (!empty($sort_on[$sort])) {
a18594b2 186 $query = "SORT ($sort_on[$sort]) ".strtoupper($default_charset).' ALL';
6201339c 187 $sort_test = sqimap_run_command ($imap_stream, $query, true, $response, $message, TRUE);
aa0da530 188 }
2728fa19 189 if (isset($sort_test[0])) {
a8a70251 190 for ($i=0,$iCnt=count($sort_test);$i<$iCnt;++$i) {
191 if (preg_match("/^\* SORT (.+)$/", $sort_test[$i], $regs)) {
cdca177a 192 $server_sort_array = preg_split("/ /", trim($regs[1]));
a8a70251 193 break;
cdca177a 194 }
a8a70251 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
26b22b20 207/**
208 * Get sort order from server if server does not have the SORT extension
209 * and return it as array for mailbox_display.
210 *
211 * @param resource $imap_stream
212 * @param array $mbxresponse response from a sqimap_mailbox_select
213 * @return array $php_sort_array
214 */
2d34da11 215function sqimap_get_php_sort_order ($imap_stream, $mbxresponse) {
2d34da11 216
d7c82551 217 if (sqsession_is_registered('php_sort_array')) {
9eb0fbd4 218 sqsession_unregister('php_sort_array');
2d34da11 219 }
220
2d34da11 221 $php_sort_array = array();
222
6201339c 223 if (isset($mbxresponse['UIDNEXT']) && $mbxresponse['UIDNEXT']) {
224 $uidnext = $mbxresponse['UIDNEXT']-1;
225 } else {
226 $uidnext = '*';
227 }
228 $query = "SEARCH UID 1:$uidnext";
229 $uids = sqimap_run_command ($imap_stream, $query, true, $response, $message, true);
230 if (isset($uids[0])) {
231 $php_sort_array = array();
232 // EIMS workaround. EIMS returns the result as multiple untagged SEARCH responses
233 foreach($uids as $line) {
234 if (preg_match("/^\* SEARCH (.+)$/", $line, $regs)) {
235 $php_sort_array += preg_split("/ /", trim($regs[1]));
cdca177a 236 }
237 }
6201339c 238 }
239 if (!preg_match("/OK/", $response)) {
240 $php_sort_array = 'no';
2d34da11 241 }
9eb0fbd4 242 sqsession_register($php_sort_array, 'php_sort_array');
2d34da11 243 return $php_sort_array;
cdca177a 244}
245
2d34da11 246
48af4b64 247/**
248 * Returns an indent array for printMessageinfo()
249 * This represents the amount of indent needed (value),
250 * for this message number (key)
251 */
7c612fdd 252function get_parent_level ($imap_stream) {
253 global $sort_by_ref, $default_charset, $thread_new;
48af4b64 254 $parent = '';
255 $child = '';
256 $cutoff = 0;
cdca177a 257
288bbce0 258 /* loop through the threads and take unwanted characters out
259 of the thread string then chop it up
260 */
7c612fdd 261 for ($i=0;$i<count($thread_new);$i++) {
262 $thread_new[$i] = preg_replace("/\s\(/", "(", $thread_new[$i]);
263 $thread_new[$i] = preg_replace("/(\d+)/", "$1|", $thread_new[$i]);
264 $thread_new[$i] = preg_split("/\|/", $thread_new[$i], -1, PREG_SPLIT_NO_EMPTY);
265 }
266 $indent_array = array();
267 if (!$thread_new) {
474528eb 268 $thread_new = array();
7c612fdd 269 }
288bbce0 270 /* looping through the parts of one message thread */
cdca177a 271
7c612fdd 272 for ($i=0;$i<count($thread_new);$i++) {
288bbce0 273 /* first grab the parent, it does not indent */
cdca177a 274
7c612fdd 275 if (isset($thread_new[$i][0])) {
288bbce0 276 if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) {
277 $parent = $regs[1];
278 }
7c612fdd 279 }
280 $indent_array[$parent] = 0;
288bbce0 281
282 /* now the children, checking each thread portion for
283 ),(, and space, adjusting the level and space values
284 to get the indent level
285 */
286 $level = 0;
474528eb 287 $spaces = array();
288 $spaces_total = 0;
7c612fdd 289 $indent = 0;
288bbce0 290 $fake = FALSE;
7c612fdd 291 for ($k=1;$k<(count($thread_new[$i]))-1;$k++) {
292 $chars = count_chars($thread_new[$i][$k], 1);
288bbce0 293 if (isset($chars['40'])) { /* testing for ( */
294 $level = $level + $chars['40'];
7c612fdd 295 }
288bbce0 296 if (isset($chars['41'])) { /* testing for ) */
297 $level = $level - $chars['41'];
474528eb 298 $spaces[$level] = 0;
288bbce0 299 /* if we were faking lets stop, this portion
300 of the thread is over
301 */
302 if ($level == $cutoff) {
303 $fake = FALSE;
7c612fdd 304 }
305 }
288bbce0 306 if (isset($chars['32'])) { /* testing for space */
474528eb 307 if (!isset($spaces[$level])) {
308 $spaces[$level] = 0;
309 }
310 $spaces[$level] = $spaces[$level] + $chars['32'];
311 }
312 for ($x=0;$x<=$level;$x++) {
313 if (isset($spaces[$x])) {
314 $spaces_total = $spaces_total + $spaces[$x];
315 }
288bbce0 316 }
474528eb 317 $indent = $level + $spaces_total;
288bbce0 318 /* must have run into a message that broke the thread
319 so we are adjusting for that portion
320 */
321 if ($fake == TRUE) {
322 $indent = $indent +1;
7c612fdd 323 }
324 if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) {
325 $child = $regs[1];
326 }
288bbce0 327 /* the thread must be broken if $indent == 0
328 so indent the message once and start faking it
329 */
330 if ($indent == 0) {
331 $indent = 1;
332 $fake = TRUE;
333 $cutoff = $level;
334 }
335 /* dont need abs but if indent was negative
336 errors would occur
337 */
7c612fdd 338 $indent_array[$child] = abs($indent);
474528eb 339 $spaces_total = 0;
cdca177a 340 }
7c612fdd 341 }
342 return $indent_array;
343}
344
345
48af4b64 346/**
347 * Returns an array with each element as a string representing one
348 * message-thread as returned by the IMAP server.
349 */
7c612fdd 350function get_thread_sort ($imap_stream) {
6201339c 351 global $thread_new, $sort_by_ref, $default_charset, $server_sort_array;
d7c82551 352 if (sqsession_is_registered('thread_new')) {
9eb0fbd4 353 sqsession_unregister('thread_new');
7c612fdd 354 }
d7c82551 355 if (sqsession_is_registered('server_sort_array')) {
9eb0fbd4 356 sqsession_unregister('server_sort_array');
60a3e687 357 }
7c612fdd 358 $thread_temp = array ();
359 if ($sort_by_ref == 1) {
360 $sort_type = 'REFERENCES';
361 }
362 else {
363 $sort_type = 'ORDEREDSUBJECT';
364 }
a18594b2 365 $query = "THREAD $sort_type ".strtoupper($default_charset)." ALL";
6201339c 366 $thread_test = sqimap_run_command ($imap_stream, $query, true, $response, $message, TRUE);
2728fa19 367 if (isset($thread_test[0])) {
9978560b 368 for ($i=0,$iCnt=count($thread_test);$i<$iCnt;++$i) {
297e99ab 369 if (preg_match("/^\* THREAD (.+)$/", $thread_test[$i], $regs)) {
9978560b 370 $thread_list = trim($regs[1]);
371 break;
372 }
373 }
7c612fdd 374 }
375 else {
376 $thread_list = "";
377 }
26eca02e 378 if (!preg_match("/OK/", $response)) {
cdca177a 379 $server_sort_array = 'no';
380 return $server_sort_array;
381 }
474528eb 382 if (isset($thread_list)) {
383 $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
384 }
7c612fdd 385 $char_count = count($thread_temp);
386 $counter = 0;
387 $thread_new = array();
388 $k = 0;
389 $thread_new[0] = "";
390 for ($i=0;$i<$char_count;$i++) {
391 if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
392 $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
393 }
394 elseif ($thread_temp[$i] == '(') {
395 $thread_new[$k] .= $thread_temp[$i];
396 $counter++;
397 }
398 elseif ($thread_temp[$i] == ')') {
399 if ($counter > 1) {
400 $thread_new[$k] .= $thread_temp[$i];
401 $counter = $counter - 1;
402 }
403 else {
404 $thread_new[$k] .= $thread_temp[$i];
405 $k++;
406 $thread_new[$k] = "";
407 $counter = $counter - 1;
408 }
409 }
410 }
9eb0fbd4 411 sqsession_register($thread_new, 'thread_new');
7c612fdd 412 $thread_new = array_reverse($thread_new);
413 $thread_list = implode(" ", $thread_new);
414 $thread_list = str_replace("(", " ", $thread_list);
415 $thread_list = str_replace(")", " ", $thread_list);
416 $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
60a3e687 417 $server_sort_array = $thread_list;
9eb0fbd4 418 sqsession_register($server_sort_array, 'server_sort_array');
7c612fdd 419 return $thread_list;
420}
421
034fddf9 422
7b07404c 423function elapsedTime($start) {
424 $stop = gettimeofday();
425 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec'];
426 return $timepassed;
427}
7c612fdd 428
ea5fa593 429// only used in sqimap_get_small_header_list
a18594b2 430function parseString($read,&$i) {
431 $char = $read{$i};
432 $s = '';
433 if ($char == '"') {
434 $iPos = ++$i;
435 while (true) {
436 $iPos = strpos($read,'"',$iPos);
437 if (!$iPos) break;
438 if ($iPos && $read{$iPos -1} != '\\') {
439 $s = substr($read,$i,($iPos-$i));
440 $i = $iPos;
441 break;
442 }
443 $iPos++;
444 if ($iPos > strlen($read)) {
445 break;
446 }
447 }
448 } else if ($char == '{') {
449 $lit_cnt = '';
450 ++$i;
451 $iPos = strpos($read,'}',$i);
452 if ($iPos) {
453 $lit_cnt = substr($read, $i, $iPos - $i);
454 $i += strlen($lit_cnt) + 3; /* skip } + \r + \n */
455 /* Now read the literal */
456 $s = ($lit_cnt ? substr($read,$i,$lit_cnt): '');
457 $i += $lit_cnt;
458 /* temp bugfix (SM 1.5 will have a working clean version)
459 too much work to implement that version right now */
460 --$i;
461 } else { /* should never happen */
462 $i += 3; /* } + \r + \n */
463 $s = '';
464 }
465 } else {
466 return false;
467 }
468 ++$i;
469 return $s;
470}
471
ea5fa593 472// only used in sqimap_get_small_header_list
a18594b2 473function parseArray($read,&$i) {
474 $i = strpos($read,'(',$i);
475 $i_pos = strpos($read,')',$i);
476 $s = substr($read,$i+1,$i_pos - $i -1);
477 $a = explode(' ',$s);
478 if ($i_pos) {
479 $i = $i_pos+1;
480 return $a;
481 } else {
482 return false;
483 }
484}
485
486function sqimap_get_small_header_list ($imap_stream, $msg_list, $show_num=false) {
e7be116b 487 global $squirrelmail_language, $color, $data_dir, $username, $imap_server_type;
6201339c 488 global $allow_server_sort;
97f7ddf2 489 /* Get the small headers for each message in $msg_list */
97f7ddf2 490 $maxmsg = sizeof($msg_list);
a18594b2 491 if ($show_num != '999999') {
492 $msgs_str = sqimap_message_list_squisher($msg_list);
493 } else {
494 $msgs_str = '1:*';
495 }
cef054e4 496 $messages = array();
97f7ddf2 497 $read_list = array();
a18594b2 498
3411d4ec 499 /*
97f7ddf2 500 * We need to return the data in the same order as the caller supplied
501 * in $msg_list, but IMAP servers are free to return responses in
502 * whatever order they wish... So we need to re-sort manually
503 */
504 for ($i = 0; $i < sizeof($msg_list); $i++) {
a18594b2 505 $messages["$msg_list[$i]"] = array();
97f7ddf2 506 }
cdca177a 507
7b07404c 508 $internaldate = getPref($data_dir, $username, 'internal_date_sort');
509 if ($internaldate) {
a18594b2 510 $query = "FETCH $msgs_str (FLAGS UID RFC822.SIZE INTERNALDATE BODY.PEEK[HEADER.FIELDS (Date To Cc From Subject X-Priority Content-Type)])";
7b07404c 511 } else {
a18594b2 512 $query = "FETCH $msgs_str (FLAGS UID RFC822.SIZE BODY.PEEK[HEADER.FIELDS (Date To Cc From Subject X-Priority Content-Type)])";
7b07404c 513 }
6201339c 514 $read_list = sqimap_run_command_list ($imap_stream, $query, true, $response, $message, TRUE);
2d34da11 515 $i = 0;
a18594b2 516
517 foreach ($read_list as $r) {
2a9b0fad 518 /* initialize/reset vars */
97f7ddf2 519 $subject = _("(no subject)");
10c50790 520 $from = _("Unknown sender");
97f7ddf2 521 $priority = 0;
9b761dbd 522 $messageid = '<>';
2a9b0fad 523 $type = array('','');
524 $cc = $to = $inrepto = '';
525 // use unset because we do isset below
526 unset($date);
cef054e4 527 $flag_seen = $flag_answered = $flag_deleted = $flag_flagged = false;
a18594b2 528
529 $read = implode('',$r);
530
531 /*
532 * #id<space>FETCH<space>(
533 */
534
535 /* extract the message id */
536 $i_space = strpos($read,' ',2);
537 $id = substr($read,2,$i_space-2);
538 $fetch = substr($read,$i_space+1,5);
539 if (!is_numeric($id) && $fetch !== 'FETCH') {
540 set_up_language($squirrelmail_language);
541 echo '<br><b><font color=$color[2]>' .
542 _("ERROR : Could not complete request.") .
543 '</b><br>' .
544 _("Unknown response from IMAP server: ") . ' 1.' .
545 htmlspecialchars($read) . "</font><br>\n";
546 break;
547 }
548 $i = strpos($read,'(',$i_space+5);
549 $read = substr($read,$i+1);
550 $i_len = strlen($read);
551 $i = 0;
552 while ($i < $i_len && $i !== false) {
553 /* get argument */
554 $read = trim(substr($read,$i));
555 $i_len = strlen($read);
556 $i = strpos($read,' ');
557 $arg = substr($read,0,$i);
558 ++$i;
559 switch ($arg)
560 {
561 case 'UID':
562 $i_pos = strpos($read,' ',$i);
563 if (!$i_pos) {
564 $i_pos = strpos($read,')',$i);
cdca177a 565 }
a18594b2 566 if ($i_pos) {
567 $unique_id = substr($read,$i,$i_pos-$i);
568 $i = $i_pos+1;
569 } else {
570 break 3;
cdca177a 571 }
a18594b2 572 break;
573 case 'FLAGS':
574 $flags = parseArray($read,$i);
575 if (!$flags) break 3;
576 foreach ($flags as $flag) {
577 $flag = strtolower($flag);
578 switch ($flag)
579 {
580 case '\\seen': $flag_seen = true; break;
581 case '\\answered': $flag_answered = true; break;
582 case '\\deleted': $flag_deleted = true; break;
583 case '\\flagged': $flag_flagged = true; break;
584 default: break;
cdca177a 585 }
586 }
a18594b2 587 break;
588 case 'RFC822.SIZE':
589 $i_pos = strpos($read,' ',$i);
590 if (!$i_pos) {
591 $i_pos = strpos($read,')',$i);
cdca177a 592 }
a18594b2 593 if ($i_pos) {
594 $size = substr($read,$i,$i_pos-$i);
595 $i = $i_pos+1;
596 } else {
597 break 3;
598 }
599
600 break;
601 case 'INTERNALDATE':
27932b08 602 $date = parseString($read,$i);
603 //if ($tmpdate === false) break 3;
604 //$tmpdate = str_replace(' ',' ',$tmpdate);
605 //$tmpdate = explode(' ',$tmpdate);
606 //$date = str_replace('-',' ',$tmpdate[0]) . " " .
607 // $tmpdate[1] . ' ' . $tmpdate[2];
a18594b2 608 break;
609 case 'BODY.PEEK[HEADER.FIELDS':
610 case 'BODY[HEADER.FIELDS':
611 $i = strpos($read,'{',$i);
612 $header = parseString($read,$i);
92a52cda 613 if ($header === false) break 2;
2a9b0fad 614 /* First we replace all \r\n by \n, and unfold the header */
615 $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $header));
a18594b2 616 /* Now we can make a new header array with */
617 /* each element representing a headerline */
2a9b0fad 618 $hdr = explode("\n" , $hdr);
a18594b2 619 foreach ($hdr as $line) {
620 $pos = strpos($line, ':');
621 if ($pos > 0) {
622 $field = strtolower(substr($line, 0, $pos));
623 if (!strstr($field,' ')) { /* valid field */
624 $value = trim(substr($line, $pos+1));
625 switch($field)
626 {
627 case 'to': $to = $value; break;
628 case 'cc': $cc = $value; break;
629 case 'from': $from = $value; break;
27932b08 630 case 'date': $date = $value; break;
a18594b2 631 case 'x-priority': $priority = $value; break;
632 case 'subject':
633 $subject = $value;
cdca177a 634 if ($subject == "") {
635 $subject = _("(no subject)");
636 }
637 break;
a18594b2 638 case 'content-type':
639 $type = $value;
cdca177a 640 if ($pos = strpos($type, ";")) {
641 $type = substr($type, 0, $pos);
642 }
643 $type = explode("/", $type);
cef054e4 644 if(!is_array($type)) {
645 $type[0] = 'text';
646 }
cdca177a 647 break;
a18594b2 648 default: break;
649 }
cdca177a 650 }
651 }
652 }
a18594b2 653 break;
654 default:
655 ++$i;
656 break;
cdca177a 657 }
cdca177a 658 }
cef054e4 659 if (isset($date)) {
660 $date = str_replace(' ', ' ', $date);
661 $tmpdate = explode(' ', trim($date));
662 } else {
2a9b0fad 663 $tmpdate = $date = array();
cef054e4 664 }
6201339c 665 $msgi ="$unique_id";
666 $messages[$msgi]['ID'] = $unique_id;
cef054e4 667 $messages[$msgi]['TIME_STAMP'] = getTimeStamp($tmpdate);
668 $messages[$msgi]['DATE_STRING'] = getDateString($messages[$msgi]['TIME_STAMP']);
0c1c4c19 669 $messages[$msgi]['FROM'] = $from; //parseAddress($from);
098ea084 670 $messages[$msgi]['SUBJECT'] = $subject;
671// if (handleAsSent($mailbox)) {
0c1c4c19 672 $messages[$msgi]['TO'] = $to; //parseAddress($to);
098ea084 673// }
cef054e4 674 $messages[$msgi]['PRIORITY'] = $priority;
0c1c4c19 675 $messages[$msgi]['CC'] = $cc; //parseAddress($cc);
cef054e4 676 $messages[$msgi]['SIZE'] = $size;
677 $messages[$msgi]['TYPE0'] = $type[0];
678 $messages[$msgi]['FLAG_DELETED'] = $flag_deleted;
679 $messages[$msgi]['FLAG_ANSWERED'] = $flag_answered;
680 $messages[$msgi]['FLAG_SEEN'] = $flag_seen;
681 $messages[$msgi]['FLAG_FLAGGED'] = $flag_flagged;
682
683 /* non server sort stuff */
684 if (!$allow_server_sort) {
a18594b2 685 $from = parseAddress($from);
686 if ($from[0][1]) {
687 $from = decodeHeader($from[0][1]);
688 } else {
689 $from = $from[0][0];
690 }
691 $messages[$msgi]['FROM-SORT'] = $from;
692 $subject_sort = strtolower(decodeHeader($subject));
693 if (preg_match("/^(vedr|sv|re|aw):\s*(.*)$/si", $subject_sort, $matches)){
cef054e4 694 $messages[$msgi]['SUBJECT-SORT'] = $matches[2];
a18594b2 695 } else {
696 $messages[$msgi]['SUBJECT-SORT'] = $subject_sort;
697 }
cdca177a 698 }
a18594b2 699 ++$msgi;
97f7ddf2 700 }
a18594b2 701 array_reverse($messages);
702 $new_messages = array();
703 foreach ($messages as $i =>$message) {
704 $new_messages[] = $message;
705 }
706 return $new_messages;
97f7ddf2 707}
708
48af4b64 709/**
3411d4ec 710 * Returns a message array with all the information about a message.
711 * See the documentation folder for more information about this array.
712 */
97f7ddf2 713function sqimap_get_message ($imap_stream, $id, $mailbox) {
cdca177a 714
2d34da11 715 $flags = array();
6201339c 716 $read = sqimap_run_command ($imap_stream, "FETCH $id (FLAGS BODYSTRUCTURE)", true, $response, $message, TRUE);
114f2a24 717 if ($read) {
b69a13a4 718 if (preg_match('/.+FLAGS\s\((.*)\)\s/AUi',$read[0],$regs)) {
719 if (trim($regs[1])) {
720 $flags = preg_split('/ /', $regs[1],-1,'PREG_SPLIT_NI_EMPTY');
721 }
722 }
114f2a24 723 } else {
b69a13a4 724 /* the message was not found, maybe the mailbox was modified? */
725 global $sort, $startMessage, $color;
726
727 $errmessage = _("The server couldn't find the message you requested.") .
728 '<p>'._("Most probably your message list was out of date and the message has been moved away or deleted (perhaps by another program accessing the same mailbox).");
729 /* this will include a link back to the message list */
730 error_message($errmessage, $mailbox, $sort, $startMessage, $color);
731 exit;
2d34da11 732 }
733 $bodystructure = implode('',$read);
734 $msg = mime_structure($bodystructure,$flags);
6201339c 735 $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, TRUE);
19d470aa 736 $rfc822_header = new Rfc822Header();
767ace1f 737 $rfc822_header->parseHeader($read);
738 $msg->rfc822_header = $rfc822_header;
2d34da11 739 return $msg;
97f7ddf2 740}
741
052e0c26 742?>