Base64Encoder.php 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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,
  28. $maxLineLength = 0)
  29. {
  30. if (0 >= $maxLineLength || 76 < $maxLineLength)
  31. {
  32. $maxLineLength = 76;
  33. }
  34. $encodedString = base64_encode($string);
  35. $firstLine = '';
  36. if (0 != $firstLineOffset)
  37. {
  38. $firstLine = substr(
  39. $encodedString, 0, $maxLineLength - $firstLineOffset
  40. ) . "\r\n";
  41. $encodedString = substr(
  42. $encodedString, $maxLineLength - $firstLineOffset
  43. );
  44. }
  45. return $firstLine . trim(chunk_split($encodedString, $maxLineLength, "\r\n"));
  46. }
  47. /**
  48. * Does nothing.
  49. */
  50. public function charsetChanged($charset)
  51. {
  52. }
  53. }