<?php
require_once('../ls_admin/inc/ls_syscode.php');

if(strtolower($_POST['code'])!=strtolower($_SESSION['code'])){
	echo "<script>
	alert('Verification code error.".$code."');
	history.go(-1);
	</script>";
}else{
	if($_POST['ls2']=="sendmail" || $_POST['submit']!=""){
		$ls_msg=sendmail($_POST);
	}
}

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

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

$Back=$HTTP_REFERER;      /* 返回路径 */
$Subject="Information from ".$_SERVER['HTTP_HOST'];  /* 标题*/

$config_sql="select * from ls_config ";
$config_query=$opendb->query($config_sql);
$config_rows=mysql_fetch_assoc($config_query);
$emailAddress=$config_rows['feedbackEmail'];

	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,
  `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 (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 "Mail":
			$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');//设置时区为中国时间
		
		if($path == NULL)
			$path = $subject;
		
		$gifo = new get_gust_info();
		$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($pro_id != null){
			$insert_pro_feedback_sql=sprintf("insert into ls_pro_feedback(requirement,name,company,tel,email,path,date,ip,explorer,home,system,proID,cateNameID) values (%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($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,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)",
						GetSQLValueString($comments,"text"),
						GetSQLValueString($username,"text"),
						GetSQLValueString($companyname,"text"),
						GetSQLValueString($tel,"text"),
						GetSQLValueString($http,"text"),
						GetSQLValueString($fax,"text"),
						GetSQLValueString($mail,"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);
		}
		
		if($lang=="cn")
		{
			return($sendmailOKMsgcn==""?"谢谢，给我们的反馈信息已经发送，我们尽快和您取得联系":$sendmailOKMsgcn);//成功 
		}
		else
		{
			return($sendmailOKMsg==""?"THANK YOU FOR CONTACTING US, OUR CUSTOMER SERVICE REPRESENTATIVE WILL CONTACT YOU WITHIN 24 HOURS. THANK YOU AGAIN":$sendmailOKMsg);//成功 
			
		}
		
	}
	else
	{
		if($lang=="cn")
		{
			return($sendmailFailMsgcn==""?"抱歉，您的反馈信息未能发出":$sendmailFailMsgcn);// 失败给提示
		}
		else
		{
			return($sendmailFailMsg==""?"SORRY, YOUR MESSAGE SENDING FAILURE":$sendmailFailMsg);
		}
	}
}

?>