Base64HeaderEncoder.php 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. require_once dirname(__FILE__) . '/../HeaderEncoder.php';
  10. require_once dirname(__FILE__) . '/../../Encoder/Base64Encoder.php';
  11. /**
  12. * Handles Base64 (B) Header Encoding in Swift Mailer.
  13. * @package Swift
  14. * @subpackage Mime
  15. * @author Chris Corbyn
  16. */
  17. class Swift_Mime_HeaderEncoder_Base64HeaderEncoder extends Swift_Encoder_Base64Encoder implements Swift_Mime_HeaderEncoder
  18. {
  19. /**
  20. * Get the name of this encoding scheme.
  21. * Returns the string 'B'.
  22. * @return string
  23. */
  24. public function getName()
  25. {
  26. return 'B';
  27. }
  28. /**
  29. * Takes an unencoded string and produces a Base64 encoded string from it.
  30. * If the charset is iso-2022-jp, it uses mb_encode_mimeheader instead of
  31. * default encodeString, otherwise pass to the parent method.
  32. * @param string $string to encode
  33. * @param int $firstLineOffset
  34. * @param int $maxLineLength, optional, 0 indicates the default of 76 bytes
  35. * @param string $charset
  36. * @return string
  37. */
  38. public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0, $charset = 'utf-8')
  39. {
  40. if (strtolower($charset) === 'iso-2022-jp') {
  41. $old = mb_internal_encoding();
  42. mb_internal_encoding('utf-8');
  43. $newstring = mb_encode_mimeheader($string, $charset, $this->getName(), "\r\n");
  44. mb_internal_encoding($old);
  45. return $newstring;
  46. }
  47. return parent::encodeString($string, $firstLineOffset, $maxLineLength);
  48. }
  49. }