<?

require("datafile/superadmin.php");
require("datafile/config.php");
require("datafile/style.php");
require("global.php");

get_forum_info();

if (empty($forumid) || empty($filename) || 
	!file_exists("forum$forumid/$filename")
	|| ($action=="del" && empty($article))
	|| ($action!="del" && $action!="btfront" && $action!="holdfront" && $action!="unhold")
	|| !ereg("^f_[0-9]+$",$filename)
    ) {
	$status="未知的工作指示，别乱调用程序呀";
	include("header.php");
	print_bar();
	print_err();
	include("footer.php");
	exit;
}

//---------check-----------
	$check_user=0;
	if ($login_status==1 && $username==$admin_name) $check_user=1;
	if ($check_user==0 && $login_status==1 && file_exists("datafile/admin.php")) {
		$adminlist=file("datafile/admin.php");
		$admincount=count($adminlist);
		for ($i=0; $i<$admincount; $i++) {
			$detail=explode("|", trim($adminlist[$i]));
			if ($detail[0]==$forumid && $detail[1]==$username) { $check_user=1; break; }
		}
	}
	if ($check_user==0) {
		$status="您没有权利进行操作，请您以合适的身份登录(管理员,斑竹)";
		include("header.php");
		print_bar();
		print_err();
		include("footer.php");
		exit;
	}

if (empty($step)) {
	include("header.php");
	print_bar();
	print_confirm();
	include("footer.php");
	exit;
}

if ($step && file_exists("forum$forumid/list.php")) {
	$oldlist=readfromfile("forum$forumid/list.php");
	$oldlist=explode("\n",$oldlist);
	$count=count($oldlist);
	for ($i=0; $i<$count; $i++) {
		$detail=explode("|",$oldlist[$i]);
		if ($detail[5]==$filename) {
			if ($action=="del") {
				$detail[6]--;
				$oldlist[$i]=implode("|",$detail);
				$new_line="";
				break;
			} elseif($action=="btfront") {
				$new_line=$oldlist[$i]."\n";
				unset($oldlist[$i]);
				break;
			} elseif($action=="holdfront") {
				if (empty($detail[10])) $detail[10]=0;
				$detail[10]+=3;
				$new_line=implode("|",$detail)."\n";
				unset($oldlist[$i]);
				break;
			} elseif($action=="unhold") {
				if ($detail[10]>=3) $detail[10]=$detail[10]-3;
				$new_line=implode("|",$detail)."\n";
				unset($oldlist[$i]);
				break;
			}
		}
	}
	$oldlist=implode("\n",$oldlist);
	$content=$new_line.$oldlist;
	$content=push_heldtopics($content);

	writetofile("forum$forumid/list.php",$content);

	if ($action=="del") {
		$articlelist=file("forum$forumid/$filename");
		unset($articlelist[$article]);
		$articlelist=implode("",$articlelist);
		writetofile("forum$forumid/$filename",$articlelist);
	}

	jump_page("viblist.php?forumid=$forumid","执行成功",
"管理程式已经成功执行了操作：
<ul>
<li><a href='viblist.php?forumid=$forumid'>返回版块</a></li><br><br>
<li><a href='viboard.php'>返回论坛</a></li><br>
</ul><br>");
}

function print_err() {
global $status;
$title="发生错误";
$content="管理程式发生了错误，情况：<br>
<br>$status<br><ul>
<li><a href='javascript:history.back(1)'>返回前一页</a></li>
</ul>";
msg_box($title,$content);
}
function print_confirm() {
global $forumid,$filename,$article,$action;
$title="确认";
$content="管理程式需要您确认这次操作：<br>
<br>真的要继续吗？<ul>
<li><a href='javascript:history.back(1)'>返回前一页</a><br><br></li>
<li><a href='vibmanage2.php?action=$action&forumid=$forumid&filename=$filename&step=2";

if (!empty($article)) $content.="&article=$article";

$content.="'>确认并继续</a></li><br><br>";

msg_box($title,$content);
}
function print_bar() {
global $forum_name,$forumid;
navi_bar("您正处在<br>VIB 论坛 <br>的 MA 程式中",
"<a href=\"viblist.php?forumid=$forumid\">$forum_name</a>",
"MA程式","no");
}
