Possibly the most accurate RFC 2045 QP implementation found in PHP.
Author: Chris Corbyn
Inheritance: implements Swift_Encoder
 public function testEncodingAndDecodingSamples()
 {
     $sampleFp = opendir($this->_samplesDir);
     while (false !== ($encodingDir = readdir($sampleFp))) {
         if (substr($encodingDir, 0, 1) == '.') {
             continue;
         }
         $encoding = $encodingDir;
         $charStream = new Swift_CharacterStream_ArrayCharacterStream($this->_factory, $encoding);
         $encoder = new Swift_Encoder_QpEncoder($charStream);
         $sampleDir = $this->_samplesDir . '/' . $encodingDir;
         if (is_dir($sampleDir)) {
             $fileFp = opendir($sampleDir);
             while (false !== ($sampleFile = readdir($fileFp))) {
                 if (substr($sampleFile, 0, 1) == '.') {
                     continue;
                 }
                 $text = file_get_contents($sampleDir . '/' . $sampleFile);
                 $encodedText = $encoder->encodeString($text);
                 foreach (explode("\r\n", $encodedText) as $line) {
                     $this->assertLessThanOrEqual(76, strlen($line));
                 }
                 $this->assertEquals(quoted_printable_decode($encodedText), $text, '%s: Encoded string should decode back to original string for sample ' . $sampleDir . '/' . $sampleFile);
             }
             closedir($fileFp);
         }
     }
     closedir($sampleFp);
 }
 protected function initSafeMap()
 {
     parent::initSafeMap();
     if ($this->_dotEscape) {
         /* Encode . as =2e for buggy remote servers */
         unset($this->_safeMap[0x2e]);
     }
 }
 /**
  * Creates a new QpContentEncoder for the given CharacterStream.
  * @param Swift_CharacterStream $charStream to use for reading characters
  * @param Swift_StreamFilter $filter if canonicalization should occur
  * @param boolean $dotEscape if dot stuffing workaround must be enabled
  */
 public function __construct(Swift_CharacterStream $charStream, Swift_StreamFilter $filter = null, $dotEscape = false)
 {
     parent::__construct($charStream, $filter);
     if ($dotEscape) {
         /* Encode . as =2e for buggy remote servers */
         unset($this->_safeMap[0x2e]);
     }
 }
Exemple #4
0
 public function testFirstLineLengthCanBeDifferent()
 {
     $input = str_repeat('a', 140);
     $charStream = $this->_createCharStream();
     $charStream->shouldReceive('flushContents')->once();
     $charStream->shouldReceive('importString')->once()->with($input);
     $output = '';
     for ($i = 0; $i < 140; ++$i) {
         $charStream->shouldReceive('readBytes')->once()->andReturn(array(ord('a')));
         if (53 == $i || 53 + 75 == $i) {
             $output .= "=\r\n";
         }
         $output .= 'a';
     }
     $charStream->shouldReceive('readBytes')->once()->andReturn(false);
     $encoder = new Swift_Encoder_QpEncoder($charStream);
     $this->assertEquals($output, $encoder->encodeString($input, 22), '%s: First line should start at offset 22 so can only have max length 54');
 }
 /**
  * Creates a new QpContentEncoder for the given CharacterStream.
  * @param Swift_CharacterStream $charStream to use for reading characters
  * @param Swift_StreamFilter $filter if canonicalization should occur
  */
 public function __construct(Swift_CharacterStream $charStream, Swift_StreamFilter $filter = null)
 {
     parent::__construct($charStream, $filter);
 }
 /**
  * Takes an unencoded string and produces a QP encoded string from it.
  *
  * @param string $string          string to encode
  * @param int    $firstLineOffset optional
  * @param int    $maxLineLength   optional, 0 indicates the default of 76 chars
  *
  * @return string
  */
 public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0)
 {
     return str_replace(array(' ', '=20', "=\r\n"), array('_', '_', "\r\n"), parent::encodeString($string, $firstLineOffset, $maxLineLength));
 }
 public function testFirstLineLengthCanBeDifferent()
 {
     $input = str_repeat('a', 140);
     $charStream = $this->_createCharStream();
     $seq = $this->_mockery()->sequence('byte-sequence');
     $exps = Expectations::create();
     $exps->one($charStream)->flushContents();
     $exps->one($charStream)->importString($input);
     $output = '';
     for ($i = 0; $i < 140; ++$i) {
         $exps->one($charStream)->readBytes(optional())->inSequence($seq)->returns(array(ord('a')));
         if (53 == $i || 53 + 75 == $i) {
             $output .= "=\r\n";
         }
         $output .= 'a';
     }
     $exps->one($charStream)->readBytes(optional())->inSequence($seq)->returns(false);
     $this->_checking($exps);
     $encoder = new Swift_Encoder_QpEncoder($charStream);
     $this->assertEqual($output, $encoder->encodeString($input, 22), '%s: First line should start at offset 22 so can only have max length 54');
 }