updating pot
[squirrelmail.git] / functions / imap_messages.php
CommitLineData
59177427 1<?php
7350889b 2
35586184 3/**
0fdc2fb6 4* imap_messages.php
5*
6* Copyright (c) 1999-2004 The SquirrelMail Project Team
7* Licensed under the GNU GPL. For full terms see the file COPYING.
8*
9* This implements functions that manipulate messages
10* NOTE: Quite a few functions in this file are obsolete
11*
12* @version $Id$
13* @package squirrelmail
14* @subpackage imap
15*/
052e0c26 16
d6c32258 17/**
0fdc2fb6 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) {
1c198ef7 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/**
0fdc2fb6 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/**
0fdc2fb6 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/**
0fdc2fb6 105* Sort the message list and crunch to be as small as possible
106* (overflow could happen, so make it small if possible)
107*/
97f7ddf2 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/**
ffb776c4 133* Retrieves an array with a sorted uid list. Sorting is done on the imap server
134*
135* @param resource $imap_stream IMAP socket connection
136* @param string $sSortField Field to sort on
137* @param bool $reverse Reverse order search
138* @return array $id sorted uid list
0fdc2fb6 139*/
e0e30169 140function sqimap_get_sort_order ($imap_stream, $sSortField,$reverse) {
ffb776c4 141 global $default_charset,
142 $sent_folder;
2d34da11 143
ffb776c4 144 $id = array();
aa0da530 145 $sort_test = array();
146 $sort_query = '';
2d34da11 147
ffb776c4 148 if ($sSortField) {
149 if ($reverse) {
150 $sSortField = 'REVERSE '.$sSortField;
151 }
152 $query = "SORT ($sSortField) ".strtoupper($default_charset).' ALL';
6201339c 153 $sort_test = sqimap_run_command ($imap_stream, $query, true, $response, $message, TRUE);
aa0da530 154 }
2728fa19 155 if (isset($sort_test[0])) {
ffb776c4 156 for ($i=0,$iCnt=count($sort_test);$i<$iCnt;++$i) {
157 if (preg_match("/^\* SORT (.+)$/", $sort_test[$i], $regs)) {
158 $id = preg_split("/ /", trim($regs[1]));
159 break;
160 }
cdca177a 161 }
0fdc2fb6 162 }
26eca02e 163 if (!preg_match("/OK/", $response)) {
ffb776c4 164 return false;
165 } else {
166 return $id;
cdca177a 167 }
aa0da530 168}
2d34da11 169
26b22b20 170/**
ffb776c4 171* Retrieves an array with a sorted uid list. Sorting is done by SquirrelMail
0fdc2fb6 172*
ffb776c4 173* @param resource $imap_stream IMAP socket connection
174* @param string $sSortField Field to sort on
175* @param bool $reverse Reverse order search
76f29d49 176* @return array $aUid sorted uid list
0fdc2fb6 177*/
ffb776c4 178function get_squirrel_sort ($imap_stream, $sSortField, $reverse = false) {
e0e30169 179 if ($sSortField != 'RFC822.SIZE' && $sSortField != 'INTERNALDATE') {
ffb776c4 180 $msgs = sqimap_get_small_header_list($imap_stream, false, '*',
e0e30169 181 array($sSortField), array());
ffb776c4 182 } else {
183 $msgs = sqimap_get_small_header_list($imap_stream, false, '*',
e0e30169 184 array(), array($sSortField));
ffb776c4 185 }
c2e29558 186 $aUid = array();
76f29d49 187 $walk = false;
ffb776c4 188 switch ($sSortField) {
76f29d49 189 // natcasesort section
ffb776c4 190 case 'FROM':
ffb776c4 191 case 'TO':
76f29d49 192 case 'CC':
193 if(!$walk) {
194 array_walk($msgs, create_function('&$v,&$k,$f',
195 '$v[$f] = (isset($v[$f])) ? $v[$f] : "";
196 $addr = parseAddress($v[$f]);
197 $v[$f] = ($addr[0][1]) ? decodeHeader($addr[0][1]):$addr[0][0];'),$sSortField);
198 $walk = true;
cdca177a 199 }
76f29d49 200 // nobreak
ffb776c4 201 case 'SUBJECT':
76f29d49 202 if(!$walk) {
203 array_walk($msgs, create_function('&$v,&$k,$f',
204 '$v[$f] = (isset($v[$f])) ? $v[$f] : "";
205 $v[$f] = strtolower(decodeHeader(trim($v[$f])));
206 $v[$f] = (preg_match("/^(vedr|sv|re|aw|\[\w\]):\s*(.*)$/si", $v[$f], $matches)) ?
207 $matches[2] : $v[$f];'),$sSortField);
208 $walk = true;
209 }
ffb776c4 210 foreach ($msgs as $item) {
76f29d49 211 $aUid[$item['ID']] = $item[$sSortField];
ffb776c4 212 }
76f29d49 213 natcasesort($aUid);
214 $aUid = array_keys($aUid);
ffb776c4 215 if ($reverse) {
e432a21c 216 $aUid = array_reverse($aUid);
ffb776c4 217 }
218 break;
76f29d49 219 // \natcasesort section
220 // sort_numeric section
ffb776c4 221 case 'DATE':
76f29d49 222 case 'INTERNALDATE':
223 if(!$walk) {
224 array_walk($msgs, create_function('&$v,$k,$f',
225 '$v[$f] = (isset($v[$f])) ? $v[$f] : "";
226 $v[$f] = getTimeStamp(explode(" ",$v[$f]));'),$sSortField);
227 $walk = true;
ffb776c4 228 }
76f29d49 229 // nobreak;
ffb776c4 230 case 'RFC822.SIZE':
c2e29558 231 if(!$walk) {
232 // redefine $sSortField to maintain the same namespace between
233 // server-side sorting and squirrelmail sorting
234 $sSortField = 'SIZE';
235 }
ffb776c4 236 foreach ($msgs as $item) {
c2e29558 237 $aUid[$item['ID']] = (isset($item[$sSortField])) ? $item[$sSortField] : 0;
ffb776c4 238 }
239 if ($reverse) {
76f29d49 240 arsort($aUid,SORT_NUMERIC);
ffb776c4 241 } else {
76f29d49 242 asort($aUid, SORT_NUMERIC);
ffb776c4 243 }
76f29d49 244 $aUid = array_keys($aUid);
ffb776c4 245 break;
76f29d49 246 // \sort_numeric section
ffb776c4 247 case 'UID':
76f29d49 248 $aUid = array_reverse($msgs);
ffb776c4 249 break;
6201339c 250 }
76f29d49 251 return $aUid;
cdca177a 252}
253
48af4b64 254/**
0fdc2fb6 255* Returns an indent array for printMessageinfo()
256* This represents the amount of indent needed (value),
257* for this message number (key)
258*/
76f29d49 259
260/*
261 * Notes for future work:
262 * indent_array should contain: indent_level, parent and flags,
263 * sibling notes ..
264 * To achieve that we need to define the following flags:
265 * 0: hasnochildren
266 * 1: haschildren
267 * 2: is first
268 * 4: is last
269 * a node has sibling nodes if it's not the last node
270 * a node has no sibling nodes if it's the last node
271 * By using binary comparations we can store the flag in one var
272 *
273 * example:
274 * -1 par = 0, level = 0, flag = 1 + 2 + 4 = 7 (haschildren, isfirst, islast)
275 * \-2 par = 1, level = 1, flag = 0 + 2 = 2 (hasnochildren, isfirst)
276 * |-3 par = 1, level = 1, flag = 1 + 4 = 5 (haschildren, islast)
277 * \-4 par = 3, level = 2, flag = 1 + 2 + 4 = 7 (haschildren, isfirst, islast)
278 * \-5 par = 4, level = 3, flag = 0 + 2 + 4 = 6 (hasnochildren, isfirst, islast)
279 */
ffb776c4 280function get_parent_level ($thread_new) {
48af4b64 281 $parent = '';
282 $child = '';
283 $cutoff = 0;
cdca177a 284
76f29d49 285 /*
286 * loop through the threads and take unwanted characters out
287 * of the thread string then chop it up
288 */
7c612fdd 289 for ($i=0;$i<count($thread_new);$i++) {
290 $thread_new[$i] = preg_replace("/\s\(/", "(", $thread_new[$i]);
291 $thread_new[$i] = preg_replace("/(\d+)/", "$1|", $thread_new[$i]);
292 $thread_new[$i] = preg_split("/\|/", $thread_new[$i], -1, PREG_SPLIT_NO_EMPTY);
1c198ef7 293 }
7c612fdd 294 $indent_array = array();
76f29d49 295 if (!$thread_new) {
296 $thread_new = array();
297 }
288bbce0 298 /* looping through the parts of one message thread */
cdca177a 299
7c612fdd 300 for ($i=0;$i<count($thread_new);$i++) {
76f29d49 301 /* first grab the parent, it does not indent */
cdca177a 302
7c612fdd 303 if (isset($thread_new[$i][0])) {
288bbce0 304 if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) {
305 $parent = $regs[1];
306 }
7c612fdd 307 }
308 $indent_array[$parent] = 0;
288bbce0 309
76f29d49 310 /*
311 * now the children, checking each thread portion for
312 * ),(, and space, adjusting the level and space values
313 * to get the indent level
314 */
288bbce0 315 $level = 0;
474528eb 316 $spaces = array();
317 $spaces_total = 0;
7c612fdd 318 $indent = 0;
288bbce0 319 $fake = FALSE;
76f29d49 320 for ($k=1,$iCnt=count($thread_new[$i])-1;$k<$iCnt;++$k) {
7c612fdd 321 $chars = count_chars($thread_new[$i][$k], 1);
288bbce0 322 if (isset($chars['40'])) { /* testing for ( */
76f29d49 323 $level += $chars['40'];
7c612fdd 324 }
288bbce0 325 if (isset($chars['41'])) { /* testing for ) */
76f29d49 326 $level -= $chars['41'];
474528eb 327 $spaces[$level] = 0;
288bbce0 328 /* if we were faking lets stop, this portion
76f29d49 329 * of the thread is over
330 */
288bbce0 331 if ($level == $cutoff) {
332 $fake = FALSE;
7c612fdd 333 }
334 }
288bbce0 335 if (isset($chars['32'])) { /* testing for space */
474528eb 336 if (!isset($spaces[$level])) {
337 $spaces[$level] = 0;
338 }
76f29d49 339 $spaces[$level] += $chars['32'];
474528eb 340 }
341 for ($x=0;$x<=$level;$x++) {
342 if (isset($spaces[$x])) {
76f29d49 343 $spaces_total += $spaces[$x];
474528eb 344 }
288bbce0 345 }
474528eb 346 $indent = $level + $spaces_total;
288bbce0 347 /* must have run into a message that broke the thread
76f29d49 348 * so we are adjusting for that portion
349 */
288bbce0 350 if ($fake == TRUE) {
351 $indent = $indent +1;
7c612fdd 352 }
353 if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) {
354 $child = $regs[1];
355 }
288bbce0 356 /* the thread must be broken if $indent == 0
76f29d49 357 * so indent the message once and start faking it
358 */
288bbce0 359 if ($indent == 0) {
360 $indent = 1;
361 $fake = TRUE;
362 $cutoff = $level;
363 }
364 /* dont need abs but if indent was negative
76f29d49 365 * errors would occur
366 */
367 $indent_array[$child] = ($indent < 0) ? 0 : $indent;
474528eb 368 $spaces_total = 0;
cdca177a 369 }
7c612fdd 370 }
371 return $indent_array;
372}
373
374
48af4b64 375/**
0fdc2fb6 376* Returns an array with each element as a string representing one
377* message-thread as returned by the IMAP server.
378*/
7c612fdd 379function get_thread_sort ($imap_stream) {
ffb776c4 380 global $thread_new, $sort_by_ref, $default_charset, $server_sort_array, $indent_array;
d7c82551 381 if (sqsession_is_registered('thread_new')) {
9eb0fbd4 382 sqsession_unregister('thread_new');
7c612fdd 383 }
ffb776c4 384 if (sqsession_is_registered('indent_array')) {
385 sqsession_unregister('indent_array');
386 }
d7c82551 387 if (sqsession_is_registered('server_sort_array')) {
9eb0fbd4 388 sqsession_unregister('server_sort_array');
60a3e687 389 }
7c612fdd 390 $thread_temp = array ();
391 if ($sort_by_ref == 1) {
392 $sort_type = 'REFERENCES';
76f29d49 393 } else {
7c612fdd 394 $sort_type = 'ORDEREDSUBJECT';
395 }
a18594b2 396 $query = "THREAD $sort_type ".strtoupper($default_charset)." ALL";
6201339c 397 $thread_test = sqimap_run_command ($imap_stream, $query, true, $response, $message, TRUE);
2728fa19 398 if (isset($thread_test[0])) {
9978560b 399 for ($i=0,$iCnt=count($thread_test);$i<$iCnt;++$i) {
76f29d49 400 if (preg_match("/^\* THREAD (.+)$/", $thread_test[$i], $regs)) {
401 $thread_list = trim($regs[1]);
402 break;
403 }
1c198ef7 404 }
76f29d49 405 } else {
406 $thread_list = "";
7c612fdd 407 }
26eca02e 408 if (!preg_match("/OK/", $response)) {
76f29d49 409 $server_sort_array = 'no';
410 return $server_sort_array;
cdca177a 411 }
474528eb 412 if (isset($thread_list)) {
413 $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
414 }
76f29d49 415
7c612fdd 416 $char_count = count($thread_temp);
417 $counter = 0;
418 $thread_new = array();
419 $k = 0;
420 $thread_new[0] = "";
76f29d49 421 /*
422 * parse the thread response into separate threads
423 *
424 * example:
425 * [0] => (540)
426 * [1] => (1386)
427 * [2] => (1599 759 959 37)
428 * [3] => (492 1787)
429 * [4] => ((933)(1891))
430 * [5] => (1030 (1497)(845)(1637))
431 */
432 for ($i=0,$iCnt=count($thread_temp);$i<$iCnt;$i++) {
ffb776c4 433 if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
434 $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
76f29d49 435 } elseif ($thread_temp[$i] == '(') {
ffb776c4 436 $thread_new[$k] .= $thread_temp[$i];
437 $counter++;
76f29d49 438 } elseif ($thread_temp[$i] == ')') {
439 if ($counter > 1) {
440 $thread_new[$k] .= $thread_temp[$i];
441 $counter = $counter - 1;
442 } else {
443 $thread_new[$k] .= $thread_temp[$i];
444 $k++;
445 $thread_new[$k] = "";
446 $counter = $counter - 1;
447 }
ffb776c4 448 }
7c612fdd 449 }
9eb0fbd4 450 sqsession_register($thread_new, 'thread_new');
7c612fdd 451 $thread_new = array_reverse($thread_new);
76f29d49 452 /* place the threads after each other in one string */
7c612fdd 453 $thread_list = implode(" ", $thread_new);
454 $thread_list = str_replace("(", " ", $thread_list);
455 $thread_list = str_replace(")", " ", $thread_list);
456 $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
60a3e687 457 $server_sort_array = $thread_list;
ffb776c4 458
459 $indent_array = get_parent_level ($thread_new);
460 sqsession_register($indent_array, 'indent_array');
461
9eb0fbd4 462 sqsession_register($server_sort_array, 'server_sort_array');
7c612fdd 463 return $thread_list;
464}
465
034fddf9 466
7b07404c 467function elapsedTime($start) {
0fdc2fb6 468 $stop = gettimeofday();
469 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec'];
470 return $timepassed;
7b07404c 471}
7c612fdd 472
ea5fa593 473// only used in sqimap_get_small_header_list
a18594b2 474function parseString($read,&$i) {
475 $char = $read{$i};
476 $s = '';
477 if ($char == '"') {
0fdc2fb6 478 $iPos = ++$i;
479 while (true) {
480 $iPos = strpos($read,'"',$iPos);
481 if (!$iPos) break;
482 if ($iPos && $read{$iPos -1} != '\\') {
483 $s = substr($read,$i,($iPos-$i));
484 $i = $iPos;
485 break;
486 }
487 $iPos++;
488 if ($iPos > strlen($read)) {
489 break;
490 }
491 }
a18594b2 492 } else if ($char == '{') {
493 $lit_cnt = '';
494 ++$i;
495 $iPos = strpos($read,'}',$i);
496 if ($iPos) {
0fdc2fb6 497 $lit_cnt = substr($read, $i, $iPos - $i);
498 $i += strlen($lit_cnt) + 3; /* skip } + \r + \n */
499 /* Now read the literal */
500 $s = ($lit_cnt ? substr($read,$i,$lit_cnt): '');
501 $i += $lit_cnt;
502 /* temp bugfix (SM 1.5 will have a working clean version)
503 too much work to implement that version right now */
504 --$i;
505 } else { /* should never happen */
a18594b2 506 $i += 3; /* } + \r + \n */
507 $s = '';
0fdc2fb6 508 }
a18594b2 509 } else {
0fdc2fb6 510 return false;
a18594b2 511 }
512 ++$i;
513 return $s;
514}
515
ea5fa593 516// only used in sqimap_get_small_header_list
a18594b2 517function parseArray($read,&$i) {
518 $i = strpos($read,'(',$i);
519 $i_pos = strpos($read,')',$i);
520 $s = substr($read,$i+1,$i_pos - $i -1);
521 $a = explode(' ',$s);
522 if ($i_pos) {
523 $i = $i_pos+1;
524 return $a;
525 } else {
526 return false;
527 }
528}
529
ffb776c4 530function sqimap_get_small_header_list ($imap_stream, $msg_list, $show_num=false,
531 $aHeaderFields = array('Date', 'To', 'Cc', 'From', 'Subject', 'X-Priority', 'Content-Type'),
8cc8ec79 532 $aFetchItems = array('FLAGS', 'RFC822.SIZE', 'INTERNALDATE')) {
ffb776c4 533
534 $messages = array();
535 $read_list = array();
536
8cc8ec79 537 if (array_search('UID',$aFetchItems,true) !== false) {
538 $bUidFetch = false;
539 } else {
540 $bUidFetch = true;
541 }
542
97f7ddf2 543 /* Get the small headers for each message in $msg_list */
ffb776c4 544 if ($show_num != '999999' && $show_num != '*' ) {
a18594b2 545 $msgs_str = sqimap_message_list_squisher($msg_list);
ffb776c4 546 /*
547 * We need to return the data in the same order as the caller supplied
548 * in $msg_list, but IMAP servers are free to return responses in
549 * whatever order they wish... So we need to re-sort manually
550 */
8cc8ec79 551 if ($bUidFetch) {
552 for ($i = 0; $i < sizeof($msg_list); $i++) {
553 $messages["$msg_list[$i]"] = array();
554 }
ffb776c4 555 }
1c198ef7 556 } else {
a18594b2 557 $msgs_str = '1:*';
558 }
ffb776c4 559
560
a18594b2 561
3411d4ec 562 /*
ffb776c4 563 * Create the query
564 */
cdca177a 565
ffb776c4 566 $sFetchItems = '';
567 $query = "FETCH $msgs_str (";
568 if (count($aFetchItems)) {
569 $sFetchItems = implode(' ',$aFetchItems);
570 }
571 if (count($aHeaderFields)) {
572 $sHeaderFields = implode(' ',$aHeaderFields);
573 $sFetchItems .= ' BODY.PEEK[HEADER.FIELDS ('.$sHeaderFields.')]';
7b07404c 574 }
ffb776c4 575 $query .= trim($sFetchItems) . ')';
8cc8ec79 576
577 $read_list = sqimap_run_command_list ($imap_stream, $query, true, $response, $message, $bUidFetch);
2d34da11 578 $i = 0;
1c198ef7 579
a18594b2 580 foreach ($read_list as $r) {
2a9b0fad 581 // use unset because we do isset below
a18594b2 582 $read = implode('',$r);
583
1c198ef7 584 /*
a18594b2 585 * #id<space>FETCH<space>(
586 */
1c198ef7 587
a18594b2 588 /* extract the message id */
589 $i_space = strpos($read,' ',2);
590 $id = substr($read,2,$i_space-2);
591 $fetch = substr($read,$i_space+1,5);
592 if (!is_numeric($id) && $fetch !== 'FETCH') {
8cc8ec79 593 $msg['ERROR'] = $read; // htmlspecialchars should be done just before display. this is backend code
594 break;
a18594b2 595 }
596 $i = strpos($read,'(',$i_space+5);
597 $read = substr($read,$i+1);
598 $i_len = strlen($read);
599 $i = 0;
600 while ($i < $i_len && $i !== false) {
601 /* get argument */
602 $read = trim(substr($read,$i));
603 $i_len = strlen($read);
604 $i = strpos($read,' ');
605 $arg = substr($read,0,$i);
606 ++$i;
607 switch ($arg)
608 {
609 case 'UID':
610 $i_pos = strpos($read,' ',$i);
611 if (!$i_pos) {
612 $i_pos = strpos($read,')',$i);
cdca177a 613 }
a18594b2 614 if ($i_pos) {
615 $unique_id = substr($read,$i,$i_pos-$i);
616 $i = $i_pos+1;
617 } else {
618 break 3;
cdca177a 619 }
a18594b2 620 break;
621 case 'FLAGS':
622 $flags = parseArray($read,$i);
623 if (!$flags) break 3;
ffb776c4 624 $aFlags = array();
a18594b2 625 foreach ($flags as $flag) {
626 $flag = strtolower($flag);
ffb776c4 627 $aFlags[$flag] = true;
cdca177a 628 }
ffb776c4 629 $msg['FLAGS'] = $aFlags;
a18594b2 630 break;
631 case 'RFC822.SIZE':
632 $i_pos = strpos($read,' ',$i);
633 if (!$i_pos) {
634 $i_pos = strpos($read,')',$i);
cdca177a 635 }
a18594b2 636 if ($i_pos) {
ffb776c4 637 $msg['SIZE'] = substr($read,$i,$i_pos-$i);
a18594b2 638 $i = $i_pos+1;
639 } else {
640 break 3;
641 }
1c198ef7 642
8cc8ec79 643 break;
644 case 'ENVELOPE':
645 break; // to be implemented, moving imap code out of the nessages class
646 sqimap_parse_address($read,$i,$msg);
647 break; // to be implemented, moving imap code out of the nessages class
648 case 'BODYSTRUCTURE':
a18594b2 649 break;
650 case 'INTERNALDATE':
c2e29558 651 $msg['INTERNALDATE'] = str_replace(' ', ' ',parseString($read,$i));
a18594b2 652 break;
653 case 'BODY.PEEK[HEADER.FIELDS':
654 case 'BODY[HEADER.FIELDS':
655 $i = strpos($read,'{',$i);
656 $header = parseString($read,$i);
92a52cda 657 if ($header === false) break 2;
2a9b0fad 658 /* First we replace all \r\n by \n, and unfold the header */
659 $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $header));
a18594b2 660 /* Now we can make a new header array with */
661 /* each element representing a headerline */
2a9b0fad 662 $hdr = explode("\n" , $hdr);
a18594b2 663 foreach ($hdr as $line) {
664 $pos = strpos($line, ':');
665 if ($pos > 0) {
666 $field = strtolower(substr($line, 0, $pos));
667 if (!strstr($field,' ')) { /* valid field */
668 $value = trim(substr($line, $pos+1));
669 switch($field)
670 {
ffb776c4 671 case 'to': $msg['TO'] = $value; break;
672 case 'cc': $msg['CC'] = $value; break;
673 case 'from': $msg['FROM'] = $value; break;
674 case 'date':
675 $msg['DATE'] = str_replace(' ', ' ', $value);
cdca177a 676 break;
ffb776c4 677 case 'x-priority': $msg['PRIORITY'] = $value; break;
678 case 'subject': $msg['SUBJECT'] = $value; break;
a18594b2 679 case 'content-type':
680 $type = $value;
cdca177a 681 if ($pos = strpos($type, ";")) {
682 $type = substr($type, 0, $pos);
683 }
684 $type = explode("/", $type);
cef054e4 685 if(!is_array($type)) {
ffb776c4 686 $msg['TYPE0'] = 'text';
687 $msg['TYPE1'] = 'plain';
cef054e4 688 }
cdca177a 689 break;
a18594b2 690 default: break;
691 }
cdca177a 692 }
693 }
694 }
a18594b2 695 break;
696 default:
697 ++$i;
698 break;
cdca177a 699 }
cdca177a 700 }
6201339c 701 $msgi ="$unique_id";
ffb776c4 702 $msg['ID'] = $unique_id;
703
704 $messages[$msgi] = $msg;
a18594b2 705 ++$msgi;
97f7ddf2 706 }
a18594b2 707 array_reverse($messages);
1c198ef7 708 return $messages;
97f7ddf2 709}
710
8cc8ec79 711function sqimap_parse_envelope($read, &$i, &$msg) {
712 $arg_no = 0;
713 $arg_a = array();
714 ++$i;
715 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++$i) {
716 $char = strtoupper($read{$i});
717 switch ($char) {
718 case '{':
719 case '"':
720 $arg_a[] = parseString($read,$i);
721 ++$arg_no;
722 break;
723 case 'N':
724 /* probably NIL argument */
725 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
726 $arg_a[] = '';
727 ++$arg_no;
728 $i += 2;
729 }
730 break;
731 case '(':
732 /* Address structure (with group support)
733 * Note: Group support is useless on SMTP connections
734 * because the protocol doesn't support it
735 */
736 $addr_a = array();
737 $group = '';
738 $a=0;
739 for (; $i < $cnt && $read{$i} != ')'; ++$i) {
740 if ($read{$i} == '(') {
741 $addr = sqimap_parse_address($read, $i);
742 if (($addr[3] == '') && ($addr[2] != '')) {
743 /* start of group */
744 $group = $addr[2];
745 $group_addr = $addr;
746 $j = $a;
747 } else if ($group && ($addr[3] == '') && ($addr[2] == '')) {
748 /* end group */
749 if ($a == ($j+1)) { /* no group members */
750 $group_addr[4] = $group;
751 $group_addr[2] = '';
752 $group_addr[0] = "$group: Undisclosed recipients;";
753 $addr_a[] = $group_addr;
754 $group ='';
755 }
756 } else {
757 $addr[4] = $group;
758 $addr_a[] = $addr;
759 }
760 ++$a;
761 }
762 }
763 $arg_a[] = $addr_a;
764 break;
765 default: break;
766 }
767 }
768
769 if (count($arg_a) > 9) {
770 $d = strtr($arg_a[0], array(' ' => ' '));
771 $d = explode(' ', $d);
772 if (!$arg_a[1]) $arg_1[1] = '';
773 $msg['DATE'] = $d; /* argument 1: date */
774 $msg['SUBJECT'] = $arg_a[1]; /* argument 2: subject */
775 $msg['FROM'] = is_array($arg_a[2]) ? $arg_a[2][0] : ''; /* argument 3: from */
776 $msg['SENDER'] = is_array($arg_a[3]) ? $arg_a[3][0] : ''; /* argument 4: sender */
777 $msg['REPLY-TO'] = is_array($arg_a[4]) ? $arg_a[4][0] : ''; /* argument 5: reply-to */
778 $msg['TO'] = $arg_a[5]; /* argument 6: to */
779 $msg['CC'] = $arg_a[6]; /* argument 7: cc */
780 $msg['BCC'] = $arg_a[7]; /* argument 8: bcc */
781 $msg['IN-REPLY-TO'] = $arg_a[8]; /* argument 9: in-reply-to */
782 $msg['MESSAGE-ID'] = $arg_a[9]; /* argument 10: message-id */
783 }
784}
785
786function sqimap_parse_address($read, &$i) {
787 $arg_a = array();
788 for (; $read{$i} != ')'; ++$i) {
789 $char = strtoupper($read{$i});
790 switch ($char) {
791 case '{':
792 case '"': $arg_a[] = parseString($read,$i); break;
793 case 'n':
794 case 'N':
795 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
796 $arg_a[] = '';
797 $i += 2;
798 }
799 break;
800 default: break;
801 }
802 }
803
804 if (count($arg_a) == 4) {
805 return $arg_a;
806
807// $adr = new AddressStructure();
808// $adr->personal = $arg_a[0];
809// $adr->adl = $arg_a[1];
810// $adr->mailbox = $arg_a[2];
811// $adr->host = $arg_a[3];
812 } else {
813 $adr = '';
814 }
815 return $adr;
816}
817
48af4b64 818/**
0fdc2fb6 819* Returns a message array with all the information about a message.
820* See the documentation folder for more information about this array.
821*/
97f7ddf2 822function sqimap_get_message ($imap_stream, $id, $mailbox) {
461eda6c 823 // typecast to int to prohibit 1:* msgs sets
824 $id = (int) $id;
2d34da11 825 $flags = array();
6201339c 826 $read = sqimap_run_command ($imap_stream, "FETCH $id (FLAGS BODYSTRUCTURE)", true, $response, $message, TRUE);
114f2a24 827 if ($read) {
b69a13a4 828 if (preg_match('/.+FLAGS\s\((.*)\)\s/AUi',$read[0],$regs)) {
829 if (trim($regs[1])) {
830 $flags = preg_split('/ /', $regs[1],-1,'PREG_SPLIT_NI_EMPTY');
831 }
832 }
114f2a24 833 } else {
b69a13a4 834 /* the message was not found, maybe the mailbox was modified? */
835 global $sort, $startMessage, $color;
836
837 $errmessage = _("The server couldn't find the message you requested.") .
0fdc2fb6 838 '<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).");
b69a13a4 839 /* this will include a link back to the message list */
461eda6c 840 error_message($errmessage, $mailbox, $sort, (int) $startMessage, $color);
b69a13a4 841 exit;
1c198ef7 842 }
2d34da11 843 $bodystructure = implode('',$read);
844 $msg = mime_structure($bodystructure,$flags);
6201339c 845 $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, TRUE);
19d470aa 846 $rfc822_header = new Rfc822Header();
767ace1f 847 $rfc822_header->parseHeader($read);
848 $msg->rfc822_header = $rfc822_header;
2d34da11 849 return $msg;
97f7ddf2 850}
851
052e0c26 852?>