Location: PHPKode > projects > Laikwan WMC > wmc_20070325/wmcLib/mime.php
<?


require_once 'Mail/mimeDecode.php';

// show name and id for conv panel
function wmcMime_getAttachmentListOfMsg($structure){
	$primary = $structure->ctype_primary;
	$secondary = $structure->ctype_secondary;
	if($primary == 'multipart' && $secondary == 'mixed'){
		$list = Array();
		for($i = 1; $i < count($structure->parts); $i++){
			$node = $structure->parts[$i];
			$pp = $node->ctype_primary;
			//if($pp != 'text' && $pp != 'multipart'){
				
				$desc = 0;
				if($pp == 'message' && $node->parts[0]->headers['subject']){
					$desc = $node->parts[0]->headers['subject'];
				}else if($node->ctype_parameters['name']){
					$desc = $node->ctype_parameters['name'];
				}else if($node->headers['content-description']){
					$desc = $node->headers['content-description'];
				}else{
					$desc = $node->headers['content-type'];
				}
				
				$elt = Array();					
				$elt['desc'] = $desc;					
				$elt['attach_num'] = $i;	
				$list[] = $elt;				
			//}
		}
		return $list;
	}else{
		return 0;	 
	}
}

// get attachement
// always follow num convention from "wmcMime_getAttachmentListOfMsg"
function wmcMime_getAttachment($structure, $attach_num){
	$primary = $structure->ctype_primary;
	$secondary = $structure->ctype_secondary;
	if($primary == 'multipart' && $secondary == 'mixed'){

		$node = $structure->parts[$attach_num];

		$attached = Array();
		$attached['contenttype'] = $node->headers['content-type'];
		if($node->ctype_primary == 'text'){
			$attached['data'] = $node->body;

		}else if($node->ctype_primary == 'message'){

			// todo: more complete parsing (headers, attach, etc)
			$attached['contenttype'] = "text/html; charset=utf-8";

			$bodies = wmcMime_getMainBodyOfMsg( $node->parts[0] );
			$body = "";
			if($bodies['html']){
				$body = $bodies['html'];
			}else if( $bodies['plain']){
				$body = $bodies['plain'];
			}
			$attached['data'] = "<html><body>" . $body . "</body></html>";

		}else if($node->ctype_primary == 'image'){
			$attached['data'] = $node->body;

		}else {
			// todo: binary stuff? - dangerous!!
			// list pdf, word, xls, etc as acceptable
			// '=== ' . $node->headers['content-type'] . ' ===\n\n' . 
			$attached['data'] = $node->body;
		}

		return $attached;
		
	}else{
		return "";	 
	}
}


// unused !!
function wmcMime_getAttachmentCountOfMsg($structure){
	$primary = $structure->ctype_primary;
	$secondary = $structure->ctype_secondary;
	if($primary == 'multipart'){
		if($secondary == 'mixed'){
			$count = 0;
			$cs_debug = '';
			for($i = 0; $i < count($structure->parts); $i++){
				$pp = $structure->parts[$i]->ctype_primary;
				if($pp != 'text' && $pp != 'multipart'){
					$cs_debug .= $pp . ", \n";
					$count++; 
				}
			}

			return $count . $cs_debug;
		}else{	// 'related'/'mixed' - take 0
			return 0;
		}
	}else{
		return 0;	 
	}
}

