<?php
define('THIS_SCRIPT', 'group');
include_once 'global.php';
$lang = load_lang($lang,'group');
checklogin();
header("content-Type: text/html; charset=".$charset);
head();
if ( strpos($sysinfo['flag'],'15,') === false ){
	show_msg($lang['readme_tit'],$lang['error.str1'],0,'','','','');
	exit;
}
switch($action){
	case 'editgroup':
		editgroup();
		break;
	case 'savesysgroup':
		savesysgroup();
		break;
	case 'saveusergroup':
		saveusergroup();
		break;
	case 'delusergroup':
		delusergroup();
		break;
	case 'online':
		grouponline();
		break;
	case 'saveonline':
		saveonline();
		break;
	default:
		usergroup();
		break;
}

function usergroup(){
	global $dv,$db,$action,$lang;
	$query=$db->query("Select usergroupid,usertitle,minarticle,grouppic,usergroupid as usercount,title,issetting From {$dv}usergroups Where parentgid=3 Order By minarticle");
	while($gs = $db->fetch_array($query)){
		$usercount=$db->scalar("Select Count(*) From {$dv}user Where usergroupid={$gs[usergroupid]}");
		$gs['usercount'] = $usercount;
		$_group1[] = $gs;
	}
	$query=$db->query("Select usergroupid,usertitle,minarticle,grouppic,usergroupid as usercount,title,issetting From {$dv}usergroups Where parentgid=1 Order By minarticle");
	while($gs = $db->fetch_array($query)){
		$usercount=$db->scalar("Select Count(*) From {$dv}user Where usergroupid={$gs[usergroupid]}");
		$gs['usercount'] = $usercount;
		$_group2[] = $gs;
	}
	$query=$db->query("Select usergroupid,usertitle,minarticle,grouppic,usergroupid as usercount,title,issetting From {$dv}usergroups Where parentgid=2 Order By minarticle");
	while($gs = $db->fetch_array($query)){
		$usercount=$db->scalar("Select Count(*) From {$dv}user Where usergroupid={$gs[usergroupid]}");
		$gs['usercount'] = $usercount;
		$_group3[] = $gs;
	}
	$query=$db->query("Select usergroupid,usertitle,minarticle,grouppic,usergroupid as usercount,title,issetting From {$dv}usergroups Where parentgid=4 Order By minarticle");
	while($gs = $db->fetch_array($query)){
		$usercount=$db->scalar("Select Count(*) From {$dv}user Where usergroupid={$gs[usergroupid]}");
		$gs['usercount'] = $usercount;
		$_group4[] = $gs;
	}
	$query=$db->query("Select usergroupid,usertitle,minarticle,grouppic,usergroupid as usercount,title,issetting From {$dv}usergroups Where parentgid=5 Order By minarticle");
	while($gs = $db->fetch_array($query)){
		$usercount=$db->scalar("Select Count(*) From {$dv}user Where usergroupid={$gs[usergroupid]}");
		$gs['usercount'] = $usercount;
		$_group5[] = $gs;
	}
	require_once printout('group');
}

