⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class.uebimiau_mail.php

📁 极限网络智能办公系统 Office Automation V3.0官方100%源代码.
💻 PHP
📖 第 1 页 / 共 2 页
字号:
<?
class UebiMiau extends UebiMiau_core {

	var $_current_folder = "";
	var $_system_folders = Array("inbox","trash","sent");

	function UebiMiau() {
		$this->_sid = uniqid("");
	}

	function mail_connected() {
        if(!empty($this->mail_connection)) {
            $sock_status = @socket_get_status($this->mail_connection);
			
            if($sock_status["eof"]) {
                @fclose($this->mail_connection);
                return 0;
            }
            return 1; 
        } 
        return 0;
	}

	function mail_get_line() {
		$buffer = chop(fgets($this->mail_connection,1024));
		if($this->debug) {
			$sendtodebug = true;
			if(eregi("^(\\* )",$buffer) || eregi("^([A-Za-z0-9]+ (OK|NO|BAD))",$buffer) || eregi("^(\\+OK|\\-ERR)",$buffer)) {
				$output = "<- <b>".htmlspecialchars($buffer)."</b>";
			} else {
				$sendtodebug = ($this->debug > 1)?false:true;
				$output = htmlspecialchars($buffer);
			}
			if ($sendtodebug)
				echo("<font style=\"font-size:12px; font-family: Courier New; background-color: white; color: black;\"> $output</font><br>\r\n");
			flush();
		}
		return $buffer;
	}

	function mail_send_command($cmd) {
		if($this->mail_connected()) {
			$output = (eregi("^(PASS|LOGIN)",$cmd,$regs))?$regs[1]." ****":$cmd;
			if($this->mail_protocol == "imap") {
				$cmd = $this->_sid." ".$cmd;
				$output = $this->_sid." ".$output;
			}
			fwrite($this->mail_connection,"$cmd\r\n");
			if($this->debug) {
				echo("<font style=\"font-size:12px; font-family: Courier New; background-color: white; color: black;\">-&gt; <em><b>".htmlspecialchars($output)."</b></em></font><br>\r\n");
				flush();
			}
			return 1;
		}
		return 0;
	}

	function mail_connect() {
		if($this->debug)
			for($i=0;$i<20;$i++)
				echo("<!-- buffer sux -->\r\n");

		//ob_start();
		if(!$this->mail_connected()) {
	
			$this->mail_connection = @fsockopen($this->mail_server, $this->mail_port, $errno, $errstr, 15);
			//or printf("网络连接错误!");


			if($this->mail_connection) {

				$buffer = $this->mail_get_line();


				if($this->mail_protocol == "imap") $regexp = "^([ ]?\\*[ ]?OK)";
				else $regexp = "^(\\+OK)";

				if(ereg($regexp,$buffer)) return 1;
				else return 0;
			}
			return 0;
		} else return 1;
	}


	function mail_auth($checkfolders=false) {
		if($this->mail_connected()) {
			if ($this->mail_protocol == "imap") {
				$this->mail_send_command("LOGIN ".$this->mail_user." ".$this->mail_pass);
				$buffer = $this->mail_get_line();
				if(ereg("^(".$this->_sid." OK)",$buffer)) { 
					if($checkfolders)
						$this->_check_folders();
					return 1;
				} else { 
					$this->mail_error_msg = $buffer; 
					return 0; 
				}
			} else {
				$this->mail_send_command("USER ".$this->mail_user);
				$buffer = $this->mail_get_line();
				if(ereg("^(\+OK)",$buffer)) {
					$this->mail_send_command("PASS ".$this->mail_pass);
					$buffer = $this->mail_get_line();
					if(ereg("^(\+OK)",$buffer)) { 
						if($checkfolders)
							$this->_check_folders();
						return 1;
					} else { 
						$this->mail_error_msg = $buffer; 
						return 0; 
					}
				} else 
					return 0;
			}
		}
		return 0;
	}

