Changeset ee289dc in github for program/lib/Mail/mimePart.php


Ignore:
Timestamp:
Jun 13, 2008 3:42:15 PM (5 years ago)
Author:
alecpl <alec@…>
Branches:
master, HEAD, courier-fix, dev-browser-capabilities, pdo, release-0.6, release-0.7, release-0.8
Children:
f0d4b72
Parents:
5d725ed
Message:
File:
1 edited

Legend:

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

    rf294da0 ree289dc  
    136136        } 
    137137 
     138        $contentType = array(); 
     139        $contentDisp = array(); 
    138140        foreach ($params as $key => $value) { 
    139141            switch ($key) { 
    140142                case 'content_type': 
    141                     $headers['Content-Type'] = $value . (isset($charset) ? '; charset="' . $charset . '"' : ''); 
     143                    $contentType['type'] = $value; 
     144                    //$headers['Content-Type'] = $value . (isset($charset) ? '; charset="' . $charset . '"' : ''); 
    142145                    break; 
    143146 
     
    152155 
    153156                case 'disposition': 
    154                     $headers['Content-Disposition'] = $value . (isset($dfilename) ? '; filename="' . $dfilename . '"' : ''); 
     157                    $contentDisp['disp'] = $value; 
    155158                    break; 
    156159 
    157160                case 'dfilename': 
    158                     if (isset($headers['Content-Disposition'])) { 
    159                         $headers['Content-Disposition'] .= '; filename="' . $value . '"'; 
    160                     } else { 
    161                         $dfilename = $value; 
    162                     } 
     161                    $contentDisp['filename'] = $value; 
     162                    $contentType['name'] = $value; 
    163163                    break; 
    164164 
     
    168168 
    169169                case 'charset': 
    170                     if (isset($headers['Content-Type'])) { 
    171                         $headers['Content-Type'] .= '; charset="' . $value . '"'; 
    172                     } else { 
    173                         $charset = $value; 
    174                     } 
    175                     break; 
     170                    $contentType['charset'] = $value; 
     171                    $contentDisp['charset'] = $value; 
     172                    break; 
     173 
     174                case 'language': 
     175                    $contentType['language'] = $value; 
     176                    $contentDisp['language'] = $value; 
     177                    break; 
     178 
     179                case 'location': 
     180                    $headers['Content-Location'] = $value; 
     181                    break; 
     182 
    176183            } 
    177184        } 
    178  
     185        if (isset($contentType['type'])) { 
     186            $headers['Content-Type'] = $contentType['type']; 
     187            if (isset($contentType['name'])) { 
     188                $headers['Content-Type'] .= ';' . MAIL_MIMEPART_CRLF; 
     189                $headers['Content-Type'] .= $this->_buildHeaderParam('name', $contentType['name'],  
     190                                                isset($contentType['charset']) ? $contentType['charset'] : 'US-ASCII',  
     191                                                isset($contentType['language']) ? $contentType['language'] : NULL); 
     192            } elseif (isset($contentType['charset'])) { 
     193                $headers['Content-Type'] .= "; charset=\"{$contentType['charset']}\""; 
     194            } 
     195        } 
     196 
     197 
     198        if (isset($contentDisp['disp'])) { 
     199            $headers['Content-Disposition'] = $contentDisp['disp']; 
     200            if (isset($contentDisp['filename'])) { 
     201                $headers['Content-Disposition'] .= ';' . MAIL_MIMEPART_CRLF; 
     202                $headers['Content-Disposition'] .= $this->_buildHeaderParam('filename', $contentDisp['filename'],  
     203                                                isset($contentDisp['charset']) ? $contentDisp['charset'] : 'US-ASCII',  
     204                                                isset($contentDisp['language']) ? $contentDisp['language'] : NULL); 
     205            } 
     206        } 
     207         
     208         
     209         
     210         
    179211        // Default content-type 
    180212        if (!isset($headers['Content-Type'])) { 
     
    208240        $encoded =& $this->_encoded; 
    209241 
    210         if (!empty($this->_subparts)) { 
    211 // http://pear.php.net/bugs/bug.php?id=13032 
    212 //            srand((double)microtime()*1000000); 
     242        if (count($this->_subparts)) { 
     243            srand((double)microtime()*1000000); 
    213244            $boundary = '=_' . md5(rand() . microtime()); 
    214245            $this->_headers['Content-Type'] .= ';' . MAIL_MIMEPART_CRLF . "\t" . 'boundary="' . $boundary . '"'; 
     
    221252                    $headers[] = $key . ': ' . $value; 
    222253                } 
    223                 $subparts[] = implode(MAIL_MIMEPART_CRLF, $headers) . MAIL_MIMEPART_CRLF . MAIL_MIMEPART_CRLF . $tmp['body']; 
     254                $subparts[] = implode(MAIL_MIMEPART_CRLF, $headers) . MAIL_MIMEPART_CRLF . MAIL_MIMEPART_CRLF . $tmp['body'] . MAIL_MIMEPART_CRLF; 
    224255            } 
    225256 
    226             $encoded['body'] = '--' . $boundary . MAIL_MIMEPART_CRLF . 
    227                                implode('--' . $boundary . MAIL_MIMEPART_CRLF, $subparts) . 
    228                                '--' . $boundary.'--' . MAIL_MIMEPART_CRLF . MAIL_MIMEPART_CRLF; 
     257            $encoded['body'] = '--' . $boundary . MAIL_MIMEPART_CRLF .  
     258                               rtrim(implode('--' . $boundary . MAIL_MIMEPART_CRLF , $subparts), MAIL_MIMEPART_CRLF) . MAIL_MIMEPART_CRLF .  
     259                               '--' . $boundary.'--' . MAIL_MIMEPART_CRLF; 
    229260 
    230261        } else { 
    231             $encoded['body'] = $this->_getEncodedData($this->_body, $this->_encoding) . MAIL_MIMEPART_CRLF; 
     262            $encoded['body'] = $this->_getEncodedData($this->_body, $this->_encoding); 
    232263        } 
    233264 
     
    308339        $output = ''; 
    309340 
    310         while(list(, $line) = each($lines)){ 
     341        while (list(, $line) = each($lines)) { 
    311342 
    312343            $line    = preg_split('||', $line, -1, PREG_SPLIT_NO_EMPTY); 
     
    318349                $dec  = ord($char); 
    319350 
    320                 if (($dec == 32) AND ($i == ($linlen - 1))){    // convert space at eol only 
     351                if (($dec == 32) AND ($i == ($linlen - 1))) {    // convert space at eol only 
    321352                    $char = '=20'; 
    322353 
    323                 } elseif(($dec == 9) AND ($i == ($linlen - 1))) {  // convert tab at eol only 
     354                } elseif (($dec == 9) AND ($i == ($linlen - 1))) {  // convert tab at eol only 
    324355                    $char = '=09'; 
    325                 } elseif($dec == 9) { 
     356                } elseif ($dec == 9) { 
    326357                    ; // Do nothing if a tab. 
    327                 } elseif(($dec == 61) OR ($dec < 32 ) OR ($dec > 126)) { 
     358                } elseif (($dec == 61) OR ($dec < 32 ) OR ($dec > 126)) { 
    328359                    $char = $escape . strtoupper(sprintf('%02s', dechex($dec))); 
     360                } elseif (($dec == 46) AND (($newline == '') || ((strlen($newline) + strlen("=2E")) >= $line_max))) { 
     361                    //Bug #9722: convert full-stop at bol, 
     362                    //some Windows servers need this, won't break anything (cipri) 
     363                    //Bug #11731: full-stop at bol also needs to be encoded 
     364                    //if this line would push us over the line_max limit. 
     365                    $char = '=2E'; 
    329366                } 
    330367 
     368                //Note, when changing this line, also change the ($dec == 46) 
     369                //check line, as it mimics this line due to Bug #11731 
    331370                if ((strlen($newline) + strlen($char)) >= $line_max) {        // MAIL_MIMEPART_CRLF is not counted 
    332371                    $output  .= $newline . $escape . $eol;                    // soft line break; " =\r\n" is okay 
     
    340379        return $output; 
    341380    } 
     381 
     382    /** 
     383     * _buildHeaderParam() 
     384     * 
     385     * Encodes the paramater of a header. 
     386     * 
     387     * @param $name         The name of the header-parameter 
     388     * @param $value        The value of the paramter 
     389     * @param $charset      The characterset of $value 
     390     * @param $language     The language used in $value 
     391     * @param $maxLength    The maximum length of a line. Defauls to 75 
     392     * 
     393     * @access private 
     394     */ 
     395    function _buildHeaderParam($name, $value, $charset=NULL, $language=NULL, $maxLength=75) 
     396    { 
     397        //If we find chars to encode, or if charset or language 
     398        //is not any of the defaults, we need to encode the value. 
     399        $shouldEncode = 0; 
     400        $secondAsterisk = ''; 
     401        if (preg_match('#([\x80-\xFF]){1}#', $value)) { 
     402            $shouldEncode = 1; 
     403        } elseif ($charset && (strtolower($charset) != 'us-ascii')) { 
     404            $shouldEncode = 1; 
     405        } elseif ($language && ($language != 'en' && $language != 'en-us')) { 
     406            $shouldEncode = 1; 
     407        } 
     408        if ($shouldEncode) { 
     409            $search  = array('%',   ' ',   "\t"); 
     410            $replace = array('%25', '%20', '%09'); 
     411            $encValue = str_replace($search, $replace, $value); 
     412            $encValue = preg_replace('#([\x80-\xFF])#e', '"%" . strtoupper(dechex(ord("\1")))', $encValue); 
     413            $value = "$charset'$language'$encValue"; 
     414            $secondAsterisk = '*'; 
     415        } 
     416        $header = " {$name}{$secondAsterisk}=\"{$value}\"; "; 
     417        if (strlen($header) <= $maxLength) { 
     418            return $header; 
     419        } 
     420 
     421        $preLength = strlen(" {$name}*0{$secondAsterisk}=\""); 
     422        $sufLength = strlen("\";"); 
     423        $maxLength = MAX(16, $maxLength - $preLength - $sufLength - 2); 
     424        $maxLengthReg = "|(.{0,$maxLength}[^\%][^\%])|"; 
     425 
     426        $headers = array(); 
     427        $headCount = 0; 
     428        while ($value) { 
     429            $matches = array(); 
     430            $found = preg_match($maxLengthReg, $value, $matches); 
     431            if ($found) { 
     432                $headers[] = " {$name}*{$headCount}{$secondAsterisk}=\"{$matches[0]}\""; 
     433                $value = substr($value, strlen($matches[0])); 
     434            } else { 
     435                $headers[] = " {$name}*{$headCount}{$secondAsterisk}=\"{$value}\""; 
     436                $value = ""; 
     437            } 
     438            $headCount++; 
     439        } 
     440        $headers = implode(MAIL_MIMEPART_CRLF, $headers) . ';'; 
     441        return $headers; 
     442    } 
    342443} // End of class 
Note: See TracChangeset for help on using the changeset viewer.