You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.3 KiB

3 years ago
  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 Chris Corbyn
  13. */
  14. final class Base64MimeHeaderEncoder extends Base64Encoder implements MimeHeaderEncoderInterface
  15. {
  16. public function getName(): string
  17. {
  18. return 'B';
  19. }
  20. /**
  21. * Takes an unencoded string and produces a Base64 encoded string from it.
  22. *
  23. * If the charset is iso-2022-jp, it uses mb_encode_mimeheader instead of
  24. * default encodeString, otherwise pass to the parent method.
  25. */
  26. public function encodeString(string $string, ?string $charset = 'utf-8', int $firstLineOffset = 0, int $maxLineLength = 0): string
  27. {
  28. if ('iso-2022-jp' === strtolower($charset)) {
  29. $old = mb_internal_encoding();
  30. mb_internal_encoding('utf-8');
  31. $newstring = mb_encode_mimeheader($string, 'iso-2022-jp', $this->getName(), "\r\n");
  32. mb_internal_encoding($old);
  33. return $newstring;
  34. }
  35. return parent::encodeString($string, $charset, $firstLineOffset, $maxLineLength);
  36. }
  37. }