improved encodeHeader function and decodeHeader handling to follow RFC's
authorstekkel <stekkel@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Fri, 7 Mar 2003 22:11:22 +0000 (22:11 +0000)
committerstekkel <stekkel@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Fri, 7 Mar 2003 22:11:22 +0000 (22:11 +0000)
better.

git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@4612 7612ce4b-ef26-0410-bec9-ea0150e637f0

functions/file_prefs.php
functions/imap_general.php
functions/mailbox_display.php
functions/mime.php

index 79852012d53f229bb1edebfce15eb422c62ad836..1cde4c88ffd71b854266878faf6562eccdcc359b 100644 (file)
@@ -18,7 +18,7 @@ function cachePrefValues($data_dir, $username) {
     global $prefs_are_cached, $prefs_cache;
        
     if ( isset($prefs_are_cached) && $prefs_are_cached) {
-        return;
+        return $prefs_cache;
     }
     
     sqsession_unregister('prefs_cache');
@@ -79,6 +79,7 @@ function cachePrefValues($data_dir, $username) {
 
     sqsession_register($prefs_cache, 'prefs_cache');
     sqsession_register($prefs_are_cached, 'prefs_are_cached');
+    return $prefs_cache;
 }
    
 /**
@@ -89,7 +90,7 @@ function getPref($data_dir, $username, $string, $default = '') {
 
     $result = do_hook_function('get_pref_override',array($username,$string));
     if (!$result) {
-       cachePrefValues($data_dir, $username);
+       $prefs_cache = cachePrefValues($data_dir, $username);
        if (isset($prefs_cache[$string])) {
            $result = $prefs_cache[$string];
        } else {
@@ -105,11 +106,16 @@ function getPref($data_dir, $username, $string, $default = '') {
 /**
  * Save the preferences for this user.
  */
-function savePrefValues($data_dir, $username) {
-    global $prefs_cache;
+function savePrefValues($data_dir, $username, $prefs_cache) {
    
     $filename = getHashedFile($username, $data_dir, "$username.pref");
 
+    if (!is_array($prefs_cache) || count($prefs_cache) == 0) {
+        include_once(SM_PATH . 'functions/display_messages.php');
+        logout_error( _("Houston we have a problem, the preference cache is lost!!!."));
+        exit;
+    }
+
     /* Open the file for writing, or else display an error to the user. */
     if(!$file = @fopen($filename.'.tmp', 'w'))
     {
@@ -138,13 +144,11 @@ function savePrefValues($data_dir, $username) {
 function removePref($data_dir, $username, $string) {
     global $prefs_cache;
 
-    cachePrefValues($data_dir, $username);
     if (isset($prefs_cache[$string])) {
         unset($prefs_cache[$string]);
     }
  
-    savePrefValues($data_dir, $username);
+    savePrefValues($data_dir, $username,$prefs_cache);
 }
 
 /**
@@ -153,18 +157,16 @@ function removePref($data_dir, $username, $string) {
 function setPref($data_dir, $username, $string, $value) {
     global $prefs_cache;
 
-    cachePrefValues($data_dir, $username);
     if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
         return;
     }
 
     if ($value === '') {
-        removePref($data_dir, $username, $string);
-        return;
+        unset($prefs_cache[$string]);
+    } else {
+        $prefs_cache[$string] = $value;
     }
-
-    $prefs_cache[$string] = $value;
-    savePrefValues($data_dir, $username);
+    savePrefValues($data_dir, $username, $prefs_cache);
 }
 
 /**
index 4504ce9d3634d78fdebd5a8ebd5c93386c06a258..b2880f87810c0d79325ffeed5598975a25f855be 100755 (executable)
@@ -449,7 +449,7 @@ function parseAddress($address, $max=0, $addr_ar = array(), $group = '', $host='
         switch ($char) {
             case '=':
                 if (preg_match('/^(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(.*)/Ui',substr($address,$pos),$reg)) {
-                    $personal = $reg[1];
+                    $personal .= $reg[1];
                     $pos += strlen($personal);
                 }
                 ++$pos;
index 73b1ee603cf944b3f00368c46472375de011a268..36c643393c975207a4e9b5c4ba14f60b24027d16 100644 (file)
@@ -93,8 +93,8 @@ function printMessageInfo($imapConnection, $t, $not_last=true, $key, $mailbox,
         }
     } 
     
-
-    $subject = processSubject(decodeHeader($msg['SUBJECT']), $indent_array[$msg['ID']]);
+    $subject_full = decodeHeader($msg['SUBJECT']);
+    $subject = processSubject($subject_full, $indent_array[$msg['ID']]);
 
     echo html_tag( 'tr','','','','VALIGN="top"') . "\n";
 
@@ -209,11 +209,11 @@ function printMessageInfo($imapConnection, $t, $not_last=true, $key, $mailbox,
                 $td_str .= '<a href="read_body.php?mailbox='.$urlMailbox
                         .  '&amp;passed_id='. $msg["ID"]
                         .  '&amp;startMessage='.$start_msg.$searchstr.'"';
-                $td_str .= ' ' .concat_hook_function('subject_link');
-                if ($subject != $msg['SUBJECT']) {
+                do_hook("subject_link");
+                if ($subject != $subject_full) {
                     $title = get_html_translation_table(HTML_SPECIALCHARS);
                     $title = array_flip($title);
-                    $title = strtr($msg['SUBJECT'], $title);
+                    $title = strtr($subject_full, $title);
                     $title = str_replace('"', "''", $title);
                     $td_str .= " title=\"$title\"";
                 }
index b957f986f7699b4c6602558427455c676b1348ae..44f198ff48afa9f47eb28c8de4b5824899406023 100644 (file)
@@ -34,6 +34,7 @@ function mime_structure ($bodystructure, $flags=array()) {
     if (!is_object($msg)) {
         include_once(SM_PATH . 'functions/display_messages.php');
         global $color, $mailbox;
+        /* removed urldecode because $_GET is auto urldecoded ??? */
         displayPageHeader( $color, $mailbox );
         echo "<BODY TEXT=\"$color[8]\" BGCOLOR=\"$color[4]\" LINK=\"$color[7]\" VLINK=\"$color[7]\" ALINK=\"$color[7]\">\n\n" .
          '<CENTER>';
@@ -548,7 +549,7 @@ function decodeHeader ($string, $utfencode=true,$htmlsave=true) {
     if (is_array($string)) {
         $string = implode("\n", $string);
     }
-
+    
     if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
         function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
         $string = $languages[$squirrelmail_language]['XTRA_CODE']('decodeheader', $string);
@@ -562,7 +563,7 @@ function decodeHeader ($string, $utfencode=true,$htmlsave=true) {
         $aString[$i] = '';
         while (preg_match('/^(.*)=\?([^?]*)\?(Q|B)\?([^?]*)\?=(.*)$/Ui',$chunk,$res)) {
             //$aString[$i] .= $res[1];
-           //echo "match ". $res[5] . "<br>";
+           //echo "$chunk match ". $res[5] . "<br>";
             $encoding = ucfirst($res[3]);
             switch ($encoding)
             {
@@ -581,7 +582,7 @@ function decodeHeader ($string, $utfencode=true,$htmlsave=true) {
                     $replace = charset_decode($res[2], $replace);
                 } else {
                     if ($htmlsave) {
-                        $replace = htmlspecialchars($res[4]);
+                        $replace = htmlspecialchars($replace);
                     }
                 }
                 $aString[$i] .= $replace;
@@ -599,11 +600,8 @@ function decodeHeader ($string, $utfencode=true,$htmlsave=true) {
         }
         ++$i;
     }
-    if ($htmlsave) {
-       return implode('&nbsp;',$aString);
-    } else {
-       return implode (' ',$aString);
-    }
+    return implode (' ',$aString);
+
 }
 
 /*
@@ -618,117 +616,115 @@ function encodeHeader ($string) {
         function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
         return  $languages[$squirrelmail_language]['XTRA_CODE']('encodeheader', $string);
     }
+    if (strtolower($default_charset) == 'iso-8859-1') {
+        $string = str_replace("\240",' ',$string);
+    }
 
     // Encode only if the string contains 8-bit characters or =?
     $j = strlen($string);
-    $l = strstr($string, '=?');         // Must be encoded ?
     $max_l = 75 - strlen($default_charset) - 7;
     $aRet = array();
     $ret = '';
-    $enc = false;
+    $iEncStart = $enc_init = false;
     $cur_l = $iOffset = 0;
     for($i = 0; $i < $j; ++$i) {
-        switch($string{$i}) {
-            case '=':
-                $cur_l+=3;
-                if ($cur_l > $max_l) {
-                   if ($enc) {
-                       $aRet[] = "=?$default_charset?Q?$ret?=";
-                       $enc = false;
-                   } else {
-                       $aRet[] = substr($string,$iOffset,$i-$iOffset+1);
-                   }
-                   $iOffset = $i+1;
-                   $cur_l = 3;
-                   $ret = '';
-               }
-                $ret .= '=3D';
-                break;
-            case '?':
-                $cur_l+=3;
-                if ($cur_l > $max_l) {
-                   if ($enc) {
-                       $aRet[] = "=?$default_charset?Q?$ret?=";
-                       $enc = false;
-                   } else {
-                       $aRet[] = substr($string,$iOffset,$i-$iOffset+1);
-                   }
-                   $iOffset = $i+1;
-                   $cur_l = 3;
-                   $ret = '';
-               }
-                $ret .= '=3F';
-                break;
-            case '_':
-                $cur_l+=3;
-                if ($cur_l > $max_l) {
-                   if ($enc) {
-                       $aRet[] = "=?$default_charset?Q?$ret?=";
-                       $enc = false;
-                   } else {
-                       $aRet[] = substr($string,$iOffset,$i-$iOffset+1);
-                   }
-                   $iOffset = $i+1;
-                   $cur_l = 3;
-                    $ret = '';
-               }
-                $ret .= '=5F';
-                break;
-            case ' ':
+        switch($string{$i})
+        {
+        case '=':
+        case '<':
+        case '>':
+        case ',':
+        case '?':
+        case '_':
+            if ($iEncStart === false) {
+                $iEncStart = $i;
+            }
+            $cur_l+=3;
+            if ($cur_l > ($max_l-2)) {
+                $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset);
+                $aRet[] = "=?$default_charset?Q?$ret?=";
+                $iOffset = $i;
+                $cur_l = 0;
+                $ret = '';
+                $iEncStart = false;
+            } else {
+                $ret .= sprintf("=%02X",ord($string{$i}));
+            }
+            break;
+        case '(':
+        case ')':
+            if ($iEncStart !== false) {
+                $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset);                        
+                $aRet[] = "=?$default_charset?Q?$ret?=";
+                $iOffset = $i;
+                $cur_l = 0;
+                $ret = '';
+                $iEncStart = false;
+            }
+            break;
+        case ' ':
+            if ($iEncStart !== false) {
                 $cur_l++;
                 if ($cur_l > $max_l) {
-                   if ($enc) {
-                       $aRet[] = "=?$default_charset?Q?$ret?=";
-                       $enc = false;
-                   } else {
-                       $aRet[] = substr($string,$iOffset,$i-$iOffset+1);
-                   }
-                   $iOffset = $i+1;
-                   $cur_l = 1;
-                   $ret = '';
-                }                
-                $ret .= '_';
-                break;
-            default:
-                $k = ord($string{$i});
-                if ($k > 126) {
-                   $enc = true;
-                    $s = sprintf("=%02X", $k);
-                    $cur_l += strlen($s);
-                    if ($cur_l > $max_l) {
+                    $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset);                        
+                    $aRet[] = "=?$default_charset?Q?$ret?=";
+                    $iOffset = $i;
+                    $cur_l = 0;
+                    $ret = '';
+                    $iEncStart = false;
+                } else {                
+                    $ret .= '_';
+                }
+            }
+            break;
+        default:
+            $k = ord($string{$i});
+            if ($k > 126) {
+                if ($iEncStart === false) {
+                    $iEncStart = $i;
+                }
+                $cur_l += 3;
+                if ($cur_l > ($max_l-2)) {
+                    if ($iEncStart !== false) {
+                        $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset);                        
                         $aRet[] = "=?$default_charset?Q?$ret?=";
-                        $cur_l = strlen($s);
-                        $ret = '';
-                       $l = false;
-                       $enc = false;
-                       $iOffset = $i+1;
-                    }                     
-                    $ret .= $s;
-                    $l = TRUE;
-                } else {
+                    } else {
+                        $aRet[] = substr($string,$iOffset,$i-$iOffset);
+                    }
+                    $cur_l = 3;
+                    $ret = '';
+                    $iOffset = $i;
+                }
+                $enc_init = true;                    
+                $ret .= sprintf("=%02X", $k);
+            } else {
+                if ($iEncStart !== false) {
                     $cur_l++;
                     if ($cur_l > $max_l) {
-                       if ($enc) {
-                          $aRet[] = "=?$default_charset?Q?$ret?=";
-                          $enc = false;
-                       } else {
-                          $aRet[] = substr($ret,$iOffset,$i-$iOffset+1);
-                       }
-                       $iOffset = $i+1;
-                       $l = false;
-                        $cur_l = 1;
+                        $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset);  
+                        $aRet[] = "=?$default_charset?Q?$ret?=";
+                        $iEncStart = false;
+                        $iOffset = $i;
+                        $cur_l = 0;
                         $ret = '';
-                    }                     
-                    $ret .= $string{$i};
+                    } else {                     
+                        $ret .= $string{$i};
+                    }
                 }
-                break;
+            }
+            break;
         }
     }
 
-    if ($enc) {
-        $string = implode('',$aRet) . "=?$default_charset?Q?$ret?=";
+    if ($enc_init) {
+        if ($iEncStart !== false) {
+            $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset);
+            $aRet[] = "=?$default_charset?Q?$ret?=";
+        } else {
+            $aRet[] = substr($string,$iOffset);
+        }
+        $string = implode('',$aRet);
     }
-
     return $string;
 }