NativeQpContentEncoder.php 3.4 KB

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