<?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;
			
			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;
						
					case 'file':
						
						if( $_FILES[$temp]["name"] && !$_FILES[$temp]['error'] ){
							
							//检查上传文件
							$result = uploader_check( $_FILES[$temp]["name"] );
							
							//子文件夹
							$folder = $Project["upload"].$result['type'];
							
							//创建子文件夹
							if( !file_exists($folder) ){
								create_dir($folder);
							}
							
							//大小
							$size = $_FILES[$temp]["size"];
							
							//临时
							$tmpn = $_FILES[$temp]["tmp_name"];
						
							//如果是图片
							if( $result['type']=="image" ){
								list($width, $height, $image_type, $attr) = getimagesize($tmpn);
							}else{
								$width=0;		$height=0;	
							}		
				
							//移动上传文件
							$value = uploader_save($folder,$tmpn,$_FILES[$temp]["name"],$result["extra"]);
							
							$value = readpath(VI_BASE.$value);
							
						}
						
						//上传成功
						if( $value ){						
				
							//写入数据库
							$sql="insert into ".VI_DBMANPRE."attach(name,input,dateline,type,size,ip,width,height,remote) values('".$value."','".$temp."',".time().",'".$result["extra"]."',$size,'".GetIP()."',$width,$height,0)";
							
							mysql_query($sql,$mysql);
							
							$Project["queries"]++;
							
							//写入日志
							event_insert($mysql,'upload',time(),time(),'通过 Forms 上传文件 <a href="'.$value.'" target="_blank">'.$value.'</a>，大小 '.GetFileSize($size,"byte").'，类型 '.$result["extra"]);
				  
						}
						
					break;
						
					default:
						$value = $_POST[$temp];
					break;
				}
				
				$arr[$temp] = $value;
				
				//更新统计
				if ( $value && in_array($group["type"],array("radio","checkbox","select")) ){
					$sql="update ".VI_DBMODPRE."options set stat=stat+1 where id in(".$value.")";
					mysql_query($sql,$mysql);
					
					$Project["queries"]++;
					
					$res = true;
				}
				
				//更新统计
				if ( $value && in_array($group["type"],array("text","textarea","file")) ){
					$sql="update ".VI_DBMODPRE."groups set stat=stat+1 where id = ".$gid;
					mysql_query($sql,$mysql);
					
					$Project["queries"]++;
					
					$res = true;
				}
				
			}
			
			//已接收数据
			if( $res ){
				
				//更新邀请统计
				if ( $hash["uid"] ){
					
					$sql = "select * from `".VI_DBMODPRE."invite` where `fid`='".$fid."' and `uid`='".$hash["uid"]."'";
					$query = mysql_query($sql,$mysql);
					
					if(!mysql_num_rows($query)){
					
						$sql = "insert into `".VI_DBMODPRE."invite`(`fid`,`uid`,`account`,`stat`,`dateline`) values ('".$fid."','".$hash["uid"]."','".$hash["account"]."','1','".time()."')";
					
					}else{
						
						$sql = "update `".VI_DBMODPRE."invite` set `stat`=`stat`+1 where `uid`='".$hash["uid"]."'";
					}
					
					mysql_query($sql,$mysql);
					
				}
				
				//使用匿名扩展模式
				//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);
					
				}
				
				//更新人数统计
				$sql="update ".VI_DBMODPRE."forms set stat=stat+1 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( $config["FORM_NOTICE"] == "Y" ){
				
					//载入函数包
					require("../../../function/mail.php");
					
					//创建者邮件地址
					$address = $VICACHE["sys.admin"][ $VICACHE["mod.forms"]["aid"] ]["email"];	
					
					if( $address ){
					
						$content = str_replace( array('{TIME}','{USER}','{EVENT}','{URL}','{VERYIDE}'),
									array( date('Y-m-d H:i:s') ,$VICACHE["mod.forms"]["account"], $VICACHE["mod.forms"]["name"], uripath()."?id=".$fid, uripath(true) ) ,$Global["notice"] );
						
						if( $address ){
						
							mail_send( $VICACHE["mod.forms"]["name"] ,$address,$config["FORM_SEND"], $content );
						
						}
					
					}
				
				}
				
				/////////////////////////////////
				
				$smarty->assign("error", "" );
			
			}else{
				
				$smarty->assign("error", "null" );
			
			}
	  
		}
	  
		//mysql close
		reader_mysql(true);		
		
	}
?>