$lineLength) { $ptr = $lineLength; } // Ensure we are not splitting across an encoded character $pos = strrpos(substr($str, 0, $ptr), '='); if ($pos !== false && $pos >= $ptr - 2) { $ptr = $pos; } // Check if there is a space at the end of the line and rewind if ($ptr > 0 && $str[$ptr - 1] == ' ') { --$ptr; } // Add string and continue $out .= substr($str, 0, $ptr) . '=' . $lineEnd; $str = substr($str, $ptr); } $out = rtrim($out, $lineEnd); $out = rtrim($out, '='); return $out; } /** * Encode a given string in base64 encoding and break lines * according to the maximum linelength. * * @param string $str * @param int $lineLength Defaults to {@link LINELENGTH} * @param int $lineEnd Defaults to {@link LINEEND} * @return string */ public static function encodeBase64($str, $lineLength = self::LINELENGTH, $lineEnd = self::LINEEND) { return rtrim(chunk_split(base64_encode($str), $lineLength, $lineEnd)); } /** * Constructor * * @param null|string $boundary * @access public * @return void */ public function __construct($boundary = null) { // This string needs to be somewhat unique if ($boundary === null) { $this->_boundary = '=_' . md5(microtime(1) . self::$makeUnique++); } else { $this->_boundary = $boundary; } } /** * Encode the given string with the given encoding. * * @param string $str * @param string $encoding * @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND} * @return string */ public static function encode($str, $encoding, $EOL = self::LINEEND) { switch ($encoding) { case self::ENCODING_BASE64: return self::encodeBase64($str, self::LINELENGTH, $EOL); case self::ENCODING_QUOTEDPRINTABLE: return self::encodeQuotedPrintable($str, self::LINELENGTH, $EOL); default: /** * @todo 7Bit and 8Bit is currently handled the same way. */ return $str; } } /** * Return a MIME boundary * * @access public * @return string */ public function boundary() { return $this->_boundary; } /** * Return a MIME boundary line * * @param mixed $EOL Defaults to {@link LINEEND} * @access public * @return string */ public function boundaryLine($EOL = self::LINEEND) { return $EOL . '--' . $this->_boundary . $EOL; } /** * Return MIME ending * * @access public * @return string */ public function mimeEnd($EOL = self::LINEEND) { return $EOL . '--' . $this->_boundary . '--' . $EOL; } }