[dev] [imp-patch] invalid date headers

Francois Marier francois at nit.ca
Thu Jul 29 11:52:58 PDT 2004


Here's an updated patch in which I moved the function to a method.

Feel free to use this one instead of the previous one if you think the
performance impact will not be too significant.

Francois
-------------- next part --------------
diff -rpuN -X ../ignorelist ../build/imp/lib/Mailbox.php imp/lib/Mailbox.php
--- ../build/imp/lib/Mailbox.php	Mon Jun 28 11:30:57 2004
+++ imp/lib/Mailbox.php	Thu Jul 29 14:47:00 2004
@@ -266,7 +266,7 @@ class IMP_Mailbox {
         /* Retrieve information from each mailbox. */
         foreach ($mboxes as $mbox => $ids) {
             $imp_imap->changeMbox($mbox, OP_READONLY);
-            $imapOverview = @imap_fetch_overview($imp['stream'], implode(',', array_keys($ids)), FT_UID);
+            $imapOverview = $this->_imap_fetch_overview($imp['stream'], implode(',', array_keys($ids)), FT_UID);
             foreach ($imapOverview as $header) {
                 $key = $ids[$header->uid];
                 $overview[$key] = array();
@@ -1017,7 +1017,7 @@ class IMP_Mailbox {
                     $this->_threadIndent[$val] = count($branches);
 
                     if ($this->_delhide) {
-                        $overview = @imap_fetch_overview($imp['stream'], $val, FT_UID);
+                        $overview = $this->_imap_fetch_overview($imp['stream'], $val, FT_UID);
                         if (is_array($overview) &&
                             ($overview[0]->deleted == 0)) {
                             $sorted[] = $val;
@@ -1192,6 +1192,51 @@ class IMP_Mailbox {
                 $this->_sortby = SORTFROM;
             }
         }
     }
+
+    /**
+     * Runs the imap_fetch_overview function and fixes the date headers
+     * afterwards if necessary.
+     *
+     * @access private
+     */
+    function _imap_fetch_overview($imap_stream, $sequence, $options)
+    {
+        $overview = @imap_fetch_overview($imap_stream, $sequence, $options);
+        while (list($key, $header) = each($overview)) {
+            # Fix invalid "Date:" headers
+            if (empty($header->date) || (strtotime($header->date) == -1)) {
+                $full_header = explode("\n", imap_fetchheader($imap_stream, $header->uid, $options));
+                $in_received = false;
+                foreach ($full_header as $line) {
+                    # Find out whether we are in a "Received:" field
+                    if (substr($line, 0, 9) == 'Received:') {
+                        $in_received = true;
+                    } else {
+                        $first_char = substr($line, 0, 1);
+                        if (($first_char != " ") && ($first_char != "\t")) {
+                            $in_received = false;
+                        }
+                    }
+
+                    # Extract date from the "Received:" field
+                    if ($in_received) {
+                        if (($semicolon = strpos($line, ';')) != FALSE) {
+                            $candidate = substr($line, $semicolon + 1);
+                        } else {
+                            $candidate = $line;
+                        }
+
+                        $candidate = trim($candidate);
+                        if (!empty($candidate) && ($new_date = strtotime($candidate)) != -1) {
+                            $overview[$key]->date = $candidate;
+                            break; // choose the first date from the top
+                        }
+                    }
+                }
+            }
+        }
+        return $overview;
+    }
 
 }


More information about the dev mailing list