Base64ContentEncoder.php 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 Base 64 Transfer Encoding in Swift Mailer.
  11. * @package Swift
  12. * @subpackage Mime
  13. * @author Chris Corbyn
  14. */
  15. class Swift_Mime_ContentEncoder_Base64ContentEncoder extends Swift_Encoder_Base64Encoder implements Swift_Mime_ContentEncoder
  16. {
  17. /**
  18. * Encode stream $in to stream $out.
  19. * @param Swift_OutputByteStream $in
  20. * @param Swift_InputByteStream $out
  21. * @param int $firstLineOffset
  22. * @param int $maxLineLength, optional, 0 indicates the default of 76 bytes
  23. */
  24. public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
  25. {
  26. if (0 >= $maxLineLength || 76 < $maxLineLength) {
  27. $maxLineLength = 76;
  28. }
  29. $remainder = 0;
  30. while (false !== $bytes = $os->read(8190)) {
  31. $encoded = base64_encode($bytes);
  32. $encodedTransformed = '';
  33. $thisMaxLineLength = $maxLineLength - $remainder - $firstLineOffset;
  34. while ($thisMaxLineLength < strlen($encoded)) {
  35. $encodedTransformed .= substr($encoded, 0, $thisMaxLineLength) . "\r\n";
  36. $firstLineOffset = 0;
  37. $encoded = substr($encoded, $thisMaxLineLength);
  38. $thisMaxLineLength = $maxLineLength;
  39. $remainder = 0;
  40. }
  41. if (0 < $remainingLength = strlen($encoded)) {
  42. $remainder += $remainingLength;
  43. $encodedTransformed .= $encoded;
  44. $encoded = null;
  45. }
  46. $is->write($encodedTransformed);
  47. }
  48. }
  49. /**
  50. * Get the name of this encoding scheme.
  51. * Returns the string 'base64'.
  52. * @return string
  53. */
  54. public function getName()
  55. {
  56. return 'base64';
  57. }
  58. }