NativeQpContentEncoder.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 string|null
  18. */
  19. private $charset;
  20. /**
  21. * @param string|null $charset
  22. */
  23. public function __construct($charset = null)
  24. {
  25. $this->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 ('utf-8' !== $this->charset) {
  49. throw new RuntimeException(sprintf('Charset "%s" not supported. NativeQpContentEncoder only supports "utf-8"', $this->charset));
  50. }
  51. $string = '';
  52. while (false !== $bytes = $os->read(8192)) {
  53. $string .= $bytes;
  54. }
  55. $is->write($this->encodeString($string));
  56. }
  57. /**
  58. * Get the MIME name of this content encoding scheme.
  59. *
  60. * @return string
  61. */
  62. public function getName()
  63. {
  64. return 'quoted-printable';
  65. }
  66. /**
  67. * Encode a given string to produce an encoded string.
  68. *
  69. * @param string $string
  70. * @param int $firstLineOffset if first line needs to be shorter
  71. * @param int $maxLineLength 0 indicates the default length for this encoding
  72. *
  73. * @throws RuntimeException
  74. *
  75. * @return string
  76. */
  77. public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0)
  78. {
  79. if ('utf-8' !== $this->charset) {
  80. throw new RuntimeException(sprintf('Charset "%s" not supported. NativeQpContentEncoder only supports "utf-8"', $this->charset));
  81. }
  82. return $this->standardize(quoted_printable_encode($string));
  83. }
  84. /**
  85. * Make sure CRLF is correct and HT/SPACE are in valid places.
  86. *
  87. * @param string $string
  88. *
  89. * @return string
  90. */
  91. protected function standardize($string)
  92. {
  93. // transform CR or LF to CRLF
  94. $string = preg_replace('~=0D(?!=0A)|(?<!=0D)=0A~', '=0D=0A', $string);
  95. // transform =0D=0A to CRLF
  96. $string = str_replace(["\t=0D=0A", ' =0D=0A', '=0D=0A'], ["=09\r\n", "=20\r\n", "\r\n"], $string);
  97. switch (\ord(substr($string, -1))) {
  98. case 0x09:
  99. $string = substr_replace($string, '=09', -1);
  100. break;
  101. case 0x20:
  102. $string = substr_replace($string, '=20', -1);
  103. break;
  104. }
  105. return $string;
  106. }
  107. }