Base64Encoder.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 Encoding in Swift Mailer.
  11. *
  12. * @author Chris Corbyn
  13. */
  14. class Swift_Encoder_Base64Encoder implements Swift_Encoder
  15. {
  16. /**
  17. * Takes an unencoded string and produces a Base64 encoded string from it.
  18. *
  19. * Base64 encoded strings have a maximum line length of 76 characters.
  20. * If the first line needs to be shorter, indicate the difference with
  21. * $firstLineOffset.
  22. *
  23. * @param string $string to encode
  24. * @param int $firstLineOffset
  25. * @param int $maxLineLength optional, 0 indicates the default of 76 bytes
  26. *
  27. * @return string
  28. */
  29. public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0)
  30. {
  31. if (0 >= $maxLineLength || 76 < $maxLineLength) {
  32. $maxLineLength = 76;
  33. }
  34. $encodedString = base64_encode($string);
  35. $firstLine = '';
  36. if (0 != $firstLineOffset) {
  37. $firstLine = substr(
  38. $encodedString, 0, $maxLineLength - $firstLineOffset
  39. ) . "\r\n";
  40. $encodedString = substr(
  41. $encodedString, $maxLineLength - $firstLineOffset
  42. );
  43. }
  44. return $firstLine . trim(chunk_split($encodedString, $maxLineLength, "\r\n"));
  45. }
  46. /**
  47. * Does nothing.
  48. */
  49. public function charsetChanged($charset)
  50. {
  51. }
  52. }