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