[ Index ]

MailPress 7.2

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/composer/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/ContentEncoder/ -> Base64ContentEncoder.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of SwiftMailer.
   5   * (c) 2004-2009 Chris Corbyn
   6   *
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  /**
  12   * Handles Base 64 Transfer Encoding in Swift Mailer.
  13   *
  14   * @author Chris Corbyn
  15   */
  16  class Swift_Mime_ContentEncoder_Base64ContentEncoder extends Swift_Encoder_Base64Encoder implements Swift_Mime_ContentEncoder
  17  {
  18      /**
  19       * Encode stream $in to stream $out.
  20       *
  21       * @param int $firstLineOffset
  22       */
  23      public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
  24      {
  25          if (0 >= $maxLineLength || 76 < $maxLineLength) {
  26              $maxLineLength = 76;
  27          }
  28  
  29          $remainder = 0;
  30          $base64ReadBufferRemainderBytes = null;
  31  
  32          // To reduce memory usage, the output buffer is streamed to the input buffer like so:
  33          //   Output Stream => base64encode => wrap line length => Input Stream
  34          // HOWEVER it's important to note that base64_encode() should only be passed whole triplets of data (except for the final chunk of data)
  35          // otherwise it will assume the input data has *ended* and it will incorrectly pad/terminate the base64 data mid-stream.
  36          // We use $base64ReadBufferRemainderBytes to carry over 1-2 "remainder" bytes from the each chunk from OutputStream and pre-pend those onto the
  37          // chunk of bytes read in the next iteration.
  38          // When the OutputStream is empty, we must flush any remainder bytes.
  39          while (true) {
  40              $readBytes = $os->read(8192);
  41              $atEOF = (false === $readBytes);
  42  
  43              if ($atEOF) {
  44                  $streamTheseBytes = $base64ReadBufferRemainderBytes;
  45              } else {
  46                  $streamTheseBytes = $base64ReadBufferRemainderBytes.$readBytes;
  47              }
  48              $base64ReadBufferRemainderBytes = null;
  49              $bytesLength = strlen($streamTheseBytes);
  50  
  51              if (0 === $bytesLength) { // no data left to encode
  52                  break;
  53              }
  54  
  55              // if we're not on the last block of the ouput stream, make sure $streamTheseBytes ends with a complete triplet of data
  56              // and carry over remainder 1-2 bytes to the next loop iteration
  57              if (!$atEOF) {
  58                  $excessBytes = $bytesLength % 3;
  59                  if (0 !== $excessBytes) {
  60                      $base64ReadBufferRemainderBytes = substr($streamTheseBytes, -$excessBytes);
  61                      $streamTheseBytes = substr($streamTheseBytes, 0, $bytesLength - $excessBytes);
  62                  }
  63              }
  64  
  65              $encoded = base64_encode($streamTheseBytes);
  66              $encodedTransformed = '';
  67              $thisMaxLineLength = $maxLineLength - $remainder - $firstLineOffset;
  68  
  69              while ($thisMaxLineLength < strlen($encoded)) {
  70                  $encodedTransformed .= substr($encoded, 0, $thisMaxLineLength)."\r\n";
  71                  $firstLineOffset = 0;
  72                  $encoded = substr($encoded, $thisMaxLineLength);
  73                  $thisMaxLineLength = $maxLineLength;
  74                  $remainder = 0;
  75              }
  76  
  77              if (0 < $remainingLength = strlen($encoded)) {
  78                  $remainder += $remainingLength;
  79                  $encodedTransformed .= $encoded;
  80                  $encoded = null;
  81              }
  82  
  83              $is->write($encodedTransformed);
  84  
  85              if ($atEOF) {
  86                  break;
  87              }
  88          }
  89      }
  90  
  91      /**
  92       * Get the name of this encoding scheme.
  93       * Returns the string 'base64'.
  94       *
  95       * @return string
  96       */
  97      public function getName()
  98      {
  99          return 'base64';
 100      }
 101  }


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1