	function _check_folders() {

		$userfolder = $this->user_folder;
		$temporary_directory = $this->temp_folder;
		$idle_timeout = $this->timeout;

		if(!file_exists($this->user_folder))
			if(!@mkdir($this->user_folder,0777)) die("<h1><br><br><br><center>$error_permiss</center></h1>");

		$boxes = $this->mail_list_boxes();


		if($this->mail_protocol == "imap") {
			$tmp = $this->_system_folders;

			for($i=0;$i<count($boxes);$i++) {
				$current_folder = $boxes[$i]["name"];

				if(in_array(strtolower($current_folder),$this->_system_folders)) 
					$current_folder = strtolower($current_folder);

				while(list($index,$value) = each($tmp)) {
					if(strtolower($current_folder) == strtolower($value)) {
						unset($tmp[$index]);
					}
				}

				reset($tmp);
			}

			while(list($index,$value) = each($tmp)) {
				$this->mail_create_box($value);
			}

			for($i=0;$i<count($boxes);$i++) {
				$current_folder = $boxes[$i]["name"];
				if(!in_array(strtolower($current_folder),$this->_system_folders))
					if(!file_exists($this->user_folder.$current_folder))
						mkdir($this->user_folder.$current_folder,0777);
			}

		}

		$system_folders = array_merge($this->_system_folders,Array("_attachments","_infos"));

		while(list($index,$value) = each($system_folders)) {
			if(!file_exists($this->user_folder.$value)) {
				if(in_array(strtolower($value),$this->_system_folders)) 
					$value = strtolower($value);
				mkdir($this->user_folder.$value,0777);
			}
		}


		$sessiondir = $temporary_directory."_sessions/";

		// Clean old sessions
		$all=opendir($sessiondir); 
		while ($file=readdir($all)) { 
			$thisfile = $sessiondir.$file;
			if (is_file($thisfile)) {
				$idle = intval((time()-@filemtime($thisfile))/60);
				if(($idle_timeout+10) < $idle)
					@unlink($thisfile);
			}
		}

		closedir($all); 
		unset($all);

	}
	
	
	function mail_retr_msg($msg,$check=1) {

		global $mail_use_top,$appname,$appversion,$error_retrieving;
		$msgheader = $msg["header"];

		if($this->mail_protocol == "imap") {

			if($check) {
				if(strtolower($this->_current_folder) != strtolower($msg["folder"]))
					$boxinfo = $this->mail_select_box($msg["folder"]);

				$this->mail_send_command("FETCH ".$msg["id"].":".$msg["id"]." BODY.PEEK[HEADER.FIELDS (Message-Id)]");
				$buffer = $this->mail_get_line();

				if(eregi("^(".$this->_sid." (NO|BAD))",$buffer)) { $this->mail_error_msg = $buffer; return 0; }
				while(!eregi("^(".$this->_sid." OK)",$buffer)) {
					if(eregi("message-id: (.*)",$buffer,$regs))
						$current_id = ereg_replace("<(.*)>","\\1",$regs[1]);
					$buffer = $this->mail_get_line();
				}
				if(base64_encode($current_id) != base64_encode($msg["message-id"])) {
					$this->mail_error_msg = $error_retrieving;
					return 0;
				}
			}

			if(file_exists($msg["localname"])) {
				$msgcontent = $this->_read_file($msg["localname"]);
			} else {
				$this->mail_send_command("FETCH ".$msg["id"].":".$msg["id"]." BODY[TEXT]");
				$buffer = $this->mail_get_line();
				if(eregi("^(".$this->_sid." (NO|BAD))",$buffer)) { $this->mail_error_msg = $buffer; return 0; }
				if(ereg("\\{(.*)\\}",$buffer,$regs))
					$bytes = $regs[1];

				$buffer = $this->mail_get_line();
				while(!eregi("^(".$this->_sid." OK)",$buffer)) {
					if(!eregi("[ ]?\\*[ ]?[0-9]+[ ]?FETCH",$buffer))
						$msgbody .= "$buffer\r\n";
					$buffer = $this->mail_get_line();
				}

				$pos = strrpos($msgbody, ")");
				if(!($pos === false))
					$msgbody = substr($msgbody,0,$pos);

				$msgcontent = "$msgheader\r\n\r\n$msgbody";
				$this->_save_file($msg["localname"],$msgcontent);

			}

		} else {

			if($check && strtolower($msg["folder"]) == "inbox") {
				$this->mail_send_command("TOP ".$msg["id"]." 0");
				$buffer = $this->mail_get_line();

				if(!ereg("^(\+OK)",$buffer))  { $this->mail_error_msg = $buffer; return 0; }

				unset($header);

				while (!feof($this->mail_connection)) {
					$buffer = $this->mail_get_line();
					if(trim($buffer) == ".") break;
					$header .= "$buffer\r\n";
				}
				$mail_info = $this->get_mail_info($header);

				if(base64_encode($mail_info["message-id"]) != base64_encode($msg["message-id"])) {
					$this->mail_error_msg = $error_retrieving;
					return 0;
				}

			}

			if(file_exists($msg["localname"])) {
				$msgcontent = $this->_read_file($msg["localname"]);

			} elseif (strtolower($msg["folder"]) == "inbox") {
				$command = ($mail_use_top)?"TOP ".$msg["id"]." ".$msg["size"]:"RETR ".$msg["id"];
				$this->mail_send_command($command);
				$buffer = $this->mail_get_line();
	
				if(!ereg("^(\+OK)",$buffer)) { $this->mail_error_msg = $buffer; return 0; }
				$last_buffer = 0;

				while (!feof($this->mail_connection)) {
					$buffer = ereg_replace("(\n|\r)","",$this->mail_get_line());
					if(trim($buffer) == ".") break;
					if($body_started)
						$msgbody .= "$buffer\r\n";
					if(!$body_started && trim($buffer) == "") $body_started = true;
				}
				$msgcontent = "$msgheader\r\n\r\n$msgbody";
				$this->_save_file($msg["localname"],$msgcontent);
			}
		}

		return $msgcontent;
	}


