<?php
if(!defined('R_P')) exit('Access Denied');
include_once(R_P . "/include/class.phpmailer.php");
class sendmail{
	var $username = '';
	var $domain = '';
	var $mailto = '';
	var $passport = '';
	var $_mails = array();
	function sendmail($username, $mailto){
		global $main_domain, $_SET;
		$this->username = $username;
		$this->mailto = $mailto;
		$this->domain = $main_domain;
		$this->passport = $_SET['passport_key'];
		require_once(R_P . "/config/mail_config.php");
		if(empty($_mails)){
			$this->_mails[] = array(
				'smtpserver' => $_SET['smtpserver'],
				'smtpserverport' => $_SET['smtpserverport'],
				'smtpuser' => $_SET['smtpuser'],
				'smtppass' => $_SET['smtppass'],
			);
		}else{
			$this->_mails = $_mails;
		}
	}

	function op_reg($uid, $session){
		$file = R_P . "/config/mail/reg.html";
		$subject = "在{$this->domain}的注册";
		$info =array(
			'uid' => $uid,
			'session'=> $session,
			'email'=>$this->mailto
		);
		$verify = qbase::authcode(qbase::array2string($info), "ENCODE");
		$url = W_P . "user.php?ac=check&verify=" . urlencode($verify);
		$msg = "<p>点击下面的链接验证Email</p>";
		$msg .= "<p><a href='{$url}' target='_blank'>{$url}</a></p>";
		return $this->send($subject, $msg, $file);
	}
	
	function op_password($uid){
		$file = R_P . "/config/mail/get_password.html";
		$subject = "在{$this->domain}使用找回密码";
		$info =array(
			'uid' => $uid,
			'email'=> $this->mailto,
			'dateline'=> $_SERVER['REQUEST_TIME']
		);
		$info['verify'] = substr(md5(qbase::array2string($info) . $this->passport), 0, 12);
		$verify = qbase::authcode(qbase::array2string($info), "ENCODE");
		$url = W_P . "user.php?ac=password&step=3&verify=" . urlencode($verify);
		$msg = "<p>点击下面的链接修改密码</p>";
		$msg .= "<p><a href='{$url}' target='_blank'>{$url}</a></p>";
		return $this->send($subject, $msg, $file);		
	}

	function op_guestbook(){
		global $_SET;
		$file = R_P . "/config/mail/guestbook_reply.html";
		$subject = "管理员回复了您在{$_SET['title']}留言";
		$url = W_P . "index.php?ac=guestbook";
		$msg = "<p>点击下面的链接查看回复</p>";
		$msg .= "<p><a href='{$url}' target='_blank'>{$url}</a></p>";
		return $this->send($subject, $msg, $file);
	}

	function send($subject, $msg, $file, $debug = false){
		global $_SET;
		if(strtolower($_SET['def_charset']) == 'utf-8') $subject = "=?UTF-8?B?".base64_encode($subject)."?=";
		$index = count($this->_mails) > 1 ? array_rand($this->_mails, 1) : 0;
		$mail = new PHPMailer();
		$body = $this->replace($file, $msg);
		$body = eregi_replace("[\]",'',$body);
		$mail->IsSMTP();
		$mail->SMTPAuth   = true;                  // enable SMTP authentication
		$mail->SMTPSecure = "";                 // sets the prefix to the servier
		$mail->Host = $this->_mails[$index]['smtpserver'];      // sets GMAIL as the SMTP server
		$mail->Port = $this->_mails[$index]['smtpserverport'];                   // set the SMTP port for the GMAIL server
		$mail->Username   = $this->_mails[$index]['smtpuser'];  // GMAIL username
		$mail->Password   = $this->_mails[$index]['smtppass'];            // GMAIL password
		$mail->From       = $this->_mails[$index]['smtpuser'];
		$mail->FromName   = "webmater";
		$mail->Subject    = $subject;
		$mail->AltBody    = "To view the message, please use an HTML compatible email viewer! ";
		$mail->WordWrap   = 50;
		$mail->MsgHTML($body);
		$mail->AddAddress($this->mailto, $this->username);
		$mail->IsHTML(true);
		$mail->CharSet = $_SET['def_charset'];
		$mail->SMTPDebug = $debug;
		return $mail->Send();
	}
	
	function replace($file, $msg = ''){
		global $_SET;
		$content = qbase::readfromfile($file);
		$param = array(
			'charset'=>	$_SET['def_charset'],
			'username'=>	$this->username,
			'dateline'=> qbase::formatTime("Y-n-d H:i", time()),
			'site'=>$_SET['title'],
			'content'=>$msg,
			'site_url'=>W_P
		);
		foreach($param as $key=>$val){
			$content = str_replace("{" . $key. "}", $val, $content);
		}
		return $content;
	}
}
?>