QpHeaderEncoder.php 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/QpEncoder.php';
  11. require_once dirname(__FILE__) . '/../../CharacterStream.php';
  12. /**
  13. * Handles Quoted Printable (Q) Header Encoding in Swift Mailer.
  14. * @package Swift
  15. * @subpackage Mime
  16. * @author Chris Corbyn
  17. */
  18. class Swift_Mime_HeaderEncoder_QpHeaderEncoder extends Swift_Encoder_QpEncoder
  19. implements Swift_Mime_HeaderEncoder
  20. {
  21. /**
  22. * Creates a new QpHeaderEncoder for the given CharacterStream.
  23. * @param Swift_CharacterStream $charStream to use for reading characters
  24. */
  25. public function __construct(Swift_CharacterStream $charStream)
  26. {
  27. parent::__construct($charStream);
  28. }
  29. protected function initSafeMap()
  30. {
  31. foreach (array_merge(
  32. range(0x61, 0x7A), range(0x41, 0x5A),
  33. range(0x30, 0x39), array(0x20, 0x21, 0x2A, 0x2B, 0x2D, 0x2F)
  34. ) as $byte)
  35. {
  36. $this->_safeMap[$byte] = chr($byte);
  37. }
  38. }
  39. /**
  40. * Get the name of this encoding scheme.
  41. * Returns the string 'Q'.
  42. * @return string
  43. */
  44. public function getName()
  45. {
  46. return 'Q';
  47. }
  48. /**
  49. * Takes an unencoded string and produces a Q encoded string from it.
  50. * @param string $string to encode
  51. * @param int $firstLineOffset, optional
  52. * @param int $maxLineLength, optional, 0 indicates the default of 76 chars
  53. * @return string
  54. */
  55. public function encodeString($string, $firstLineOffset = 0,
  56. $maxLineLength = 0)
  57. {
  58. return str_replace(array(' ', '=20', "=\r\n"), array('_', '_', "\r\n"),
  59. parent::encodeString($string, $firstLineOffset, $maxLineLength)
  60. );
  61. }
  62. }