<?php
if (!defined('USING_SYSTEM')) exit;

class p105_sxExe extends xIExecutor {
	
	public function main() {
		global $_SLEX;
		if(isset($_GET['get']) && ($_GET['get']=='ajax')) {
			// --- 发信测试 ---
			if($_GET['do'] == 'sendTest') {
				include_once(__ROOT__.'exts/PHPMailer_5.2.4/class.phpmailer.php');
				$mail = new PHPMailer();
				$mail->CharSet = 'UTF-8';
				$mail->IsSMTP();
				$mail->SMTPAuth = true;
				if($_POST['ssl']=='1') $mail->SMTPSecure = 'ssl';
				$mail->SetFrom($_POST['from'], $_POST['from']);
				$mail->AddReplyTo($_POST['from'], $_POST['from']);
				$mail->Port = $_POST['port'];
				$mail->Host = $_POST['smtp'];
				$mail->Username = $_POST['username'];
				$mail->Password = $_POST['password'];
				$mail->Subject = $_POST['title'];
				$mail->AltBody = '';
				$mail->MsgHTML($_POST['html']);
				$mail->AddAddress($_POST['to'], $_POST['to']);
				if(!$mail->Send()) {
					echo json_encode(array(
						'result' => '0',
						'msg' => $mail->ErrorInfo
					));
				} else {
					echo json_encode(array(
						'result' => '1'
					));
				}
			}
		} else {
			// --- 写入SMTP信息 ---
			$tips = '';
			if(isset($_POST['smtpSslTxt'])) {
				$_SLEX['smtp']['ssl'] = ($_POST['smtpSslTxt']=='1')?true:false;
				$_SLEX['smtp']['port'] = $_POST['smtpPortTxt'];
				$_SLEX['smtp']['smtp'] = $_POST['smtpSmtpTxt'];
				$_SLEX['smtp']['username'] = $_POST['smtpUsernameTxt'];
				$_SLEX['smtp']['password'] = $_POST['smtpPasswordTxt'];
				$_SLEX['smtp']['from'] = $_POST['smtpFromTxt'];
				$slexInfo = file_get_contents(__ROOT__.'confs/slex.i.php');
				$slexInfo = preg_replace('/_SLEX\[\'smtp\'\].+?\);/s', "_SLEX['smtp'] = array(
'ssl' => ".($_SLEX['smtp']['ssl']?'true':'false').",
'port' => ".$_SLEX['smtp']['port'].",
'smtp' => '".$_SLEX['smtp']['smtp']."',
'username' => '".$_SLEX['smtp']['username']."',
'password' => '".$_SLEX['smtp']['password']."',
'from' => '".$_SLEX['smtp']['from']."'
);", $slexInfo);
				file_put_contents(__ROOT__.'confs/slex.i.php', $slexInfo);
				$tips = '您的设置已经成功被保存';
			}
			include_once(__ROOT__.'admin/ui.p105.php');
		}
	}
	
	public function onError($result) {
		
	}
	
}
