Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
Replace EncodeQ function by the latest phpmailer version. We should p…
Browse files Browse the repository at this point in the history
…robably totally switch to this mailer.

Optionally pass images to be attached in the layout.
  • Loading branch information
cdujeu committed Dec 18, 2013
1 parent cf902ac commit 7e98f19
Showing 1 changed file with 44 additions and 46 deletions.
90 changes: 44 additions & 46 deletions core/src/plugins/mailer.phpmailer-lite/lib/class.phpmailer-lite.php
Expand Up @@ -1511,56 +1511,54 @@ public function EncodeQP($string, $line_max = 76, $space_conv = false)
return $out;
}

/**
* Encode string to q encoding.
* @link http://tools.ietf.org/html/rfc2047
* @param string $str the text to encode
* @param string $position Where the text is going to be used, see the RFC for what that means
* @access public
* @return string
*/
public function EncodeQ ($str, $position = 'text')
{
// There should not be any EOL in the string
$encoded = preg_replace('/[\r\n]*/', '', $str);

switch (strtolower($position)) {
case 'phrase':
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
$encoded = preg_replace_callback("/([^A-Za-z0-9!*+\/ -])/", function($matches){
return sprintf('%02X', ord($matches[1]));
}, $encoded);
} else {
$encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
/**
* Encode a string using Q encoding.
* @link http://tools.ietf.org/html/rfc2047
* @param string $str the text to encode
* @param string $position Where the text is going to be used, see the RFC for what that means
* @access public
* @return string
*/
public function encodeQ($str, $position = 'text')
{
//There should not be any EOL in the string
$pattern = '';
$encoded = str_replace(array("\r", "\n"), '', $str);
switch (strtolower($position)) {
case 'phrase':
//RFC 2047 section 5.3
$pattern = '^A-Za-z0-9!*+\/ -';
break;
/** @noinspection PhpMissingBreakStatementInspection */
case 'comment':
//RFC 2047 section 5.2
$pattern = '\(\)"';
//intentional fall-through
//for this reason we build the $pattern without including delimiters and []
case 'text':
default:
//RFC 2047 section 5.1
//Replace every high ascii, control, =, ? and _ characters
$pattern = '\000-\011\013\014\016-\037\075\077\137\177-\377' . $pattern;
break;
}
break;
case 'comment':
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
$encoded = preg_replace_callback("/([\(\)\"])/", function($matches){
return sprintf('%02X', ord($matches[1]));
}, $encoded);
} else {
$encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
}
case 'text':
default:
// Replace every high ascii, control =, ? and _ characters
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
$encoded = preg_replace_callback('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/', function($matches){
return sprintf('%02X', ord($matches[1]));
}, $encoded);
} else {
$encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
"'='.sprintf('%02X', ord('\\1'))", $encoded);
$matches = array();
if (preg_match_all("/[{$pattern}]/", $encoded, $matches)) {
//If the string contains an '=', make sure it's the first thing we replace
//so as to avoid double-encoding
$s = array_search('=', $matches[0]);
if ($s !== false) {
unset($matches[0][$s]);
array_unshift($matches[0], '=');
}
foreach (array_unique($matches[0]) as $char) {
$encoded = str_replace($char, '=' . sprintf('%02X', ord($char)), $encoded);
}
}
break;
//Replace every spaces to _ (more readable than =20)
return str_replace(' ', '_', $encoded);
}

// Replace every spaces to _ (more readable than =20)
$encoded = str_replace(' ', '_', $encoded);

return $encoded;
}

/**
* Adds a string or binary attachment (non-filesystem) to the list.
Expand Down

0 comments on commit 7e98f19

Please sign in to comment.