You are here

public function PHPMailer::EncodeQ in SMTP Authentication Support 7

Same name and namespace in other branches
  1. 5 smtp.module \PHPMailer::EncodeQ()
  2. 7.2 smtp.phpmailer.inc \PHPMailer::EncodeQ()

Encode string to q encoding. @link http://tools.ietf.org/html/rfc2047

@access public

Parameters

string $str the text to encode:

string $position Where the text is going to be used, see the RFC for what that means:

Return value

string

1 call to PHPMailer::EncodeQ()
PHPMailer::EncodeHeader in ./smtp.phpmailer.inc
Encode a header string to best (shortest) of Q, B, quoted or none. @access public

File

./smtp.phpmailer.inc, line 1727
The mail handler class in smtp module, based on code of the phpmailer library, customized and relicensed to GPLv2.

Class

PHPMailer
PHPMailer - PHP email transport class NOTE: Requires PHP version 5 or later @package PHPMailer @author Andy Prevost @author Marcus Bointon @copyright 2004 - 2009 Andy Prevost

Code

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;
  }
  $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);
    }
  }

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