page counter next up previous contents
Next: page_header.php Up: 目錄 functions Previous: mailbox_display.php   Contents

mime.php

  1. MIME是通過標準化電子郵件報文的頭部的附加域(fields)而實現的;這些頭部的附加域,描述新的報文類型的內容和組織形式。mime.php 包含檢查及解碼 MIME 郵件所需的函式。
    [root@mail squirrelmail-1.4.8]# diff -uwBN functions/mime.php /var/www/squirrelmail/functions/mime.php
    --- functions/mime.php	2006-06-20 14:14:53.000000000 +0800
    +++ /var/www/squirrelmail/functions/mime.php	2014-01-10 09:35:57.104824062 +0800
    
  2. 修改郵件內容顯示方式。
    @@ -288,7 +288,7 @@
     
             $body_ary[$i] = $line;
         }
    -    $body = '<pre>' . implode("\n", $body_ary) . '</pre>';
    +    $body = implode("<br>\n", $body_ary);// . '</pre>'; //dywang
     }
     
     /**
    
  3. 原檔名常出現亂碼,改成先經過自行撰寫的函式 utf8convert 解碼後不再出現亂碼。
    @@ -465,7 +465,7 @@
                 }
             }
     
    -        $display_filename = $filename;
    +        $display_filename = utf8convert($filename,'');  //dywang
             if (isset($passed_ent_id)) {
                 $passed_ent_id_link = '&amp;passed_ent_id='.$passed_ent_id;
             } else {
    
  4. 修改郵件 header 解碼方式。
    @@ -495,7 +499,7 @@
             $defaultlink = $hookresults[6];
     
             $attachments .= '<tr><td>' .
    -            '<a href="'.$defaultlink.'">'.decodeHeader($display_filename).'</a>&nbsp;</td>' .
    +            '<a href="'.$defaultlink.'">'.decodeHeader($display_filename,false,false,true).'</a>&nbsp;</td>' .//dywang
                 '<td><small><b>' . show_readable_size($header->size) .
                 '</b>&nbsp;&nbsp;</small></td>' .
                 '<td><small>[ '.htmlspecialchars($type0).'/'.htmlspecialchars($type1).' ]&nbsp;</small></td>' .
    
  5. decodeHeader 函式主導郵件編碼的解碼,$utfencode=true 表示郵件為 utf8 編碼,$htmlsave=true 表示保留 html 格式,$decide=false 表示決定不必解碼。如果 Header 出現 =?,而且沒出現 =?,也就是出現編碼開始,但沒有編碼結束,就刪除換行等符號,直到變數 $pre_chunk 包含整個郵件的 header,以利解碼。
    -function decodeHeader ($string, $utfencode=true,$htmlsave=true,$decide=false) {
    +function decodeHeader ($string, $utfencode=true,$htmlsave=true,$decide=false) {  //dywang debug this function
         global $languages, $squirrelmail_language,$default_charset;
         if (is_array($string)) {
             $string = implode("\n", $string);
    @@ -604,8 +615,10 @@
         $iLastMatch = -2;
         $encoded = false;
     
    +	static $pre_chunk;  //dywang
         $aString = explode(' ',$string);
         $ret = '';
         foreach ($aString as $chunk) {
             if ($encoded && $chunk === '') {
                 continue;
    @@ -616,8 +629,14 @@
             $encoded = false;
             /* if encoded words are not separated by a linear-space-white we still catch them */
             $j = $i-1;
    -
    +		if (preg_match('/^.*=\?[^?]*/',$chunk) && !preg_match('/[^?]*\?=.*$/',$chunk)) {  //dywang begin
    +			$pre_chunk = preg_replace("/[\n\t\r]/", '',$chunk);
    +			$chunk='';
    +		} else{// if (!preg_match(preg_match('/^.*=\?/',$pre_chunk) && preg_match('/\?=.*$/',$chunk)) {
    +			$chunk = $pre_chunk . $chunk;
    +			$pre_chunk = '';
    +		}  // dywang end
                 /* if the last chunk isn't an encoded string then put back the space, otherwise don't */
                 if ($iLastMatch !== $j) {
                     if ($htmlsave) {
    
  6. 因已自行設計解碼函式,故不再先測試是否可安全轉碼。
    @@ -634,9 +653,12 @@
                     $ret .= $res[1];
                 }
                 $encoding = ucfirst($res[3]);
     
                 /* decide about valid decoding */
    -            if ($decide && is_conversion_safe($res[2])) {
    +            if ($decide) {
                     $can_be_encoded=true;
                 } else {
                     $can_be_encoded=false;
    
  7. 如果必須解碼,改採用自行設計解碼函式 utf8convert。
    @@ -648,10 +670,12 @@
                         $replace = base64_decode($res[4]);
                         if ($can_be_encoded) {
                             // string is converted from one charset to another. sanitizing depends on $htmlsave
    -                        $replace =  charset_convert($res[2],$replace,$default_charset,$htmlsave);
    +						$replace = utf8convert($replace,$res[2]);  //dywang
                         } elseif ($utfencode) {
                             // string is converted to htmlentities and sanitized
                             $replace = charset_decode($res[2],$replace);
    @@ -664,7 +688,8 @@
                                 $replace);
                         if ($can_be_encoded) {
                             // string is converted from one charset to another. sanitizing depends on $htmlsave
    -                        $replace = charset_convert($res[2], $replace,$default_charset,$htmlsave);
    +						$replace = utf8convert($replace,$res[2]);  //dywang
                         } elseif ($utfencode) {
                             // string is converted to html entities and sanitized
                             $replace = charset_decode($res[2], $replace);
    
  8. 改變編碼偵測順序。
    @@ -723,7 +749,7 @@
         }
     
         // Use B encoding for multibyte charsets
    -    $mb_charsets = array('utf-8','big5','gb2313','euc-kr');
    +    $mb_charsets = array('utf-8','gb2313','big-5','euc-cn','cp936');
         if (in_array($default_charset,$mb_charsets) &&
             in_array($default_charset,sq_mb_list_encodings()) &&
             sq_is8bit($string)) {
    
  9. encodeHeader 函式郵件抬頭長度 75 不適合中文,改成 120 比較適合。
    @@ -735,9 +761,10 @@
         // Encode only if the string contains 8-bit characters or =?
         $j = strlen($string);
    -    $max_l = 75 - strlen($default_charset) - 7;
    +    $max_l = 120 - strlen($default_charset) - 7; //dywang 75 亂碼 for full_name
         $aRet = array();
         $ret = '';
         $iEncStart = $enc_init = false;
    @@ -897,7 +924,7 @@
          * remove =? ? ?= (5 chars)
          * remove 2 more chars (\r\n ?)
          */
    -    $iMaxLength = 75 - strlen($charset) - 7;
    +    $iMaxLength = 120 - strlen($charset) - 7; //dywang
     
         // set first character position
         $iStartCharNum = 0;
    
  10. 如果使用 IE 瀏覽器,取消檔名轉換成 RFC1738 編碼。
    @@ -2152,7 +2326,7 @@
         // version
         //set all the Cache Control Headers for IE
         if ($isIE) {
    -        $filename=rawurlencode($filename);
    +        //$filename=rawurlencode($filename); //dywang
             header ("Pragma: public");
             header ("Cache-Control: no-store, max-age=0, no-cache, must-revalidate"); // HTTP/1.1
             header ("Cache-Control: post-check=0, pre-check=0", false);
    
  11. 自行建立的解碼函式,目前已是最佳狀況,出現亂碼的機率很低。
    +/**
    + * This is a function to convert string coding to UTF-8.
    + * 
    + * Author  De-Yu Wang 2012/04/28 dywang                               
    + * @param  $str  the string
    + * @param  $default_charset
    + * @return        a string encoded by UTF-8
    + */
    +function utf8convert($str, $actural) {  //dywang optimal, Don't change
    +		global $default_charset;
    +		if(stristr($actural,'UTF-8')) return $str;//.'!&'.$actural;
    +		if(stristr($actural,'gb2312') || stristr($actural,'gb18030')) 
    +			return mb_convert_encoding($str,$default_charset,'GBK');//.'!#'.$actural;
    +		if($actural!="" && !stristr($actural,'gb18030'))
    +			return mb_convert_encoding($str,$default_charset,$actural);//.'!+'.$actural;//.$default_charset.$detect;
    +		$encoding_list = 'UTF-8,GB2312,BIG-5,CP936, GBK, EUC-CN';
    +		$detect = mb_detect_encoding($str,$encoding_list);
    +		if(stristr($detect,'UTF')) 
    +			return $str;//.'!/'. $detect;
    +		if(stristr($detect,'GB') || stristr($detect,'!EUC-CN')) 
    +			return mb_convert_encoding($str,$default_charset,'GB2312');//.'!-'.$detect;
    +		if(stristr($detect,'C' ) || stristr($detect,'BIG')) 
    +			return mb_convert_encoding($str,$default_charset,$detect);//.'!x'.$detect;
    +		if($detect == '') 
    +			return mb_convert_encoding($str,$default_charset,'EUC-CN');//.'!C'.$detect;
    +		return $str;//.'!/'. $detect;
    +}
    +
     ?>
    



2016-03-11