cvs: pear /Mail_Mime mime.php

From: Date: Sun, 22 Apr 2007 00:06:39 +0000
Subject: cvs: pear /Mail_Mime mime.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-46082@lists.php.net to get a copy of this message
cipri Sun Apr 22 00:06:39 2007 UTC Modified files: /pear/Mail_Mime mime.php Log: Fixed problem with base64 encoding. Refactored _encodeHeader() code to improve stability, readability and speed.

http://cvs.php.net/viewvc.cgi/pear/Mail_Mime/mime.php?r1=1.68&r2=1.69&diff_format=u Index: pear/Mail_Mime/mime.php diff -u pear/Mail_Mime/mime.php:1.68 pear/Mail_Mime/mime.php:1.69 --- pear/Mail_Mime/mime.php:1.68 Sat Apr 21 18:09:35 2007 +++ pear/Mail_Mime/mime.php Sun Apr 22 00:06:39 2007 @@ -47,7 +47,7 @@ * @author Sean Coates <sean@php.net> * @copyright 2003-2006 PEAR <pear-group@php.net> * @license http://www.opensource.org/licenses/bsd-license.php BSD License - * @version CVS: $Id: mime.php,v 1.68 2007/04/21 18:09:35 cipri Exp $ + * @version CVS: $Id: mime.php,v 1.69 2007/04/22 00:06:39 cipri Exp $ * @link http://pear.php.net/package/Mail_mime * @notes This class is based on HTML Mime Mail class from * Richard Heyes <richard@phpguru.org> which was based also @@ -859,11 +859,10 @@ } //$hdr_name: Name of the heaer //$hdr_value: Full line of header value. - //$hdr_val: Atom part of $hdr_value //$hdr_value_out: The recombined $hdr_val-atoms, or the encoded string. foreach ($input as $hdr_name => $hdr_value) { if (preg_match('#([\x80-\xFF]){1}#', $hdr_value)){ - if (function_exists('iconv_mime_encode')){ + if (function_exists('iconv_mime_encode') && (!isset($build_params['ignore-iconv']) || !$build_params['ignore-iconv'])){ $imePrefs = array(); if ($build_params['head_encoding'] == 'base64'){ $imePrefs['scheme'] = 'B'; @@ -877,109 +876,99 @@ $hdr_value = iconv_mime_encode($hdr_name, $hdr_value, $imePrefs); $hdr_value = preg_replace("#^{$hdr_name}\:\ #", "", $hdr_value); - }else{ - $hdr_vals = preg_split("|(\s)|", $hdr_value, -1, PREG_SPLIT_DELIM_CAPTURE); - $hdr_value_out=""; - $previous = ""; - foreach ($hdr_vals as $hdr_val){ - $encodedLineNeedsSplitting = 0; - if (!trim($hdr_val)){ - //whitespace needs to be handled with another string, or it - //won't show between encoded strings. Prepend this to the next item. - $previous .= $hdr_val; - continue; - }else{ - $hdr_val = $previous . $hdr_val; - $previous = ""; - } - //Fix for Bug #10298, Ota Mares <om@viazenetti.de> - //Check if there is a double quote at beginning or end of the string to - //prevent that an open or closing quote gets ignored because its encapsuled - //by an encoding prefix or suffix. - - //Remove the double quote and set the specific prefix or suffix variable - //so later we can concat the encoded string and the double quotes back - //together to get the intended string. - $quotePrefix = $quoteSuffix = ''; - if ($hdr_val{0} == '"') { - $hdr_val = substr($hdr_val, 1); - $quotePrefix = '"'; - } - if ($hdr_val{strlen($hdr_val)-1} == '"') { - $hdr_val = substr($hdr_val, 0, -1); - $quoteSuffix = '"'; - } - switch ($build_params['head_encoding']) { - case 'base64': - //Base64 encoding has been selected. - - //Generate the header using the specified params and dynamicly - //determine the maximum length of such strings. - //75 is the value specified in the RFC. The first -2 is there so - //the later regexp doesn't break any of the translated chars. - //The -2 on the first line-regexp is to compensate for the ": " - //between the header-name and the header value - //The -1 finally is to compensate for 0/1 based strings - $prefix = '=?' . $build_params['head_charset'] . '?B?'; - $suffix = '?='; - $maxLength = 75 - strlen($prefix . $suffix) - 2 - 1; - $maxLength1stLine = $maxLength - strlen($hdr_name) - 2; - - //Base64 encode the entire string - $hdr_val = base64_encode($hdr_val); - - //This regexp will break base64-encoded text at every - //$maxLength but will not break any encoded letters. - $reg1st = "|.{0,$maxLength1stLine}[^\=][^\=]|"; - $reg2nd = "|.{0,$maxLength}[^\=][^\=]|"; - break; - case 'quoted-printable': - default: - //quoted-printable encoding has been selected - - //Generate the header using the specified params and dynamicly - //determine the maximum length of such strings. - //75 is the value specified in the RFC. The -2 is there so - //the later regexp doesn't break any of the translated chars. - //The -2 on the first line-regexp is to compensate for the ": " - //between the header-name and the header value - //The -1 finally is to compensate for 0/1 based strings - $prefix = '=?' . $build_params['head_charset'] . '?Q?'; - $suffix = '?='; - $maxLength = 75 - strlen($prefix . $suffix) - 2 - 1; - $maxLength1stLine = $maxLength - strlen($hdr_name) - 2; - - //Replace all special characters used by the encoder. - $search = array('=', '_', '?', ' '); - $replace = array('=3D', '=5F', '=3F', '_'); - $hdr_val = str_replace($search, $replace, $hdr_val); - - //Replace all extended characters (\x80-xFF) with their - //ASCII values. - $hdr_val = preg_replace( - '#([\x80-\xFF])#e', - '"=" . strtoupper(dechex(ord("\1")))', - $hdr_val - ); - //This regexp will break QP-encoded text at every $maxLength - //but will not break any encoded letters. - $reg1st = "|(.{0,$maxLength1stLine})[^\=]|"; - $reg2nd = "|(.{0,$maxLength})[^\=]|"; - break; - } - //Fix for Bug #10298, Ota Mares <om@viazenetti.de> - //Concat the double quotes if existant and encoded string together - $hdr_val = $quotePrefix . $hdr_val . $quoteSuffix; - //Append $hdr_val to $hdr_value_out, the final result. - $hdr_value_out .= $hdr_val; - } - if (strlen($hdr_value_out) >= $maxLength1stLine){ + }elseif ($build_params['head_encoding'] == 'base64'){ + //Base64 encoding has been selected. + //Base64 encode the entire string + $hdr_value = base64_encode($hdr_value); + + //Generate the header using the specified params and dynamicly + //determine the maximum length of such strings. + //75 is the value specified in the RFC. The first -2 is there so + //the later regexp doesn't break any of the translated chars. + //The -2 on the first line-regexp is to compensate for the ": " + //between the header-name and the header value + $prefix = '=?' . $build_params['head_charset'] . '?B?'; + $suffix = '?='; + $maxLength = 75 - strlen($prefix . $suffix) - 2; + $maxLength1stLine = $maxLength - strlen($hdr_name) - 2; + + //We can cut base4 every 4 characters, so the real max we can get must be rounded down. + $maxLength = $maxLength - ($maxLength % 4); + $maxLength1stLine = $maxLength1stLine - ($maxLength1stLine % 4); + + $cutpoint = $maxLength1stLine; + $hdr_value_out = $hdr_value; + $output = ""; + while ($hdr_value_out) { + //Split translated string at every $maxLength + $part = substr($hdr_value_out, 0, $cutpoint); + $hdr_value_out = substr($hdr_value_out, $cutpoint); + $cutpoint = $maxLength; + //RFC 2047 specifies that any split header should be seperated + //by a CRLF SPACE. + if ($output){ + $output .= "\r\n "; + } + $output .= $prefix . $part . $suffix; + } + $hdr_value = $output; + }else{ + //quoted-printable encoding has been selected + + //Fix for Bug #10298, Ota Mares <om@viazenetti.de> + //Check if there is a double quote at beginning or end of the string to + //prevent that an open or closing quote gets ignored because its encapsuled + //by an encoding prefix or suffix. + //Remove the double quote and set the specific prefix or suffix variable + //so later we can concat the encoded string and the double quotes back + //together to get the intended string. + $quotePrefix = $quoteSuffix = ''; + if ($hdr_value{0} == '"') { + $hdr_value = substr($hdr_value, 1); + $quotePrefix = '"'; + } + if ($hdr_value{strlen($hdr_value)-1} == '"') { + $hdr_value = substr($hdr_value, 0, -1); + $quoteSuffix = '"'; + } + + //Generate the header using the specified params and dynamicly + //determine the maximum length of such strings. + //75 is the value specified in the RFC. The -2 is there so + //the later regexp doesn't break any of the translated chars. + //The -2 on the first line-regexp is to compensate for the ": " + //between the header-name and the header value + $prefix = '=?' . $build_params['head_charset'] . '?Q?'; + $suffix = '?='; + $maxLength = 75 - strlen($prefix . $suffix) - 2 - 1; + $maxLength1stLine = $maxLength - strlen($hdr_name) - 2; + $maxLength = $maxLength - 1; + + //Replace all special characters used by the encoder. + $search = array('=', '_', '?', ' '); + $replace = array('=3D', '=5F', '=3F', '_'); + $hdr_value = str_replace($search, $replace, $hdr_value); + + //Replace all extended characters (\x80-xFF) with their + //ASCII values. + $hdr_value = preg_replace( + '#([\x80-\xFF])#e', + '"=" . strtoupper(dechex(ord("\1")))', + $hdr_value + ); + //This regexp will break QP-encoded text at every $maxLength + //but will not break any encoded letters. + $reg1st = "|(.{0,$maxLength1stLine}[^\=][^\=])|"; + $reg2nd = "|(.{0,$maxLength}[^\=][^\=])|"; + //Fix for Bug #10298, Ota Mares <om@viazenetti.de> + //Concat the double quotes if existant and encoded string together + $hdr_value = $quotePrefix . $hdr_value . $quoteSuffix; + + + $hdr_value_out = $hdr_value; + if (strlen($hdr_value_out) >= ($maxLength1stLine + strlen($prefix . $suffix))){ //Begin with the regexp for the first line. $reg = $reg1st; - //Prevent lins that are just way to short; - if ($maxLength1stLine >1){ - $reg = $reg2nd; - } $output = ""; while ($hdr_value_out) { //Split translated string at every $maxLength @@ -1009,7 +998,9 @@ $output .= $prefix . $part . $suffix; } $hdr_value_out = $output; - } + }else{ + $hdr_value_out = $prefix . $hdr_value_out . $suffix; + } $hdr_value = $hdr_value_out; } }
« previous php.pear.cvs (#46082) next »