<?php
// 隐含变量 ls2 lssend7062120331
session_start();
require_once('./ls_admin/inc/ls_syscode.php');
require_once('./ls_admin/inc/constant.php');
$hidden=$_POST['business'];
$str=trim(strtolower($_POST['Comments']));
if(strtolower($_POST['code'])!=strtolower($_SESSION['code'])){
	if($hidden=="select.php")
	{
	echo "<script>
	alert('verify code is error!');
	history.go(-2);
	</script>";
	}
	else{
		echo "<script>
	alert('verify code is error!');
	history.go(-1);
	</script>";
	}
}
foreach($filterWords as $v){
	if($str!=$v)
	{
		if($_POST['ls2']=="lssend7062120331" ){ 
			$ls_msg=sendmail($_POST);
			$b=false;
		}
	}
	else{
		if($hidden=="select.php")
		{
			echo "<script>
			alert('Invalid message information.');
			history.go(-2);
			</script>";
		}
		else{
		echo "<script>
		alert('Invalid message information.');
		history.go(-1);
		</script>";}
	}
}
/*if(strtolower($_POST['code'])!=strtolower($_SESSION['code'])){
	echo "<script>
	alert('Verification code error.');
	history.go(-1);
	</script>";
}else{
	if($_POST['ls2']=="sendmail" || $_POST['submit']!=""){
		$ls_msg=sendmail($_POST);
	}
}*/

//------------------------------------------------------
//  发送邮件的函数，适用于webmotor,从数据库获得反馈邮箱
//　在一般php文件中，用于php文件头的下面，适用于一个form反馈
//　使用方法：sendmail($_POST)  返回相关信息
//------------------------------------------------------
function sendmail($postvars){

require_once('./ls_admin/inc/conn.php');
$opendb=new mydb();
require_once('./ls_admin/inc/char_set.php');
require('./ls_admin/inc/constant.php');
global $sendmailOKMsg,$sendmailFailMsg,$sendmailFailMsgcn,$sendmailOKMsgcn;

if($lang != "en"){
	$lan = $lang_cn;
}else{
	
	$lan = $lang_en;
}

$Back=$HTTP_REFERER;      //返回路径
$Subject="Information from ".$_SERVER['HTTP_HOST'];  // 标题
$gifo = new get_gust_info();
$config_sql="select * from ls_config ";
$config_query=$opendb->query($config_sql);
$config_rows=mysql_fetch_assoc($config_query);
$emailAddress=$config_rows['feedbackEmail'];
		//禁用词
		$select_sql="select * from ls_banword  ";
		$select_query=$opendb->query($select_sql);
		$select_rows=mysql_fetch_assoc($select_query);
		
		$banword_vars=explode(',',$select_rows['banword']);
		$nobanword=0;
		$message=trim(strtolower($_POST['Comments']));
		//禁用ip
		$selectip_sql="select * from ls_config  ";
		$selectip_query=$opendb->query($selectip_sql);
		$selectip_rows=mysql_fetch_assoc($selectip_query);
		$banwordip_vars=explode(',',$selectip_rows['shieldIP']);
		$ip=$gifo->Getip();
	
		$nobanwordip=0;
		$ip=$gifo->Getip();
		$a=strrpos($ip,'.');
		$delip3= substr($ip,0,$a+1);
		$delip3star=$delip3.'*';
		
		$selectip_sql="select * from ls_config";
		$selectip_query=$opendb->query($selectip_sql);
		$selectip_rows=mysql_fetch_assoc($selectip_query);
		//循环判断要警用的ip是否存在于所在网段
		$allip=trim($selectip_rows['shieldIP']);
		$allip3star=$allip.'*';
		
		if(strpos($allip,$delip3)!==false)
		{
			if(strpos($allip,$delip3star)===false)
			{
				if(strpos($allip,$ip)===false)
				{
					$nobanwordip=0;
				}
				else{
						$nobanwordip=1;
						echo "<script>
						alert('You can not leave a message.');
						history.go(-1);
						</script>";
				}
			}
			else{
						$nobanwordip=1;
						echo "<script>
						alert('You can not leave a message.');
						history.go(-1);
						</script>";
			}
		}  
		
				/*禁用词*/
		else if($select_rows!=""){
			for($i=(count($banword_vars)-1);$i>=0;$i--)
			{	
					$varname1=trim($banword_vars[$i]);
					if($varname1=='')continue;
					if($varname1!=''){
					if(strpos($message,$varname1)!==false)
					{
						$nobanword=1;
						echo "<script>
						alert('含有违禁词，无法添加留言。');
						history.go(-1);
						</script>";
						break;
					}  
				}
			}
		}
		else 
		{
				$nobanwordip=0;
		}
	if(count($postvars)!=0)  //pass vars fron URL
	{
		while(list($akey,$avalue)=each($postvars))
		{
			if(strpos($akey,"mail")!==false)  //获得客户邮件地址
			{$email_from=trim($avalue);}
    		switch ($akey){
    		case "Mr":
       		$Mr=$avalue;
       		break;
    		case "Name":
        	$name=$avalue;
			
       		break;
    		case "Submit":
        	break;
    		case "submit":
        	break;
    		case "ec_cid":
        	break;
    		case "guid":
        	break;
    		case "ls2":
        	break;
			case "code":
        	break;
			
   			default:
			if(strpos($akey,"select")===0||strpos($akey,"button")===0){break;}
			
	    	$Content.=urldecode($akey).":  ";
	    	$Content.=urldecode($avalue)."<br/>\r\n";
        	break;
    		}
		}
	}
	
	$headers = "MIME-Version:1.0\r\n"; 
	$headers .= "Content-type:text/html; charset=utf-8" . "\r\n"; 
	$headers .= "From:$email_from"; 
	$Content .="<br/>\r\n".date('Y-m-d');
	
	if(mail($emailAddress,$Subject,$Content, $headers))
	{
		$openck=new cktb();
		if($openck->checktable("ls_pro_feedback","CREATE TABLE IF NOT EXISTS `ls_pro_feedback` (
  `ID` int(4) NOT NULL AUTO_INCREMENT,
  `requirement` text,
  `name` varchar(100) DEFAULT NULL,
  `company` varchar(200) DEFAULT NULL,
  `tel` varchar(100) DEFAULT NULL,
  `email` varchar(150) DEFAULT NULL,
  `date` varchar(200) DEFAULT NULL,
  `proName` varchar(1000) DEFAULT NULL,
  `path` varchar(1000) DEFAULT NULL,
  `address` varchar(500) DEFAULT NULL,
  `fax` varchar(100) DEFAULT NULL,
  `http` varchar(100) DEFAULT NULL,
  `proID` int(4) DEFAULT NULL,
  `ip` varchar(50) DEFAULT NULL,
  `explorer` varchar(100) DEFAULT NULL,
  `home` varchar(300) DEFAULT NULL,
  `system` varchar(50) DEFAULT NULL,
  `enable` int(1) DEFAULT '1',
  `cateNameID` int(4) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;"))	$ls_msg = $ls_msg .$openck->ls_msg;

		foreach($postvars  as $akey => $avalue){
			
			switch (ucfirst(urldecode($akey))){
			case "Subject":
       		$subject=$avalue;
       		break;
    		case "Comments":
       		$comments=$avalue;
       		break;
    		case "Name":
        	$username=$avalue;
       		break;
    		case "Companyname":
			$companyname=$avalue;
        	break;
			case "Address":
			$address=$avalue;
        	break;
    		case "Tel":
			$tel=$avalue;
        	break;
			case "Fax":
			$fax=$avalue;
        	break;
    		case "Email":
			$mail=$avalue;
        	break;
			case "Web":
			$http=$avalue;
        	break;
    		case "Path":
			$path=$avalue;
        	break;
    		case "Pro_id":
			$pro_id=$avalue;
        	break;
			case "Guid":
			$guid=$avalue;
        	break;
			case "Business":
			$business=$avalue;
        	break;
    		}
		}
		//反馈写入数据库
		require_once('ls_admin/inc/conn.php');
		$opendb=new mydb();
		require_once('ls_admin/inc/char_set.php');
		require_once('ls_admin/inc/ls_syscode.php');
		
		date_default_timezone_set('Asia/Shanghai');//设置时区为中国时间
		
		
		$ip_home = '';
		$ipadds = $gifo->Getaddress();
		foreach($ipadds[0] as $value){
			 $ip_home .= " ".iconv("utf-8","utf-8",$value);    
		}
		if($ip_home == NULL)
			 $ip_home = "本地";
		if($nobanword==0 && $nobanwordip==0)	{
		if($pro_id != null){
			$insert_pro_feedback_sql=sprintf("insert into ls_pro_feedback(requirement,name,company,tel,email,proName,path,date,ip,explorer,home,system,proID,cateNameID) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
						GetSQLValueString($comments,"text"),
						GetSQLValueString($username,"text"),
						GetSQLValueString($companyname,"text"),
						GetSQLValueString($tel,"text"),
						GetSQLValueString($mail,"text"),
						GetSQLValueString($subject,"text"),
						GetSQLValueString($path,"text"),
						GetSQLValueString(date("Y-m-d H:i:s"),"text"),
						GetSQLValueString($gifo->Getip(),"text"),
						GetSQLValueString($gifo->GetBrowser()." - ".$gifo->GetLang(),"text"),
						GetSQLValueString($ip_home,"text"),
						GetSQLValueString($gifo->GetOs(),"text"),
						GetSQLValueString($pro_id,"int"),
						GetSQLValueString($business,"int")
						);
			$insert_query=$opendb->query($insert_pro_feedback_sql);

		}else{
			
			$insert_pro_feedback_sql=sprintf("insert into ls_pro_feedback(requirement,name,company,tel,http,fax,email,proName,path,address,date,ip,explorer,home,system,proID,cateNameID) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
						GetSQLValueString($comments,"text"),
						GetSQLValueString($username,"text"),
						GetSQLValueString($companyname,"text"),
						GetSQLValueString($tel,"text"),
						GetSQLValueString($http,"text"),
						GetSQLValueString($fax,"text"),
						GetSQLValueString($mail,"text"),
						GetSQLValueString($subject,"text"),
						GetSQLValueString($path,"text"),
						GetSQLValueString($address,"text"),
						GetSQLValueString(date("Y-m-d H:i:s"),"text"),
						GetSQLValueString($gifo->Getip(),"text"),
						GetSQLValueString($gifo->GetBrowser()." - ".$gifo->GetLang(),"text"),
						GetSQLValueString($ip_home,"text"),
						GetSQLValueString($gifo->GetOs(),"text"),
						GetSQLValueString($pro_id,"int"),
						GetSQLValueString($business,"int")
						);
			$insert_query=$opendb->query($insert_pro_feedback_sql);
		}
		//return($sendmailOKMsgcn==""?$lan['sendmailOKMsg']:$sendmailOKMsgcn);//成功 
		return($lang=="en"?$lan['sendmailOKMsg']:$sendmailOKMsgcn);//成功 
		}
	}
	else
	{
		//return($sendmailFailMsgcn==""?$lan['sendmailFailMsg']:$sendmailFailMsgcn);// 失败给提示
		return($lang=="en"?$lan['sendmailFailMsg']:$sendmailFailMsgcn);// 失败给提示
	}
}


/*------------------------------------------------------
//  jmailer是jmail类的发送邮件的函数
//  入口参数与相同
//  $header=="lanscend" 使用lanscend官方邮箱发送
//　 14-3-5 赵
------------------------------------------------------*/
function jmailer($emailAddress,$Subject,$Content, $headers="", $addressee="", $attachedFile=array())
{
 date_default_timezone_set("Asia/Shanghai");//设定时区东八区
$jmail = new COM('JMail.Message')  or die('无法调用Jmail组件');
$jmail->silent = true; //屏蔽例外错误
$jmail->charset = 'gb2312'; //否则中文会乱码
$jmail->From = '1310824407@qq.com'; //这里理论上是可以不与发件人地址相同的，但用163的试了几次都不行，改成一样的就可以了
$jmail->FromName = $_SERVER['HTTP_HOST'];
$jmail->AddRecipient($emailAddress); //可添加多个邮件接受者
$jmail->Subject =  $Subject;
$jmail->Body = $Content; 
$jmail->MailServerUserName = '1310824407@qq.com'; //发信邮件账号
$jmail->MailServerPassword = 'ls2120331'; //账户的密码

try{
     $email = $jmail->Send('smtp.qq.com');
     if($email) 
	 	return true;
     else  
		 echo '邮件发送失败！错误原因:' ;
	} catch (Exception $e){
     		echo $e->getMessage();
		}


}

/*------------------------------------------------------
//  pmailer 模拟mail的发送邮件的函数
//　使用phpmailer类，入口参数与相同
//  $header=="lanscend" 使用lanscend官方邮箱发送
//　  14-3-5 赵
------------------------------------------------------*/
function pmailer($emailAddress,$Subject,$Content, $headers="", $addressee="", $attachedFile=array())
{
 date_default_timezone_set("Asia/Shanghai");//设定时区东八区

// Include PHP Mailer Class
require_once("mail/class.phpmailer.php");
//include("mail/class.smtp.php"); 
$mail = new PHPMailer();
$mail->CharSet = "UTF-8";
$mail->Encoding = "base64";     
$mail->IsSMTP();
$mail->SMTPDebug  = 1;
$mail->SMTPAuth   = true;
$mail->Port = 25;
if($headers=="lanscend")
{
	$mail->Host = "smtp.qq.com";
	$mail->Username   = "1310824407@qq.com";
	$mail->Password = "ls2120331";
}
else
{
	$mail->Host = "smtp.ym.163.com";
	$mail->Username   = "sendmail@lanscend.com";
	$mail->Password = "ls2120331";
}
if($addressee=="")
{$addressee=substr($emailAddress,0,strpos($emailAddress,"@"));}
$mail->SetFrom($mail->Username, "lanscend mailer");

if($attachedFile[0] !='')
{
	$mail->addAttachment("$attachedFile[0]");  // 增加附件
}
  
$mail->Subject= $Subject;
$mail->MsgHTML($Content);
$mail->AddAddress($emailAddress, $addressee);
if($mail->Send())
{return true;}
else
{echo '邮件发送失败！错误原因:'.$mail->ErrorInfo;}
}

/*------------------------------------------------------
//  邮件附件处理的函数
//　由使用pmailer
//  $name 附件名称，默认是文件名， doc类型
//　author: 
------------------------------------------------------*/
function addAttachment($path, $name = "", $encoding = "base64", $type = "doc/docx") {
        if(!@is_file($path))
        {
            $this->error_handler(sprintf("Could not access [%s] file", $path));
            return false;
        }

        $filename = basename($path);
        if($name == "")
            $name = $filename;

        // Append to $attachment array
        $cur = count($this->attachment);
        $this->attachment[$cur][0] = $path;
        $this->attachment[$cur][1] = $filename;
        $this->attachment[$cur][2] = $name;
        $this->attachment[$cur][3] = $encoding;
        $this->attachment[$cur][4] = $type;
        $this->attachment[$cur][5] = false; // isStringAttachment
        $this->attachment[$cur][6] = "attachment";
        $this->attachment[$cur][7] = 0;

        return true;
    }
	
?>