<?php
include_once "header.php";
$xoopsOption['template_main'] = "contactusform.tpl";
include_once XOOPS_ROOT_PATH."/header.php";


$xoopsTpl->assign('xoops_module_header', "
<script>var xoopsjsurl='".XOOPS_URL."';var moduleid='".$moduleid."';var themefolder='".$themefolder."';</script>	
<script src='" . XOOPS_URL . "/modules/".$xoopsModule->getVar('dirname')."/js/formvalidation.js'></script>	
<script src='" . XOOPS_URL . "/modules/".$xoopsModule->getVar('dirname')."/js/ajaxjs.js'></script>		
<script src='" . XOOPS_URL . "/modules/neillibrary/js/dist/sweetalert.min.js'></script>	
<link rel='stylesheet' type='text/css' media='screen' href='" . XOOPS_URL . "/modules/neillibrary/js/dist/sweetalert.css'>			

");

$op=isset($_REQUEST['op'])?$_REQUEST['op']:"";
$cfid=(empty($_REQUEST['cfid']))?"":$_REQUEST['cfid'];
$status=(empty($_REQUEST['status']))?"":$_REQUEST['status'];


//刪除紀錄及解除表單IP封鎖時間(天)
autodeleterobotip();
//機器人IP判斷
if(!empty(robottimeip($type="2"))) redirect_header(XOOPS_URL,0 , _MS_SHARED75);

 


//xoops_redirect設定
function xoopsredirect($url=""){
global $uid,$xoopsUser,$xoopsModule;
if(!empty($xoopsUser)){
$page=basename(__FILE__);		
if(!empty($url)){
//base64解碼	
$url64=base64_decode($url);
redirect_header(XOOPS_URL. "/modules/".$xoopsModule->getVar("dirname")."/".$page."".$url64."" ,0 , _MS_SHARED38);   
}
}
$url=httphosturl();
//base64編碼
$url64=base64_encode($url['page']);
$page=basename(__FILE__);
//會員判斷
if(empty($xoopsUser)){ 
redirect_header(XOOPS_URL. "/user.php?xoops_redirect=/modules/".$xoopsModule->getVar('dirname')."/".$page."?url=".$url64."" ,0 , _MS_SHARED37);  
}
}

//未登入自動登入頁面帶入返回值
if($op=='usercontent' && !empty($cfid)  or !empty($_REQUEST['url'])){
xoopsredirect($url=$_REQUEST['url']);
}




switch($op){
  case "contactustosend":  	  
//機器人檢查
if(empty(alignmenturl())) redirect_header(XOOPS_URL.robottimeipwrite($type="1"),0 , _MS_SHARED75);	

if($_POST['enable']==='0' or $_POST['enable']==='1') $enablevar=true;
if(empty($enablevar)) redirect_header(XOOPS_URL.robottimeipwrite($type="1"),0 , _MS_SHARED75);	

if(empty($_POST['verification']) or  empty(defenserobot($var=$_POST['verification'],$type=2))) redirect_header(XOOPS_URL.robottimeipwrite($type="1"),0 , _MS_SHARED75); 


//機器人關閉JS判斷
if(!empty(robottimeip($type="1"))) redirect_header(XOOPS_URL,0 , _MS_SHARED75);	

//人工時間驗證
if(empty($enablevar) or  empty(robottime($var=$_POST['enable']))) redirect_header(XOOPS_URL,0 , _MS_SHARED75);	




redirect_header(XOOPS_URL.contactustosend(),0 , _MD_COMPLETETHEFORM);   
  break;
  
  
  case "replyupdate": 
//權限判斷
if(empty($isAdmin) && empty(readpermission($user_uid="",$type="1",$dbvar="neocontactusform",$where=" where `cfid` = '".$cfid."' "))){
  redirect_header(XOOPS_URL. "/modules/".$xoopsModule->getVar("dirname")."/index.php" ,0 , _MS_SHARED18);  
}
  	  
$url=replyupdate();
redirect_header(XOOPS_URL. "/modules/".$xoopsModule->getVar("dirname")."/contactusform.php".$url."" ,0 , _MS_SHARED02);   


 	 
 break;	  
  
  
  case "usercontent":  	


//權限判斷
if(empty($isAdmin) && empty(readpermission($user_uid="",$type="1",$dbvar="neocontactusform",$where=" where `cfid` = '".$cfid."' "))){
  redirect_header(XOOPS_URL. "/modules/".$xoopsModule->getVar("dirname")."/index.php" ,0 , _MS_SHARED18);  
}

if(!empty($status)){
redirect_header(XOOPS_URL. "/modules/".$xoopsModule->getVar("dirname")."/admin/contactustable.php" ,0 , _MS_SHARED02);   
}

snidup(); 	 
$contactusform=usercontent();  
    break;
  
  default:
  	  
$op="default";$contactusform=contactusform();
}

//觀看寄送人資料

function usercontent(){
global $cfid,$xoopsModule;  


//開啟聯絡我們資料表
$dbneme="neocontactusform";
$where=" where `cfid` = '".$cfid."'";   //where數值-修改
$contactuscontentlist=moduledb($dbneme,$where);



//性別
switch ($contactuscontentlist['radiog']){
 case "male":   	  
$gender=_MD_CONTACTUSTABLEPHP21;
break;
 case "female":   	  
$gender=_MD_CONTACTUSTABLEPHP22;
break;
}

if(!empty(base64Judge($var=$contactuscontentlist['texe_user']))){  //base64判斷
$texe_user=neillibrary_textprocessing($val=neillibrary_replaceHtmlEntity(base64_decode($contactuscontentlist['texe_user'])));
}else{
$texe_user=neillibrary_replaceHtmlEntity($contactuscontentlist['texe_user']);  
}

$contactusform['cfidtimebox']=sprintf(_MD_CONTACTUSTABLEPHP27,$contactuscontentlist['cfidtime']);
$contactusform['name_userbox']=sprintf(_MD_CONTACTUSTABLEPHP28,$contactuscontentlist['name_user']);
$contactusform['email_userbox']=sprintf(_MD_CONTACTUSTABLEPHP29,$contactuscontentlist['email_user']);
$contactusform['tel_userbox']=sprintf(_MD_CONTACTUSTABLEPHP30,$contactuscontentlist['tel_user']);
$contactusform['citytel_userbox']=sprintf(_MD_CONTACTUSTABLEPHP31,$contactuscontentlist['citytel_user']);
$contactusform['genderbox']=sprintf(_MD_CONTACTUSTABLEPHP32,$gender);
$contactusform['contactmattersbox']=sprintf(_MD_CONTACTUSTABLEPHP33,$contactuscontentlist['contactmatters']);
$contactusform['texe_userbox']=sprintf(_MD_CONTACTUSTABLEPHP34,$texe_user);
$contactusform['cfid']=$cfid;
$contactusform['replytextbox']=$contactuscontentlist['replytext'];

$contactusform['id']=empty($contactuscontentlist['enable']) ? '128' : '129'; // get TRUE



//是否轉寄
$contactusform['enablebox']=radioinline($class="",$name="enable",$text=array(_MD_CONTACTUSTABLEPHP40,_MD_CONTACTUSTABLEPHP41),$checked="0",$id="enablebtn");  


 return $contactusform;
}





//填寫表單
function contactusform(){

//開啟neothemesconfig資料表
$dbneme="neothemesconfig";
$where=" where `nsn` = '1'";   //where數值-修改
$neothemesconfig=moduledb($dbneme,$where);

$fieldsplit=phpconversion($conversion=$neothemesconfig['field'],$type="unserialize"); 

if(empty($fieldsplit[3]) or !empty($neothemesconfig['contactus'])){
redirect_header(XOOPS_URL  ,0 , '');    	
}



//姓名
$contactusform['name_user']=inputtext($class="",$name="name_user",$value="",$placeholder=""._MD_CONTACTENTER.""._MD_CONTACTNAME."",$id="name_user");
//E-MAIL
$contactusform['email_user']=inputtext($class="",$name="email_user",$value="",$placeholder=""._MD_CONTACTENTER.""._MD_CONTACTEMAIL."",$id="email_user");
//聯絡手機
$contactusform['tel_user']=inputtext($class="",$name="tel_user",$value="",$placeholder=""._MD_CONTACTENTER.""._MD_CONTACTTELEPHONE."",$id="tel_user");
//連絡市話
$contactusform['citytel_user']=inputtext($class="",$name="citytel_user",$value="",$placeholder=""._MD_CONTACTENTER.""._MD_CONTACTTELECITYPHONE."",$id="citytel_user");
//您的性別
//選擇操作
$radiogArr = array(); //建構陣列
$i=0;
$radiogArr[$i]['value']="male";
$radiogArr[$i]['text']=_MD_CONTACTGENDER01;
$radiogArr[$i]['id']="male";
$i++;
$radiogArr[$i]['value']="female";
$radiogArr[$i]['text']=_MD_CONTACTGENDER02;
$radiogArr[$i]['id']="female";
$i++;

foreach($radiogArr as $key=> $val){
$contactusform['radiog'].=radioinlinelist($dataid=$radiogArr[$key]['id'],$class="",$name="radiog",$text=$radiogArr[$key]['text'],$value=$radiogArr[$key]['value'],$oid="male",$id=$radiogArr[$key]['id']);
}

//聯絡事項
$dbneme="neothemesconfig";
$where="  order by  nsn  ASC";  //where數值 
$neothemesconfigbox=moduledb($dbneme,$where);

//切割陣列
$fieldsplit=cuttingfunction($cutting=$neothemesconfigbox['contactmatters'],$sign=","); 	

foreach($fieldsplit as $key=> $val){
if(!empty($val)){
$contactusform['contactmatters'].=optionselect($dataid=$val,$optionname=$val,$oid="",$value=$val);
}
}

//事項說明
$contactusform['texe_user']=inputtextarea($class="",$name="texe_user",$id="texe_user",$rows="6",$cols="80",$value=""); 	


//備註說明
$contactusform['remarkstextbox']=$neothemesconfigbox['remarkstext'];





 return $contactusform;
}

//送出聯絡我們表單
function contactustosend(){
  global $xoopsModule;		



//建構陣列-去資料表標籤
/*
$sqlinjectionArr = array(); //建構陣列
$i=0;
 $sqlinjectionArr[$i]=$_POST["name_user"];  //name_user
$i++;
 $sqlinjectionArr[$i]=$_POST["email_user"];  //email_user
$i++;
 $sqlinjectionArr[$i]=$_POST["tel_user"];  //tel_user
$i++;
 $sqlinjectionArr[$i]=$_POST["citytel_user"];  //citytel_user
$i++;
 $sqlinjectionArr[$i]=$_POST['texe_user'];  //texe_user
$i++;
//迴圈去除SQL標籤
$Arrclean=removesqlforeach($var=$sqlinjectionArr);   

 $_POST["name_user"]=$Arrclean[0];
 $_POST["email_user"]=$Arrclean[1];
 $_POST["tel_user"]=$Arrclean[2];
 $_POST["citytel_user"]=$Arrclean[3];
 $_POST['texe_user']=$Arrclean[4];
*/
$_POST=strip_tagsremovesqlArr($allowvar=array(""),$postvar=$_POST,$sqltags=array("name_user","email_user","tel_user","citytel_user","texe_user"));  



//無害化處理
if(judgeHtml($_POST["name_user"])==true or !is_numeric($_POST["tel_user"]) or judgeHtml($_POST["citytel_user"])==true or judgeHtml($_POST['texe_user'])==true or judgeEmail($_POST["email_user"])==true ){
$harmful=true;
} 


  
if($harmful==true){
redirect_header(XOOPS_URL. "/modules/".$xoopsModule->getVar("dirname")."/contactusform.php" ,0 , _MS_SHARED34);   
}

//轉換base64
$texe_user=base64_encode($_POST['texe_user']);
/*
echo $_POST['texe_user'];
die();
*/	
$_POST['radiog']=empty($_POST['radiog']) ? 'male' : $_POST['radiog']; // get TRUE
$valuesvar="(`name_user`, `email_user`, `tel_user`, `citytel_user`, `radiog`, `texe_user`, `cfidtime`, `readtrue`, `contactmatters`, `replytext`, `replytime`, `enable`, `directions`, `uid`, `transfer`) values ( '".$_POST['name_user']."', '".$_POST['email_user']."', '".$_POST['tel_user']."', '".$_POST['citytel_user']."', '".$_POST['radiog']."', '".$texe_user."', current_timestamp(), '0', '".$_POST['contactmatters']."', '', NULL, '0', '', '0', NULL)";	  


$cfid=insert($dbname="neocontactusform",$values=$valuesvar); 

//讀取最新一筆cfid
$formDB=moduledb($dbneme="neocontactusform",$where=" where cfid='".$cfid."'");

//發送通知管理員
contactusadminmail($cfid=$formDB['cfid'],$usermane=$_POST["name_user"],$getGroupid=1,$timedate="".timedate($datevar="Y-m-d H:i:s")."",$contactmatters=$_POST["contactmatters"]);   
//發送通知會員
notifyusermail($usermail=$_POST['email_user'],$usermane=$_POST['name_user'],$timedate=timedate($datevar="Y-m-d H:i:s"));
}


function replyupdate(){
 global $cfid;

	
if(empty($_POST['enable'])){  //儲存回覆內容-管理員
	
if(!empty(base64Judge($var=$_POST['texe_user']))){  //base64判斷
$texe_user=base64_decode($_POST['texe_user']); 
}else{
$texe_user=$_POST['texe_user'];  
}  

$enable=empty($_POST['respond']) ? $_POST['enable'] : '1'; // get TRUE



$setvar="set replytext='".sqlinjectionfound($var=$_POST['replytext'])."', replytime='".timedate($datevar="Y-m-d H:i:s")."' , enable='".$enable."'  where cfid='".$cfid."'";
update($dbname="neocontactusform",$set=$setvar);  
contactforreplymail($usermail="".$_POST['email_user']."",$text="".$_POST['replytext']."",$usermane="".$_POST['name_user']."",$timedate="".timedate($datevar="Y-m-d H:i:s")."",$texe_user=$texe_user);

$url="?op=usercontent&cfid=".$cfid.""; 	 


}else{  //指定會員回覆

//開啟聯絡我們資料表
$dbneme="neocontactusform";
$where=" where `cfid` = '".$cfid."'";   //where數值-修改
$contactuscontentlist=moduledb($dbneme,$where);



$setvar="set enable='".$_POST['enable']."', directions='".sqlinjectionfound($var=$_POST['directions'])."' , uid='".$_POST['uid']."' , transfer='".timedate($datevar="Y-m-d H:i:s")."'   where cfid='".$cfid."'";
update($dbname="neocontactusform",$set=$setvar);  

//開啟user資料表
$dbneme="users";
$where=" where `uid` = '".$_POST['uid']."'";   //where數值-修改
$userlist=moduledb($dbneme,$where);


//mail通知會員
noticeyusermail($cfid=$cfid,$usermail=$userlist['email'],$usermane=$userlist['uname'],$timedate=timedate($datevar="Y-m-d H:i:s"));

$url="?op=usercontent&cfid=".$cfid."&status=1"; 	 

}



	
return $url;
}












function snidup(){
  global $cfid; 
 $setvar="set readtrue='1'  where cfid='".$cfid."'";
update($dbname="neocontactusform",$set=$setvar);   
  
	}



$xoopsTpl->assign( "op" , $op) ;
$xoopsTpl->assign( "contactusform" , $contactusform) ;
$xoopsTpl->assign( "xoops_pagetitle" , _MD_TITLEM) ;


include_once XOOPS_ROOT_PATH.'/footer.php';
?> 