Base64Encoder.php 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. * @package Swift
  12. * @subpackage Encoder
  13. * @author Chris Corbyn
  14. */
  15. class Swift_Encoder_Base64Encoder implements Swift_Encoder
  16. {
  17. /**
  18. * Takes an unencoded string and produces a Base64 encoded string from it.
  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. * @param string $string to encode
  23. * @param int $firstLineOffset
  24. * @param int $maxLineLength, optional, 0 indicates the default of 76 bytes
  25. * @return string
  26. */
  27. public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0)
  28. {
  29. if (0 >= $maxLineLength || 76 < $maxLineLength) {
  30. $maxLineLength = 76;
  31. }
  32. $encodedString = base64_encode($string);
  33. $firstLine = '';
  34. if (0 != $firstLineOffset) {
  35. $firstLine = substr(
  36. $encodedString, 0, $maxLineLength - $firstLineOffset
  37. ) . "\r\n";
  38. $encodedString = substr(
  39. $encodedString, $maxLineLength - $firstLineOffset
  40. );
  41. }
  42. return $firstLine . trim(chunk_split($encodedString, $maxLineLength, "\r\n"));
  43. }
  44. /**
  45. * Does nothing.
  46. */
  47. public function charsetChanged($charset)
  48. {
  49. }
  50. }