Open Journal Systems  3.3.0
QpContentEncoder.php
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 
18 {
19  public function encodeByteStream($stream, int $maxLineLength = 0): iterable
20  {
21  if (!\is_resource($stream)) {
22  throw new \TypeError(sprintf('Method "%s" takes a stream as a first argument.', __METHOD__));
23  }
24 
25  // we don't use PHP stream filters here as the content should be small enough
26  if (stream_get_meta_data($stream)['seekable'] ?? false) {
27  rewind($stream);
28  }
29 
30  yield $this->encodeString(stream_get_contents($stream), 'utf-8', 0, $maxLineLength);
31  }
32 
33  public function getName(): string
34  {
35  return 'quoted-printable';
36  }
37 
38  public function encodeString(string $string, ?string $charset = 'utf-8', int $firstLineOffset = 0, int $maxLineLength = 0): string
39  {
40  return $this->standardize(quoted_printable_encode($string));
41  }
42 
46  private function standardize(string $string): string
47  {
48  // transform CR or LF to CRLF
49  $string = preg_replace('~=0D(?!=0A)|(?<!=0D)=0A~', '=0D=0A', $string);
50  // transform =0D=0A to CRLF
51  $string = str_replace(["\t=0D=0A", ' =0D=0A', '=0D=0A'], ["=09\r\n", "=20\r\n", "\r\n"], $string);
52 
53  switch (\ord(substr($string, -1))) {
54  case 0x09:
55  $string = substr_replace($string, '=09', -1);
56  break;
57  case 0x20:
58  $string = substr_replace($string, '=20', -1);
59  break;
60  }
61 
62  return $string;
63  }
64 }
Symfony\Component\Mime\Encoder\QpContentEncoder\getName
getName()
Definition: QpContentEncoder.php:33
Symfony\Component\Mime\Encoder\QpContentEncoder\encodeString
encodeString(string $string, ?string $charset='utf-8', int $firstLineOffset=0, int $maxLineLength=0)
Definition: QpContentEncoder.php:38
Symfony\Component\Mime\Encoder
Definition: AddressEncoderInterface.php:12
Symfony\Component\Mime\Encoder\QpContentEncoder
Definition: QpContentEncoder.php:17
Symfony\Component\Mime\Encoder\QpContentEncoder\encodeByteStream
encodeByteStream($stream, int $maxLineLength=0)
Definition: QpContentEncoder.php:19
Symfony\Component\Mime\Encoder\ContentEncoderInterface
Definition: ContentEncoderInterface.php:17