	function mail_delete_msg($msg, $send_to_trash = 1, $save_only_read = 0) {

		$read = (ereg("\\SEEN",$msg["flags"]))?1:0;

		/* choose your protocol */
		if($this->mail_protocol == "imap") {
			
			
			/* check the message id to make sure that the messages still in the server */
			if(strtolower($this->_current_folder) != strtolower($msg["folder"]))
				$boxinfo = $this->mail_select_box($msg["folder"]);
	
			$this->mail_send_command("FETCH ".$msg["id"].":".$msg["id"]." BODY.PEEK[HEADER.FIELDS (Message-Id)]");
			$buffer = $this->mail_get_line();

			/* if any problem with the server, stop the function */
			if(eregi("^(".$this->_sid." (NO|BAD))",$buffer)) { $this->mail_error_msg = $buffer; return 0; }

			while(!eregi("^(".$this->_sid." OK)",$buffer)) {
				/* we need only the message id yet */

				if(eregi("message-id: (.*)",$buffer,$regs))
					$current_id = ereg_replace("<(.*)>","\\1",$regs[1]);

				$buffer = $this->mail_get_line();
			}


			/* compare the old and the new message id, if different, stop*/
			if(base64_encode($current_id) != base64_encode($msg["message-id"])) {
				$this->mail_error_msg = $error_retrieving;
				return 0;
			}

			/*if the pointer is here, no one problem occours*/

			
			if( $send_to_trash && 
				strtoupper($msg["folder"]) != "TRASH" &&
				(!$save_only_read || ($save_only_read && $read))) {

				$this->mail_send_command("COPY ".$msg["id"].":".$msg["id"]." \"trash\"");
				$buffer = $this->mail_get_line();

				/* if any problem with the server, stop the function */
				if(!eregi("^(".$this->_sid." OK)",$buffer)) { $this->mail_error_msg = $buffer; return 0; }

				if(file_exists($msg["localname"])) {
					$currentname = $msg["localname"];
					$basename = basename($currentname);
					$newfilename = $this->user_folder."trash/$basename";
					copy($currentname,$newfilename);
					unlink($currentname);
				}
			}
			$this->mail_set_flag($msg,"\\DELETED","+");

			$this->_require_expunge = true;

			return 1;

		} else {
			/* now we are working with POP3 */
			/* check the message id to make sure that the messages still in the server */
			if(strtoupper($msg["folder"]) == "INBOX") {

				$this->mail_send_command("TOP ".$msg["id"]." 0");
				$buffer = $this->mail_get_line();
	
				/* if any problem with the server, stop the function */
				if(!ereg("^(\+OK)",$buffer))  { $this->mail_error_msg = $buffer; return 0; }
	
				unset($header);
	
				while (!feof($this->mail_connection)) {
					$buffer = $this->mail_get_line();
					if(trim($buffer) == ".") break;
					$header .= "$buffer\r\n";
				}
				$mail_info = $this->get_mail_info($header);
	
	
				/* compare the old and the new message id, if different, stop*/
				if(base64_encode($mail_info["message-id"]) != base64_encode($msg["message-id"])) {
					$this->mail_error_msg = $error_retrieving;
					return 0;
				}

				if(!file_exists($msg["localname"])) {
					if(!$this->mail_retr_msg($msg,0)) return 0;
					$this->mail_set_flag($msg,"\\SEEN","-");
				}
				$this->mail_send_command("DELE ".$msg["id"]);
				$buffer = $this->mail_get_line();
				if(!ereg("^(\+OK)",$buffer)) { $this->mail_error_msg = $buffer; return 0; }
			}

			if( $send_to_trash && 
				strtoupper($msg["folder"]) != "TRASH" &&
				(!$save_only_read || ($save_only_read && $read))) {

				if(file_exists($msg["localname"])) {
					$currentname = $msg["localname"];
					$basename = basename($currentname);
					$newfilename = $this->user_folder."trash/$basename";
					copy($currentname,$newfilename);
					unlink($currentname);
				}
			} else {
				unlink($msg["localname"]);
			}

		}
		return 1;
	}


