<?

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

get_forum_info();

if (empty($forumid) || empty($filename) || empty($action) || !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 ($login_status==1 && $check_user==0 && file_exists("datafile/admin.php")) {
		$adminlist=file("datafile/admin.php");
		$admincount=count($adminlist);
		for ($i=0; $i<$admincount; $i++) {
			$detail=explode("|", str_replace("\n","", $adminlist[$i]));
			if ($detail[0]==$forumid && $detail[1]==$username) $check_user=1;
		}
	}
	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 ($action=="move" || $action=="copy") {
	if (file_exists("forum$forumid/list.php")) {

//------get the line from the original list------
		$fp=fopen("forum$forumid/list.php","r");
		flock($fp,LOCK_SH);
		$oldlist=file("forum$forumid/list.php");
		fclose($fp);
		$count=count($oldlist);
		for ($i=0; $i<$count; $i++) {
			$detail=explode("|",$oldlist[$i]);
			if ($detail[5]==$filename) {
				$gotline=$oldlist[$i];
				unset($oldlist[$i]);
				break;
			}
		}

		if ($action=="move") {
		//------Delete from the original list-------
			$oldlist=implode("",$oldlist);
			writetofile("forum$forumid/list.php",$oldlist);
		}

//------move the file---------
		$fileinfo=explode("|",$gotline);
		$source="forum$forumid/$fileinfo[5]";

		if (file_exists("forum$newforumid/list.php")) {
			$newlist=readfromfile("forum$newforumid/list.php");
			$desname=get_next_filename($newlist,$newforumid);
		} else {
			$newlist="";
			$desname="f_1";
		}

		copy($source, "forum$newforumid/$desname");

		if ($action=="move") unlink($source);
		if (file_exists($source.".sta")) {
			copy($source.".sta", "forum$newforumid/$desname.sta");
			if ($action=="move") unlink($source.".sta");
		}
			if (file_exists($source.".u")) {
			copy($source.".u", "forum$newforumid/$desname.u");
			if ($action=="move") unlink($source.".u");
		}

//------write to the target LIST-----
		$gotline=str_replace($fileinfo[5],$desname,$gotline);
		$newlist=$gotline.$newlist;
		$newlist=push_heldtopics($newlist);
		writetofile("forum$newforumid/list.php",$newlist);

//----change the information in newforum's last_mo.php
		$temp=explode("|",readfromfile("forum$newforumid/last_mo.php"));
		$temp[0]="$detail[0],$detail[1],".get_date($timestamp)." ".get_time($timestamp).",$desname";
		$temp[1]++;
		writetofile("forum$newforumid/last_mo.php","$temp[0]|$temp[1]");

//----change the information in oldforum's last_mo.php
		$temp=explode("|",readfromfile("forum$forumid/last_mo.php"));
		$temp[1]--;
		writetofile("forum$forumid/last_mo.php","$temp[0]|$temp[1]");

	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>");
	}
}

if (file_exists("forum$forumid/list.php")) {
	$fp=fopen("forum$forumid/list.php","r");
	flock($fp,LOCK_SH);
	$oldlist=file("forum$forumid/list.php");
	fclose($fp);

	$count=count($oldlist);
	for ($i=0; $i<$count; $i++) {
		$detail=explode("|",$oldlist[$i]);
		if ($detail[5]==$filename) {
			if ($action=="lock") { $detail[9]=1; $oldlist[$i]=implode("|",$detail); break;}
			elseif ($action=="unlock") { $detail[9]=0; $oldlist[$i]=implode("|",$detail); break;}
			elseif ($action=="del") {
				$oldlist[$i]="";
				unlink("forum$forumid/$detail[5]");
				if (file_exists("forum$forumid/$detail[5].sta")) unlink("forum$forumid/$detail[5].sta");
				if (file_exists("forum$forumid/$detail[5].u")) unlink("forum$forumid/$detail[5].u");
				break;
			}
		}
	}
	$oldlist=implode("",$oldlist);
	writetofile("forum$forumid/list.php",$oldlist);

	if ($action=="del") {
//----change the information in newforum's last_mo.php
		$temp=explode("|",readfromfile("forum$forumid/last_mo.php"));
		$temp[1]--;
		writetofile("forum$forumid/last_mo.php","$temp[0]|$temp[1]");

//----change the information of the total posts of the bbs
		$temp=explode("|",readfromfile("datafile/newuser.php"));
		$temp[2]--;
		writetofile("datafile/newuser.php","$temp[0]|$temp[1]|$temp[2]");
	}
	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 $action,$forumid,$filename;
$title="确认";
if ($action=="move" || $action=="copy") {
	$content="<form action=\"vibmanage.php\" method=POST><select name=newforumid>";
	$forumlist=file("datafile/forumdata.php");
	$count=count($forumlist)-1;
	for ($i=0; $i<=$count; $i++) {
		$forum_list_info=explode("|",$forumlist[$i]);
		if ($forum_list_info[0]!="category" && $forum_list_info[3]!=$forumid) $content.="<OPTION value=\"$forum_list_info[3]\">$forum_list_info[1]";
	}
	$content.="</OPTION></select>
<br><br><input type=submit value=' 确 认 继 续  '><br><br>
<input type=hidden name=action value='$action'>
<input type=hidden name=filename value='$filename'>
<input type=hidden name=forumid value='$forumid'>
<input type=hidden name=step value='2'></form>";
}
else $content="
管理程式需要您确认这次操作：<br>
<br>
真的要继续吗？<br>";
$content.="<ul>
<li><a href='javascript:history.back(1)'>返回前一页</a></li>";
if ($action!="move" && $action!="copy") $content.="<br><br><li><a href='vibmanage.php?action=$action&forumid=$forumid&filename=$filename&step=2'>确认并继续</a></li>";
$content.="</ul>";
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");
}
