QpHeaderEncoder.php 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. // Reset the safeMap
  29. $this->_safeMap=array();
  30. foreach (array_merge(
  31. range(0x61, 0x7A), range(0x41, 0x5A),
  32. range(0x30, 0x39), array(0x20, 0x21, 0x2A, 0x2B, 0x2D, 0x2F)
  33. ) as $byte)
  34. {
  35. $this->_safeMap[$byte] = chr($byte);
  36. }
  37. }
  38. /**
  39. * Get the name of this encoding scheme.
  40. * Returns the string 'Q'.
  41. * @return string
  42. */
  43. public function getName()
  44. {
  45. return 'Q';
  46. }
  47. /**
  48. * Takes an unencoded string and produces a Q encoded string from it.
  49. * @param string $string to encode
  50. * @param int $firstLineOffset, optional
  51. * @param int $maxLineLength, optional, 0 indicates the default of 76 chars
  52. * @return string
  53. */
  54. public function encodeString($string, $firstLineOffset = 0,
  55. $maxLineLength = 0)
  56. {
  57. return str_replace(array(' ', '=20', "=\r\n"), array('_', '_', "\r\n"),
  58. parent::encodeString($string, $firstLineOffset, $maxLineLength)
  59. );
  60. }
  61. }