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