class.uebimiau.php

来自「极限网络智能办公系统 Office Automation V3.0官方100%源」· PHP 代码 · 共 636 行 · 第 1/2 页

PHP
636
字号

	function build_text_body($body) {
		$body = ereg_replace("\n","",$body);
		$body = ereg_replace("\r","<br>\r\n",$this->make_link_clickable(htmlspecialchars($body)));
		return "<font face=\"Courier New\" size=2>$body</font>";
	}

	function decode_qp($text) {
		$text = ereg_replace("\r\n","\r",$text);
		$text = ereg_replace("=\r","",$text);
		$text = ereg_replace("\r","\r\n",$text);
		$text = quoted_printable_decode($text);
		return $text;
	}

	function make_link_clickable($str){

		$str = eregi_replace("([[:space:]])((f|ht)tps?:\/\/[a-z0-9~#%@\&:=?+\/\.,_-]+[a-z0-9~#%@\&=?+\/_-]+)", "\\1<a class=autolink href=\"\\2\" target=\"_blank\">\\2</a>", $str); //http 
		$str = eregi_replace("([[:space:]])(www\.[a-z0-9~#%@\&:=?+\/\.,_-]+[a-z0-9~#%@\&=?+\/_-]+)", "\\1<a class=autolink href=\"http://\\2\" target=\"_blank\">\\2</a>", $str); // www. 
		$str = eregi_replace("([[:space:]])([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})","\\1<a class=autolink href=\"mailto:\\2\">\\2</a>", $str); // mail 
		
		$str = eregi_replace("^((f|ht)tp:\/\/[a-z0-9~#%@\&:=?+\/\.,_-]+[a-z0-9~#%@\&=?+\/_-]+)", "<a href=\"\\1\" target=\"_blank\">\\1</a>", $str); //http 
		$str = eregi_replace("^(www\.[a-z0-9~#%@\&:=?+\/\.,_-]+[a-z0-9~#%@\&=?+\/_-]+)", "<a class=autolink href=\"http://\\1\" target=\"_blank\">\\1</a>", $str); // www. 
		$str = eregi_replace("^([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})","<a class=autolink href=\"mailto:\\1\">\\1</a>", $str); // mail 

		return $str;
	}

	function process_message($header,$body) {
		$mail_info = $this->get_mail_info($header);

		$ctype = $mail_info["content-type"];
		$ctenc = $mail_info["content-transfer-encoding"];

		if($ctype == "") $ctype = "text/plain";

		$type = $ctype;

		$ctype = split(";",$ctype);
		$types = split("/",$ctype[0]);

		$maintype = trim(strtolower($types[0]));
		$subtype = trim(strtolower($types[1]));


		switch($maintype) {
		case "text":
			$body = $this->compile_body($body,$ctenc,$mail_info["content-type"]);
			switch($subtype) {
			case "html":
				if(!$this->use_html) {
					$body = eregi_replace("(\r|\n)","",$body);
					$body = eregi_replace("<br[ ]?[/]?>","\r\n",$body);
					$body = eregi_replace("</p>","\r\n\r\n",$body);
					$body = $this->build_text_body(strip_tags($body));
				}
				$msgbody = $body;
				break;
			default:
				$msgbody = $this->build_text_body($body);
				break;
			}
			$this->add_body($msgbody);
			break;
		case "multipart":
			if(ereg($subtype,"signed,mixed,related"))
				$subtype = "complex";
			switch($subtype) {
			case "alternative":
				$msgbody = $this->build_alternative_body($ctype[1],$body);
				break;
			case "complex":
				$msgbody = $this->build_complex_body($type,$body);
				break;
			default:
				$thisattach = $this->build_attach($header,$body,"",0);
			}
			break;
		default:
			$thisattach = $this->build_attach($header,$body,"",0);
		}
	}

	function build_attach($header,$body,$boundary,$part,$mode="info",$down=0,$nametosave="tmp.eml") {

		global $mail,$temporary_directory,$userfolder;

		$headers = $this->decode_header($header);

		$cdisp = $headers["content-disposition"];
		$ctype = $headers["content-type"]; 
		$ctype2 = explode(";",$ctype); 
		$ctype2 = $ctype2[0];

		$Atype = split("/",$ctype);
		$Acdisp = split(";",$cdisp);

		$tenc = $headers["content-transfer-encoding"];

		$is_embebed = ($headers["content-id"] != "");
		$body = $this->compile_body($body,$tenc,$ctype);
		$fname = $Acdisp[1];

		if(ereg("filename=(.*)",$fname,$regs))
			$filename = $regs[1];
		if($filename == "" && ereg("name=(.*)",$ctype,$regs))
			$filename = $regs[1];
		$filename = ereg_replace("\"(.*)\"","\\1",$filename);

		$filename = trim($this->decode_mime_string($filename));

		if($filename == "" && $Atype[0] == "message") {

			$attachheader = $this->fetch_structure($body);
			$attachheader = $this->decode_header($attachheader["header"]);
			$filename = $this->decode_mime_string($attachheader["subject"]);
			unset($attachheader);
			$filename = substr(ereg_replace("[^A-Za-z0-9]","_",$filename),0,20).".eml";
		} elseif($filename == "") {
			$filename = uniqid("").".tmp";
		}


		switch($mode) {
		case "info":
			$temp_array["name"] = $filename;
			$temp_array["size"] = strlen($body);
			$temp_array["temp"] = $temp;
			$temp_array["content-type"] = $ctype2;
			$temp_array["content-disposition"] = $Acdisp[0];
			$temp_array["boundary"] = $boundary;
			$temp_array["part"] = $part;
			$indice = count($this->_content["attachments"]);
			if(!$is_embebed)
				$this->_content["attachments"][$indice] = $temp_array;
			return $temp_array;
			break;
		case "down":
			$content_type = ($down)?"application/octet-stream":strtolower($ctype2);
			$filesize = strlen($body);
			header("Content-Type: $content_type; name=\"$filename\"\r\n"
			."Content-Length: $filesize\r\n");
			$cdisp = ($down)?"attachment":"inline";
			header("Content-Disposition: $cdisp; filename=\"$filename\"\r\n");
			echo($body);
			break;
		case "save":
			if(!ereg("\\.\\.",$nametosave))
				$this->_save_file($nametosave,$body);
			return 1;
			break;
		}
	}

	function compile_body($body,$enctype,$ctype) {
		$enctype = explode(" ",$enctype); $enctype = $enctype[0];
		if(strtolower($enctype) == "base64")
			$body = base64_decode($body);
		elseif(strtolower($enctype) == "quoted-printable")
			$body = $this->decode_qp($body);
		if(ereg("koi8", $ctype)) $body = convert_cyr_string($body, "k", "w");
		return $body;
	}


	function download_attach($header,$body,$bound="",$part=0,$down=1) {
		if($bound != "") {
			$parts = $this->split_parts($bound,$body);
			// split the especified part of mail, body and headers
			$email = $this->fetch_structure($parts[$part]);
			$header = $email["header"];
			$body = $email["body"];
			unset($email);
		}
		$this->build_attach($header,$body,"",0,$mode="down",$down);
	}

	function save_attach($header,$body,$filename) {
		$this->build_attach($header,$body,"",0,$mode="save",0,$filename);
	}

	function get_mail_info($header) {

		$myarray = Array();
		$headers = $this->decode_header($header);

		$message_id = ereg_replace("<(.*)>","\\1",$headers["message-id"]);

		$myarray["content-type"] = $headers["content-type"];
		$myarray["priority"] = $headers["x-priority"][0];

		$myarray["content-transfer-encoding"] = str_replace("GM","-",$headers["content-transfer-encoding"]);
		$myarray["message-id"] = trim($message_id);

		$received	= ereg_replace("  "," ",$headers["received"]);
		$user_date	= ereg_replace("  "," ",$headers["date"]);

		if(eregi("([0-9]{1,2}[ ]+[A-Z]{3}[ ]+[0-9]{4}[ ]+[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})[ ]?((\+|-)[0-9]{4})?",$received,$regs)) {
			//eg. Tue, 4 Sep 2001 16:22:31 -0000
			$mydate = $regs[1];
			$mytimezone = $regs[2];
			if(empty($mytimezone))
				if(eregi("((\\+|-)[0-9]{4})",$user_date,$regs)) $mytimezone = $regs[1];
				else $mytimezone = $this->timezone;
		} elseif(eregi("(([A-Z]{3})[ ]+([0-9]{1,2})[ ]+([0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})[ ]+([0-9]{4}))",$received,$regs)) {
			//eg. Tue Sep 4 16:26:17 2001 (Cubic Circle's style)
			$mydate = $regs[3]." ".$regs[2]." ".$regs[5]." ".$regs[4];
			if(eregi("((\\+|-)[0-9]{4})",$user_date,$regs)) $mytimezone = $regs[1];
			else $mytimezone = $this->timezone;

		} elseif(eregi("([0-9]{1,2}[ ]+[A-Z]{3}[ ]+[0-9]{4}[ ]+[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})[ ]?((\+|-)[0-9]{4})?",$user_date,$regs)) {
			//eg. Tue, 4 Sep 2001 16:22:31 -0000 (from Date header)
			$mydate = $regs[1];
			$mytimezone = $regs[2];
			if(empty($mytimezone))
				if(eregi("((\\+|-)[0-9]{4})",$user_date,$regs)) $mytimezone = $regs[1];
				else $mytimezone = $this->timezone;
		} else {
			$mydate		= date("d M Y H:i");
			$mytimezone	= $this->timezone;
		}
		$myarray["date"] = $this->build_mime_date($mydate,$mytimezone);
		$myarray["subject"] = $this->decode_mime_string($headers["subject"]);
		$myarray["from"] = $this->get_names($headers["from"]);
		$myarray["to"] = $this->get_names($headers["to"]);
		$myarray["cc"] = $this->get_names($headers["cc"]);
		$myarray["status"] = $headers["status"];
		$myarray["read"] = $headers["x-um-status"];

		return $myarray;

	}

	function build_mime_date($mydate,$timezone = "+0000") {
		if(!ereg("((\\+|-)[0-9]{4})",$timezone)) $timezone = "+0000";
		$parts = explode(" ",$mydate);
		if(count($parts) < 4) { return time(); }
		$day = $parts[0];
		switch(strtolower($parts[1])) {
			case "jan": $mon = 1; break;
			case "feb":	$mon = 2; break;
			case "mar":	$mon = 3; break;
			case "apr":	$mon = 4; break;
			case "may":	$mon = 5; break;
			case "jun": $mon = 6; break;
			case "jul": $mon = 7; break;
			case "aug": $mon = 8; break;
			case "sep": $mon = 9; break;
			case "oct": $mon = 10; break;
			case "nov": $mon = 11; break;
			case "dec": $mon = 12; break;
		}
		$year = $parts[2];
		$ahours = explode(":",$parts[3]);
		$hour = $ahours[0]; $min = $ahours[1]; $sec = $ahours[2];
		$timezone_oper	= $timezone[0];
		$timezone_hour	= intval("$timezone_oper".substr($timezone,1,2))*3600;
		$timezone_min	= intval("$timezone_oper".substr($timezone,3,2))*60;
		$timezone_diff	= $timezone_hour+$timezone_min;
		$user_timezone_oper	= $this->timezone[0];
		$user_timezone_hour	= intval("$user_timezone_oper".substr($this->timezone,1,2))*3600;
		$user_timezone_min	= intval("$user_timezone_oper".substr($this->timezone,3,2))*60;
		$user_timezone_diff	= $user_timezone_hour+$user_timezone_min;
		$diff 				= $timezone_diff-$user_timezone_diff;
		$mytimestamp	= mktime ($hour, $min, $sec, $mon, $day, $year)-$diff;
		return $mytimestamp;
	}

	function Decode($email) {
		$email = $this->fetch_structure($email);
		$this->_msgbody = "";
		$body = $email["body"];
		$header = $email["header"];
		$mail_info = $this->get_mail_info($header);
		$this->process_message($header,$body);
		$this->_content["headers"] = $header;
		$this->_content["date"] = $mail_info["date"];
		$this->_content["subject"] = $mail_info["subject"];
		$this->_content["message-id"] = $mail_info["message-id"];
		$this->_content["from"] = $mail_info["from"];
		$this->_content["to"] = $mail_info["to"];
		$this->_content["cc"] = $mail_info["cc"];
		$this->_content["body"] = $this->_msgbody;
		$this->_content["read"] = $mail_info["read"];
		$this->_content["priority"] = $mail_info["priority"];
		return $this->_content;
	}

	function split_parts($boundary,$body) {
		$startpos = strpos($body,$boundary)+strlen($boundary)+2;
		$lenbody = strpos($body,"\r\n$boundary--") - $startpos;
		$body = substr($body,$startpos,$lenbody);
		$parts = split($boundary,$body);
		return split("$boundary\r\n",$body);
	}

	function fetch_structure($email) {
		$ARemail = Array();
		$separador = "\r\n\r\n";
		$header = trim(substr($email,0,strpos($email,$separador)));
		$bodypos = strlen($header)+strlen($separador);
		$body = substr($email,$bodypos,strlen($email)-$bodypos);
		$ARemail["header"] = $header; $ARemail["body"] = $body;
		return $ARemail;
	}

	function get_boundary($ctype){
		if(preg_match('/boundary[ ]?=[ ]?(["]?.*)/i',$ctype,$regs)) {
			$boundary = $regs[1];
			$boundary = preg_replace('/^\"(.*)\"$/', "\\1", $boundary);
			$boundary = "--".str_replace("[","\\[",str_replace("]","\\]",$boundary));
			return $boundary;
		}
	}

}
require("./inc/class.uebimiau_mail.php");
?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?