Changeset a8435bd in github


Ignore:
Timestamp:
Jun 9, 2006 12:47:21 PM (7 years ago)
Author:
svncommit <devs@…>
Branches:
master, HEAD, courier-fix, dev-browser-capabilities, pdo, release-0.6, release-0.7, release-0.8
Children:
1966c53
Parents:
d72d411
Message:

Updated to latest version. Resolves issues with non-ISO-8859-1 characters in attachment filenames and headers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • program/lib/Mail/mime.php

    r856110d ra8435bd  
    115115        $this->_setEOL($crlf); 
    116116        $this->_build_params = array( 
     117                                     'head_encoding' => 'quoted-printable', 
    117118                                     'text_encoding' => '7bit', 
    118119                                     'html_encoding' => 'quoted-printable', 
     
    248249     *                              Defaults to attachment. 
    249250     *                              Possible values: attachment, inline. 
     251     * @param  string  $charset     The character set used in the filename 
     252     *                              of this attachment. 
    250253     * @return mixed true on success or PEAR_Error object 
    251254     * @access public 
     
    254257                           $name = '', $isfilename = true, 
    255258                           $encoding = 'base64', 
    256                            $disposition = 'attachment') 
     259                           $disposition = 'attachment', $charset = '') 
    257260    { 
    258261        $filedata = ($isfilename === true) ? $this->_file2str($file) 
     
    280283                                'c_type'      => $c_type, 
    281284                                'encoding'    => $encoding, 
     285                                'charset'     => $charset, 
    282286                                'disposition' => $disposition 
    283287                               ); 
     
    434438    { 
    435439        $params['content_type'] = $value['c_type'] . '; ' . 
    436                                   'name="' . $params['dfilename'] . '"'; 
     440                                  'name="' . $value['name'] . '"'; 
    437441        $params['encoding']     = 'base64'; 
    438442        $params['disposition']  = 'inline'; 
     
    455459    function &_addAttachmentPart(&$obj, $value) 
    456460    { 
     461        $params['dfilename']    = $value['name']; 
     462        $params['encoding']     = $value['encoding']; 
     463        if ($value['disposition'] != "inline") { 
     464            $fname = array("fname" => $value['name']); 
     465            $fname_enc = $this->_encodeHeaders($fname); 
     466            $params['dfilename'] = $fname_enc['fname']; 
     467        } 
     468        if ($value['charset']) { 
     469            $params['charset'] = $value['charset']; 
     470        } 
    457471        $params['content_type'] = $value['c_type'] . '; ' . 
    458472                                  'name="' . $params['dfilename'] . '"'; 
    459         $params['encoding']     = $value['encoding']; 
    460473        $params['disposition']  = isset($value['disposition']) ?  
    461474                                  $value['disposition'] : 'attachment'; 
    462         $params['dfilename']    = $value['name']; 
    463475        $ret = $obj->addSubpart($value['body'], $params); 
    464476        return $ret; 
     
    501513     * @param  array  Build parameters that change the way the email 
    502514     *                is built. Should be associative. Can contain: 
     515     *                head_encoding  -  What encoding to use for the headers.  
     516     *                                  Options: quoted-printable or base64 
     517     *                                  Default is quoted-printable 
    503518     *                text_encoding  -  What encoding to use for plain text 
     519     *                                  Options: 7bit, 8bit, base64, or quoted-printable 
    504520     *                                  Default is 7bit 
    505521     *                html_encoding  -  What encoding to use for html 
     522     *                                  Options: 7bit, 8bit, base64, or quoted-printable 
    506523     *                                  Default is quoted-printable 
    507524     *                7bit_wrap      -  Number of characters before text is 
     
    744761 
    745762    /** 
     763     * Since the PHP send function requires you to specifiy  
     764     * recipients (To: header) separately from the other 
     765     * headers, the To: header is not properly encoded. 
     766     * To fix this, you can use this public method to  
     767     * encode your recipients before sending to the send 
     768     * function 
     769     * 
     770     * @param  string $recipients A comma-delimited list of recipients 
     771     * @return string Encoded data 
     772     * @access public 
     773     */ 
     774    function encodeRecipients($recipients) 
     775    { 
     776        $input = array("To" => $recipients); 
     777        $retval = $this->_encodeHeaders($input); 
     778        return $retval["To"] ; 
     779    } 
     780 
     781    /** 
    746782     * Encodes a header as per RFC2047 
    747783     * 
    748      * @param  string $input The header data to encode 
    749      * @return string Encoded data 
     784     * @param  array $input The header data to encode 
     785     * @return array Encoded data 
    750786     * @access private 
    751787     */ 
     
    753789    { 
    754790        foreach ($input as $hdr_name => $hdr_value) { 
    755             preg_match_all('/(\w*[\x80-\xFF]+\w*)/', $hdr_value, $matches); 
    756             foreach ($matches[1] as $value) { 
    757                 $replacement = preg_replace('/([\x80-\xFF])/e', 
    758                                             '"=" . 
    759                                             strtoupper(dechex(ord("\1")))', 
    760                                             $value); 
    761                 $hdr_value = str_replace($value, '=?' . 
    762                                          $this->_build_params['head_charset'] . 
    763                                          '?Q?' . $replacement . '?=', 
    764                                          $hdr_value); 
     791            if (function_exists('iconv_mime_encode') && preg_match('#[\x80-\xFF]{1}#', $hdr_value)){ 
     792                $imePref = array(); 
     793                if ($this->_build_params['head_encoding'] == 'base64'){ 
     794                    $imePrefs['scheme'] = 'B'; 
     795                }else{ 
     796                    $imePrefs['scheme'] = 'Q'; 
     797                } 
     798                $imePrefs['input-charset']  = $this->_build_params['head_charset']; 
     799                $imePrefs['output-charset'] = $this->_build_params['head_charset']; 
     800                $hdr_value = iconv_mime_encode($hdr_name, $hdr_value, $imePrefs); 
     801                $hdr_value = preg_replace("#^{$hdr_name}\:\ #", "", $hdr_value); 
     802            }elseif (preg_match('#[\x80-\xFF]{1}#', $hdr_value)){ 
     803                //This header contains non ASCII chars and should be encoded. 
     804                switch ($this->_build_params['head_encoding']) { 
     805                case 'base64': 
     806                    //Base64 encoding has been selected. 
     807                     
     808                    //Generate the header using the specified params and dynamicly  
     809                    //determine the maximum length of such strings. 
     810                    //75 is the value specified in the RFC. The -2 is there so  
     811                    //the later regexp doesn't break any of the translated chars. 
     812                    $prefix = '=?' . $this->_build_params['head_charset'] . '?B?'; 
     813                    $suffix = '?='; 
     814                    $maxLength = 75 - strlen($prefix . $suffix) - 2; 
     815                    $maxLength1stLine = $maxLength - strlen($hdr_name); 
     816                     
     817                    //Base64 encode the entire string 
     818                    $hdr_value = base64_encode($hdr_value); 
     819 
     820                    //This regexp will break base64-encoded text at every  
     821                    //$maxLength but will not break any encoded letters. 
     822                    $reg1st = "|.{0,$maxLength1stLine}[^\=][^\=]|"; 
     823                    $reg2nd = "|.{0,$maxLength}[^\=][^\=]|"; 
     824                    break; 
     825                case 'quoted-printable': 
     826                default: 
     827                    //quoted-printable encoding has been selected 
     828                     
     829                    //Generate the header using the specified params and dynamicly  
     830                    //determine the maximum length of such strings. 
     831                    //75 is the value specified in the RFC. The -2 is there so  
     832                    //the later regexp doesn't break any of the translated chars. 
     833                    $prefix = '=?' . $this->_build_params['head_charset'] . '?Q?'; 
     834                    $suffix = '?='; 
     835                    $maxLength = 75 - strlen($prefix . $suffix) - 2; 
     836                    $maxLength1stLine = $maxLength - strlen($hdr_name); 
     837                     
     838                    //Replace all special characters used by the encoder. 
     839                    $search  = array("=",   "_",   "?",   " "); 
     840                    $replace = array("=3D", "=5F", "=3F", "_"); 
     841                    $hdr_value = str_replace($search, $replace, $hdr_value); 
     842                     
     843                    //Replace all extended characters (\x80-xFF) with their 
     844                    //ASCII values. 
     845                    $hdr_value = preg_replace( 
     846                        '#([\x80-\xFF])#e', 
     847                        '"=" . strtoupper(dechex(ord("\1")))', 
     848                        $hdr_value 
     849                    ); 
     850                    //This regexp will break QP-encoded text at every $maxLength 
     851                    //but will not break any encoded letters. 
     852                    $reg1st = "|(.{0,$maxLength})[^\=]|"; 
     853                    $reg2nd = "|(.{0,$maxLength})[^\=]|"; 
     854                    break; 
     855                } 
     856                //Begin with the regexp for the first line. 
     857                $reg = $reg1st; 
     858                $output = ""; 
     859                while ($hdr_value) { 
     860                    //Split translated string at every $maxLength 
     861                    //But make sure not to break any translated chars. 
     862                    $found = preg_match($reg, $hdr_value, $matches); 
     863                     
     864                    //After this first line, we need to use a different 
     865                    //regexp for the first line. 
     866                    $reg = $reg2nd; 
     867 
     868                    //Save the found part and encapsulate it in the 
     869                    //prefix & suffix. Then remove the part from the 
     870                    //$hdr_value variable. 
     871                    if ($found){ 
     872                        $part = $matches[0]; 
     873                        $hdr_value = substr($hdr_value, strlen($matches[0])); 
     874                    }else{ 
     875                        $part = $hdr_value; 
     876                        $hdr_value = ""; 
     877                    } 
     878                     
     879                    //RFC 2047 specifies that any split header should be seperated 
     880                    //by a CRLF SPACE.  
     881                    if ($output){ 
     882                        $output .=  "\r\n "; 
     883                    } 
     884                    $output .= $prefix . $part . $suffix; 
     885                } 
     886                $hdr_value = $output; 
    765887            } 
    766888            $input[$hdr_name] = $hdr_value; 
     
    788910} // End of class 
    789911?> 
     912 
Note: See TracChangeset for help on using the changeset viewer.