- $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];
- }
+ }
+ } else {
+ $msgs_str = '1:*';
+ }
+
+ /*
+ * 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()) {
+ 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;
+ }
+ $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;
+ switch ($arg)
+ {
+ case 'UID':
+ $i_pos = strpos($read,' ',$i);
+ if (!$i_pos) {
+ $i_pos = strpos($read,')',$i);