<?php
include_once "header.php";
$xoopsOption['template_main'] = "contactusform.tpl";
include_once XOOPS_ROOT_PATH."/header.php";
include_once XOOPS_ROOT_PATH."/modules/neillibrary/function/cityadminfunction.php"; //引入城市鄉鎮管理function	

$xoopsTpl->assign('xoops_module_header', "
<script>var xoopsjsurl='".XOOPS_URL."';var moduleid='".$moduleid."';var themefolder='".$themefolder."';</script>	
<script src='" . XOOPS_URL . "/modules/neillibrary/js/customize.js'></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(); 	 
$cform=usercontent();  
    break;
  
  default:
  	  
$op="default";$cform=contactusform();
}

//觀看寄送人資料

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


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



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

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

$cform['cfidtimebox']=sprintf(_MD_CONTACTUSTABLEPHP27,$clist['cfidtime']);
$cform['name_userbox']=sprintf(_MD_CONTACTUSTABLEPHP28,$clist['name_user']);
$cform['email_userbox']=sprintf(_MD_CONTACTUSTABLEPHP29,$clist['email_user']);
$cform['tel_userbox']=sprintf(_MD_CONTACTUSTABLEPHP30,$clist['tel_user']);
if(!empty($clist['citytel_user'])) $cform['citytel_userbox']=sprintf(_MD_CONTACTUSTABLEPHP31,$clist['citytel_user']);
$cform['genderbox']=sprintf(_MD_CONTACTUSTABLEPHP32,$gender);
if(!empty($clist['contactmatters'])) $cform['contactmattersbox']=sprintf(_MD_CONTACTUSTABLEPHP33,$clist['contactmatters']);

$cform['texe_userbox']=sprintf(_MD_CONTACTUSTABLEPHP34,$texe_user);
$cform['cfid']=$cfid;
$cform['replytextbox']=$clist['replytext'];

//聯絡地址
$cform['addressbox']=!empty(neillibrary_addresscombination($address=$clist['address'])) ? sprintf(_MD_CONTACTUSTABLEPHP47,neillibrary_addresscombination($address=$clist['address'])) : ''; 

//表單欄位設定
$cform['settings']=phpconversion($conversion=$xoopsModuleConfig['neothemesadmin12'],$type="unserialize");

//儲存自訂表單
$cform['form']=phpconversion($conversion=$clist['field'],$type="unserialize");



//就讀學校
if(!empty($cform['settings']['3']) &&  !empty($cform['form']['1'])){
$cform['school_field']=sprintf(_MD_CONTACTENTER_04,$cform['form']['1']);
}

//就讀年級
if(!empty($cform['settings']['5']) &&  !empty($cform['form']['2'])){
$cform['grade_field']=sprintf(_MD_CONTACTENTER_05,$cform['form']['2']);
}


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



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


 return $cform;
}





//填寫表單
function contactusform(){
global $xoopsModuleConfig;	


//開啟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 , '');    	
}


//縣市鄉鎮
$cform=neillibrary_countiesajaxmenu($cityid="",$status="",$show);
//聯絡地址
$cform['addressbox']=inputtext($class="",$name="of_cityid2",$value="",$placeholder=""._MS_SHARED21.""._MS_SHARED267."",$id="of_cityid2");

//就讀學校欄位
$cform['school_field']=inputtext($class="",$name="form_1",$value="",$placeholder=_MS_SHARED21._MD_CONTACTENTER_02,$id="school_field");

//表單欄位設定
$cform['settings']=phpconversion($conversion=$xoopsModuleConfig['neothemesadmin12'],$type="unserialize");



//下拉選單(輸入年級欄位)
foreach(explode(',', $cform['settings']['7']) as $key=> $val){
$cform['grade_select'].=optionselect($dataid=$val,$optionname=$val,$oid="",$value=$val);
}


//姓名
$cform['name_user']=inputtext($class="",$name="name_user",$value="",$placeholder=""._MD_CONTACTENTER.""._MD_CONTACTNAME."",$id="name_user");
//E-MAIL
$cform['email_user']=inputtext($class="",$name="email_user",$value="",$placeholder=""._MD_CONTACTENTER.""._MD_CONTACTEMAIL."",$id="email_user");
//聯絡手機
$cform['tel_user']=inputtext($class="",$name="tel_user",$value="",$placeholder=""._MD_CONTACTENTER.""._MD_CONTACTTELEPHONE."",$id="tel_user");
//連絡市話
$cform['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){
$cform['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)){
$cform['contactmatters'].=optionselect($dataid=$val,$optionname=$val,$oid="",$value=$val);
}
}

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


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








 return $cform;
}

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

$_POST=strip_tagsremovesqlArr($allowvar=array("of_cityid","of_cityid1"),$postvar=$_POST,$sqltags=array("name_user","email_user","tel_user","citytel_user","texe_user","of_cityid2"));  


//無害化處理
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;
} 



$arr = array();
$show['settings']=phpconversion($conversion=$xoopsModuleConfig['neothemesadmin12'],$type="unserialize");
//自訂表單必填欄位驗證
foreach(array("2"=>"of_cityid2","4"=>"form_1","6"=>"form_2")  as $key=> $val){
if(!empty($show['settings'][$key])){
array_push($arr, $val);
}
}

foreach($arr as  $val){	
if(empty($_POST[$val])){
$harmful=true;
break;
}

}

  
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();
*/	

foreach(array("1"=>"form_1","2"=>"form_2")  as $key=> $val){	
$_POST['form'][$key]=$_POST[$val];
}






$_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`, `address`, `field`) 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, '".phpconversion($conversion=array('of_cityid'=>$_POST['of_cityid'],'of_cityid1'=>$_POST['of_cityid1'],'of_cityid2'=>$_POST['of_cityid2']),$type="serialize")."','".phpconversion($conversion=$_POST['form'],$type="serialize")."')";	  


$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數值-修改
$clist=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( "themefolder" , $themefolder) ;
$xoopsTpl->assign( "op" , $op) ;
$xoopsTpl->assign( "contactusform" , $cform) ;
$xoopsTpl->assign( "xoops_pagetitle" , _MD_TITLEM) ;


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