<?php 

//禁止直接访问
!defined('IN_SYSTEM') && exit('Access Denied');

//留言回复内容，如果未提交则设置为空
if(!isset($_POST['ReplyContent'])){
	$_POST['ReplyContent'] = '';
}

//单独自定义邮件内容，如果未提交则设置为空
if(!isset($_POST['AloneMailContent'])){
	$_POST['AloneMailContent'] = '';
}

//是否提交
if($AdminLogin->IsPost(array('ReplyMsgId', 'ReplyContent', 'MailOption', 'AloneMailContent', 'ReplyInputKey'), 'SubmitReply')){
	//来路判断
	if($AdminLogin->CheckHttpReferer(WEB_URL.'/index.php?action=reply&id=sel') == true || $AdminLogin->CheckHttpReferer('{'.WEB_URL.'/index.php\?action=reply&id=[1-9]\d*}', 1) == true){
		//验证方法
		$AdminLogin->CheckPostFun = array(
				'ReplyMsgId' => array(array('ToUtf8'), 'CheckNull'),
				'ReplyContent' => array(array('ToUtf8'), 'CheckOK'),
				'MailOption' => array(array('ToUtf8'), 'CheckOK'),
				'AloneMailContent' => array(array('ToUtf8'), 'CheckOK'),
				'ReplyInputKey' => array(array('ToUtf8'), 'CheckNull')
		);
		//验证数据
		if($AdminLogin->CheckPostValue()){
			//过滤后的用户提交的数据
			$UserPostValue = $AdminLogin->Post;
			//判断InputKey
			if($AdminLogin->CheckValue($UserPostValue['ReplyInputKey'], $_SESSION['ReplyInputKey'])){
				//创建变量用于存放待回复的留言ID字符串
				$RepMsgArr = '';
				//将留言ID字符串添加单引号
				foreach(explode(",", $UserPostValue['ReplyMsgId']) as $v){
					$RepMsgArr = $RepMsgArr."'".$v."',";
				}
				//去除多余的逗号
				$RepMsgArr = rtrim($RepMsgArr, ",");

				//连接到MYSQL并打开系统数据库
				$DataBase->open_db();
				
				//这里开始写邮件回复代码	Reply E-mail Start	------
				//查询每一条留言的Email
				$DataBase->sel('message', "WHERE `id` in (".$RepMsgArr.")");
				//待回复邮箱地址数组
				$ReplyEmailArr = array();
				//如果邮件不为空，则加入待回复邮件数组
				foreach($DataBase->Result as $v){
					if($v['email'] !== ''){
						array_push($ReplyEmailArr, $v['email']);
					}
				}
				//判断回复邮件选项
				if(in_array("SendEmail", $UserPostValue['MailOption'])){
					$SendEmail = true;
				}
				else{
					$SendEmail = false;
				}
				if(in_array("AloneSend", $UserPostValue['MailOption'])){
					$AloneSend = true;
				}
				else{
					$AloneSend = false;
				}
				//实例化smtp类
				$mail = new MySendMail();	//实例化smtp类
				if(USESSL == 1){
					$mail->setServer(SMTPSERVER, SMTPUSER, SMTPPASS, SMTPSERVERPORT, true); //设置smtp服务器，到服务器的SSL连接
				}
				else{
					$mail->setServer(SMTPSERVER, SMTPUSER, SMTPPASS, SMTPSERVERPORT); //设置smtp服务器，到服务器的SSL连接
				}
				$mail->setFrom(SMTPUSERMAIL); //设置发件人
				//发送回复通知邮件
				if($SendEmail == true){
					if($AloneSend == true){		//使用单独自定义邮件
						if(MAILTYPE == 'HTML'){
							foreach($ReplyEmailArr as $email){
								//发送邮件
								$mail->setReceiver($email); //设置收件人，多个收件人，调用多次
								$mail->setMail('您的留言已经被回复', $UserPostValue['AloneMailContent']); //设置邮件主题、内容
								if($mail->sendMail() == true){
									$SendmailRetrun = true;
								}
								else{
									$SendmailRetrun = false;
								}
							}
						}
						elseif(MAILTYPE == 'TXT'){
							foreach($ReplyEmailArr as $email){
								//发送邮件
								$mail->setReceiver($email); //设置收件人，多个收件人，调用多次
								$mail->setMail('您的留言已经被回复', $UserPostValue['AloneMailContent']); //设置邮件主题、内容
								if($mail->sendMail() == true){
									$SendmailRetrun = true;
								}
								else{
									$SendmailRetrun = false;
								}
							}
						}
					}
					else{	//使用系统邮件
						if(MAILTYPE == 'HTML'){
							foreach($ReplyEmailArr as $email){
								//发送邮件
								$mail->setReceiver($email); //设置收件人，多个收件人，调用多次
								$mail->setMail('您的留言已经被回复', SysReplyHtmlMail()); //设置邮件主题、内容
								if($mail->sendMail() == true){
									$SendmailRetrun = true;
								}
								else{
									$SendmailRetrun = false;
								}
							}
						}
						elseif(MAILTYPE == 'TXT'){
							foreach($ReplyEmailArr as $email){
								//发送邮件
								$mail->setReceiver($email); //设置收件人，多个收件人，调用多次
								$mail->setMail('您的留言已经被回复', SysReplyHtmlMail()); //设置邮件主题、内容
								if($mail->sendMail() == true){
									$SendmailRetrun = true;
								}
								else{
									$SendmailRetrun = false;
								}
							}
						}
					}
				}
				//这里是邮件回复代码结束	Reply E-mail End	------				
				
				//回复每一条留言
				if($DataBase->up('message', "`reply` = '".$UserPostValue['ReplyContent']."'", "WHERE `id` in (".$RepMsgArr.")")){
					ShowMessage("回复成功！", 1, "", "", WEB_URL.'/index.php', 1);
					if(isset($SendmailRetrun)){
						if($SendmailRetrun == false){
							echo "<script>alert(\"邮件发送失败！\");</script>";
						}
					}
				}
				else{
					ShowMessage("留言回复失败！请检查原因！", 2, "", "", WEB_URL.'/index.php', 1);	//回复失败
				}

				//关闭数据库连接
				$DataBase->close_dblink();
			}
			else{
				ShowMessage("来路不正确！", 2, "", "", WEB_URL.'/index.php', 1);	//InputKey不正确
			}
		}
		else{
			print_r($AdminLogin->ErrorInfo);
			ShowMessage("您提交的数据不合法！", 2, "", "", WEB_URL.'/index.php', 1);	//验证未通过
		}
	}
	else{
		ShowMessage("来路不正确！", 2, "", "", WEB_URL.'/index.php', 1);	//来路不正确
	}
	//销毁inputkey确保只能使用一次
	$_SESSION['ReplyInputKey'] = '';
}
else{
	ShowMessage("表单未提交！", 2, "", "", WEB_URL.'/index.php', 1);	//表单未提交
}

?>