<?php
/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	$Id: veryide.action.php,v2 19:47 2008-10-08 Leilei $
*/


//载入全局配置和函数包
require("../../../include/global.php");

//require("../veryide/module/pk/config.php");

//系统动作配置，修改时请小心
header('Content-type: text/xml');

echo ("<?xml version='1.0' encoding='".$Project["charset"]."'?>");
echo ("<response>");
	
$action = GetQuery("action");
$fid = GetNumber("fid",0);

echo ("<action>".$action."</action>");

switch ($action){
	
	//提交数据_已废弃
	case "submit":	
	
		$user = GetQuery("user");
		
		$vote = GetQuery("vote");
		$vote=($vote=="posi-box"?1:2);
		
		$message = GetQuery("message");
		$mode = GetQuery("mode");
		
		//COOKIE ID
		$sid = "PK-COOKIE-".$id;
		
		if(!$id || !$vote){
			echo ("<result>false</result>");
		}else{
			
			//与最近一次提交对比
			if( $_COOKIE[$sid] ){
				echo ("<result>voted</result>");
				
			}else{
		
				//mysql open
				reader_mysql();
	
			
				//验证表单
				$sql="select f_expire,f_state,f_suffrage,f_speed,f_ext1,f_check,f_login from ".VI_DBMODPRE."forms where appid='".$Modules["pk"]["appid"]."' and f_id=".$id;
				
				$result=mysql_query($sql,$mysql);
				
				$Project["queries"]++;
				
				if(mysql_num_rows($result)==1){
					$row=mysql_fetch_assoc($result);
				
					$f_expire=$row["f_expire"];
					$f_state=$row["f_state"];
					
					$f_suffrage=$row["f_suffrage"];
					$f_speed=$row["f_speed"];
					$f_check=$row["f_check"];
					$f_login=$row["f_login"];
					
					$f_message=$row["f_ext1"];
					
					$post=true;
					
			
					//用户登录检查
					if($f_login && @file_exists("../veryide/extends/")){
						
						require("../veryide/extends/config.inc.php");
						require("../veryide/extends/client/client.php");
						
						if(!empty($_COOKIE[VI_UC_CID])) {
							list($u_id, $u_user) = explode("\t", uc_authcode($_COOKIE[VI_UC_CID], 'DECODE'));
							
							if( UC_DBCHARSET != $Project["charset"] ){
								$u_user = iconv( UC_DBCHARSET ,$Project["charset"] ,$u_user);
							}
							
						}else{
							echo ("<result>login</result>");
							$post=false;
						}
				
					}
					
					//不允许用户名重复
					if(!$f_suffrage && $user){
						
						$sql="SELECT u_id FROM ".VI_DBMODPRE."apps_user where u_name = '".$user."' and f_id=".$id;
								
						$result=mysql_query($sql,$mysql);
						
						$Project["queries"]++;
						
						if(mysql_num_rows($result)){
							echo ("<result>user</result>");
							$post=false;
						}
					}
					
					//IP提交数检查
					if($f_speed && $f_check){				
						$sql="SELECT u_id FROM ".VI_DBMODPRE."apps_user where u_ip = '".GetIP()."' and u_date>".(time()-$f_speed*60)." and f_id=".$f_id;
								
						$result=mysql_query($sql,$mysql);
						
						$Project["queries"]++;
						
						if(mysql_num_rows($result)){
							echo ("<result>ip</result>");
							$post=false;
						}				
					}
					
					if(!$f_state){
						echo ("<result>closed</result>");
						$post=false;
						
					}elseif (time() > $f_expire){
						echo ("<result>closed</result>");
						$post=false;
						
					}elseif ($post){
					
						if($mode=="simple"){
												
							//update
							$sql="update ".VI_DBMODPRE."forms set ";
							
							if($vote==1){
								$sql.="f_ext2=f_ext2+1";
							}else{
								$sql.="f_ext4=f_ext4+1";
							}
							$sql.=" where f_id=".$id;
							
							@mysql_query($sql,$mysql);
							
							$Project["queries"]++;
							
							//MySQL错误
							if(!@mysql_affected_rows()){
								echo ("<result>mysql</result>");
							}else{
								echo ("<result>true</result>");
							}
						
							//写入Cookie
							setcookie($sid,"voted",(time()+($f_speed*60)));
						}else{
						
							if( $f_message && (!$user || !$message) ){
								echo ("<result>false</result>");
							}else{
								
								//insert
								$sql="insert into ".VI_DBMODPRE."apps_user(f_id,u_uid,u_name,u_ext1,u_ext2,u_ip,u_date) values('".$id."','".$u_id."','".$user."','".$vote."','".$message."','".GetIP()."',".time().")";
								
								@mysql_query($sql,$mysql);
								
								$Project["queries"]++;
								
								$uid = mysql_insert_id($mysql);
								
								//MySQL错误
								if(!$uid){
									echo ("<result>mysql</result>");
								}
								
								//update
								$sql="update ".VI_DBMODPRE."forms set ";
								
								if($vote==1){
									$sql.="f_ext2=f_ext2+1";
								}else{
									$sql.="f_ext4=f_ext4+1";
								}
								$sql.=" where f_id=".$id;
								
								@mysql_query($sql,$mysql);
								
								$Project["queries"]++;
								
								//MySQL错误
								if(!@mysql_affected_rows()){
									echo ("<result>mysql</result>");
								}else{
									echo ("<result>true</result>");
								}
								
								//写入Cookie
								setcookie($sid,"voted",(time()+($f_speed*60)));
							}
						}
					}
					
				}else{
					echo ("<result>closed</result>");
				}
				
				//mysql close
				reader_mysql(true);
				
			}
		}
		
	break;

	//定时获取票数
	case "vote":
		
		//写入log
		if( !$id){
			echo ("<result>false</result>");
		}else{
		
			//mysql open
			reader_mysql();

			//查询数据库
			$sql="select f_ext2,f_ext4 from ".VI_DBMODPRE."forms where f_id=".$id;
			$result=@mysql_query($sql,$mysql);
			
			$Project["queries"]++;
			
			if(@mysql_num_rows($result)==1){
				$row=mysql_fetch_assoc($result);
				
				$f_posicount=$row["f_ext2"];
				$f_negacount=$row["f_ext4"];

				echo ("<result>true</result>");
				echo ("<positive>".$f_posicount."</positive>");
				echo ("<negative>".$f_negacount."</negative>");
			}else{
				echo ("<result>mysql</result>");
			}
			
	  
			//mysql close
			reader_mysql(true);

		}
	break;

	//评论分页_Ajax
	case "message":
	
		$object = GetQuery("object");
		//$object=($object=="posi-box"?1:2);
		
		if(!$object || !$fid){
			echo ("<result>false</result>");
		}else{
		
			//mysql open
			reader_mysql();
	
			//读取配置
			//reader_setting();	
	
			//查询数据库
			$sql="select * from ".VI_DBMODPRE."values where fid='".$fid."' and config like '%\"OBJECT\":\"".$object."\"%' order by id desc";
			
			$result=@mysql_query($sql,$mysql);
			
			$Project["queries"]++;			
			
			//查询数据库_总记录数
			$row_count = mysql_num_rows($result);
			
			if( $row_count ==0 ){
			
				echo ("<result>zero</result>");

			}else{
			
				//分页参数
				$page=GetPage("page");
				$page_start=$Global["pagesize"]*($page-1);
				$sql=$sql." limit $page_start,".$Global["pagesize"];
				
				//echo $sql;
					
				echo ('<result rowcount="'.$row_count.'" pagecount="'.ceil($row_count/$Global["pagesize"]).'" pagecurrent="'.$page.'" pagesize="'.$Global["pagesize"].'">true</result>');
			
				$result=mysql_query($sql,$mysql);
				
				$Project["queries"]++;
				
				//查询数据
				while($row=mysql_fetch_assoc($result)){
					
					$config = fix_json($row["config"]);
	
					echo "<message>";
						echo "<username>".( $row["account"] ? $row["account"] : $config["ACCOUNT"] )."</username>";
						echo "<dateline>".date("y年m月d日 H:i:s",$row["dateline"])."</dateline>";
						echo "<content><![CDATA[".str_replace(chr(13),'<br />',ClearHtml($config["MESSAGE"]))."]]></content>";
					echo "</message>";
				}
			}
			
			//mysql close
			reader_mysql(true);

		}

		break;

}
	
echo ("</response>");
?>