<?php
include("require.php");
submitfilter($_GET['bid']);
if(isset($_GET['action']) && $_GET['action']=="vote")
{
  $phpcolor->table="vote";
  $phpcolor->field="options,result,already,state,zdhf";
  $phpcolor->where="tid='".$_GET['bid']."'";
  $vote=$phpcolor->exist(1);
  if($_POST['option']=="")
    die("4");
  if($vote['state']==1 && !isset($_COOKIE['core_user_name']))
    die("1");
  if(($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");
  $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']!="")
  {
    $_POST['qita']=iconv("UTF-8", "gbk", $_POST['qita']);
	$content=str_replace("[其他]","[其他：".nl2br($_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*3600);
    if(isset($_COOKIE["core_user_name"]))
    {
      $phpcolor->table="user";
      $phpcolor->field="reply=reply+1";
      $phpcolor->where="username = '".$_COOKIE["core_user_name"]."'";
      $phpcolor->upgrade();
    }
    $phpcolor->table="topic";
    $phpcolor->field="top,reply";
    $phpcolor->where="id = '".$_GET['bid']."'";
    $post=$phpcolor->exist(1);
    $post['reply']=$post['reply']+1;
	if($post['top']=="1")
	  $phpcolor->field="reauthor='".$author."',republished = '".$time."',reply = '".$post['reply']."'";
	else
      $phpcolor->field="reauthor='".$author."',republished = '".$time."',pxtime = '".$time."',reply = '".$post['reply']."'";
    $phpcolor->upgrade();
    $phpcolor->table="repost";
    $phpcolor->field="content, file, author, ip, published, belong, floor, state";
    $phpcolor->where="'".$content."','','".$author."','".$_SERVER['REMOTE_ADDR']."','".$time."','".$_GET['bid']."','".$post['reply']."','1'";
    $phpcolor->write();
  }
  die("3");
}
if(isset($_GET['action']) && $_GET['action']=="submit")
{
  $_POST['theme']=iconv("UTF-8", "gbk", $_POST['theme']);
  $_POST['content']=iconv("UTF-8", "gbk", $_POST['content']);
  $_POST['content']=nl2br(autolink($_POST['content']));
  for($i=1;$i<=$_POST['num'];$i++)
  {
    $_POST['option'.$i]=iconv("UTF-8", "gbk", $_POST['option'.$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("6");
  //获取禁用词语
  $phpcolor->table="filter";
  $phpcolor->field="content";
  $phpcolor->where="type='1'";
  $rss=$phpcolor->assignment();
  for ($count=0;$count<count($rss);$count++)
  {
    if(strpos($_POST['theme'],$rss[$count]['theme']) || strpos($_POST['content'],$rss[$count]['content']))
	  die("1");
  }
  if(strlen($_POST['theme'])<$core_themeshort or strlen($_POST['theme'])>$core_themelong)
    die("2");
  if(strlen($_POST['content'])<$core_contentshort or strlen($_POST['content'])>$core_contentlong)
    die("3");
  $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
  //禁止发言
  $phpcolor->table="block";
  $phpcolor->field="*";
  $phpcolor->where="(username='".$_COOKIE['core_user_name']."' or username='".$_SERVER['REMOTE_ADDR']."') and (removetime>'".$time."' or removetime='0000-00-00 00:00:00') and (barid ='".$_GET['bid']."' or barid='0')";
  if($phpcolor->exist(2))
    die("11");
  for($i=0;$i<count($rs);$i++)
  {
    if(!isset($result))
      $result="0";
    else
	  $result.="|0";
  }
  if($_POST['qita']==1)
  {
    $option.="|其他";
	$result.="|0";
  }
  $phpcolor->table="topic";
  $phpcolor->field="theme, content, file, author, ip, published, republished, pxtime, belong, vote";
  $phpcolor->where="'".$_POST['theme']."','".$_POST['content']."','','".$_COOKIE['core_user_name']."','".$_SERVER['REMOTE_ADDR']."','".$time."','".$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="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();
  $phpcolor->table="bar_user";
  $phpcolor->field="*";
  $phpcolor->where="uid = '".$userv['id']."' and bid = '".$_GET['bid']."'";
  if($phpcolor->exist(2))
  {
    $phpcolor->upbaruser($int_fbt,$_GET['bid'],$userv['id']);
	//更新吧排名（会员发投票贴）
	$phpcolor->upranking($int_fbtpu,$_GET['bid']);
  }
  //更新吧排名（非会员发投票贴）
  else
    $phpcolor->upranking($int_fbtpd,$_GET['bid']);
  if($int_ufbt>0)
    $phpcolor->upintegral($_COOKIE['core_user_name'],"",$int_ufbt,11,$time);
  if($core_postaudit==0 || ($core_postaudit==1 && isset($_COOKIE["core_user_name"])))
    die("4");
  die("5");
}
$phpcolor->table="bar";
$phpcolor->field="barname,style";
$phpcolor->where="id = '".$_GET['bid']."'";
if(!$bar=$phpcolor->exist(1))
  jump("index.php");
if(!isset($_COOKIE["core_user_name"]))
  jump(htmlurl($core_url,"list",$_GET['bid'],0,$core_rewrite));
if($bar['style']!="0")
{
  $phpcolor->table="style";
  $phpcolor->field="link,phpfile";
  $phpcolor->where="id=".$bar['style'];
  if($style=$phpcolor->exist(1))
    $core_style=$style['link'];
}
$phpcolor->table="bar_domain";
$phpcolor->field="domain";
$phpcolor->where="bid = '".$bar['id']."'";
if($bardomain=$phpcolor->exist(1))
  $bar['url']="http://".$bardomain['domain']."/";
else
  $bar['url']=htmlurl($core_url,"list",$bar['id'],0,$core_rewrite);
$tpl -> assign("barid", $_GET['bid']);
$tpl -> assign("core_style", $core_style);
$tpl -> assign("barname", $bar['barname']);
$tpl -> assign("core_header_title", $bar['barname']."吧发起投票 - ");
$tpl -> display("$core_style/vote.htm");
?>