<?php
include("require.php");
submitfilter(1,$_GET['bid']);
  if(isset($_GET['action']) && $_GET['action']=="vote")
  {
    $phpcolor->table=$core_mysql_prefix."vote";
    $phpcolor->field="options,result,already,state,zdhf";
    $phpcolor->where="tid='".$_GET['bid']."'";
	$vote=$phpcolor->exist(1);
    if($_POST['option']=="")
	  die("0");
	elseif($vote['state']==1 && !isset($_COOKIE['core_user_name']))
	  die("1");
    elseif(($vote['already']!="" && $vote['state']=="1" && eregi($vote['already'],$_COOKIE['core_user_name'])) || ($vote['already']!="" && $vote['state']=="0" && eregi($vote['already'],$_SERVER['REMOTE_ADDR'])))
      die("2");
	else
	{
	  $rs=split("\|", $vote['result']);
	  $rs1=split("\|", $_POST['option']);
	  $rs2=split("\|", $vote['options']);
	  $content="本贴作者参与投票 选择了 ";
	  for($i=0;$i<count($rs1);$i++)
	  {
	    $rs[$rs1[$i]]+=1;
		$content.="[".$rs2[$rs1[$i]]."]";
	  }
	  for($i=0;$i<count($rs);$i++)
	  {
	    if(!isset($result))
	      $result=$rs[$i];
	    else
		  $result.="|".$rs[$i];
      }
	  if($_POST['qita']!="")
	  {
		$content=str_replace("[其他]","[其他：".nl2br(html_replace($_POST['qita']))."]",$content);
	  }
	  if($vote['state']==1)
	  {
	    if($vote['already']=="")
	      $vote['already']=$_COOKIE['core_user_name'];
	    else
	      $vote['already'].="|".$_COOKIE['core_user_name'];
      }
	  else
	  {
	    if($vote['already']=="")
	      $vote['already']=$_SERVER['REMOTE_ADDR'];
	    else
	    $vote['already'].="|".$_SERVER['REMOTE_ADDR'];
	  }
	  $phpcolor->field="result='".$result."',already='".$vote['already']."'";
	  $phpcolor->upgrade();
	  if($vote['zdhf']==1 or ($vote['zdhf']==2 and $_POST['qita']!=""))
	  {
	    $author=$_SERVER['REMOTE_ADDR'];
        if(isset($_COOKIE["core_user_name"]))
          $author = $_COOKIE["core_user_name"];
        $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*60*60);
	    if(isset($_COOKIE["core_user_name"]))
        {
	      $phpcolor->table=$core_mysql_prefix."user";
          $phpcolor->field="reply=reply+1";
          $phpcolor->where="username = '".$_COOKIE["core_user_name"]."'";
          $phpcolor->upgrade();
        }
        $phpcolor->table=$core_mysql_prefix."topic";
        $phpcolor->field="theme,reply";
        $phpcolor->where="id = '".$_GET['bid']."'";
        $post=$phpcolor->exist(1);
	    $post['reply']=$post['reply']+1;
        $phpcolor->field="reauthor='".$author."',republished = '".$time."',reply = '".$post['reply']."'";
        $phpcolor->upgrade();
	    $phpcolor->table=$core_mysql_prefix."repost";
        $phpcolor->field="theme, content, file, author, ip, published, belong, floor, state";
        $phpcolor->where="'回复：".$post['reply']."','".$content."','','".$author."','".$_SERVER['REMOTE_ADDR']."','".$time."','".$_GET['bid']."','".$post['reply']."','1'";
	    $phpcolor->write();
	  }
	  die("3");
	}
  }
  elseif(isset($_GET['action']) && $_GET['action']=="submit")
  {
	$_POST['theme']=html_replace($_POST['theme']);
    $_POST['content']=nl2br(html_replace(autolink($_POST['content'])));
	for($i=1;$i<=$_POST['num'];$i++)
	{
	  $options[$i-1]=$_POST['option'.$i];
	}
	for($i=0;$i<count($options);$i++)
	{
	  if($options[$i]!="")
	  {
	    if(!isset($option))
	      $option=$options[$i];
		else
		  $option.="|".$options[$i];
	  }
	}
	$rs=split("\|", $option);
	if(count($rs)<2)
	  die("0");
	elseif($filter!="" && (eregi($filter,$_POST['theme']) || eregi($filter,$_POST['content'])))
      die("1");
    elseif(strlen($_POST['theme'])<$core_themeshort or strlen($_POST['theme'])>$core_themelong)
      die("2");
    elseif(strlen($_POST['content'])<$core_contentshort or strlen($_POST['content'])>$core_contentlong)
      die("3");
	else
	{
	  for($i=0;$i<count($rs);$i++)
	  {
	    if(!isset($result))
          $result="0";
        else
		  $result.="|0";
	  }
	  if($_POST['qita']==1)
	  {
	    $option.="|其他";
		$result.="|0";
	  }
      $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*60*60);
      $phpcolor->table=$core_mysql_prefix."topic";
	  $phpcolor->field="theme, content, file, author, ip, published, republished, belong, vote";
	  $phpcolor->where="'".$_POST['theme']."','".$_POST['content']."','','".$_COOKIE['core_user_name']."','".$_SERVER['REMOTE_ADDR']."','".$time."','".$time."','".$_GET['bid']."','1'";
	  $phpcolor->write();
	  $phpcolor->field="id";
	  $phpcolor->where="author='".$_COOKIE['core_user_name']."' and published ='".$time."' and vote='1'";
	  $post=$phpcolor->exist(1);
	  $phpcolor->table=$core_mysql_prefix."vote";
	  $phpcolor->field="tid, options, result, type, state, already, qita, zdhf";
	  $phpcolor->where="'".$post['id']."','".$option."','".$result."','".$_POST['type']."','".$_POST['state']."','','".$_POST['qita']."','".$_POST['zdhf']."'";
	  $phpcolor->write();
	  if($core_postaudit==0 || ($core_postaudit==1 && isset($_COOKIE["core_user_name"])))
        die("4");
	  else
	    die("5");
	}
  }
  else
  {
    $phpcolor->table=$core_mysql_prefix."bar";
    $phpcolor->field="barname,barmaster";
    $phpcolor->where="id = '".$_GET['bid']."'";
    if(!$bar=$phpcolor->exist(1))
	  Header("Location: index.php");
	else
	{
	  if(!isset($_COOKIE["core_user_name"]))
	  {
		if($core_rewrite=="1")
	      Header("Location: list-".$_GET['bid']."-0.html");
	    else
	      Header("Location: list.php?bid=".$_GET['bid']);
	  }
	  else
	  {
	    $tpl -> assign("barid", $_GET['bid']);
	    $tpl -> assign("barname", $bar['barname']);
	    $tpl -> assign("core_header_title", $bar['barname']."吧发起投票 - ");
        $tpl -> display("$core_style/vote.htm");
	  }
	}
  }
?>