<?php
!function_exists('readover') && exit('Forbidden');
include_once(D_P.'data/bbscache/mail_config.php');

$M_db= new Mailconfig(
	array(
		'method'=> $ml_mailmethod,
		'host'	=> $ml_smtphost,
		'port'	=> $ml_smtpport,
		'auth'	=> $ml_smtpauth,
		'from'	=> $ml_smtpfrom,
		'user'	=> $ml_smtpuser,
		'pass'	=> $ml_smtppass
	)
);

Class Mailconfig {
	var $S_method = 1;
	var $smtp;
	function Mailconfig($smtp=array()){
		$this->S_method = $smtp['method'];
		if ($this->S_method == 1){
			//²»ÓÃÉèÖÃ
		} elseif ($this->S_method == 2){
			$this->smtp['host'] = $smtp['host'];
			$this->smtp['port'] = $smtp['port'];
			$this->smtp['auth'] = $smtp['auth'];
			$this->smtp['from'] = $smtp['from'];
			$this->smtp['user'] = $smtp['user'];
			$this->smtp['pass'] = $smtp['pass'];
		} elseif ($this->S_method == 3){
			//hacker
		} else {
			//hacker
		}
	}
}
function sendemail($toemail,$subject,$message,$additional){
	global $M_db,$db_bbsname,$rg_name,$db_bbsurl,$windid,$winduid,$timestamp,$regpwd,$manager,$db_ceoemail,$fromemail,$pwd_user,$submit,$receiver,$old_title,$fid,$tid,$pwuser,$db_charset,$sendtoname;
	!$fromemail && $fromemail = $db_ceoemail;
	require_once GetLang('email');
	$lang[$subject]		&& $subject    = $lang[$subject];
	$lang[$message]		&& $message    = $lang[$message];
	$lang[$additional]	&& $additional = $lang[$additional];
	$subject = Char_cv($subject);
	$message = Char_cv($message);
	if ($M_db->S_method == 1){
		if (@mail($toemail,$subject,$message,$additional)){
			return 1;
		} else{
			return 0;
		}
	} elseif ($M_db->S_method == 2){
		if(!$fp=fsockopen($M_db->smtp['host'],$M_db->smtp['port'],&$errno,&$errstr,30)){
			return false;
		}
		if(substr(fgets($fp,512),0,3)!="220"){
			return false;
		}
		if($M_db->smtp['auth']) {
			fwrite($fp,"EHLO phpwind\r\n");
			while($rt=strtolower(fgets($fp,512))){
				if(strpos($rt,"-")!==3 || empty($rt)){
					break;
				}elseif(strpos($rt,"2")!==0){
					return false;
				}
			}
			fwrite($fp, "AUTH LOGIN \r\n");
			if(substr(fgets($fp,512),0,3) != 334){
				return false;
			}
			fwrite($fp, base64_encode($M_db->smtp['user'])." \r\n");
			if(substr(fgets($fp,512),0,3) != 334){
				return false;
			}
			fwrite($fp, base64_encode($M_db->smtp['pass'])." \r\n");
			if(substr(fgets($fp,512),0,3) != 235){
				return false;
			}
		} else {
			fwrite($fp, "HELO phpwind\r\n");
		}
		$from = $M_db->smtp['from'];
		$from = preg_replace("/.*\<(.+?)\>.*/", "\\1", $from);
		fwrite($fp, "MAIL FROM: <$from>\r\n");
		if(substr(fgets($fp,512),0,3) != 250){
			return false;
		}
		fwrite($fp, "RCPT TO: <$toemail>\r\n");
		if(substr(fgets($fp,512),0,3) != 250){
			return false;
		}
		fwrite($fp, "DATA\r\n");
		if(substr(fgets($fp,512),0,3) != 354){
			return false;
		}
		$subject = str_replace("\n",' ',$subject);
		$msg  = "Date: ".Date("r")."\r\n";
		$msg .= "From: =?$db_charset?B?.".base64_encode($windid)."?=<$fromemail>\r\n";
		$msg .= "To: =?$db_charset?B?".base64_encode($sendtoname)."?=<$toemail>\r\n";
		$msg .= "Subject: =?$db_charset?B?".base64_encode($subject)."?=\r\n";
		$msg .= "X-mailer: PhpWind mailer\r\n";
		$msg .= "Mime-Version: 1.0\r\n";
		$msg .= "Content-Type: text/plain;\r\n";
		$msg .= "\tcharset=\"$db_charset\"\r\n";
		$msg .= "Content-Transfer-Encoding: base64\r\n\r\n";
		$msg .= base64_encode($message)."\r\n.\r\n";
		fwrite($fp, $msg);
		fwrite($fp, "QUIT\r\n");
		fclose($fp);
		return true;
	} elseif ($M_db->S_method == 3){
		//hacker
	} else {
		//hacker
	}
}
?>