<?php
!function_exists('readover') && exit('Forbidden');

if ($job == 'member') {

	if ($_POST['step'] != 2) {

		require_once PrintHack('home');
		(int)$page < 1 && $page = 1;
		$pageid = ($page-1)*$db_perpage;
		$count = count($memberdb);
		$memberdb = array_slice($memberdb,$pageid,$db_perpage);
		$cymembers = array();
		foreach ($memberdb as $key => $value) {
			if (!$admindb[$winduid] && $value['ifadmin'] == -1) continue;
			$cymembers[$key] = $value;
		}
		unset($memberdb);
		if ($count > $db_perpage) {
			require_once(R_P.'require/forum.php');
			$pages = numofpage($count,$page,ceil($count/$db_perpage),"$basename&cyid=$cyid&job=member&$addpage");
		}

	} else {

		$selids = array();
		$ifadmin = '';
		InitGP(array('selid','action'),'P');
		if (is_array($selid)) {
			foreach ($selid as $value) {
				if (is_numeric($value) && $value!=$winduid) {
					$selids[] = $value;
				}
			}
			$selids && $selids = pwImplode($selids);
		}
		$passdb = array();
		if ($selids && in_array($action,array('deladmin','pass','del'))) {
			$query = $db->query("SELECT cm.uid,cm.username,cm.ifadmin FROM pw_cmembers cm WHERE cm.colonyid=".pwEscape($cyid)."AND cm.uid IN($selids)");
			while ($rt = $db->fetch_array($query)) {
				if ($rt['ifadmin'] != -1 && $action == 'deladmin') {
					$alldb['admin'] == $rt['username'] && Showmsg('colony_delladminfail');
					$ifadmin .= ($ifadmin ? ',' : '')."'$rt[uid]'";
				} elseif ($rt['ifadmin'] == -1 && $action == 'pass') {
					$alldb['intomoney'] > $credit->get($rt['uid'],$cn_moneytype) && Showmsg('colony_passfail');
					$passdb[] = $rt;
					$ifadmin .= ($ifadmin ? ',' : '')."'$rt[uid]'";
				} elseif ($action == 'del') {
					if ($alldb['admin'] == $rt['username']) {
						Showmsg('colony_delfail');
					} elseif ($windid != $alldb['admin'] && $rt['ifadmin'] == '1') {
						Showmsg('colony_manager');
					}
					$ifadmin .= ($ifadmin ? ',' : '')."'$rt[uid]'";
				}
			}
			$selids = $ifadmin;
			$ifadmin = 0;
		}
		!$selids && Showmsg('selid_illegal');

		if ($action == 'addamin' || $action == 'deladmin') {

			$action == 'addamin' && $ifadmin = 1;
			$db->update("UPDATE pw_cmembers SET ifadmin=".pwEscape($ifadmin,false)."WHERE colonyid=".pwEscape($cyid)."AND uid IN($selids)");
			Showmsg("colony_$action");

		} elseif ($action == 'pass') {

			$count = count($passdb);
			$u_a = array();

			foreach ($passdb as $key => $value) {
				$u_a[] = $value['uid'];
				$credit->addLog('hack_cyjoin',array($cn_moneytype => -$alldb['intomoney']),array(
					'uid'		=> $value['uid'],
					'username'	=> $value['username'],
					'ip'		=> $onlineip,
					'operator'	=> $windid,
					'cnname'	=> $alldb['cname']
				));
				$log = array(
					'type'      => 'cy_join',
					'username1' => Char_cv($value['username']),
					'username2' => Char_cv($windid),
					'field1'    => (int)$alldb['intomoney'],
					'field2'    => $cyid,
					'field3'    => Char_cv($alldb['cname']),
					'timestamp' => $timestamp,
					'ip'        => $onlineip,
					'descrip'   => 'join_descrip',
				);
				require_once(R_P.'require/writelog.php');
				writeforumlog($log);
				require_once(R_P.'require/msg.php');
				$message = array(
					'toUser'	=> $value['username'],
					'fromUid'	=> $winduid,
					'fromUser'	=> $windid,
					'subject'	=> 'join_title',
					'content'	=> 'join_content',
					'other'		=> array(
						'cn_name'	=> $cn_name,
						'allcname'	=> Char_cv($alldb['cname']),
						'cyid'		=> $cyid
					)
				);
				pwSendMsg($message);
				pwAddFeed($value['uid'], 'colony', array('lang' => 'colony_pass', 'colonyid' => $cyid, 'cname' => $alldb['cname']));
			}
			$credit->setus($u_a,array($cn_moneytype => -$alldb['intomoney']));

			$db->update("UPDATE pw_cmembers SET ifadmin=0 WHERE colonyid=".pwEscape($cyid)."AND uid IN($selids)");
			$db->update("UPDATE pw_colonys SET cmoney=cmoney+".pwEscape($alldb['intomoney']*$count)."WHERE id=".pwEscape($cyid));

			Showmsg('colony_pass');

		} elseif ($action == 'del') {

			$db->update("DELETE FROM pw_cmembers WHERE colonyid=".pwEscape($cyid)."AND uid IN($selids)");
			$count = $db->affected_rows();
			$db->update("UPDATE pw_colonys SET members=members-".pwEscape($count,false)."WHERE id=".pwEscape($cyid));
			Showmsg('colony_del');
		}
	}
} elseif ($job == 'seemember') {

	$uid = (int)$_GET['uid'];
	$uid != $winduid && !$admindb[$winduid] && $groupid!=3 && Showmsg('colony_cnmenber');
	@extract($db->get_one("SELECT id,username,realname,gender,tel,email,address,introduce FROM pw_cmembers WHERE colonyid=".pwEscape($cyid)."AND uid=".pwEscape($uid)));
	!$id && Showmsg('colony_noseecard');
	require_once PrintHack('home');

} elseif ($job == 'editmember') {

	$rt = $db->get_one("SELECT id,realname,gender,tel,email,address,introduce FROM pw_cmembers WHERE colonyid=".pwEscape($cyid)."AND uid=".pwEscape($winduid));
	if ($_POST['step'] != 2) {
		@extract($rt);
		!$id && Showmsg('colony_nocard');
		${'gender_'.$gender} = 'selected';
		require_once PrintHack('home');
	} else {
		InitGP(array('realname','tel','email','address','introduce'),'P');
		$gender = (int)$_POST['gender'];
		!$realname && Showmsg('colony_realname');
		if ($realname != $rt['realname']) {
			$rts = $db->get_one("SELECT id FROM pw_cmembers WHERE realname=".pwEscape($realname)."AND colonyid=".pwEscape($cyid));
			$rts['id'] && Showmsg('colony_samerealname');
			$updatesql = ",realname=".pwEscape($realname);
		}
		$db->update("UPDATE pw_cmembers"
			. " SET " . pwSqlSingle(array(
					'gender'	=> $gender,
					'tel'		=> $tel,
					'email'		=> $email,
					'address'	=> $address,
					'introduce'	=> $introduce
				)) . $updatesql
			. " WHERE colonyid=".pwEscape($cyid)
				. " AND uid=".pwEscape($winduid)
		);
		refreshto("$basename&cyid=$cyid&job=seemember&uid=$winduid",'colony_cardsuccess');
	}
} elseif ($job == 'boardlist') {

	require_once PrintHack('home');
	$ifsort=0;
	(int)$page < 1 && $page = 1;
	$limit = pwLimit(($page-1)*$db_perpage,$db_perpage);
	$argudb = array();
	$query  = $db->query("SELECT tid,author,authorid,lastpost,subject,topped,toppedtime FROM pw_argument WHERE gid=".pwEscape($cyid)."AND tpcid='0' ORDER BY topped DESC,lastpost DESC $limit");
	while ($rt = $db->fetch_array($query)) {
		if ($rt['topped']>0 && $rt['toppedtime'] && ($timestamp > $rt['toppedtime'])) {
			$db->update("UPDATE pw_argument SET topped='0',toppedtime='0' WHERE tid=".pwEscape($rt['tid']));
		}
		$rt['topped'] && $ifsort=1;
		$rt['lastpost'] = get_date($rt['lastpost']);
		$argudb[] = $rt;
	}
	$db->free_result($query);
	@extract($db->get_one("SELECT COUNT(*) AS count FROM pw_argument WHERE gid=".pwEscape($cyid)."AND tpcid='0'"));
	if ($count > $db_perpage) {
		require_once(R_P.'require/forum.php');
		$pages = numofpage($count,$page,ceil($count/$db_perpage),"$basename&cyid=$cyid&job=boardlist&");
	}
} elseif ($job == 'delboardlist') {

	if (!$admindb[$winduid] && $groupid != 3) {
		Showmsg('colony_delright');
	}
	$selids = '';
	InitGP(array('selid'),'P');
	if (is_array($selid)) {
		foreach ($selid as $value) {
			if (is_numeric($value)) {
				$selids[] = $value;
			}
		}
		$selids = pwImplode($selids);
	}
	!$selids && Showmsg('selid_illegal');
	$selids = strpos($selids,',')!==false ? "IN ($selids)" : "= $selids";
	if ($_POST['step'] != 2) {
		require_once PrintHack('home');
		$query = $db->query("SELECT tid,author,authorid,lastpost,subject FROM pw_argument WHERE gid=".pwEscape($cyid)."AND tpcid='0' AND tid $selids ORDER BY lastpost DESC");
		while ($rt = $db->fetch_array($query)) {
			$rt['lastpost'] = get_date($rt['lastpost']);
			$argudb[] = $rt;
		}
	} else {
		$db->update("DELETE FROM pw_argument WHERE tid $selids");
		$db->update("DELETE FROM pw_argument WHERE gid=".pwEscape($cyid)."AND tpcid $selids");
		refreshto("$basename&cyid=$cyid&job=boardlist",'colony_delsuccess');
	}
} elseif ($job == 'addboard') {

	if ($_POST['step'] != 2) {
		$tid = 0;
		$subject = $content = '';
		require_once PrintHack('home');
	} else {
		InitGP(array('subject','content'),'P');
		$tid <= 0 && !$subject && Showmsg('colony_posterror');
		strlen($subject)>50 && Showmsg('colony_subject');
		!$content && Showmsg('colony_posterror');
		$tid>0 && strlen($content)>255 && $content = substrs($content,255,'N');
		require_once(R_P.'require/postfunc.php');
		$content = autourl($content);
		$db->update("INSERT INTO pw_argument"
			. " SET " . pwSqlSingle(array(
				'tpcid'		=> $tid,
				'gid'		=> $cyid,
				'author'	=> $windid,
				'authorid'	=> $winduid,
				'postdate'	=> $timestamp,
				'lastpost'	=> $timestamp,
				'subject'	=> $subject,
				'content'	=> $content
		)));
		if ($tid) {
			$db->update("UPDATE pw_argument SET lastpost=".pwEscape($timestamp)."WHERE tid=".pwEscape($tid));
		} else {
			$tid = $db->insert_id();
		}
		refreshto("$basename&job=readboard&cyid=$cyid&tid=$tid",'colony_postsuccess');
	}
} elseif ($job == 'editboard') {

	if ($_POST['step'] != 2) {
		@extract($db->get_one("SELECT subject,content FROM pw_argument WHERE tid=".pwEscape($tid)));
		!$subject && Showmsg('illegal_tid');
		require_once PrintHack('home');
	} else {
		InitGP(array('subject','content'),'P');
		(!$subject || !$content) && Showmsg('colony_posterror');
		strlen($subject)>50 && Showmsg('colony_subject');
		strlen($content)>255 && $content = substrs($content,255,'N');
		require_once(R_P.'require/postfunc.php');
		$content = autourl($content);
		$db->update("UPDATE pw_argument SET ".pwSqlSingle(array('subject'=>$subject,'content'=>$content))."WHERE tid=".pwEscape($tid));
		refreshto("$basename&job=readboard&cyid=$cyid&tid=$tid",'colony_postsuccess');
	}
} elseif ($job == 'delboard') {

	@extract($db->get_one("SELECT tpcid,gid,authorid FROM pw_argument WHERE tid=".pwEscape($tid)));
	!$authorid && Showmsg('illegal_tid');
	if ($cyid != $gid || ($authorid!=$winduid && !$admindb[$winduid] && $groupid!=3)) {
		Showmsg('colony_delright');
	}
	$db->update("DELETE FROM pw_argument WHERE tid=".pwEscape($tid));
	if (!$tpcid) {
		$db->update("DELETE FROM pw_argument WHERE gid=".pwEscape($gid)."AND tpcid=".pwEscape($tid));
		$refreshto = "$basename&job=boardlist&cyid=$gid";
	} else {
		$refreshto = "$basename&job=readboard&cyid=$gid&tid=$tpcid";
	}
	refreshto($refreshto,'colony_delsuccess');

} elseif ($job == 'readboard') {

	@extract($db->get_one("SELECT a.tpcid,a.gid,a.author,a.authorid,a.postdate,a.lastpost,a.subject,a.content,cm.ifadmin FROM pw_argument a LEFT JOIN pw_cmembers cm ON a.authorid=cm.uid WHERE a.tid=".pwEscape($tid)));
	(!$subject || $cyid != $gid || $tpcid != 0) && Showmsg('illegal_tid');
	require_once PrintHack('home');
	$adminimg = $ifadmin==1 ? "<img src=\"$imgpath/$stylepath/group/3.gif\" align=\"absmiddle\"> " : "<img src=\"$imgpath/$stylepath/group/6.gif\" align=\"absmiddle\"> ";
	require_once(R_P.'require/bbscode.php');
	$havereply = $postdate != $lastpost ? true : false;
	$postdate = get_date($postdate);
	$content = convert(nl2br($content),$db_windpost);
	$count = (int)$count;
	$readdb = array();
	if ($havereply) {
		(int)$page < 1 && $page = 1;
		@extract($db->get_one("SELECT COUNT(*) AS count FROM pw_argument WHERE gid=".pwEscape($gid)."AND tpcid=".pwEscape($tid)));
		if ($count > $db_perpage) {
			require_once(R_P.'require/forum.php');
			$pages = numofpage($count,$page,ceil($count/$db_perpage),"$basename&job=readboard&cyid=$gid&tid=$tid&");
		} else {
			$page = 1;
		}
		$limit = pwLimit(($page-1)*$db_perpage,$db_perpage);
		$query = $db->query("SELECT DISTINCT a.tid,a.tpcid,a.gid,a.author,a.authorid,a.postdate,a.subject,a.content,cm.ifadmin FROM pw_argument a LEFT JOIN pw_cmembers cm ON a.authorid=cm.uid WHERE a.gid=".pwEscape($gid)."AND a.tpcid=".pwEscape($tid)."AND cm.colonyid=".pwEscape($gid)."ORDER BY postdate DESC $limit");
		while ($rt = $db->fetch_array($query)) {
			$start_limit++;
			$rt['lou'] = $start_limit;
			$rt['postdate'] = get_date($rt['postdate']);
			$rt['content'] = convert(nl2br($rt['content']),$db_windpost);
			$rt['adminimg'] = $rt['ifadmin']==1 ? "<img src=\"$imgpath/$stylepath/group/3.gif\" align=\"absmiddle\"> " : "<img src=\"$imgpath/$stylepath/group/6.gif\" align=\"absmiddle\"> ";
			$readdb[] = $rt;
		}
		$db->free_result($query);
	}
} elseif ($job == 'topboard') {

	if (!$admindb[$winduid] && $groupid != 3) {
		Showmsg('colony_topright');
	}
	$selids = array();
	InitGP(array('selid'),'P');
	if (is_array($selid)) {
		foreach ($selid as $value) {
			if (is_numeric($value)) {
				$selids[] = $value;
			}
		}
		$selids && $selids = pwImplode($selids);
	}
	!$selids && Showmsg('selid_illegal');
	$selids = strpos($selids,',')!==false ? "IN ($selids)" : "= $selids";

	if ($_POST['step'] != 2) {
		require_once PrintHack('home');
		$query = $db->query("SELECT tid,author,authorid,lastpost,subject FROM pw_argument WHERE gid=".pwEscape($cyid)."AND tpcid='0' AND tid $selids ORDER BY lastpost DESC");
		while($rt = $db->fetch_array($query)){
			$rt['lastpost'] = get_date($rt['lastpost']);
			$argudb[] = $rt;
		}
	} else {
		InitGP(array('topped','toppedtime'),'P');
		$toppedtime = (int)$toppedtime;
		$toppedtime && $toppedtime = $timestamp + 86400*$toppedtime;
		$db->update("UPDATE pw_argument SET ".pwSqlSingle(array('topped'=>$topped,'toppedtime'=>$toppedtime))."WHERE tid $selids");
		refreshto("$basename&cyid=$cyid&job=boardlist",'colony_topsuccess');
	}
} elseif ($job == 'honor') {

	require_once PrintHack('home');
	$honordb = $honor_order = array();
	foreach ($memberdb as $key => $value) {
		$honor_order[$key] = (int)$value['honor'];
	}
	array_multisort($honor_order,SORT_DESC,$memberdb);
	$num = 0;
	foreach ($memberdb as $key => $value) {
		if ($num < 10 && $value['ifadmin']!=-1) {
			$num++;
			$value['order'] = $num;
			$honordb[$key] = $value;
		}
	}
	unset($memberdb);

} elseif ($job == 'donate') {


	if ($_POST['step'] != 2) {

		$usermoney = $credit->get($winduid,$cn_moneytype);
		require_once PrintHack('home');

	} else {

		InitGP(array('sendmoney'),'P',2);
		$sendmoney < 1 && Showmsg('colony_donateerror');

		if (procLock('colony_donate',$winduid)) {

			$usermoney = $credit->get($winduid,$cn_moneytype);
			$sendmoney > $usermoney && Showmsg('colony_donatefail');
			$honor = $sendmoney*1;//荣誉点与金钱的比率
			/*
			$lockfile = D_P.'data/bbscache/lock_bank.txt';
			$fp = fopen($lockfile,'wb+');
			flock($fp,LOCK_EX);
			*/
			$credit->addLog('hack_cydonate',array($cn_moneytype => -$sendmoney),array(
				'uid'		=> $winduid,
				'username'	=> $windid,
				'ip'		=> $onlineip,
				'cnname'	=> $alldb['cname']
			));
			$credit->set($winduid,$cn_moneytype,-$sendmoney);

			$db->update("UPDATE pw_colonys SET cmoney=cmoney+".pwEscape($sendmoney)."WHERE id=".pwEscape($cyid));
			$db->update("UPDATE pw_cmembers SET honor=honor+".pwEscape($honor)."WHERE uid=".pwEscape($winduid)."AND colonyid=".pwEscape($cyid));

			//fclose($fp);
			$log = array(
				'type'      => 'cy_donate',
				'username1' => Char_cv($windid),
				'username2' => '',
				'field1'    => $sendmoney,
				'field2'    => $cyid,
				'field3'    => Char_cv($alldb['cname']),
				'timestamp' => $timestamp,
				'ip'        => $onlineip,
				'cname'		=> $alldb['cname'],
				'descrip'   => 'donate_descrip',
			);
			require_once(R_P.'require/writelog.php');
			writeforumlog($log);

			require_once(R_P.'require/msg.php');
			$message = array(
				'toUser'	=> $alldb['admin'],
				'subject'	=> 'donate_title',
				'content'	=> 'donate_content',
				'other'		=> array(
					'windid'	=> $windid,
					'cn_name'	=> $cn_name,
					'allcname'	=> $alldb['cname'],
					'moneyname'	=> $credit->cType[$cn_moneytype],
					'sendmoney'	=> $sendmoney
				)
			);
			pwSendMsg($message);

			procUnLock('colony_donate',$winduid);
		}
		Showmsg('colony_donatesuccess');
	}
} elseif ($job == 'donatelog') {

	require_once PrintHack('home');
	(int)$page < 1 && $page = 1;
	@extract($db->get_one("SELECT COUNT(*) AS count FROM pw_forumlog WHERE field2=".pwEscape($cyid)."AND type='cy_donate'"));
	if ($count > $db_perpage) {
		require_once(R_P.'require/forum.php');
		$pages = numofpage($count,$page,ceil($count/$db_perpage),"$basename&job=donatelog&cyid=$cyid&");
	} else {
		$page = 1;
	}
	$logdb = array();
	$query = $db->query("SELECT username1,descrip,timestamp FROM pw_forumlog WHERE field2=".pwEscape($cyid)."AND type='cy_donate' ORDER BY id DESC LIMIT ".($page-1)*$db_perpage.",$db_perpage");
	while ($rt = $db->fetch_array($query)) {
		$rt['timestamp'] = get_date($rt['timestamp'],"Y-m-d H:i");
		$rt['descrip'] = str_replace(array('[b]','[/b]'),array('<b>','</b>'),$rt['descrip']);
		$logdb[] = $rt;
	}
	$db->free_result($query);
}

require_once PrintHack('setting');footer();
?>