function savesysgroup(){
	global $dv,$db,$action,$lang;
	global $parentid,$usertitleid,$issetting,$usertitle,$title,$titlepic;
	$query=$db->query("Select usergroupid From {$dv}usergroups");
	while($gs = $db->fetch_array($query)){
		$groupidlist .= ',' .$gs['usergroupid'];
	}
	if ($parentid == '' || !is_numeric($parentid)){
		show_msg($lang['forum.title'],$lang['err.info1'],0,'','','javascript:void(window.history.back())',$lang['comback']);
		exit;
	}
	$foundissetting = true;
	$groupidlist = str_replace(',','|',$groupidlist);
	$groupidlist = $groupidlist. '|';
	foreach($usertitleid as $key=>$val){
		if (is_numeric($val) && $usertitle[$key] != '' && $titlepic[$key] != ''){
			$query=$db->query("Select usertitle,grouppic From {$dv}usergroups Where parentgid={$parentid} And usergroupid={$val}");
			if ($gs = $db->fetch_array($query)){
				if ($gs['usertitle'] != $usertitle[$key] || $gs['grouppic'] != $titlepic[$key]){
					$db->query("Update {$dv}user Set UserClass='{$usertitle[$key]}',TitlePic='{$titlepic[$key]}' Where usergroupid={$val}");
				}
			}
			if ($parentid == 4 && $issetting[$key] != ''){
				$missetting = explode('|',$issetting[$key]);
				for ($k=0; $k<count($missetting); $k++){
					if (strpos($groupidlist,'|'.$missetting[$k].'|') !== true){
						$foundissetting = false;
						break;
					}
				}
				if ($foundissetting !== false){
					$db->query("Update {$dv}usergroups Set title='$title[$key]',usertitle='$usertitle[$key]',grouppic='$titlepic[$key]',issetting='$issetting[$key]' Where usergroupid={$val}");
					if ($val == 0){
						$query=$db->query("Select titlepic,groupsetting From {$dv}usergroups Where usergroupid=4");
						$gs = $db->fetch_array($query);
						$db->query("Insert Into {$dv}usergroups (title,usertitle,groupsetting,orders,minarticle,titlepic,grouppic,parentgid,issetting) Values ('$title[$key]','$usertitle[$key]','$gs[groupsetting]',0,0,'$gs[titlepic]','$titlepic[$key]','$parentid','$issetting[$key]')");
					}
				}else{
					$db->query("Update {$dv}usergroups Set title='$title[$key]',usertitle='$usertitle[$key]',grouppic='$titlepic[$key]' Where usergroupid={$val}");
				}
			}else{
				$db->query("Update {$dv}usergroups Set title='$title[$key]',usertitle='$usertitle[$key]',grouppic='$titlepic[$key]' Where usergroupid={$val}");
				if ($val == 0){
					$query=$db->query("Select titlepic,groupsetting From {$dv}usergroups Where usergroupid=4");
					$gs = $db->fetch_array($query);
					$db->query("Insert Into {$dv}usergroups (title,usertitle,groupsetting,orders,minarticle,titlepic,grouppic,parentgid) Values ('$title[$key]','$usertitle[$key]','$gs[groupsetting]',0,0,'$gs[titlepic]','$titlepic[$key]','$parentid')");
				}
			}
		}
	}
	require_once INC_PATH.'BoardCache.class.php';
	$mycache =& new BoardCache($db);
	$mycache->createusergroup();
	show_msg($lang['forum.title'],$lang['suc.info1'],1,'','','group.php',$lang['comback']);
	exit;
}
function saveusergroup(){
	global $dv,$db,$action,$lang;
	global $parentid,$usertitleid,$issetting,$usertitle,$title,$titlepic,$minarticle;
	foreach($usertitleid as $key=>$val){
		if (is_numeric($val) && $usertitle[$key] != '' && $titlepic[$key] != '' && is_numeric($minarticle[$key])){
			$query=$db->query("Select usertitle,grouppic From {$dv}usergroups Where parentgid=3 And usergroupid={$val}");
			if ($gs = $db->fetch_array($query)){
				if ($gs['usertitle'] != $usertitle[$key] || $gs['grouppic'] != $titlepic[$key]){
					$db->query("Update {$dv}user Set UserClass='{$usertitle[$key]}',TitlePic='{$titlepic[$key]}' Where usergroupid={$val}");
				}
				$db->query("Update {$dv}usergroups Set usertitle='$usertitle[$key]',minarticle='$minarticle[$key]',grouppic='$titlepic[$key]' Where usergroupid={$val}");
			}
			if ($val == 0){
				$query=$db->query("Select titlepic,groupsetting From {$dv}usergroups Where usergroupid=4");
				$gs = $db->fetch_array($query);
				$db->query("Insert Into {$dv}usergroups (title,usertitle,groupsetting,orders,minarticle,titlepic,grouppic,parentgid) Values ('$title[$key]','$usertitle[$key]','$gs[groupsetting]',0,'$minarticle[$key]','$gs[titlepic]','$titlepic[$key]',3)");
			}
		}
	}
	require_once INC_PATH.'BoardCache.class.php';
	$mycache =& new BoardCache($db);
	$mycache->createusergroup();
	show_msg($lang['forum.title'],$lang['suc.info1'],1,'','','javascript:void(window.history.back())',$lang['comback']);
	exit;
}

function delusergroup(){
	global $dv,$db,$action,$lang,$id;
	if ($id == '' || !is_numeric($id)){
		show_msg($lang['forum.title'],$lang['err.info2'],0,'','','javascript:void(window.history.back())',$lang['comback']);
		exit;
	}
	$query=$db->query("Select usergroupid,parentgid,minarticle From {$dv}usergroups Where parentgid!=1 And usergroupid={$id}");
	if ($gs = $db->fetch_array($query)){
		if ($gs['usergroupid'] != 8 && $gs['parentgid'] == 2){
			$query_1=$db->query("Select usergroupid,usertitle,grouppic From {$dv}usergroups Where parentgid=3 Order by minarticle Desc Limit 1");
			if ($tgs = $db->fetch_array($query_1)){
				$db->query("UPDATE {$dv}user SET userclass = '$tgs[usertitle]', titlepic = '$tgs[grouppic]', usergroupid = '$tgs[usergroupid]' WHERE usergroupid={$id}");
				$db->query("DELETE FROM {$dv}usergroups WHERE usergroupid={$id}");
			}else{
				show_msg($lang['forum.title'],$lang['err.info4'],0,'','','javascript:void(window.history.back())',$lang['comback']);
				exit;
			}
		}else{
			$query_1=$db->query("Select usergroupid,usertitle,grouppic From {$dv}usergroups Where parentgid=3 And usergroupid!={$id} And minarticle<={$gs[minarticle]} Order By minarticle Desc Limit 1");
			if ($tgs = $db->fetch_array($query_1)){
				$db->query("UPDATE {$dv}user SET userclass = '$tgs[usertitle]', titlepic = '$tgs[grouppic]', usergroupid = '$tgs[usergroupid]' WHERE usergroupid={$id}");
				$db->query("DELETE FROM {$dv}usergroups WHERE usergroupid={$id}");
			}else{
				show_msg($lang['forum.title'],$lang['err.info5'],0,'','','javascript:void(window.history.back())',$lang['comback']);
				exit;
			}
		}
	}else{
		show_msg($lang['forum.title'],$lang['err.info3'],0,'','','javascript:void(window.history.back())',$lang['comback']);
		exit;
	}
	require_once INC_PATH.'BoardCache.class.php';
	$mycache =& new BoardCache($db);
	$mycache->createusergroup();
	show_msg($lang['forum.title'],$lang['suc.info2'],1,'','','javascript:void(window.history.back())',$lang['comback']);
	exit;
}

