/framework/vendor/swift/lib/classes/Swift/Mime/HeaderEncoder/QpHeaderEncoder.php

http://zoop.googlecode.com/ · PHP · 99 lines · 53 code · 10 blank · 36 comment · 2 complexity · 3c9cfcbc31c62c7ed981e960ab02d288 MD5 · raw file

  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. private static $_headerSafeMap = array();
  22. /**
  23. * Creates a new QpHeaderEncoder for the given CharacterStream.
  24. * @param Swift_CharacterStream $charStream to use for reading characters
  25. */
  26. public function __construct(Swift_CharacterStream $charStream)
  27. {
  28. parent::__construct($charStream);
  29. if (empty(self::$_headerSafeMap))
  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. self::$_headerSafeMap[$byte] = chr($byte);
  37. }
  38. }
  39. }
  40. /**
  41. * Get the name of this encoding scheme.
  42. * Returns the string 'Q'.
  43. * @return string
  44. */
  45. public function getName()
  46. {
  47. return 'Q';
  48. }
  49. /**
  50. * Takes an unencoded string and produces a Q encoded string from it.
  51. * @param string $string to encode
  52. * @param int $firstLineOffset, optional
  53. * @param int $maxLineLength, optional, 0 indicates the default of 76 chars
  54. * @return string
  55. */
  56. public function encodeString($string, $firstLineOffset = 0,
  57. $maxLineLength = 0)
  58. {
  59. return str_replace(array(' ', '=20', "=\r\n"), array('_', '_', "\r\n"),
  60. parent::encodeString($string, $firstLineOffset, $maxLineLength)
  61. );
  62. }
  63. // -- Overridden points of extension
  64. /**
  65. * Encode the given byte array into a verbatim QP form.
  66. * @param int[] $bytes
  67. * @return string
  68. * @access protected
  69. */
  70. protected function _encodeByteSequence(array $bytes, &$size)
  71. {
  72. $ret = '';
  73. $size=0;
  74. foreach ($bytes as $b)
  75. {
  76. if (isset(self::$_headerSafeMap[$b]))
  77. {
  78. $ret .= self::$_headerSafeMap[$b];
  79. ++$size;
  80. }
  81. else
  82. {
  83. $ret .= self::$_qpMap[$b];
  84. $size+=3;
  85. }
  86. }
  87. return $ret;
  88. }
  89. }