Reduce references header in a smart way to avoid "header too long"
[squirrelmail.git] / class / deliver / Deliver.class.php
index a77471067b9416a89b487b9215861fd379190e18..f22b5f877e05d11b819bd7f13058e82bccf4a2f4 100644 (file)
@@ -7,7 +7,7 @@
  * a delivery backend.
  *
  * @author Marc Groot Koerkamp
- * @copyright © 1999-2005 The SquirrelMail Project Team
+ * @copyright © 1999-2006 The SquirrelMail Project Team
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  * @package squirrelmail
@@ -379,7 +379,7 @@ class Deliver {
      */
     function prepareRFC822_Header($rfc822_header, $reply_rfc822_header, &$raw_length) {
         global $domain, $version, $username, $encode_header_key, 
-               $edit_identity, $hide_auth_header, $hide_squirrelmail_header;
+               $edit_identity, $hide_auth_header;
 
         /* if server var SERVER_NAME not available, use $domain */
         if(!sqGetGlobalVar('SERVER_NAME', $SERVER_NAME, SQ_SERVER)) {
@@ -430,9 +430,11 @@ class Deliver {
          *
          * Add $hide_squirrelmail_header as a candidate for config_local.php
          * to allow completely hiding SquirrelMail participation in message
-         * processing.
+         * processing; This is dangerous, especially if users can modify their 
+         * account information, as it makes mapping a sent message back to the
+         * original sender almost impossible.
          */
-        $show_sm_header = ( isset($hide_squirrelmail_header) ? ! $hide_squirrelmail_header : 1 );
+        $show_sm_header = ( defined('hide_squirrelmail_header') ? ! hide_squirrelmail_header : 1 );
 
         if ( $show_sm_header ) {
           if (isset($encode_header_key) &&
@@ -717,27 +719,43 @@ class Deliver {
     }
 
     /**
-     * function calculate_references - calculate correct Referer string
+     * function calculate_references - calculate correct References string
+     * Adds the current message ID, and makes sure it doesn't grow forever,
+     * to that extent it drops message-ID's in a smart way until the string
+     * length is under the recommended value of 1000 ("References: <986>\r\n").
+     * It always keeps the first and the last three ID's.
      *
      * @param   Rfc822Header $hdr    message header to calculate from
      *
-     * @return  string       $refer  concatenated and trimmed Referer string
+     * @return  string       $refer  concatenated and trimmed References string
      */
     function calculate_references($hdr) {
-        $refer = $hdr->references;
+        $aReferences = preg_split('/\s+/', $hdr->references);
         $message_id = $hdr->message_id;
         $in_reply_to = $hdr->in_reply_to;
-        if (strlen($refer) > 2) {
-            $refer .= ' ' . $message_id;
-        } else {
-            if ($in_reply_to) {
-                $refer .= $in_reply_to . ' ' . $message_id;
-            } else {
-                $refer .= $message_id;
-            }
+       
+        // if References already exists, add the current message ID at the end.
+        // no References exists; if we know a IRT, add that aswell
+        if (count($aReferences) == 0 && $in_reply_to) {
+            $aReferences[] = $in_reply_to;
+        }
+        $aReferences[] = $message_id;
+
+        // sanitize the array: trim whitespace, remove dupes
+        array_walk($aReferences, 'trim_value');
+        $aReferences = array_unique($aReferences);
+
+        while ( count($aReferences) > 4 && strlen(implode(' ', $aReferences)) >= 986 ) {
+            $aReferences = array_merge(array_slice($aReferences,0,1),array_slice($aReferences,2));
         }
-        trim($refer);
-        return $refer;
+        return implode(' ', $aReferences);
+    }
+
+    /**
+     * Callback function to trim whitespace from a value, to be used in array_walk
+     */
+    function trim_value ( &$value ) {
+        $value = trim($value);
     }
 
     /**