$lineLength) { $ptr = $lineLength; } $pos = strrpos(substr($str, 0, $ptr), '='); if ($pos !== false && $pos >= $ptr - 2) { $ptr = $pos; } if ($ptr > 0 && $str[$ptr - 1] == ' ') { --$ptr; } $out .= substr($str, 0, $ptr) . '=' . $lineEnd; $str = substr($str, $ptr); } $out = rtrim($out, $lineEnd); $out = rtrim($out, '='); return $out; } private static function _encodeQuotedPrintable($str) { $str = str_replace('=', '=3D', $str); $str = str_replace(self::$qpKeys, self::$qpReplaceValues, $str); $str = rtrim($str); return $str; } public static function encodeQuotedPrintableHeader($str, $charset, $lineLength = self::LINELENGTH, $lineEnd = self::LINEEND) { $prefix = sprintf('=?%s?Q?', $charset); $lineLength = $lineLength-strlen($prefix)-3; $str = self::_encodeQuotedPrintable($str); $str = str_replace(array('?', ' ', '_'), array('=3F', '=20', '=5F'), $str); $lines = array(0 => ""); $tmp = ""; while(strlen($str) > 0) { $currentLine = max(count($lines)-1, 0); $token = self::getNextQuotedPrintableToken($str); $str = substr($str, strlen($token)); $tmp .= $token; if($token == '=20') { if(strlen($lines[$currentLine].$tmp) > $lineLength) { $lines[$currentLine+1] = $tmp; } else { $lines[$currentLine] .= $tmp; } $tmp = ""; } if(strlen($str) == 0) { $lines[$currentLine] .= $tmp; } } for($i = 0; $i < count($lines); $i++) { $lines[$i] = " ".$prefix.$lines[$i]."?="; } $str = trim(implode($lineEnd, $lines)); return $str; } private static function getNextQuotedPrintableToken($str) { if(substr($str, 0, 1) == "=") { $token = substr($str, 0, 3); } else { $token = substr($str, 0, 1); } return $token; } public static function encodeBase64Header($str, $charset, $lineLength = self::LINELENGTH, $lineEnd = self::LINEEND) { $prefix = '=?' . $charset . '?B?'; $suffix = '?='; $remainingLength = $lineLength - strlen($prefix) - strlen($suffix); $encodedValue = self::encodeBase64($str, $remainingLength, $lineEnd); $encodedValue = str_replace($lineEnd, $suffix . $lineEnd . ' ' . $prefix, $encodedValue); $encodedValue = $prefix . $encodedValue . $suffix; return $encodedValue; } public static function encodeBase64($str, $lineLength = self::LINELENGTH, $lineEnd = self::LINEEND) { return rtrim(chunk_split(base64_encode($str), $lineLength, $lineEnd)); } public function __construct($boundary = null) { if ($boundary === null) { $this->_boundary = '=_' . md5(microtime(1) . self::$makeUnique++); } else { $this->_boundary = $boundary; } } 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: return $str; } } public function boundary() { return $this->_boundary; } public function boundaryLine($EOL = self::LINEEND) { return $EOL . '--' . $this->_boundary . $EOL; } public function mimeEnd($EOL = self::LINEEND) { return $EOL . '--' . $this->_boundary . '--' . $EOL; } }