	function mail_move_msg($msg,$tofolder) {

		/* choose your protocol */

		if($this->mail_protocol == "imap") {

			if(strtolower($tofolder) != strtolower($msg["folder"])) {
				/* check the message id to make sure that the messages still in the server */
				if(strtolower($this->_current_folder) != strtolower($msg["folder"]))
					$boxinfo = $this->mail_select_box($msg["folder"]);
		
				$this->mail_send_command("FETCH ".$msg["id"].":".$msg["id"]." BODY.PEEK[HEADER.FIELDS (Message-Id)]");
				$buffer = $this->mail_get_line();
	
				/* if any problem with the server, stop the function */
				if(eregi("^(".$this->_sid." (NO|BAD))",$buffer)) { $this->mail_error_msg = $buffer; return 0; }
	
				while(!eregi("^(".$this->_sid." OK)",$buffer)) {
					/* we need only the message id yet */
	
					if(eregi("message-id: (.*)",$buffer,$regs))
						$current_id = ereg_replace("<(.*)>","\\1",$regs[1]);
	
					$buffer = $this->mail_get_line();
				}
	
	
				/* compare the old and the new message id, if different, stop*/
				if(base64_encode($current_id) != base64_encode($msg["message-id"])) {
					$this->mail_error_msg = $error_retrieving;
					return 0;
				}
				/*if the pointer is her, no one problem occours*/

				/* otherwise, get it from server */
				$this->mail_send_command("COPY ".$msg["id"].":".$msg["id"]." \"$tofolder\"");
				$buffer = $this->mail_get_line();

				/* if any problem with the server, stop the function */
				if(!eregi("^(".$this->_sid." OK)",$buffer)) { $this->mail_error_msg = $buffer; return 0; }

				if(file_exists($msg["localname"])) {
					$currentname = $msg["localname"];
					$basename = basename($currentname);
					$newfilename = $this->user_folder."$tofolder/$basename";
					copy($currentname,$newfilename);
					unlink($currentname);
				}
				$this->mail_set_flag($msg,"\\DELETED","+");
			}

			return 1;

		} else {

			if(strtoupper($tofolder) != "INBOX" && strtolower($tofolder) != strtolower($msg["folder"])) {
				/* now we are working with POP3 */
				/* check the message id to make sure that the messages still in the server */
				if(strtoupper($msg["folder"]) == "INBOX") {
	

⌨️ 快捷键说明

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