Dashboard sipadu mbip
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

QpContentEncoder.php 1.9KB

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