NativeQpContentEncoder.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /**
  10. * Handles Quoted Printable (QP) Transfer Encoding in Swift Mailer using the PHP core function.
  11. *
  12. * @author Lars Strojny
  13. */
  14. class Swift_Mime_ContentEncoder_NativeQpContentEncoder implements Swift_Mime_ContentEncoder
  15. {
  16. /**
  17. * @var null|string
  18. */
  19. private $charset;
  20. /**
  21. * @param null|string $charset
  22. */
  23. public function __construct($charset = null)
  24. {
  25. $this->charset = $charset ? $charset : 'utf-8';
  26. }
  27. /**
  28. * Notify this observer that the entity's charset has changed.
  29. *
  30. * @param string $charset
  31. */
  32. public function charsetChanged($charset)
  33. {
  34. $this->charset = $charset;
  35. }
  36. /**
  37. * Encode $in to $out.
  38. *
  39. * @param Swift_OutputByteStream $os to read from
  40. * @param Swift_InputByteStream $is to write to
  41. * @param int $firstLineOffset
  42. * @param int $maxLineLength 0 indicates the default length for this encoding
  43. *
  44. * @throws RuntimeException
  45. */
  46. public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
  47. {
  48. if ($this->charset !== 'utf-8') {
  49. throw new RuntimeException(
  50. sprintf('Charset "%s" not supported. NativeQpContentEncoder only supports "utf-8"', $this->charset));
  51. }
  52. $string = '';
  53. while (false !== $bytes = $os->read(8192)) {
  54. $string .= $bytes;
  55. }
  56. $is->write($this->encodeString($string));
  57. }
  58. /**
  59. * Get the MIME name of this content encoding scheme.
  60. *
  61. * @return string
  62. */
  63. public function getName()
  64. {
  65. return 'quoted-printable';
  66. }
  67. /**
  68. * Encode a given string to produce an encoded string.
  69. *
  70. * @param string $string
  71. * @param int $firstLineOffset if first line needs to be shorter
  72. * @param int $maxLineLength 0 indicates the default length for this encoding
  73. *
  74. * @return string
  75. *
  76. * @throws RuntimeException
  77. */
  78. public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0)
  79. {
  80. if ($this->charset !== 'utf-8') {
  81. throw new RuntimeException(
  82. sprintf('Charset "%s" not supported. NativeQpContentEncoder only supports "utf-8"', $this->charset));
  83. }
  84. return $this->_standardize(quoted_printable_encode($string));
  85. }
  86. /**
  87. * Make sure CRLF is correct and HT/SPACE are in valid places.
  88. *
  89. * @param string $string
  90. *
  91. * @return string
  92. */
  93. protected function _standardize($string)
  94. {
  95. // transform CR or LF to CRLF
  96. $string = preg_replace('~=0D(?!=0A)|(?<!=0D)=0A~', '=0D=0A', $string);
  97. // transform =0D=0A to CRLF
  98. $string = str_replace(array("\t=0D=0A", " =0D=0A", "=0D=0A"), array("=09\r\n", "=20\r\n", "\r\n"), $string);
  99. switch ($end = ord(substr($string, -1))) {
  100. case 0x09:
  101. $string = substr_replace($string, '=09', -1);
  102. break;
  103. case 0x20:
  104. $string = substr_replace($string, '=20', -1);
  105. break;
  106. }
  107. return $string;
  108. }
  109. }