Base64ContentEncoder.php 2.0KB

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