Base64ContentEncoder.php 1.9KB

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