#native_company# #native_desc#
#native_cta#

MIME decoding class

By Kevin McDermott
on May 10, 2001

Version: 0.1

Type: Class

Category: Networking

License: GNU General Public License

Description: Takes a MIME encoded email message body and returns the attachments in a directly usable form. Handles base64 encoding, Quoted Printable and 7, 8 bit encoded messages.

class MimeDecode {
	var $attachments;
	var $text_version;

	# This accepts the body of a message (in Array form, split on n) and the
	# MIME encoded boundary.

	# This returns a two dimensional array, with the 'ContentType', 'filename',
	# 'ContentTypeEncoding' and the attachment 'data' for each indexed
	# attachment.
	function MimeDecode($body, $boundary) {

	$attach_no = -1;	

while ( list(, $line) = each($body) ) {

	$line = trim($line);

	if ( strstr($line, "--$boundary") ) {
		$this->attachments['number'] = ++$attach_no;
		continue;
	}

	if ( !$this->attachments[$attach_no]['ContentType'] && 
	     preg_match("/^Content-Type: ([w-/]*);?(.*)?/", $line, $matches) ) {
		$this->attachments[$attach_no]['ContentType'] = trim($matches[1]);
		if ( $matches[2] && 
		     preg_match("/name="(.*)"/i", $matches[2], 
		                $new_match)) {
			if (!$this->attachments[$attach_no]['filename'])
				$this->attachments[$attach_no]['filename']=trim($new_match[1]);	
		}
		continue;
	}

	if ( !$this->attachments[$attach_no]['filename'] &&
	     preg_match("/name="(.*)"/i", $line, $matches) ) {
		$this->attachments[$attach_no]['filename'] = trim($matches[1]);
		continue;
	}

	if ( !$this->attachments[$attach_no]['ContentTransferEncoding'] && 
	     preg_match("/^Content-Transfer-Encoding: (.*)/", $line, $matches) ) {
		$this->attachments[$attach_no]['ContentTransferEncoding'] = trim($matches[1]);
		continue;
	}	


	if ( !$line && !$this->attachments[$attach_no]['data'] ) {
		$data[0] = "";
		while ( list (, $line) = each($body) ) {
			$line = trim($line);
			$line = preg_replace("/< */? *HTML *>/i", "", $line);
			if (strstr($line, $boundary)) {
				prev($body);
				break;
			}
			array_push($data, $line."n");
		}

		# Decode attachment data before leaving...
		# Base64 encoded messages...
		if ( !strcasecmp($this->attachments[$attach_no]['ContentTransferEncoding'], "base64") ) {
			$this->attachments[$attach_no]['data'][0] = "";
			foreach ($data as $line) array_push($this->attachments[$attach_no]['data'],  base64_decode($line));

		# Quoted-Printable encoded messages
		} else if ( !strcasecmp($this->attachments[$attach_no]['ContentTransferEncoding'], "quoted-printable") ) {
			$this->attachments[$attach_no]['data'][0] = "";
			foreach ($data as $line) array_push($this->attachments[$attach_no]['data'],  quoted_printable_decode($line));

		# 7 or 8 bit "encoding"
		} else if ( !strcasecmp($this->attachments[$attach_no]['ContentTransferEncoding'], "7bit") || !strcasecmp($this->attachments[$attach_no]['ContentTransferEncoding'], "8bit") ) {
			$this->attachments[$attach_no]['data'] = $data;

		# defualt to just dumping the data
		} else {
			$this->attachments[$attach_no]['data'] = $data;
		}
		$data = NULL;
		continue;
		
	}

} # end foreach

	} # end MimeDecode


}