00001 <?php
00031 class Zend_Mime
00032 {
00033 const TYPE_OCTETSTREAM = 'application/octet-stream';
00034 const TYPE_TEXT = 'text/plain';
00035 const TYPE_HTML = 'text/html';
00036 const ENCODING_7BIT = '7bit';
00037 const ENCODING_8BIT = '8bit';
00038 const ENCODING_QUOTEDPRINTABLE = 'quoted-printable';
00039 const ENCODING_BASE64 = 'base64';
00040 const DISPOSITION_ATTACHMENT = 'attachment';
00041 const DISPOSITION_INLINE = 'inline';
00042 const LINELENGTH = 72;
00043 const LINEEND = "\n";
00044 const MULTIPART_ALTERNATIVE = 'multipart/alternative';
00045 const MULTIPART_MIXED = 'multipart/mixed';
00046 const MULTIPART_RELATED = 'multipart/related';
00047
00048 protected $_boundary;
00049 protected static $makeUnique = 0;
00050
00051
00052 public static $qpKeys = array(
00053 "\x00","\x01","\x02","\x03","\x04","\x05","\x06","\x07",
00054 "\x08","\x09","\x0A","\x0B","\x0C","\x0D","\x0E","\x0F",
00055 "\x10","\x11","\x12","\x13","\x14","\x15","\x16","\x17",
00056 "\x18","\x19","\x1A","\x1B","\x1C","\x1D","\x1E","\x1F",
00057 "\x7F","\x80","\x81","\x82","\x83","\x84","\x85","\x86",
00058 "\x87","\x88","\x89","\x8A","\x8B","\x8C","\x8D","\x8E",
00059 "\x8F","\x90","\x91","\x92","\x93","\x94","\x95","\x96",
00060 "\x97","\x98","\x99","\x9A","\x9B","\x9C","\x9D","\x9E",
00061 "\x9F","\xA0","\xA1","\xA2","\xA3","\xA4","\xA5","\xA6",
00062 "\xA7","\xA8","\xA9","\xAA","\xAB","\xAC","\xAD","\xAE",
00063 "\xAF","\xB0","\xB1","\xB2","\xB3","\xB4","\xB5","\xB6",
00064 "\xB7","\xB8","\xB9","\xBA","\xBB","\xBC","\xBD","\xBE",
00065 "\xBF","\xC0","\xC1","\xC2","\xC3","\xC4","\xC5","\xC6",
00066 "\xC7","\xC8","\xC9","\xCA","\xCB","\xCC","\xCD","\xCE",
00067 "\xCF","\xD0","\xD1","\xD2","\xD3","\xD4","\xD5","\xD6",
00068 "\xD7","\xD8","\xD9","\xDA","\xDB","\xDC","\xDD","\xDE",
00069 "\xDF","\xE0","\xE1","\xE2","\xE3","\xE4","\xE5","\xE6",
00070 "\xE7","\xE8","\xE9","\xEA","\xEB","\xEC","\xED","\xEE",
00071 "\xEF","\xF0","\xF1","\xF2","\xF3","\xF4","\xF5","\xF6",
00072 "\xF7","\xF8","\xF9","\xFA","\xFB","\xFC","\xFD","\xFE",
00073 "\xFF"
00074 );
00075
00076 public static $qpReplaceValues = array(
00077 "=00","=01","=02","=03","=04","=05","=06","=07",
00078 "=08","=09","=0A","=0B","=0C","=0D","=0E","=0F",
00079 "=10","=11","=12","=13","=14","=15","=16","=17",
00080 "=18","=19","=1A","=1B","=1C","=1D","=1E","=1F",
00081 "=7F","=80","=81","=82","=83","=84","=85","=86",
00082 "=87","=88","=89","=8A","=8B","=8C","=8D","=8E",
00083 "=8F","=90","=91","=92","=93","=94","=95","=96",
00084 "=97","=98","=99","=9A","=9B","=9C","=9D","=9E",
00085 "=9F","=A0","=A1","=A2","=A3","=A4","=A5","=A6",
00086 "=A7","=A8","=A9","=AA","=AB","=AC","=AD","=AE",
00087 "=AF","=B0","=B1","=B2","=B3","=B4","=B5","=B6",
00088 "=B7","=B8","=B9","=BA","=BB","=BC","=BD","=BE",
00089 "=BF","=C0","=C1","=C2","=C3","=C4","=C5","=C6",
00090 "=C7","=C8","=C9","=CA","=CB","=CC","=CD","=CE",
00091 "=CF","=D0","=D1","=D2","=D3","=D4","=D5","=D6",
00092 "=D7","=D8","=D9","=DA","=DB","=DC","=DD","=DE",
00093 "=DF","=E0","=E1","=E2","=E3","=E4","=E5","=E6",
00094 "=E7","=E8","=E9","=EA","=EB","=EC","=ED","=EE",
00095 "=EF","=F0","=F1","=F2","=F3","=F4","=F5","=F6",
00096 "=F7","=F8","=F9","=FA","=FB","=FC","=FD","=FE",
00097 "=FF"
00098 );
00099
00100 public static $qpKeysString =
00101 "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF";
00102
00112 public static function isPrintable($str)
00113 {
00114 return (strcspn($str, self::$qpKeysString) == strlen($str));
00115 }
00116
00125 public static function encodeQuotedPrintable($str,
00126 $lineLength = self::LINELENGTH,
00127 $lineEnd = self::LINEEND)
00128 {
00129 $out = '';
00130 $str = self::_encodeQuotedPrintable($str);
00131
00132
00133 while ($str) {
00134 $ptr = strlen($str);
00135 if ($ptr > $lineLength) {
00136 $ptr = $lineLength;
00137 }
00138
00139
00140 $pos = strrpos(substr($str, 0, $ptr), '=');
00141 if ($pos !== false && $pos >= $ptr - 2) {
00142 $ptr = $pos;
00143 }
00144
00145
00146 if ($ptr > 0 && $str[$ptr - 1] == ' ') {
00147 --$ptr;
00148 }
00149
00150
00151 $out .= substr($str, 0, $ptr) . '=' . $lineEnd;
00152 $str = substr($str, $ptr);
00153 }
00154
00155 $out = rtrim($out, $lineEnd);
00156 $out = rtrim($out, '=');
00157 return $out;
00158 }
00159
00166 private static function _encodeQuotedPrintable($str)
00167 {
00168 $str = str_replace('=', '=3D', $str);
00169 $str = str_replace(self::$qpKeys, self::$qpReplaceValues, $str);
00170 $str = rtrim($str);
00171 return $str;
00172 }
00173
00186 public static function encodeQuotedPrintableHeader($str, $charset,
00187 $lineLength = self::LINELENGTH,
00188 $lineEnd = self::LINEEND)
00189 {
00190
00191 $prefix = sprintf('=?%s?Q?', $charset);
00192 $lineLength = $lineLength-strlen($prefix)-3;
00193
00194 $str = self::_encodeQuotedPrintable($str);
00195
00196
00197 $str = str_replace(array('?', ' ', '_'), array('=3F', '=20', '=5F'), $str);
00198
00199
00200 $lines = array(0 => "");
00201
00202
00203 $tmp = "";
00204 while(strlen($str) > 0) {
00205 $currentLine = max(count($lines)-1, 0);
00206 $token = self::getNextQuotedPrintableToken($str);
00207 $str = substr($str, strlen($token));
00208
00209 $tmp .= $token;
00210 if($token == '=20') {
00211
00212
00213 if(strlen($lines[$currentLine].$tmp) > $lineLength) {
00214 $lines[$currentLine+1] = $tmp;
00215 } else {
00216 $lines[$currentLine] .= $tmp;
00217 }
00218 $tmp = "";
00219 }
00220
00221 if(strlen($str) == 0) {
00222 $lines[$currentLine] .= $tmp;
00223 }
00224 }
00225
00226
00227 for($i = 0; $i < count($lines); $i++) {
00228 $lines[$i] = " ".$prefix.$lines[$i]."?=";
00229 }
00230 $str = trim(implode($lineEnd, $lines));
00231 return $str;
00232 }
00233
00240 private static function getNextQuotedPrintableToken($str)
00241 {
00242 if(substr($str, 0, 1) == "=") {
00243 $token = substr($str, 0, 3);
00244 } else {
00245 $token = substr($str, 0, 1);
00246 }
00247 return $token;
00248 }
00249
00259 public static function encodeBase64Header($str,
00260 $charset,
00261 $lineLength = self::LINELENGTH,
00262 $lineEnd = self::LINEEND)
00263 {
00264 $prefix = '=?' . $charset . '?B?';
00265 $suffix = '?=';
00266 $remainingLength = $lineLength - strlen($prefix) - strlen($suffix);
00267
00268 $encodedValue = self::encodeBase64($str, $remainingLength, $lineEnd);
00269 $encodedValue = str_replace($lineEnd, $suffix . $lineEnd . ' ' . $prefix, $encodedValue);
00270 $encodedValue = $prefix . $encodedValue . $suffix;
00271 return $encodedValue;
00272 }
00273
00283 public static function encodeBase64($str,
00284 $lineLength = self::LINELENGTH,
00285 $lineEnd = self::LINEEND)
00286 {
00287 return rtrim(chunk_split(base64_encode($str), $lineLength, $lineEnd));
00288 }
00289
00297 public function __construct($boundary = null)
00298 {
00299
00300 if ($boundary === null) {
00301 $this->_boundary = '=_' . md5(microtime(1) . self::$makeUnique++);
00302 } else {
00303 $this->_boundary = $boundary;
00304 }
00305 }
00306
00315 public static function encode($str, $encoding, $EOL = self::LINEEND)
00316 {
00317 switch ($encoding) {
00318 case self::ENCODING_BASE64:
00319 return self::encodeBase64($str, self::LINELENGTH, $EOL);
00320
00321 case self::ENCODING_QUOTEDPRINTABLE:
00322 return self::encodeQuotedPrintable($str, self::LINELENGTH, $EOL);
00323
00324 default:
00328 return $str;
00329 }
00330 }
00331
00338 public function boundary()
00339 {
00340 return $this->_boundary;
00341 }
00342
00350 public function boundaryLine($EOL = self::LINEEND)
00351 {
00352 return $EOL . '--' . $this->_boundary . $EOL;
00353 }
00354
00361 public function mimeEnd($EOL = self::LINEEND)
00362 {
00363 return $EOL . '--' . $this->_boundary . '--' . $EOL;
00364 }
00365 }