6 * This implements functions that manipulate messages
7 * NOTE: Quite a few functions in this file are obsolete
9 * @copyright © 1999-2007 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @package squirrelmail
18 * Copy a set of messages ($id) to another mailbox ($mailbox)
19 * @param int $imap_stream The resource ID for the IMAP socket
20 * @param string $id The list of messages to copy
21 * @param string $mailbox The destination to copy to
22 * @param bool $handle_errors Show error messages in case of a NO, BAD or BYE response
23 * @return bool If the copy completed without errors
25 function sqimap_msgs_list_copy($imap_stream, $id, $mailbox, $handle_errors = true) {
26 $msgs_id = sqimap_message_list_squisher($id);
27 $read = sqimap_run_command ($imap_stream, "COPY $msgs_id " . sqimap_encode_mailbox_name($mailbox), $handle_errors, $response, $message, TRUE);
28 if ($response == 'OK') {
37 * Move a set of messages ($id) to another mailbox. Deletes the originals.
38 * @param int $imap_stream The resource ID for the IMAP socket
39 * @param string $id The list of messages to move
40 * @param string $mailbox The destination to move to
41 * @param bool $handle_errors Show error messages in case of a NO, BAD or BYE response
42 * @param string $source_mailbox (since 1.5.1) name of source mailbox. It is used to
43 * validate that target mailbox != source mailbox.
44 * @return bool If the move completed without errors
46 function sqimap_msgs_list_move($imap_stream, $id, $mailbox, $handle_errors = true, $source_mailbox = false) {
47 if ($source_mailbox!==false && $source_mailbox==$mailbox) {
50 $msgs_id = sqimap_message_list_squisher($id);
51 if (sqimap_msgs_list_copy ($imap_stream, $id, $mailbox, $handle_errors)) {
52 return sqimap_toggle_flag($imap_stream, $id, '\\Deleted', true, true);
60 * Deletes a message and move it to trash or expunge the mailbox
61 * @param resource imap connection
62 * @param string $mailbox mailbox, used for checking if it concerns the trash_folder
63 * @param array $id list with uid's
64 * @param bool $bypass_trash (since 1.5.0) skip copy to trash
65 * @return array $aMessageList array with messages containing the new flags and UID @see parseFetch
68 function sqimap_msgs_list_delete($imap_stream, $mailbox, $id, $bypass_trash=false) {
69 // FIX ME, remove globals by introducing an associative array with properties
70 // as 4th argument as replacement for the bypass_trash var
71 global $move_to_trash, $trash_folder;
72 if (($move_to_trash == true) && ($bypass_trash != true) &&
73 (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder)) ) {
75 * turn off internal error handling (fourth argument = false) and
76 * ignore copy to trash errors (allows to delete messages when overquota)
78 sqimap_msgs_list_copy ($imap_stream, $id, $trash_folder, false);
80 return sqimap_toggle_flag($imap_stream, $id, '\\Deleted', true, true);
85 * Set a flag on the provided uid list
86 * @param resource imap connection
87 * @param array $id list with uid's
88 * @param string $flag Flags to set/unset flags can be i.e.'\Seen', '\Answered', '\Seen \Answered'
89 * @param bool $set add (true) or remove (false) the provided flag
90 * @param bool $handle_errors Show error messages in case of a NO, BAD or BYE response
91 * @return array $aMessageList array with messages containing the new flags and UID @see parseFetch
93 function sqimap_toggle_flag($imap_stream, $id, $flag, $set, $handle_errors) {
94 $msgs_id = sqimap_message_list_squisher($id);
95 $set_string = ($set ?
'+' : '-');
97 for ($i=0; $i<sizeof($id); $i++
) {
98 $aMessageList["$id[$i]"] = array();
101 $aResponse = sqimap_run_command_list($imap_stream, "STORE $msgs_id ".$set_string."FLAGS ($flag)", $handle_errors, $response, $message, TRUE);
103 // parse the fetch response
104 $parseFetchResults=parseFetch($aResponse,$aMessageList);
106 // some broken IMAP servers do not return UID elements on UID STORE
107 // if this is the case, then we need to do a UID FETCH
109 if (!isset($parseFetchResults[$testkey]['UID'])) {
110 $aResponse = sqimap_run_command_list($imap_stream, "FETCH $msgs_id (FLAGS)", $handle_errors, $response, $message, TRUE);
111 $parseFetchResults = parseFetch($aResponse,$aMessageList);
114 return ($parseFetchResults);
119 * Sort the message list and crunch to be as small as possible
120 * (overflow could happen, so make it small if possible)
121 * @param array $aUid array with uid's
122 * @return string $s message set string
124 function sqimap_message_list_squisher($aUid) {
125 if( !is_array( $aUid ) ) {
128 sort($aUid, SORT_NUMERIC
);
132 for ($i=0,$iCnt=count($aUid);$i<$iCnt;++
$i) {
135 while ($i<($iCnt-1) && $aUid[$i+
1] == $iEnd +
1) {
143 if ($iStart != $iEnd) {
153 * Retrieves an array with a sorted uid list. Sorting is done on the imap server
154 * @link http://www.ietf.org/internet-drafts/draft-ietf-imapext-sort-17.txt
155 * @param resource $imap_stream IMAP socket connection
156 * @param string $sSortField Field to sort on
157 * @param bool $reverse Reverse order search
158 * @return array $id sorted uid list
160 function sqimap_get_sort_order($imap_stream, $sSortField, $reverse, $search='ALL') {
161 global $default_charset;
165 $sSortField = 'REVERSE '.$sSortField;
167 $query = "SORT ($sSortField) ".strtoupper($default_charset)." $search";
168 // FIX ME sqimap_run_command should return the parsed data accessible by $aDATA['SORT']
169 // use sqimap_run_command_list in case of unsollicited responses. If we don't we could loose the SORT response
170 $aData = sqimap_run_command_list ($imap_stream, $query, false, $response, $message, TRUE);
171 /* fallback to default charset */
172 if ($response == 'NO') {
173 if (strpos($message,'BADCHARSET') !== false ||
174 strpos($message,'character') !== false) {
175 sqm_trigger_imap_error('SQM_IMAP_BADCHARSET',$query, $response, $message);
176 $query = "SORT ($sSortField) US-ASCII $search";
177 $aData = sqimap_run_command_list ($imap_stream, $query, true, $response, $message, TRUE);
179 sqm_trigger_imap_error('SQM_IMAP_ERROR',$query, $response, $message);
181 } else if ($response == 'BAD') {
182 sqm_trigger_imap_error('SQM_IMAP_NO_SORT',$query, $response, $message);
186 if ($response == 'OK') {
187 return parseUidList($aData,'SORT');
195 * Parses a UID list returned on a SORT or SEARCH request
196 * @param array $aData imap response (retrieved from sqimap_run_command_list)
197 * @param string $sCommand issued imap command (SEARCH or SORT)
198 * @return array $aUid uid list
200 function parseUidList($aData,$sCommand) {
202 if (isset($aData) && count($aData)) {
203 for ($i=0,$iCnt=count($aData);$i<$iCnt;++
$i) {
204 for ($j=0,$jCnt=count($aData[$i]);$j<$jCnt;++
$j) {
205 if (preg_match("/^\* $sCommand (.+)$/", $aData[$i][$j], $aMatch)) {
206 $aUid +
= explode(' ', trim($aMatch[1]));
211 return array_unique($aUid);
215 * Retrieves an array with a sorted uid list. Sorting is done by SquirrelMail
217 * @param resource $imap_stream IMAP socket connection
218 * @param string $sSortField Field to sort on
219 * @param bool $reverse Reverse order search
220 * @param array $aUid limit the search to the provided array with uid's default sqimap_get_small_headers uses 1:*
221 * @return array $aUid sorted uid list
223 function get_squirrel_sort($imap_stream, $sSortField, $reverse = false, $aUid = NULL) {
224 if ($sSortField != 'RFC822.SIZE' && $sSortField != 'INTERNALDATE') {
225 $msgs = sqimap_get_small_header_list($imap_stream, $aUid,
226 array($sSortField), array());
228 $msgs = sqimap_get_small_header_list($imap_stream, $aUid,
229 array(), array($sSortField));
233 switch ($sSortField) {
234 // natcasesort section
239 array_walk($msgs, create_function('&$v,&$k,$f',
240 '$v[$f] = (isset($v[$f])) ? $v[$f] : "";
241 $addr = reset(parseRFC822Address($v[$f],1));
242 $sPersonal = (isset($addr[SQM_ADDR_PERSONAL]) && $addr[SQM_ADDR_PERSONAL]) ?
243 $addr[SQM_ADDR_PERSONAL] : "";
244 $sEmail = ($addr[SQM_ADDR_HOST]) ?
245 $addr[SQM_ADDR_MAILBOX] . "@".$addr[SQM_ADDR_HOST] :
246 $addr[SQM_ADDR_HOST];
247 $v[$f] = ($sPersonal) ? decodeHeader($sPersonal):$sEmail;'),$sSortField);
253 array_walk($msgs, create_function('&$v,&$k,$f',
254 '$v[$f] = (isset($v[$f])) ? $v[$f] : "";
255 $v[$f] = strtolower(decodeHeader(trim($v[$f])));
256 $v[$f] = (preg_match("/^(vedr|sv|re|aw|\[\w\]):\s*(.*)$/si", $v[$f], $matches)) ?
257 $matches[2] : $v[$f];'),$sSortField);
260 foreach ($msgs as $item) {
261 $aUid[$item['UID']] = $item[$sSortField];
264 $aUid = array_keys($aUid);
266 $aUid = array_reverse($aUid);
269 // \natcasesort section
270 // sort_numeric section
274 array_walk($msgs, create_function('&$v,$k,$f',
275 '$v[$f] = (isset($v[$f])) ? $v[$f] : "";
276 $v[$f] = getTimeStamp(explode(" ",$v[$f]));'),$sSortField);
282 // redefine $sSortField to maintain the same namespace between
283 // server-side sorting and SquirrelMail sorting
284 $sSortField = 'SIZE';
286 foreach ($msgs as $item) {
287 $aUid[$item['UID']] = (isset($item[$sSortField])) ?
$item[$sSortField] : 0;
290 arsort($aUid,SORT_NUMERIC
);
292 asort($aUid, SORT_NUMERIC
);
294 $aUid = array_keys($aUid);
296 // \sort_numeric section
298 $aUid = array_reverse($msgs);
305 * Returns an array with each element as a string representing one
306 * message-thread as returned by the IMAP server.
307 * @param resource $imap_stream IMAP socket connection
308 * @param string $search optional search string
310 * @link http://www.ietf.org/internet-drafts/draft-ietf-imapext-sort-13.txt
312 function get_thread_sort($imap_stream, $search='ALL') {
313 global $sort_by_ref, $default_charset;
315 if ($sort_by_ref == 1) {
316 $sort_type = 'REFERENCES';
318 $sort_type = 'ORDEREDSUBJECT';
320 $query = "THREAD $sort_type ".strtoupper($default_charset)." $search";
322 // TODO use sqimap_run_command_list as we do in get_server_sort()
323 $sRead = sqimap_run_command ($imap_stream, $query, false, $response, $message, TRUE);
325 /* fallback to default charset */
326 if ($response == 'NO') {
327 if (strpos($message,'BADCHARSET') !== false ||
328 strpos($message,'character') !== false) {
329 sqm_trigger_imap_error('SQM_IMAP_BADCHARSET',$query, $response, $message);
330 $query = "THREAD $sort_type US-ASCII $search";
331 $sRead = sqimap_run_command ($imap_stream, $query, true, $response, $message, TRUE);
333 sqm_trigger_imap_error('SQM_IMAP_ERROR',$query, $response, $message);
335 } elseif ($response == 'BAD') {
336 sqm_trigger_imap_error('SQM_IMAP_NO_THREAD',$query, $response, $message);
338 $sThreadResponse = '';
339 if (isset($sRead[0])) {
340 for ($i=0,$iCnt=count($sRead);$i<$iCnt;++
$i) {
341 if (preg_match("/^\* THREAD (.+)$/", $sRead[$i], $aMatch)) {
342 $sThreadResponse = trim($aMatch[1]);
349 if ($response !== 'OK') {
354 * S: * THREAD (2)(3 6 (4 23)(44 7 96))
367 * Notes for future work:
368 * indent_array should contain: indent_level, parent and flags,
370 * To achieve that we need to define the following flags:
375 * a node has sibling nodes if it's not the last node
376 * a node has no sibling nodes if it's the last node
377 * By using binary comparations we can store the flag in one var
380 * -1 par = 0, level = 0, flag = 1 + 2 + 4 = 7 (haschildren, isfirst, islast)
381 * \-2 par = 1, level = 1, flag = 0 + 2 = 2 (hasnochildren, isfirst)
382 * |-3 par = 1, level = 1, flag = 1 + 4 = 5 (haschildren, islast)
383 * \-4 par = 3, level = 2, flag = 1 + 2 + 4 = 7 (haschildren, isfirst, islast)
384 * \-5 par = 4, level = 3, flag = 0 + 2 + 4 = 6 (hasnochildren, isfirst, islast)
390 $aUidThread = array();
392 $aUidSubThread = array();
393 $aDepthStack = array();
396 if ($sThreadResponse) {
397 for ($i=0,$iCnt = strlen($sThreadResponse);$i<$iCnt;++
$i) {
398 $cChar = $sThreadResponse{$i};
400 case '(': // new sub thread
401 // correction for a subthread of a thread with no parents in thread
402 if (!count($aUidSubThread) && $j > 0) {
405 $aDepthStack[$j] = $l;
408 case ')': // close sub thread
410 $aUidSubThread[] = $sUid;
411 $aIndent[$sUid] = $j +
$l - 1;
417 // show message that starts the thread first.
418 $aUidSubThread = array_reverse($aUidSubThread);
419 // do not use array_merge because it's extremely slow and is causing timeouts
420 foreach ($aUidSubThread as $iUid) {
421 $aUidThread[] = $iUid;
423 $aUidSubThread = array();
425 $aDepthStack = array();
427 $l = $aDepthStack[$j];
430 case ' ': // new child
432 $aUidSubThread[] = $sUid;
433 $aIndent[$sUid] = $j +
$l - 1;
438 default: // part of UID
444 unset($sThreadResponse);
445 // show newest threads first
446 $aUidThread = array_reverse($aUidThread);
447 return array($aUidThread,$aIndent);
451 function elapsedTime($start) {
452 $stop = gettimeofday();
453 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) +
$stop['usec'] - $start['usec'];
458 * Parses a string in an imap response. String starts with " or { which means it
459 * can handle double quoted strings and literal strings
461 * @param string $read imap response
462 * @param integer $i (reference) offset in string
463 * @return string $s parsed string without the double quotes or literal count
465 function parseString($read,&$i) {
471 $iPos = strpos($read,'"',$iPos);
473 if ($iPos && $read{$iPos -1} != '\\') {
474 $s = substr($read,$i,($iPos-$i));
479 if ($iPos > strlen($read)) {
483 } else if ($char == '{') {
486 $iPos = strpos($read,'}',$i);
488 $lit_cnt = substr($read, $i, $iPos - $i);
489 $i +
= strlen($lit_cnt) +
3; /* skip } + \r + \n */
490 /* Now read the literal */
491 $s = ($lit_cnt ?
substr($read,$i,$lit_cnt): '');
493 /* temp bugfix (SM 1.5 will have a working clean version)
494 too much work to implement that version right now */
496 } else { /* should never happen */
497 $i +
= 3; /* } + \r + \n */
509 * Parses a string containing an array from an imap response. String starts with ( and end with )
511 * @param string $read imap response
512 * @param integer $i (reference) offset in string
515 function parseArray($read,&$i) {
516 $i = strpos($read,'(',$i);
517 $i_pos = strpos($read,')',$i);
518 $s = substr($read,$i+
1,$i_pos - $i -1);
519 $a = explode(' ',$s);
530 * Retrieves a list with headers, flags, size or internaldate from the imap server
532 * WARNING: function is not portable between SquirrelMail 1.2.x, 1.4.x and 1.5.x.
533 * Output format, third argument and $msg_list array format requirements differ.
534 * @param stream $imap_stream imap connection
535 * @param array $msg_list array with id's to create a msgs set from
536 * @param array $aHeaderFields (since 1.5.0) requested header fields
537 * @param array $aFetchItems (since 1.5.0) requested other fetch items like FLAGS, RFC822.SIZE
538 * @return array $aMessages associative array with messages. Key is the UID, value is an associative array
541 function sqimap_get_small_header_list($imap_stream, $msg_list,
542 $aHeaderFields = array('Date', 'To', 'Cc', 'From', 'Subject', 'X-Priority', 'Content-Type'),
543 $aFetchItems = array('FLAGS', 'RFC822.SIZE', 'INTERNALDATE')) {
545 $aMessageList = array();
548 * Catch other priority headers as well
550 if (in_array('X-Priority',$aHeaderFields,true)) {
551 $aHeaderFields[] = 'Importance';
552 $aHeaderFields[] = 'Priority';
555 $bUidFetch = ! in_array('UID', $aFetchItems, true);
557 /* Get the small headers for each message in $msg_list */
558 if ($msg_list !== NULL) {
559 $msgs_str = sqimap_message_list_squisher($msg_list);
561 * We need to return the data in the same order as the caller supplied
562 * in $msg_list, but IMAP servers are free to return responses in
563 * whatever order they wish... So we need to re-sort manually
566 for ($i = 0; $i < sizeof($msg_list); $i++
) {
567 $aMessageList["$msg_list[$i]"] = array();
579 $query = "FETCH $msgs_str (";
580 if (count($aFetchItems)) {
581 $sFetchItems = implode(' ',$aFetchItems);
583 if (count($aHeaderFields)) {
584 $sHeaderFields = implode(' ',$aHeaderFields);
585 $sFetchItems .= ' BODY.PEEK[HEADER.FIELDS ('.$sHeaderFields.')]';
587 $query .= trim($sFetchItems) . ')';
588 $aResponse = sqimap_run_command_list ($imap_stream, $query, true, $response, $message, $bUidFetch);
589 $aMessages = parseFetch($aResponse,$aMessageList);
590 array_reverse($aMessages);
596 * Parses a fetch response, currently it can hande FLAGS, HEADERS, RFC822.SIZE, INTERNALDATE and UID
597 * @param array $aResponse Imap response
598 * @param array $aMessageList Placeholder array for results. The keys of the
599 * placeholder array should be the UID so we can reconstruct the order.
600 * @return array $aMessageList associative array with messages. Key is the UID, value is an associative array
601 * @author Marc Groot Koerkamp
603 function parseFetch(&$aResponse,$aMessageList = array()) {
604 for ($j=0,$iCnt=count($aResponse);$j<$iCnt;++
$j) {
607 $read = implode('',$aResponse[$j]);
609 unset($aResponse[$j]); /* unset does not reindex the array. the for loop is safe */
611 * #id<space>FETCH<space>(
614 /* extract the message id */
615 $i_space = strpos($read,' ',2);/* position 2ed <space> */
616 $id = substr($read,2/* skip "*<space>" */,$i_space -2);
618 $fetch = substr($read,$i_space+
1,5);
619 if (!is_numeric($id) && $fetch !== 'FETCH') {
620 $aMsg['ERROR'] = $read; // htmlspecialchars should be done just before display. this is backend code
623 $i = strpos($read,'(',$i_space+
5);
624 $read = substr($read,$i+
1);
625 $i_len = strlen($read);
627 while ($i < $i_len && $i !== false) {
629 $read = trim(substr($read,$i));
630 $i_len = strlen($read);
631 $i = strpos($read,' ');
632 $arg = substr($read,0,$i);
635 * use allcaps for imap items and lowcaps for headers as key for the $aMsg array
640 $i_pos = strpos($read,' ',$i);
642 $i_pos = strpos($read,')',$i);
645 $unique_id = substr($read,$i,$i_pos-$i);
652 $flags = parseArray($read,$i);
653 if (!$flags) break 3;
655 foreach ($flags as $flag) {
656 $flag = strtolower($flag);
657 $aFlags[$flag] = true;
659 $aMsg['FLAGS'] = $aFlags;
662 $i_pos = strpos($read,' ',$i);
664 $i_pos = strpos($read,')',$i);
667 $aMsg['SIZE'] = substr($read,$i,$i_pos-$i);
674 // sqimap_parse_address($read,$i,$aMsg);
675 break; // to be implemented, moving imap code out of the Message class
676 case 'BODYSTRUCTURE':
677 break; // to be implemented, moving imap code out of the Message class
679 $aMsg['INTERNALDATE'] = trim(str_replace(' ', ' ',parseString($read,$i)));
681 case 'BODY.PEEK[HEADER.FIELDS':
682 case 'BODY[HEADER.FIELDS':
683 $i = strpos($read,'{',$i); // header is always returned as literal because it contain \n characters
684 $header = parseString($read,$i);
685 if ($header === false) break 2;
686 /* First we replace all \r\n by \n, and unfold the header */
687 $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $header));
688 /* Now we can make a new header array with
689 each element representing a headerline */
690 $aHdr = explode("\n" , $hdr);
691 $aReceived = array();
692 foreach ($aHdr as $line) {
693 $pos = strpos($line, ':');
695 $field = strtolower(substr($line, 0, $pos));
696 if (!strstr($field,' ')) { /* valid field */
697 $value = trim(substr($line, $pos+
1));
700 $aMsg['date'] = trim(str_replace(' ', ' ', $value));
702 case 'x-priority': $aMsg['x-priority'] = ($value) ?
(int) $value{0} : 3; break;
705 // duplicate code with Rfc822Header.cls:parsePriority()
706 if (!isset($aMsg['x-priority'])) {
707 $aPrio = preg_split('/\s/',trim($value));
708 $sPrio = strtolower(array_shift($aPrio));
709 if (is_numeric($sPrio)) {
710 $iPrio = (int) $sPrio;
711 } elseif ( $sPrio == 'non-urgent' ||
$sPrio == 'low' ) {
713 } elseif ( $sPrio == 'urgent' ||
$sPrio == 'high' ) {
716 // default is normal priority
719 $aMsg['x-priority'] = $iPrio;
724 if ($pos = strpos($type, ";")) {
725 $type = substr($type, 0, $pos);
727 $type = explode("/", $type);
728 if(!is_array($type) ||
count($type) < 2) {
729 $aMsg['content-type'] = array('text','plain');
731 $aMsg['content-type'] = array(strtolower($type[0]),strtolower($type[1]));
735 $aMsg['received'][] = $value;
738 $aMsg[$field] = $value;
750 if (!empty($unique_id)) {
751 $msgi = "$unique_id";
752 $aMsg['UID'] = $unique_id;
756 $aMessageList[$msgi] = $aMsg;
757 $aResponse[$j] = NULL;
759 return $aMessageList;
765 * @author Marc Groot Koerkamp
767 function sqimap_parse_envelope($read, &$i, &$msg) {
771 for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++
$i) {
772 $char = strtoupper($read{$i});
776 $arg_a[] = parseString($read,$i);
780 /* probably NIL argument */
781 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
788 /* Address structure (with group support)
789 * Note: Group support is useless on SMTP connections
790 * because the protocol doesn't support it
795 for (; $i < $cnt && $read{$i} != ')'; ++
$i) {
796 if ($read{$i} == '(') {
797 $addr = sqimap_parse_address($read, $i);
798 if (($addr[3] == '') && ($addr[2] != '')) {
803 } else if ($group && ($addr[3] == '') && ($addr[2] == '')) {
805 if ($a == ($j+
1)) { /* no group members */
806 $group_addr[4] = $group;
808 $group_addr[0] = "$group: Undisclosed recipients;";
809 $addr_a[] = $group_addr;
825 if (count($arg_a) > 9) {
826 $d = strtr($arg_a[0], array(' ' => ' '));
827 $d = explode(' ', $d);
828 if (!$arg_a[1]) $arg_a[1] = '';
829 $msg['DATE'] = $d; /* argument 1: date */
830 $msg['SUBJECT'] = $arg_a[1]; /* argument 2: subject */
831 $msg['FROM'] = is_array($arg_a[2]) ?
$arg_a[2][0] : ''; /* argument 3: from */
832 $msg['SENDER'] = is_array($arg_a[3]) ?
$arg_a[3][0] : ''; /* argument 4: sender */
833 $msg['REPLY-TO'] = is_array($arg_a[4]) ?
$arg_a[4][0] : ''; /* argument 5: reply-to */
834 $msg['TO'] = $arg_a[5]; /* argument 6: to */
835 $msg['CC'] = $arg_a[6]; /* argument 7: cc */
836 $msg['BCC'] = $arg_a[7]; /* argument 8: bcc */
837 $msg['IN-REPLY-TO'] = $arg_a[8]; /* argument 9: in-reply-to */
838 $msg['MESSAGE-ID'] = $arg_a[9]; /* argument 10: message-id */
846 * @author Marc Groot Koerkamp
848 function sqimap_parse_address($read, &$i) {
850 for (; $read{$i} != ')'; ++
$i) {
851 $char = strtoupper($read{$i});
854 case '"': $arg_a[] = parseString($read,$i); break;
857 if (strtoupper(substr($read, $i, 3)) == 'NIL') {
866 if (count($arg_a) == 4) {
869 // $adr = new AddressStructure();
870 // $adr->personal = $arg_a[0];
871 // $adr->adl = $arg_a[1];
872 // $adr->mailbox = $arg_a[2];
873 // $adr->host = $arg_a[3];
882 * Returns a message array with all the information about a message.
883 * See the documentation folder for more information about this array.
885 * @param resource $imap_stream imap connection
886 * @param integer $id uid of the message
887 * @param string $mailbox used for error handling, can be removed because we should return an error code and generate the message elsewhere
888 * @param int $hide Indicates whether or not to hide any errors: 0 = don't hide, 1 = hide (just exit), 2 = hide (return FALSE), 3 = hide (return error string) (OPTIONAL; default don't hide)
889 * @return mixed Message object or FALSE/error string if error occurred and $hide is set to 2/3
891 function sqimap_get_message($imap_stream, $id, $mailbox, $hide=0) {
892 // typecast to int to prohibit 1:* msgs sets
895 $read = sqimap_run_command($imap_stream, "FETCH $id (FLAGS BODYSTRUCTURE)", true, $response, $message, TRUE);
897 if (preg_match('/.+FLAGS\s\((.*)\)\s/AUi',$read[0],$regs)) {
898 if (trim($regs[1])) {
899 $flags = preg_split('/ /', $regs[1],-1,PREG_SPLIT_NO_EMPTY
);
904 if ($hide == 1) exit;
905 if ($hide == 2) return FALSE;
907 /* the message was not found, maybe the mailbox was modified? */
908 global $sort, $startMessage;
910 $errmessage = _("The server couldn't find the message you requested.");
912 if ($hide == 3) return $errmessage;
914 $errmessage .= '<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).");
916 /* this will include a link back to the message list */
917 error_message($errmessage, $mailbox, $sort, (int) $startMessage);
920 $bodystructure = implode('',$read);
921 $msg = mime_structure($bodystructure,$flags);
922 $read = sqimap_run_command($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, TRUE);
923 $rfc822_header = new Rfc822Header();
924 $rfc822_header->parseHeader($read);
925 $msg->rfc822_header
= $rfc822_header;