<?php
	
	if(!defined('VI_BASE')) {
		ob_clean();
		exit('Access Denied');
	}
	
	/////////////////////////////////
	
	//防止重复提交
	$verify = $_POST["verify"];
	
	if( $config['USER_MODE'] == "REG" && !$_SESSION['Member']['id'] ){
		
		$smarty->assign("error", "login" );
		
	}elseif( !$verify || $verify == $_SESSION["__VERIFY"] ){
		
		$smarty->assign("error", "verify" );	

	}elseif( $config["FORM_MAX"]>0 && $config["FORM_MAX"] <= $_CACHE[$appid]['form']["stat"] ){
		
		$smarty->assign("error", "toplimit" );
		
	}elseif( $_CACHE[$appid]['form']["start"]>time() || !$_CACHE[$appid]['form']['state'] ){
		
		$smarty->assign("error", "state" );
		
	}elseif( $_CACHE[$appid]['form']["expire"]<time() ){
		
		$smarty->assign("error", "expire" );
		
	}else{	
		
		$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 `mod:value` WHERE ip = '".GetIP()."' and dateline>".(time()-$config["FORM_SPEED"]*60)." and fid=".$fid;
							
					$row = System :: $db -> getOne( $sql );

               		if( $row ){
						
						$smarty->assign("error", "ip" );
						
						$post = false;
					}				
				break;				
			}
		}
		
		//注册用户，不允许用户名重复
		if( $post && $config['USER_MODE'] == "REG" && $config['FORM_NUMBER'] ){
			
			$sql="SELECT count(id) FROM `mod:value` WHERE uid = '".$_SESSION['Member']['id']."' and fid=".$fid;
					
			if( System :: $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"];
					$arr["QQ"] 	= $_POST["QQ"];
					$arr["EMAIL"] 	= $_POST["EMAIL"];
				}


				//记录用户提交数据	
				$value = format_json( fix_json($arr) );

				$sql="INSERT INTO `mod:value`(appid,fid,uid,account,config,state,dateline,ip) values('".$_CACHE[$appid]['form']["appid"]."',".$fid.",'".$_SESSION['Member']['id']."','".$_SESSION['Member']['account']."','".$value."','".$state."',".time().",'".GetIP()."')";
				System :: $db -> execute( $sql );

				if( $_SESSION['Member']['id'] ){				
					//更新活跃统计
					$sql="UPDATE `mod:member` SET stat_level=stat_level+1 WHERE id=".$_SESSION['Member']['id'];
					System :: $db -> execute( $sql );		

				}
				
				////////////////

				//测试结果统计				
				$config[$OBJECT."_RESULT"] = intval($config[$OBJECT."_RESULT"]);
				
				$config[$OBJECT."_RESULT"] = ( $err ? $config[$OBJECT."_RESULT"] : $config[$OBJECT."_RESULT"]+1 );

				//更新人数统计
				$sql="UPDATE `mod:form` SET stat=stat+1,config='". format_json( fix_json($config) ) ."' WHERE id = ".$fid;
				System :: $db -> execute( $sql );

				//更新缓存
				Cached :: rows( $appid, "SELECT * FROM `mod:form` WHERE id=".$fid, array( 'jsonde' => array('config') ) );

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

				//写入Cookies
				setcookie($cookie, 'Y', time() + (60*$config["FORM_SPEED"]) ,"" ,"");
				
				//送积分
				/////////////////////////////////
				
				if( $config["FORM_MONEY"] && $_SESSION['Member']['id'] ){
					
					//更新用户积分
					$money = Passport :: money( $_SESSION['Member']['id'] , $config["FORM_MONEY"] );
					
					$smarty->assign("money", $money );
					
				}
				
				/////////////////////////////////
				
				
				//发邮件
				/////////////////////////////////
				
				if( $config["FORM_NOTICE"] == "Y" ){
					
					//创建者邮件地址
					$address = $_CACHE['system']['admin'][ $_CACHE[$appid]['form']["aid"] ]["email"];	
					
					if( $address ){
					
						$content = str_replace( array('{TIME}','{USER}','{EVENT}','{URL}','{VERYIDE}'),
									array( date('Y-m-d H:i:s') ,$_CACHE[$appid]['form']["account"], $_CACHE[$appid]['form']["name"], Module::get_index($appid)."?id=".$fid."&ref=email", VI_HOST ) ,$_G['setting']['passport']["event"] );
						
						if( $address ){
						
							System :: sendmail( $_CACHE[$appid]['form']["name"] ,$address,$config["FORM_SEND"], $content );
						
						}
					
					}
				
				}
				
				/////////////////////////////////
				
				$smarty->assign("error", "" );	

			}else{

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

			}

		}

	}
?>