dolibarr/htdocs/includes/swiftmailer/lib/classes/Swift/Mime/ContentEncoder/QpContentEncoder.php

135 lines
4.1 KiB
PHP
Raw Permalink Normal View History

2016-04-16 16:15:03 +00:00
<?php
/*
* This file is part of SwiftMailer.
* (c) 2004-2009 Chris Corbyn
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Handles Quoted Printable (QP) Transfer Encoding in Swift Mailer.
*
2018-01-21 14:55:56 +00:00
* @author Chris Corbyn
2016-04-16 16:15:03 +00:00
*/
class Swift_Mime_ContentEncoder_QpContentEncoder extends Swift_Encoder_QpEncoder implements Swift_Mime_ContentEncoder
{
2018-01-21 14:55:56 +00:00
protected $dotEscape;
2016-04-16 16:15:03 +00:00
/**
* 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 bool $dotEscape if dot stuffing workaround must be enabled
*/
public function __construct(Swift_CharacterStream $charStream, Swift_StreamFilter $filter = null, $dotEscape = false)
{
2018-01-21 14:55:56 +00:00
$this->dotEscape = $dotEscape;
2016-04-16 16:15:03 +00:00
parent::__construct($charStream, $filter);
}
public function __sleep()
{
2021-12-07 16:11:34 +00:00
return ['charStream', 'filter', 'dotEscape'];
2016-04-16 16:15:03 +00:00
}
protected function getSafeMapShareId()
{
2021-12-07 16:11:34 +00:00
return static::class.($this->dotEscape ? '.dotEscape' : '');
2016-04-16 16:15:03 +00:00
}
protected function initSafeMap()
{
parent::initSafeMap();
2018-01-21 14:55:56 +00:00
if ($this->dotEscape) {
2016-04-16 16:15:03 +00:00
/* Encode . as =2e for buggy remote servers */
2018-01-21 14:55:56 +00:00
unset($this->safeMap[0x2e]);
2016-04-16 16:15:03 +00:00
}
}
/**
* Encode stream $in to stream $out.
*
* QP encoded strings have a maximum line length of 76 characters.
* If the first line needs to be shorter, indicate the difference with
* $firstLineOffset.
*
* @param Swift_OutputByteStream $os output stream
* @param Swift_InputByteStream $is input stream
* @param int $firstLineOffset
* @param int $maxLineLength
*/
public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
{
if ($maxLineLength > 76 || $maxLineLength <= 0) {
$maxLineLength = 76;
}
$thisLineLength = $maxLineLength - $firstLineOffset;
2018-01-21 14:55:56 +00:00
$this->charStream->flushContents();
$this->charStream->importByteStream($os);
2016-04-16 16:15:03 +00:00
$currentLine = '';
$prepend = '';
$size = $lineLen = 0;
2018-01-21 14:55:56 +00:00
while (false !== $bytes = $this->nextSequence()) {
2016-04-16 16:15:03 +00:00
// If we're filtering the input
2018-01-21 14:55:56 +00:00
if (isset($this->filter)) {
2016-04-16 16:15:03 +00:00
// If we can't filter because we need more bytes
2018-01-21 14:55:56 +00:00
while ($this->filter->shouldBuffer($bytes)) {
2016-04-16 16:15:03 +00:00
// Then collect bytes into the buffer
2018-01-21 14:55:56 +00:00
if (false === $moreBytes = $this->nextSequence(1)) {
2016-04-16 16:15:03 +00:00
break;
}
foreach ($moreBytes as $b) {
$bytes[] = $b;
}
}
// And filter them
2018-01-21 14:55:56 +00:00
$bytes = $this->filter->filter($bytes);
2016-04-16 16:15:03 +00:00
}
2018-01-21 14:55:56 +00:00
$enc = $this->encodeByteSequence($bytes, $size);
2016-04-16 16:15:03 +00:00
$i = strpos($enc, '=0D=0A');
2021-12-07 16:11:34 +00:00
$newLineLength = $lineLen + (false === $i ? $size : $i);
2016-04-16 16:15:03 +00:00
if ($currentLine && $newLineLength >= $thisLineLength) {
2018-01-21 14:55:56 +00:00
$is->write($prepend.$this->standardize($currentLine));
2016-04-16 16:15:03 +00:00
$currentLine = '';
$prepend = "=\r\n";
$thisLineLength = $maxLineLength;
$lineLen = 0;
}
$currentLine .= $enc;
2021-12-07 16:11:34 +00:00
if (false === $i) {
2016-04-16 16:15:03 +00:00
$lineLen += $size;
} else {
// 6 is the length of '=0D=0A'.
$lineLen = $size - strrpos($enc, '=0D=0A') - 6;
}
}
2021-12-07 16:11:34 +00:00
if (\strlen($currentLine)) {
2018-01-21 14:55:56 +00:00
$is->write($prepend.$this->standardize($currentLine));
2016-04-16 16:15:03 +00:00
}
}
/**
* Get the name of this encoding scheme.
* Returns the string 'quoted-printable'.
*
* @return string
*/
public function getName()
{
return 'quoted-printable';
}
}