function editgroup(){
	global $dv,$db,$action,$lang,$groupid,$groupaction,$title,$grouppic,$editgroupid;
	global $_POST,$_DCACHE,$CheckGroupPic;
	if ( $groupid != '' && !is_numeric(str_replace(',','',$groupid)) ){
		show_msg($lang['forum.title'],$lang['err.info1'],0,'','','javascript:void(window.history.back())',$lang['comback']);
		exit;
	}
	if ($groupaction == 'yes'){
		if ($editgroupid == ''){
			show_msg($lang['forum.title'],$lang['err.info1'],0,'','','javascript:void(window.history.back())',$lang['comback']);
			exit;
		}
		if ($title == ''){
			show_msg($lang['forum.title'],$lang['err.info7'],0,'','','javascript:void(window.history.back())',$lang['comback']);
			exit;
		}
		include_once CACHE_PATH.'forum_setting.php';
		require_once ROOT_PATH.'templates/'.$_DCACHE['settings']['styletpl'].'/grouppermission.tpl.php';
		$GroupSetting = getgrouppermission();
		$updatestr = '';
		if ( $groupid == '' ){
			$groupid = $editgroupid;
		}else{
			$groupid = $editgroupid.','.$groupid;
		}
		$query=$db->query("Select usergroupid,usertitle,grouppic,groupsetting From {$dv}usergroups Where usergroupid in ({$groupid})");
		while($gs = $db->fetch_array($query)){
			if ( $gs['usergroupid'] == $editgroupid ){
				$db->query("Update {$dv}usergroups Set usertitle='$title',grouppic='$grouppic',groupsetting='".$GroupSetting."' Where usergroupid={$gs[usergroupid]}");
			}else{
				$newstr = explode(',',$GroupSetting);
				$oldstr = explode(',',$gs['groupsetting']);
				for($k=0; $k<94; $k++){
					if ($_POST["CheckGroupSetting($k)"] == 1){
						$updatestr .= $newstr[$k];
					}else{
						$updatestr .= $oldstr[$k];
					}
					if ($k < 92){
						$updatestr .= ',';
					}
				}
				if ($CheckGroupPic == 'on'){
					$grouppicstr = ",grouppic='$grouppic'";
				}
				$db->query("Update {$dv}usergroups Set groupsetting='$updatestr'{$grouppicstr} Where usergroupid={$gs[usergroupid]}");
				$updatestr = '';
				$grouppicstr = '';
			}
		}
		require_once INC_PATH.'BoardCache.class.php';
		$mycache =& new BoardCache($db);
		$mycache->createusergroup();
		show_msg($lang['forum.title'],$lang['suc.info3'],1,'','','javascript:void(window.history.back())',$lang['comback']);
		exit;
	}else{
		$query=$db->query("Select usergroupid,usertitle,grouppic,parentgid,groupsetting From {$dv}usergroups Where usergroupid={$groupid}");
		if ($gs = $db->fetch_array($query)){
			include_once CACHE_PATH.'forum_setting.php';
			$lang = load_lang($lang, 'grouppermission',ROOT_PATH.'templates/lang/'.$_DCACHE['settings']['lang'].'/');
			require_once ROOT_PATH.'templates/'.$_DCACHE['settings']['styletpl'].'/grouppermission.tpl.php';
			$regroupsetting = $gs['groupsetting'];
			require_once printout('group');
		}else{
			show_msg($lang['forum.title'],$lang['err.info6'],0,'','','javascript:void(window.history.back())',$lang['comback']);
			exit;
		}
	}
}

function grouponline(){
	global $dv,$db,$action,$lang;
	$query=$db->query("Select usergroupid,usertitle,orders,titlepic,parentgid From {$dv}usergroups Order By parentgid,usergroupid");
	require_once printout('group');
}

function saveonline(){
	global $dv,$db,$action,$lang,$usertitleid,$orders,$titlepic;
	foreach($usertitleid as $key=>$val){
		$db->query("Update {$dv}usergroups Set orders='$orders[$key]',titlepic='$titlepic[$key]' Where usergroupid={$val}");
	}
	show_msg($lang['forum.title'],$lang['suc.info4'],1,'','','javascript:void(window.history.back())',$lang['comback']);
	exit;
}

footer();
?>