- arsort($read_list);
-
- $patterns = array (
- "/^To:(.*)\$/AUi",
- "/^From:(.*)\$/AUi",
- "/^X-Priority:(.*)\$/AUi",
- "/^Cc:(.*)\$/AUi",
- "/^Date:(.*)\$/AUi",
- "/^Subject:(.*)\$/AUi",
- "/^Content-Type:(.*)\$/AUi"
- );
- $regpattern = '';
-
- for ($msgi = 0; $msgi < $maxmsg; $msgi++) {
- $subject = _("(no subject)");
- $from = _("Unknown Sender");
- $priority = 0;
- $messageid = "<>";
- $cc = "";
- $to = "";
- $date = "";
- $type[0] = "";
- $type[1] = "";
- $inrepto = "";
- $flag_seen = false;
- $flag_answered = false;
- $flag_deleted = false;
- $flag_flagged = false;
- $read = $read_list[$msgi];
- $prevline = false;
-
- foreach ($read as $read_part) {
- //unfold multi-line headers
- if ($prevline && strpos($read_part, "\t ") === true) {
- $read_part = substr($prevline, 0, -2) . preg_replace('/(\t\s+)/',' ',$read_part);
- }
- $prevline = $read_part;
- if ($read_part{0} == '*') {
- if ($internaldate) {
- if (preg_match ("/^.+INTERNALDATE\s+\"(.+)\".+/iUA",$read_part, $reg)) {
- $tmpdate = trim($reg[1]);
- $tmpdate = str_replace(' ',' ',$tmpdate);
- $tmpdate = explode(' ',$tmpdate);
- $date = str_replace('-',' ',$tmpdate[0]) . " " .
- $tmpdate[1] . " " .
- $tmpdate[2];
- }
+
+ /*
+ * Create the query
+ */
+
+ $sFetchItems = '';
+ $query = "FETCH $msgs_str (";
+ if (count($aFetchItems)) {
+ $sFetchItems = implode(' ',$aFetchItems);
+ }
+ if (count($aHeaderFields)) {
+ $sHeaderFields = implode(' ',$aHeaderFields);
+ $sFetchItems .= ' BODY.PEEK[HEADER.FIELDS ('.$sHeaderFields.')]';
+ }
+ $query .= trim($sFetchItems) . ')';
+ $aResponse = sqimap_run_command_list ($imap_stream, $query, true, $response, $message, $bUidFetch);
+ $aMessages = parseFetch($aResponse,$aMessageList);
+ array_reverse($aMessages);
+ return $aMessages;
+}
+
+
+/**
+ * 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()) {
+ for ($j=0,$iCnt=count($aResponse);$j<$iCnt;++$j) {
+ $aMsg = array();
+
+ $read = implode('',$aResponse[$j]);
+ // free up memmory
+ unset($aResponse[$j]); /* unset does not reindex the array. the for loop is safe */
+ /*
+ * #id<space>FETCH<space>(
+ */
+
+ /* extract the message id */
+ $i_space = strpos($read,' ',2);/* position 2ed <space> */
+ $id = substr($read,2/* skip "*<space>" */,$i_space -2);
+ $aMsg['ID'] = $id;
+ $fetch = substr($read,$i_space+1,5);
+ if (!is_numeric($id) && $fetch !== 'FETCH') {
+ $aMsg['ERROR'] = $read; // htmlspecialchars should be done just before display. this is backend code
+ break;
+ }
+ $i = strpos($read,'(',$i_space+5);
+ $read = substr($read,$i+1);
+ $i_len = strlen($read);
+ $i = 0;
+ while ($i < $i_len && $i !== false) {
+ /* get argument */
+ $read = trim(substr($read,$i));
+ $i_len = strlen($read);
+ $i = strpos($read,' ');
+ $arg = substr($read,0,$i);
+ ++$i;
+ /*
+ * use allcaps for imap items and lowcaps for headers as key for the $aMsg array
+ */
+ switch ($arg)
+ {
+ case 'UID':
+ $i_pos = strpos($read,' ',$i);
+ if (!$i_pos) {
+ $i_pos = strpos($read,')',$i);