<?php
include(dirname(__FILE__)."/require.php");
if($ppurview[14]!=1)
  jump("jump.php?id=25&jump=main.php");
$phpcolor->table="topic";
$phpcolor->where="id = '".$_GET['id']."'";
if(isset($_GET['action']) and $_GET['action'] == "del")
{
  if(isset($_GET['id']))
    $_POST['id'][0]=$_GET['id'];
  $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
  for($i=0;$i<count($_POST['id']);$i++)
  {
    $phpcolor->table="topic";
    $phpcolor->field="state = 0";
    $phpcolor->where="id = '".$_POST['id'][$i]."'";
    $phpcolor->upgrade();
    $phpcolor->table="recover";
    $phpcolor->field="tid, operate, operatetime, ip, type";
    $phpcolor->where="'".$_POST['id'][$i]."', '".$_COOKIE["core_user_name"]."','".$time."','".$_SERVER['REMOTE_ADDR']."' , '1'";
    $phpcolor->write();
	$phpcolor->table="repost";
	$phpcolor->field="state='4'";
	$phpcolor->where="belong = '".$_POST['id'][$i]."' and state!='0'";
    $phpcolor->upgrade();
  } 
  jump("jump.php?id=24&jump=topic.php");
}
if(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "edit")
{
  $phpcolor->field="*";
  $post = $phpcolor->exist(1);
  $post['content'] = str_replace("<br />","",$post['content']);
  if($post['file']!="")
  {
	$files = split("\|", $post['file']);
    for($i=0;$i<count($files);$i++)
    {
      $phpcolor->table="upload";
      $phpcolor->where="id = '".$files[$i]."'";
      $file[$i]=$phpcolor->exist(1);
    }
	$tpl -> assign("file", $file);
  }
  if($post['vote']=="1")
  {
    $phpcolor->table="vote";
    $phpcolor->field="options,result";
    $phpcolor->where="tid = '".$_GET['id']."'";
    $vote = $phpcolor->exist(1);
    $voteo = split("\|", $vote['options']);
    $voter = split("\|", $vote['result']);
    $tpl -> assign("voteo", $voteo);
    $tpl -> assign("voter", $voter);
  }
  $phpcolor->table="bar";
  $phpcolor->field="barname";
  $phpcolor->where="id = '".$post['belong']."'";
  $rs=$phpcolor->exist(1);
  $post['belong']=$rs['barname'];
  $tpl -> assign("post", $post);
  $tpl -> display("topic_edit.htm");
}
elseif(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "editdo")
{
  $phpcolor->field="file,republished,vote";
  $post = $phpcolor->exist(1);
  if($post['vote']=="1")
  {
    for($i=0;$i<count($_POST['option']);$i++)
    {
      if(!isset($option))
        $option=$_POST['option'][$i];
      else
	    $option.="|".$_POST['option'][$i];
    }
    $rs=split("\|", $option);
  }
  if($post['vote']=="1" && count($rs)<2)
    jump("jump.php?id=22&jump=topic.php?action=edit=and=id=".$_GET['id']);
  if(strlen($_POST['theme'])<$core_themeshort or strlen($_POST['theme'])>$core_themelong or strlen($_POST['content'])<$core_contentshort or strlen($_POST['content'])>$core_contentlong)
    jump("jump.php?id=8&jump=topic.php?action=edit=and=id=".$_GET['id']);
  $pxtime=$post['republished'];
  if($_POST['state']=="2")
  {
    $pxtime="2036-12-31 23:59:59";
	$_POST['top']="0";
  }
  if($_POST['top']=="1")
    $pxtime="2036-12-31 23:59:59";
  $_POST['content']=nl2br(autolink($_POST['content']));
  $phpcolor->table="bar";
  $phpcolor->field="id";
  $phpcolor->where="barname = '".$_POST['belong']."'";
  if(!$rss=$phpcolor->exist(1))
    jump("jump.php?id=7&jump=topic.php?action=edit=and=id=".$_GET['id']);
  $phpcolor->table="topic";
  $phpcolor->where="id = '".$_GET['id']."'";
  $phpcolor->field="theme='".$_POST['theme']."', content='".$_POST['content']."', belong='".$rss['id']."', pxtime='".$pxtime."', top='".$_POST['top']."', boutique='".$_POST['boutique']."', locks='".$_POST['locks']."', state='".$_POST['state']."', remind='".$_POST['remind']."'";
  $phpcolor->upgrade();
  if($post['vote']=="1")
  {
    for($i=0;$i<count($_POST['result']);$i++)
    {
      if(!isset($result))
        $result=$_POST['result'][$i];
      else
        $result.="|".$_POST['result'][$i];
    }
    $phpcolor->table="vote";
    $phpcolor->field="options='".$option."', result='".$result."'";
    $phpcolor->where="tid = '".$_GET['id']."'";
    $phpcolor->upgrade();
  }
  if($post['file']!="")
  {
	$rs = split("\|", $post['file']);
    for($i=0;$i<count($_POST['file']);$i++)
    {
      if(!eregi("^[0-9]+$",$_POST['file'][$i]))
        $_POST['file'][$i]="0";
	  $phpcolor->table="upload";
      $phpcolor->field="price='".$_POST['file'][$i]."'";
      $phpcolor->where="id = '".$rs[$i]."'";
      $phpcolor->upgrade();
    }
  }
  jump("jump.php?id=24&jump=topic.php");
}
else
{
  if(!isset($_GET['page']))
    $_GET['page']=0;
  $phpcolor->field="*";
  if(isset($_GET['so']) and isset($_GET['theme']))
  {
    if($_GET['so']==1)
    {
      $phpcolor->where="(theme like '%".$_GET['theme']."%' or content like '%".$_GET['theme']."%') and state != '0'";
      $count = $phpcolor->exist(2);
      $phpcolor->where="(theme like '%".$_GET['theme']."%' or content like '%".$_GET['theme']."%') and state != '0' order by published desc limit ".($_GET['page']*20).",20";
    }
    else
    {
      $phpcolor->where="author like '%".$_GET['theme']."%' and state != '0'";
      $count = $phpcolor->exist(2);
      $phpcolor->where="author like '%".$_GET['theme']."%' and state != '0' order by published desc limit ".($_GET['page']*20).",20";
	}
    $page=pages($count,20,$_GET['page'],'topic','&theme='.$_GET['theme'].'&so=',$_GET['so'],'0');
  }
  else
  {
    if(isset($_GET['type']) and $_GET['type']=="boutique")
    {
      $phpcolor->where="state != '0' and boutique = '1'";
      $count = $phpcolor->exist(2);
      $phpcolor->where="state != '0' and boutique = '1' order by published desc limit ".($_GET['page']*20).",20";
	  $page=pages($count,20,$_GET['page'],'topic','&type=','boutique','0');
	}
    elseif(isset($_GET['type']) and $_GET['type']=="top")
    {
      $phpcolor->where="state != '0' and top = '1'";
      $count = $phpcolor->exist(2);
      $phpcolor->where="state != '0' and top = '1' order by published desc limit ".($_GET['page']*20).",20";
	  $page=pages($count,20,$_GET['page'],'topic','&type=','top','0');
	}
	else
	{
	  $phpcolor->where="state != '0'";
      $count = $phpcolor->exist(2);
      $phpcolor->where="state != '0' order by published desc limit ".($_GET['page']*20).",20";
	  $page=pages($count,20,$_GET['page'],'topic','','','0');
	}
  }
  $post=$phpcolor->assignment();
  $phpcolor->table="bar";
  for($count=0;$count<count($post);$count++)
  {
    $phpcolor->field="barname";
    $phpcolor->where="id = '".$post[$count]['belong']."'";
    $rs=$phpcolor->exist(1);
    $post[$count]['belongbn']=$rs['barname'];
    $post[$count]['authorip']=is_ip($post[$count]['author']);
	$post[$count]['content']=str_replace("<br />"," ",$post[$count]['content']);
    $post[$count]['content']=$phpcolor->mysubstr($post[$count]['content'],0,100); 
  }
  $tpl -> assign("post", $post);
  $tpl -> assign("page", $page);
  $tpl -> display("topic.htm");
}
?>