Whoops. Forgot the other part of it.
[squirrelmail.git] / functions / mime.php
index 471d23322cd1fb78e93238b4a89fefaa1161391f..f7c3379d67b4c881a1f321161a5014887be6ab04 100644 (file)
@@ -660,13 +660,6 @@ function decodeHeader ($string, $utfencode=true,$htmlsave=true) {
             $chunk = $res[5];
             $encoded = true;
         }
-
-        if (!$encoded && $htmlsave) {
-            $ret .= htmlspecialchars($chunk);
-        } else {
-            $ret .= $chunk;
-        }
-
         if (!$encoded) {
             if ($htmlsave) {
                 $ret .= ' ';
@@ -674,8 +667,22 @@ function decodeHeader ($string, $utfencode=true,$htmlsave=true) {
                 $ret .= ' ';
             }
         }
+
+        if (!$encoded && $htmlsave) {
+            $ret .= htmlspecialchars($chunk);
+        } else {
+            $ret .= $chunk;
+        }
         ++$i;
     }
+    /* remove the first added space */
+    if ($ret) {
+        if ($htmlsave) {
+            $ret = substr($ret,6);
+        } else {
+            $ret = substr($ret,1);
+        }
+    }
 
     return $ret;
 }
@@ -850,6 +857,7 @@ function sq_unbackslash($attvalue){
     /**
      * Remove any backslashes. See if there are any first.
      */
+
     if (strstr($attvalue, '\\') !== false){
         $attvalue = stripslashes($attvalue);
     }
@@ -1282,13 +1290,14 @@ function sq_getnxtag($body, $offset){
  * @param  $attvalue A string to run entity check against.
  * @return           Translated value.
  */
+
 function sq_deent($attvalue){
     $me = 'sq_deent';
     /**
      * See if we have to run the checks first. All entities must start
      * with "&".
      */
-    if (strpos($attvalue, "&") === false){
+    if (strpos($attvalue, '&') === false){
         return $attvalue;
     }
     /**
@@ -1299,22 +1308,22 @@ function sq_deent($attvalue){
      * Leave " in, as it can mess us up.
      */
     $trans = array_flip($trans);
-    unset($trans{"""});
+    unset($trans{'"'});
     while (list($ent, $val) = each($trans)){
-        $attvalue = preg_replace("/$ent*(\W)/si", "$val\\1", $attvalue);
+        $attvalue = preg_replace('/' . $ent . '*/si', $val, $attvalue);
     }
     /**
      * Now translate numbered entities from 1 to 255 if needed.
      */
-    if (strpos($attvalue, "#") !== false){
+    if (strpos($attvalue, '#') !== false){
         $omit = Array(34, 39);
-        for ($asc=1; $asc<256; $asc++){
+        for ($asc = 256; $asc >= 0; $asc--){
             if (!in_array($asc, $omit)){
                 $chr = chr($asc);
-                $attvalue = preg_replace("/\&#0*$asc;*(\D)/si", "$chr\\1",
-                                         $attvalue);
-                $attvalue = preg_replace("/\&#x0*".dechex($asc).";*(\W)/si",
-                                         "$chr\\1", $attvalue);
+                $octrule = '/\&#0*' . $asc . ';*/si';
+                $hexrule = '/\&#x0*' . dechex($asc) . ';*/si';
+                $attvalue = preg_replace($octrule, $chr, $attvalue);
+                $attvalue = preg_replace($hexrule, $chr, $attvalue);
             }
         }
     }