/framework/vendor/swift/lib/classes/Swift/Encoder/Base64Encoder.php

http://zoop.googlecode.com/ · PHP · 63 lines · 27 code · 9 blank · 27 comment · 4 complexity · 8249a9a530b6dfbe32f1a8d5bf066f83 MD5 · raw file

  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. //@require 'Swift/Encoder.php';
  10. /**
  11. * Handles Base 64 Encoding in Swift Mailer.
  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. * Base64 encoded strings have a maximum line length of 76 characters.
  21. * If the first line needs to be shorter, indicate the difference with
  22. * $firstLineOffset.
  23. * @param string $string to encode
  24. * @param int $firstLineOffset
  25. * @param int $maxLineLength, optional, 0 indicates the default of 76 bytes
  26. * @return string
  27. */
  28. public function encodeString($string, $firstLineOffset = 0,
  29. $maxLineLength = 0)
  30. {
  31. if (0 >= $maxLineLength || 76 < $maxLineLength)
  32. {
  33. $maxLineLength = 76;
  34. }
  35. $encodedString = base64_encode($string);
  36. $firstLine = '';
  37. if (0 != $firstLineOffset)
  38. {
  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. }