00001 <?php
00074 class Mail_MIMEPart
00075 {
00080 private $encoding;
00081
00086 private $subparts;
00087
00092 private $encoded;
00093
00098 private $headers;
00099
00104 private $body;
00105
00122 public function __construct($body = '', $params = array())
00123 {
00124 if (!defined('MAIL_MIMEPART_CRLF')) {
00125 define('MAIL_MIMEPART_CRLF', defined('MAIL_MIME_CRLF') ? MAIL_MIME_CRLF : "\r\n", true);
00126 }
00127
00128 foreach ($params as $key => $value) {
00129 switch ($key) {
00130 case 'content_type':
00131 $headers['Content-Type'] = $value . (isset($charset) ? '; charset="' . $charset . '"' : '');
00132 break;
00133
00134 case 'encoding':
00135 $this->encoding = $value;
00136 $headers['Content-Transfer-Encoding'] = $value;
00137 break;
00138
00139 case 'cid':
00140 $headers['Content-ID'] = '<' . $value . '>';
00141 break;
00142
00143 case 'disposition':
00144 $headers['Content-Disposition'] = $value . (isset($dfilename) ? '; filename="' . $dfilename . '"' : '');
00145 break;
00146
00147 case 'dfilename':
00148 if (isset($headers['Content-Disposition'])) {
00149 $headers['Content-Disposition'] .= '; filename="' . $value . '"';
00150 } else {
00151 $dfilename = $value;
00152 }
00153 break;
00154
00155 case 'description':
00156 $headers['Content-Description'] = $value;
00157 break;
00158
00159 case 'charset':
00160 if (isset($headers['Content-Type'])) {
00161 $headers['Content-Type'] .= '; charset="' . $value . '"';
00162 } else {
00163 $charset = $value;
00164 }
00165 break;
00166 }
00167 }
00168
00169
00170 if (!isset($headers['Content-Type'])) {
00171 $headers['Content-Type'] = 'text/plain';
00172 }
00173
00174
00175 if (!isset($this->encoding)) {
00176 $this->encoding = '7bit';
00177 }
00178
00179
00180 $this->encoded = array();
00181 $this->headers = $headers;
00182 $this->body = $body;
00183 }
00184
00193 public function encode()
00194 {
00195 $encoded =& $this->encoded;
00196
00197 if (!empty($this->subparts)) {
00198 srand((double)microtime()*1000000);
00199 $boundary = '=_' . md5(uniqid(rand()) . microtime());
00200 $this->headers['Content-Type'] .= ';' . MAIL_MIMEPART_CRLF . "\t" . 'boundary="' . $boundary . '"';
00201
00202
00203 for ($i = 0; $i < count($this->subparts); $i++) {
00204 $headers = array();
00205 $tmp = $this->subparts[$i]->encode();
00206 foreach ($tmp['headers'] as $key => $value) {
00207 $headers[] = $key . ': ' . $value;
00208 }
00209 $subparts[] = implode(MAIL_MIMEPART_CRLF, $headers) . MAIL_MIMEPART_CRLF . MAIL_MIMEPART_CRLF . $tmp['body'];
00210 }
00211
00212 $encoded['body'] = '--' . $boundary . MAIL_MIMEPART_CRLF .
00213 implode('--' . $boundary . MAIL_MIMEPART_CRLF, $subparts) .
00214 '--' . $boundary.'--' . MAIL_MIMEPART_CRLF;
00215 } else {
00216 $encoded['body'] = $this->getEncodedData($this->body, $this->encoding) . MAIL_MIMEPART_CRLF;
00217 }
00218
00219
00220 $encoded['headers'] =& $this->headers;
00221
00222 return $encoded;
00223 }
00224
00234 public function addSubPart($body, $params)
00235 {
00236 $this->subparts[] = new Mail_MIMEPart($body, $params);
00237
00238 return $this->subparts[count($this->subparts) - 1];
00239 }
00240
00248 private function getEncodedData($data, $encoding)
00249 {
00250 switch ($encoding) {
00251 case '8bit':
00252 case '7bit':
00253 return $data;
00254 break;
00255
00256 case 'quoted-printable':
00257 return $this->quotedPrintableEncode($data);
00258 break;
00259
00260 case 'base64':
00261 return rtrim(chunk_split(base64_encode($data), 76, MAIL_MIMEPART_CRLF));
00262 break;
00263
00264 default:
00265 return $data;
00266 }
00267 }
00268
00276 private function quotedPrintableEncode($input , $line_max = 76)
00277 {
00278 $lines = preg_split("/\r?\n/", $input);
00279 $eol = MAIL_MIMEPART_CRLF;
00280 $escape = '=';
00281 $output = '';
00282
00283 while(list(, $line) = each($lines)){
00284
00285 $linlen = strlen($line);
00286 $newline = '';
00287
00288 for ($i = 0; $i < $linlen; $i++) {
00289 $char = substr($line, $i, 1);
00290 $dec = ord($char);
00291
00292 if (($dec == 32) AND ($i == ($linlen - 1))){
00293 $char = '=20';
00294
00295 } elseif($dec == 9) {
00296 ;
00297 } elseif(($dec == 61) OR ($dec < 32 ) OR ($dec > 126)) {
00298 $char = $escape . strtoupper(sprintf('%02s', dechex($dec)));
00299 }
00300
00301 if ((strlen($newline) + strlen($char)) >= $line_max) {
00302 $output .= $newline . $escape . $eol;
00303 $newline = '';
00304 }
00305 $newline .= $char;
00306 }
00307 $output .= $newline . $eol;
00308 }
00309 $output = substr($output, 0, -1 * strlen($eol));
00310 return $output;
00311 }
00312 }
00313 ?>