<?php

	//防止重复提交
	$verify = $_POST["verify"];
	
	if( !$verify || $verify == $_SESSION["__VERIFY"] ){
		
		$smarty->assign("error", "verify" );
		
	}elseif( $config["FORM_MAX"]>0 && $config["FORM_MAX"] <= $VICACHE["mod.forms"]["stat"] ){
		
		$smarty->assign("error", "toplimit" );
		
	}elseif( $VICACHE["mod.forms"]["start"]>time() || !$VICACHE["mod.forms"]["status"] ){
		
		$smarty->assign("error", "status" );
		
	}elseif( $VICACHE["mod.forms"]["expire"]<time() ){
		
		$smarty->assign("error", "expire" );
		
	}else{
		
		//mysql open
		reader_mysql();
		
		$post = true;
		
		//会话验证		
		if( $config["FORM_SPEED"] ){
			
			switch( $config["VERIFY-MODE"] ){
				
				//Cookies验证
				case "CK":
				
					$cookie = $appid."-".$fid;
					
					if( $_COOKIE[$cookie] ){
						
						$smarty->assign("error", "cookie" );
												
						$post = false;
					}				
				break;
				
				//IP提交数检查
				case "IP":
					
					$sql="SELECT id FROM ".VI_DBMODPRE."values where ip = '".GetIP()."' and dateline>".(time()-$config["FORM_SPEED"]*60)." and fid=".$fid;
							
					$result=mysql_query($sql,$mysql);
					
					if(mysql_num_rows($result)){
						
						$smarty->assign("error", "ip" );
												
						$post = false;
					}				
				break;				
			}
		}
		
		//注册用户，不允许用户名重复
		if( $post && $config['USER_MODE'] == "REG" && $config['FORM_NUMBER'] ){
			
			$sql="SELECT count(id) FROM ".VI_DBMODPRE."values where uid = '".$_SESSION["Member"]["id"]."' and fid=".$fid;
					
			if( $db -> getValue($sql) >= $config['FORM_NUMBER'] ){
				
				$smarty->assign("error", "number" );
				
				$post = false;
			}
			
		}
		
		if( $post ){
		
			//处理表单
			$arr = array();
			
			//收集标记
			$res = false;
			
			//收集错误
			$err = 0;
			
			foreach( $VICACHE["mod.forms"]["groups"] as $gid => $group ){
				
				$item = '';				
				$temp = 'G-'.$gid;
				$config = json_decode($group["config"],true);		
				
				$value = '';
				
				//类型换算
				switch($group["type"]){
						
					case 'checkbox':
						$array = $_POST[$temp];
						
						for($n=0;$n<count($array);$n++){
							$value .= $array[$n].',';
						}
						
						$value=substr($value,0,strlen($value)-1);
						
					break;
						
					default:
						$value = $_POST[$temp];
					break;
				}
				
				$arr[$temp] = $value;
				
				//更新统计
				if ( $value ){
					$sql="update ".VI_DBMODPRE."options set stat=stat+1 where id in(".$value.")";
					mysql_query($sql,$mysql);
					
					$Project["queries"]++;
					
					$res = true;
				}
				
				//判断对错			
				$try = false;
				
				if( $value && $group["selected"] ){
					switch( $group["type"] ){
					
						case "radio":
						case "select":
							if( $value == $group["selected"] ){
								$try = true;	
							}
						break;
					
						case "checkbox":
							if( count(array_diff( explode(",",$group["selected"]) , $array )) == 0 ){
								$try = true;
							}					
						break;
					}
					
				}elseif( $value && !$group["selected"] ){
					$try = true;
				}
				
				if( $try ){
					$sql="update ".VI_DBMODPRE."groups set stat=stat+1 where id = ".$gid;
					mysql_query($sql,$mysql);
					
					$Project["queries"]++;
					
				}else{
					$err++;	
				}
				
			}
			
			//已接收数据
			if( $res ){
				
				//更新邀请统计
				if ( $hash["oid"] ){
					
					$sql="update ".VI_DBMODPRE."options set stat=stat+1 where id = ".$hash["oid"];
					mysql_query($sql,$mysql);
					
					$Project["queries"]++;
				
					//更新缓存
					cache_forms($hash["fid"],$hash["appid"],$mysql);				
					
				}
				
				//是否测试成功
				$arr["RESULT"] = ( $err ? "N" : "Y" );
				
				//使用匿名扩展模式
				//ACCOUNT	姓名	PHONE	电话号码	IDCARD	身份证	EMAIL	电子邮箱
				// !$_SESSION["Member"]["id"] &&
				if( $config['USER_MODE'] == "ANY" ){
					$arr["ACCOUNT"]	= $_POST["ACCOUNT"];
					$arr["NAME"]	= $_POST["NAME"];
					$arr["PHONE"] 	= $_POST["PHONE"];
					$arr["IDCARD"] 	= $_POST["IDCARD"];
				}

				//记录用户提交数据	
				$config = format_json( fix_json($arr) );
				
				$sql="insert into ".VI_DBMODPRE."values(appid,fid,uid,account,config,status,dateline,ip) values('".$VICACHE["mod.forms"]["appid"]."',".$fid.",'".$_SESSION["Member"]["id"]."','".$_SESSION["Member"]["name"]."','".$config."',0 ,".time().",'".GetIP()."')";
				mysql_query($sql,$mysql);
				
				if( $_SESSION["Member"]["id"] ){
				
					//更新活跃统计
					$sql="update ".VI_DBMODPRE."members set stat_level=stat_level+1 where id=".$_SESSION["Member"]["id"];
					mysql_query($sql);
				
				}
				
				//测试结果统计
				$config["RESULT"] = ( is_numeric($config["RESULT"]) ? $config["RESULT"] : 0 );
				$config["RESULT"] = ( $err ? $config["RESULT"] : $config["RESULT"]+=1 );
				$config = format_json( fix_json($config) );
				
				//更新人数统计
				$sql="update ".VI_DBMODPRE."forms set stat=stat+1,config='".$config."' where id = ".$fid;
				mysql_query($sql,$mysql);
				
				$Project["queries"]+3;
				
				//更新缓存
				cache_forms($fid,$VICACHE["mod.forms"]["appid"],$mysql);				
				
				//记录验证码
				$_SESSION["__VERIFY"] = $verify;
				
				//写入Cookies
				$expire=time() + (60*$config["FORM_SPEED"]);
				setcookie($cookie, 'Y', $expire ,"" ,"");
				
				if( $err ){
					
					$smarty->assign("error", $err );
					
				}else{
					
					$smarty->assign("error", "" );
					
				}
				
			
			}else{
				
				$smarty->assign("error", "null" );		
			
			}
			
		}
	  
		//mysql close
		reader_mysql(true);		
		
	}
?>