<?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 ){

			$OBJECT = $_POST["OBJECT"];
			$MESSAGE = $_POST["MESSAGE"];		//这里可以做个开关，是否强制要输入内容

			//已接收数据
			if( $OBJECT ){

				//处理表单
				$arr = array();

				//被投对象
				$arr["OBJECT"]  = $OBJECT;

				//消息主体
				$arr["MESSAGE"] = $MESSAGE;				

				//使用匿名扩展模式
				//ACCOUNT	姓名	PHONE	电话号码	IDCARD	身份证	EMAIL	电子邮箱
				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."','".$status."',".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[$OBJECT."-RESULT"] = ( $err ? $config[$OBJECT."-RESULT"] : $config[$OBJECT."-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,$appid,$mysql);

				//记录验证码
				$_SESSION["__VERIFY"] = $verify;

				//写入Cookies
				$expire = time() + (60*$config["FORM_SPEED"]);
				setcookie($cookie, 'Y', $expire ,"" ,"");

				$smarty->assign("error", "" );	

			}else{

				$smarty->assign("error", "null" );		

			}

		}

		//mysql close
		reader_mysql(true);
		
	}
?>