Base64Encoder.php 1.6KB

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