function wmcMime_getMainBodyOfMsg($structure){

	$primary = $structure->ctype_primary;
	$secondary = $structure->ctype_secondary;

	$bodies = Array();
	if($primary == 'multipart'){

		if($secondary == 'alternative'){
			$showable = Array();
			$ignore = 'ignore: ';
			$cs_debug = 'ctype_secondary: ';
			for($i = 0; $i < count($structure->parts); $i++){
				if($structure->parts[$i]->ctype_primary == 'text'){
					$cs_debug .= $structure->parts[$i]->ctype_secondary . ", \n";
					$showable[$structure->parts[$i]->ctype_secondary] = $structure->parts[$i]->body;
				}else{
					$ignore .= $structure->parts[$i]->ctype_primary . ", \n";
				}
			}


			if($showable['html']){	// todo: css/img/link parser/remover
				$bodies['html'] = getSafeHtmlEmailBody($showable['html']) ;
			}
			
			if($showable['plain']){			// for now: plain is much easier than html
				$showable['plain'] = getPlainTextEmailBody($showable['plain']);	 
				$bodies['plain'] = $showable['plain'];
//				$bodies['plain'] = "<pre>" .  $showable['plain'] . "</pre>";
			}

			if(!$showable['html'] && !$showable['plain'])  {
				$bodies['plain'] = 'multipart debug '. $ignore . "\n\n" . $cs_debug;
			}


		}else if($secondary == 'related'){	// image in html
			return wmcMime_getMainBodyOfMsg( $structure->parts[0] );
		}else if($secondary == 'mixed'){		// attachement
			return wmcMime_getMainBodyOfMsg( $structure->parts[0] );
		}else{	// 'related'/'mixed' - take 0
			return '== multipart unknown: ' .$secondary . ' ==\n\n' . wmcMime_getMainBodyOfMsg( $structure->parts[0] );
		}

	}else if($primary == 'text'){

		if($secondary == 'html'){
			$bodies['html'] =  getSafeHtmlEmailBody( $structure->body );
		}
		if($secondary == 'plain'){	// plain
			$bodies['plain'] = getPlainTextEmailBody($structure->body);	 
//			$bodies['plain'] =  "<pre>" . $structure->body . "</pre>";
		}
		
		if($secondary != 'html' && $secondary != 'plain'){	// plain
			$bodies['plain'] =  $structure->body ;
		}

	}else{
		$bodies['plain'] = '== unsupported ctype_primary = ' . $primary . "\n\n" . 
			'ctype_secondary = ' . $secondary . "\n\n";	 
	}

	return $bodies;
}
function getPlainTextEmailBody($tmp){
	$tmp = ereg_replace("<", "&lt;", $tmp);	 
	$tmp = ereg_replace(">", "&gt;", $tmp);	 

	$tmp = ereg_replace("\t", "&nbsp; &nbsp; &nbsp; &nbsp;", $tmp);	  // tab = 4 space
	$tmp = ereg_replace("\r", "", $tmp);	 
	$tmp1 = explode("\n", $tmp);	 
	$tmp2 = '';
	for($i=0; $i < count($tmp1); $i++){
		preg_match("/^( )+/", $tmp1[$i], $matches);
		$beg = '';
		if(count($matches) > 0){
			for($j=0; $j<strlen($matches[0]); $j++){
				$beg .= "&nbsp;"; 
			}
		}
		$tmp2 .= $beg . preg_replace("/^( )+/", '', $tmp1[$i]) . "<br>";
	}
	return $tmp2;
}
function getSafeHtmlEmailBody($contents){
//	$contents = ereg_replace("<style.+body(\W?|\W.+?){.+?</style>", "<div id='wmc_emailBodyRoot'", $contents);	 

	// remove html tag
	$contents = preg_replace("/<html/i", "<div id='wmc_emailHtmlRoot'", $contents);	 
	$contents = preg_replace("/<\/html/i", "</div", $contents);	 

	// remove body tag and css
	$contents = preg_replace("/(<style.+)body(.+?<\/style>)/si", "\\1#wmc_emailBodyRoot\\2", $contents);	 
	$contents = preg_replace("/<body/i", "<div id='wmc_emailBodyRoot'", $contents);	 
	$contents = preg_replace("/<\/body/i", "</div", $contents);	 

	// remove img src
/*	$contents = preg_replace('/\Wsrc=".+?"/is', ' src=""', $contents);	 */

	// move link to open in new window
	// todo: cid:
	$contents = preg_replace('/<a\W.*?href=["\'](.+?)["\'].*?>/is', '<a href="\\1" target="_blank">', $contents);	 

	return $contents;

}

?>
Return current item: Laikwan WMC