- $internaldate = getPref($data_dir, $username, 'internal_date_sort');
- if ($internaldate) {
- $query = "$sid FETCH $msgs_str (FLAGS UID RFC822.SIZE INTERNALDATE BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject X-Priority Content-Type)])\r\n";
- } else {
- $query = "$sid FETCH $msgs_str (FLAGS UID RFC822.SIZE BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject X-Priority Content-Type)])\r\n";
- }
- fputs ($imap_stream, $query);
- $readin_list = sqimap_read_data_list($imap_stream, $sid, false, $response, $message);
- $i = 0;
- foreach ($readin_list as $r) {
- if (!$uid_support) {
- if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH/iAU",$r[0], $regs)) {
- set_up_language($squirrelmail_language);
- echo '<br><b><font color=$color[2]>' .
- _("ERROR : Could not complete request.") .
- '</b><br>' .
- _("Unknown response from IMAP server: ") . ' 1.' .
- $r[0] . "</font><br>\n";
- } else if (! isset($id2index[$regs[1]]) || !count($id2index[$regs[1]])) {
- set_up_language($squirrelmail_language);
- echo '<br><b><font color=$color[2]>' .
- _("ERROR : Could not complete request.") .
- '</b><br>' .
- _("Unknown message number in reply from server: ") .
- $regs[1] . "</font><br>\n";
- } else {
- $read_list[$id2index[$regs[1]]] = $r;
- }
- } else {
- if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH.*UID\s+([0-9]+)\s+/iAU",$r[0], $regs)) {
- set_up_language($squirrelmail_language);
- echo '<br><b><font color=$color[2]>' .
- _("ERROR : Could not complete request.") .
- '</b><br>' .
- _("Unknown response from IMAP server: ") . ' 1.' .
- $r[0] . "</font><br>\n";
- } else if (! isset($id2index[$regs[2]]) || !count($id2index[$regs[2]])) {
- set_up_language($squirrelmail_language);
- echo '<br><b><font color=$color[2]>' .
- _("ERROR : Could not complete request.") .
- '</b><br>' .
- _("Unknown message number in reply from server: ") .
- $regs[2] . "</font><br>\n";
- } else {
- $read_list[$id2index[$regs[2]]] = $r;
- $unique_id = $regs[2];
- }
+/**
+ * Parses a fetch response, currently it can hande FLAGS, HEADERS, RFC822.SIZE, INTERNALDATE and UID
+ * @param array $aResponse Imap response
+ * @param array $aMessageList Placeholder array for results. The keys of the
+ * placeholder array should be the UID so we can reconstruct the order.
+ * @return array $aMessageList associative array with messages. Key is the UID, value is an associative array
+ * @author Marc Groot Koerkamp
+ */
+function parseFetch($aResponse,$aMessageList = array()) {
+ foreach ($aResponse as $r) {
+ $msg = array();
+ // use unset because we do isset below
+ $read = implode('',$r);
+
+ /*
+ * #id<space>FETCH<space>(
+ */
+
+ /* extract the message id */
+ $i_space = strpos($read,' ',2);
+ $id = substr($read,2,$i_space-2);
+ $msg['ID'] = $id;
+ $fetch = substr($read,$i_space+1,5);
+ if (!is_numeric($id) && $fetch !== 'FETCH') {
+ $msg['ERROR'] = $read; // htmlspecialchars should be done just before display. this is backend code
+ break;