<?php
define('THIS_SCRIPT', 'indivgroup_manage');
require_once 'global.php';
$lang = load_lang($lang, 'indivgroup_manage' );
include_once $fsetting['manageurl'].'inc/page.class.php';
global $title_num,$action,$reaction,$membername;
$title_num = array(0=>array('linktitle'=>$lang['template.strings0'],'linkurl'=>'indivgroup.php'),1=>array('linktitle'=>$lang['tpl.str66'],'linkurl'=>'indivgroup_index.php?groupid='.$_GET['groupid']));

if(!empty($membername))
	$mygrouplink= '<a href="indivgroup.php?action=mygrouplist">'.$lang['template.strings2'].'</a>';
else
	$mygrouplink='';

if(!checkpower()){
	head();
	showmsg($lang['error_user']);
	footer();
}
if($fsetting['indivgroup_show'] == 0)
{
	head();
	showmsg($lang['error_close']);
	footer();
}
switch ($_GET['action']) {
	case "infosave":
		save_info();
		break;
	case "newboard":
		board_new();
		break;
	case "boardmanage":
		head(1,0,0,$title_num,$mygrouplink);
		board_info();
		footer();
		break;
	case "board":
		head(1,0,0,$title_num,$mygrouplink);
		board_list();
		footer();
		break;
	case "boardeditsave":
		board_save();
		break;
	case "boarddel":
		board_del();
		break;
	case "boardedit":
		head(1,0,0,$title_num,$mygrouplink);
		board_edit_info();
		footer();
		break;
	case "user":
		head(1,0,0,$title_num,$mygrouplink);
		user_list();
		footer();
		break;
	case "useredit":
		head(1,0,0,$title_num,$mygrouplink);
		user_edit_info();
		footer();
		break;
	case "usereditsave":
		user_save();
		break;
	case "usersetadmin":
		head(1,0,0,$title_num,$mygrouplink);
		user_setadmin();
		footer();
		break;
	case "userdel":
		head(1,0,0,$title_num,$mygrouplink);
		user_del();
		footer();
		break;
	case "updatedata":
		head(1,0,0,$title_num,$mygrouplink);
		update_group();
		footer();
		break;
	default:
		head(1,0,0,$title_num,$mygrouplink);// print head;
		group_info();
		footer();
}

function checkpower(){
	global $db,$lang,$dv,$restriction,$userid,$membername,$boardmaster;
	$groupid=intval($_GET['groupid']);

	if($boardmaster)
		return true;
	else{
		$query=$db->query("select id,stats,appuserid from {$dv}group_name where id='".$groupid."'");
		if($ts = $db->fetch_array($query)) 
		{
			if($ts["stats"] == '0')
				return false;
			else if($ts["appuserid"] == $userid)
				return true;
			else{
				$query=$db->query("select id from {$dv}group_user where islock=2 and userid='".$appid."' and groupid='".$groupid."'");
				if ($ts = $db->fetch_array($query)) 
					return true;
				else
					return false;
			}
		}
		else
		{
			return false;
		}
	}
}
function save_info(){//boardname,boardinfo,indeximg,boardrules,boardstats
	global $db,$lang,$dv,$restriction,$page,$userid,$membername,$fsetting;
	$GroupName= trim($_POST['GroupName']);
	$GroupInfo=$_POST['GroupInfo'];
	$GroupStats=$_POST['GroupStats'];
	$appid=$_POST['appid'];
	$Locked=intval($_POST['Locked']);
	$ViewFlag=intval($_POST['ViewFlag']);
	$groupid=intval($_POST['groupid']);

	$retStr= "";

	if(($groupid == '') || ($groupid == 0))
	{
		$retStr= $lang['error_url'];
	}
	else if($GroupName == '')
	{
		$retStr= $lang['error_name'];
	}
	else if($membername == '')
	{
		$retStr= $lang['error_app'];
	}
	else
	{
		$GroupName= $db->escape_string($GroupName);
		$GroupInfo= $db->escape_string($GroupInfo);

		$query=$db->query("select id from {$dv}group_name where id<>'".$groupid."' and groupname='".$GroupName."'");
		if ($ts = $db->fetch_array($query)) 
		{
			$retStr= $lang['error_rename'];//."select id from {$dv}group_name where id<>".$groupid." and groupname=".$GroupName;
		}
		else
		{
			$faceurl= '';
			$upfileinfo =& $_FILES['groupface'];

			if (empty($upfileinfo) OR 4 == $upfileinfo['error']){
				$faceurl= '';
			}
			else{
				$upfileinfo['tmp_name'] = str_replace('\\\\', '\\', $upfileinfo['tmp_name']);
				if (false === is_uploaded_file(str_replace('\\\\', '\\', $upfileinfo['tmp_name']))) {
					//无法取得文件名
				}
				else{
					$fileext = str_replace('.','',strrchr($upfileinfo['name'],'.'));
					if (false === $fileext) {
						list(,$fileext) = explode('/',$upfileinfo['type']);
						if (strpos($fileext,'-') !== false) {
							$fileext = str_replace('-','',strstr($fileext,'-'));
						}
					}
					$lowerext = trim(strtolower($fileext));
					
					if (false === strpos(',gif,jpg,bmp,jpeg,png,', ",{$lowerext},")) {
						showmsg("文件类型{$fileExt}不正确!");
						return;
					}
					$upbasedir = $fsetting['forumuploadname'];

					$len = strlen($upbasedir);
					if ('/' === $upbasedir{$len - 1}) {
						$upbasedir = substr($upbasedir, 0, $lent - 1);
					}

					$filename = $userid.'_'.md5(uniqid(rand())).'.'.$lowerext;
					$destfile = $upbasedir.'/faces/'.$filename;
					$tmpfile = $upbasedir.'/tempimage/'.$filename;
					if (move_uploaded_file($upfileinfo['tmp_name'], $destfile)) {
						$faceurl= ",groupface='".$destfile."'";
					}$faceurl= ",groupface='".$destfile."'";
				}
			}
			if(($appid == '') || ($appid == '0')){
				$db->query("update {$dv}group_name set groupname='".$GroupName."',groupinfo='".$GroupInfo."',stats='".$GroupStats."',passdate=now(),locked='".$Locked."',viewflag='".$ViewFlag."'".$faceurl." where id='".$groupid."'");
			}
			else{
				$query=$db->query("select username from {$dv}group_user where islock=2 and userid='".$appid."' and groupid='".$groupid."'");
				if ($ts = $db->fetch_array($query)) 
				{
					$db->query("update {$dv}group_name set groupname='".$GroupName."',groupinfo='".$GroupInfo."',appuserid='".$appid."',appusername='".$ts["username"]."',stats='".$GroupStats."',passdate=now(),locked='".$Locked."',viewflag='".$ViewFlag."'".$faceurl." where id='".$groupid."'");
				}
				else
				{
					$retStr= $lang['new_recept'];
				}
			}
		}
	}

	if($retStr == '')
	{
		echo "<script>parent.submitform_suc();</script>";
	}
	else
		echo "<script>parent.information('".$retStr."');</script>";
}

function group_info(){
	global $dv,$db,$lang,$page,$total,$page_size,$offset,$page_count,$p,$userid,$membername,$boardmaster;
	$groupid= intval($_GET['groupid']);
	$sql= "select * from {$dv}group_name where id={$groupid}";
	if($query=$db->query($sql)){
			if ($arr=$db->fetch_array($query)){
				$group_info=$arr;

				if($group_info["viewflag"] == '1')
					$viewflag= $lang['error_user'];
				else
					$viewflag= $lang['tpl.str2'];
				if($group_info["locked"] == '1')
					$locked= $lang['tpl.str3'];
				else
					$locked= $lang['tpl.str4'];
				if($group_info['stats'] == 1)
					$stats= $lang['item_type_1'];
				else if($group_info['stats'] == 2)
					$stats= $lang['item_type_2'];
				else if($group_info['stats'] == 3)
					$stats= $lang['item_type_3'];
				else
					$stats= $lang['item_type_0'];

				if($boardmaster || ($group_info['appusername'] == $membername))
				{
					$manage_list= array();

					$sql= "select userid,username from {$dv}group_user where islock='2' and groupid='".$group_info['id']."'";
					$query=$db->query($sql);
					while($arr=$db->fetch_array($query))
						$manage_list[]=$arr;

				}
			}
	}
	$db->free_result($query);

	require_once printout('indivgroup_manage');
}
function board_info(){
	global $dv,$db,$lang,$page,$total,$page_size,$offset,$page_count,$p,$userid,$membername,$boardmaster;
	$groupid= intval($_GET['groupid']);

	require_once printout('indivgroup_manage');
}
function board_list(){
	global $dv,$db,$lang,$page,$total,$page_size,$offset,$page_count,$p,$userid,$membername,$boardmaster;
	$groupid= intval($_GET['groupid']);
	$sql= "select * from {$dv}group_board where rootid={$groupid}";
	if($query=$db->query($sql)){
			while ($arr=$db->fetch_array($query)){
				$board_list[]=$arr;
			}
	}
	$db->free_result($query);

	require_once printout('indivgroup_manage');
}
function board_edit_info(){
	global $dv,$db,$lang,$page,$total,$page_size,$offset,$page_count,$p,$userid,$membername,$boardmaster;
	$groupid= intval($_GET['groupid']);
	$groupboardid= intval($_GET['groupboardid']);
	$sql= "select * from {$dv}group_board where rootid={$groupid} and id={$groupboardid}";
	if($query=$db->query($sql)){
			if ($arr=$db->fetch_array($query)){
				$board_info=$arr;
			}
	}
	$db->free_result($query);

	require_once printout('indivgroup_manage');
}
function board_new(){//boardname,boardinfo,indeximg,boardrules,boardstats
	global $db,$lang,$dv,$restriction,$page,$userid,$membername;
	$vowels = array("/", "*", "%", "&","?", "@", "$", "|", "^", "~","#");
    $boardname = str_replace($vowels,"",$_POST['boardname']);
    $boardinfo = str_replace($vowels,"",$_POST['boardinfo']);
    $indeximg= strip_tags(trim($_POST['indeximg']));
    $boardrules = str_replace($vowels,"",$_POST['boardrules']);
	$boardstats= intval($_POST['boardstats']);
	$groupid= intval($_POST['groupid']);
    
	$retStr= "";

	if(($groupid == '') || ($groupid == 0))
	{
		$retStr= $lang['error_url'];
	}
	else if($boardname == '')
	{
		$retStr= $lang['error_name_board'];
	}
	else if($membername == '')
	{
		$retStr= $lang['error_app'];
	}
	else
	{
		$boardname= $db->escape_string($boardname);
		$boardinfo= $db->escape_string($boardinfo);
		$indeximg= $db->escape_string($indeximg);
		$boardrules= $db->escape_string($boardrules);

		$db->query("insert into {$dv}group_board(boardname,boardinfo,indeximg,rules,boardstats,rootid,founddate) values('".$boardname."','".$boardinfo."','".$indeximg."','".$boardrules."','".$boardstats."','".$groupid."',now())");
	}

	if($retStr == '')
	{
		echo "<script>parent.document.location.href='?action=board&groupid=$groupid';</script>";
	}
	else
		echo "<script>parent.information('".$retStr."');</script>";
}
function board_save(){
	global $db,$lang,$dv,$restriction,$page,$userid,$membername;
	$vowels = array("/", "*", "%", "&","?", "@", "$", "|", "^", "~","#");
    $boardname = str_replace($vowels,"",$_POST['boardname']);
    $boardinfo = str_replace($vowels,"",$_POST['boardinfo']);
    $indeximg= strip_tags(trim($_POST['indeximg']));
    $boardrules = str_replace($vowels,"",$_POST['boardrules']);
	$boardstats= intval($_POST['boardstats']);
	$groupid= intval($_POST['groupid']);
	$groupboardid= intval($_POST['groupboardid']);

	$retStr= "";

	if(($groupid == '') || ($groupid == 0) || ($groupboardid == '') || ($groupboardid == 0))
	{
		$retStr= $lang['error_url'];
	}
	else if($boardname == '')
	{
		$retStr= $lang['error_name_board'];
	}
	else if($membername == '')
	{
		$retStr= $lang['error_app'];
	}
	else
	{
		$boardname= $db->escape_string($boardname);
		$boardinfo= $db->escape_string($boardinfo);
		$indeximg= $db->escape_string($indeximg);
		$boardrules= $db->escape_string($boardrules);

		$sql= "update {$dv}group_board set boardname='".$boardname."',boardinfo='".$boardinfo."',indeximg='".$indeximg."',rules='".$boardrules."',boardstats='".$boardstats."',rootid='".$groupid."' where rootid={$groupid} and id={$groupboardid}";

		$db->query($sql);
	}

	if($retStr == '')
	{
		echo "<script>parent.document.location.href='?action=board&groupid=$groupid';</script>";
	}
	else
		echo "<script>parent.information('".$retStr."');</script>";
}

function board_del(){
	global $db,$lang,$dv,$restriction,$page,$userid,$membername;

	$groupid= intval($_GET['groupid']);
	$groupboardid= intval($_GET['groupboardid']);

	$retStr= "";

	if(($groupid == '') || ($groupid == 0) || ($groupboardid == '') || ($groupboardid == 0))
	{
		$retStr= $lang['error_url'];
	}
	else
	{
		$db->query("delete from {$dv}group_board where rootid={$groupid} and id={$groupboardid}");

	}

	if($retStr == '')
	{
		echo "<script>parent.document.location.href='?action=board&groupid=$groupid';</script>";
	}
	else
		echo "<script>parent.information('".$retStr."');</script>";
}
function user_list(){
	global $dv,$db,$lang,$info_list,$page,$total,$page_size,$offset,$page_count,$p,$userid,$membername,$boardmaster;
	$page=$_POST['page'];
	if($page==''){
		$page=$_GET['page'];
	}	$groupid= intval($_GET['groupid']);
	$mode= intval($_GET['mode']);

	if($mode == 0)
		$tempStr= "";
	else
	{
		$tempStr=" and islock=".($mode-1);
	}

	$total=intval($db->scalar("select count(1) from {$dv}group_user where groupid={$groupid}".$tempStr));
	$page_size=20;
	if ($page=="" or $page<1)
		$page=1;
	 $page_count=ceil($total/$page_size);
	if ($page>$page_count)
		$page=$page_count;
	$offset=$page;
	if( $offset < 1 ) $offset = 1;

	$sql= "select *,(case islock when 1 then '".$lang['item_type_11']."' when 2 then '".$lang['item_type_12']."' else '".$lang['item_type_13']."' end) itemislock from {$dv}group_user where groupid={$groupid}".$tempStr;
	if($query=$db->query($sql,array('absolutePage'=>$offset,'pageSize'=>$page_size))){
			while ($arr=$db->fetch_array($query)){
				$user_list[]=$arr;
			}
	}
	$db->free_result($query);

	require_once printout('indivgroup_manage');
}
function user_edit_info(){
	global $dv,$db,$lang,$page,$total,$page_size,$offset,$page_count,$p,$userid,$membername,$boardmaster;
	$groupid= intval($_GET['groupid']);
	$id= intval($_GET['id']);
	$sql= "select * from {$dv}group_user where groupid={$groupid} and id={$id}";
	if($query=$db->query($sql)){
		if ($arr=$db->fetch_array($query)){
			$user_info=$arr;
		}
	}
	$db->free_result($query);

	require_once printout('indivgroup_manage');
}
function user_save(){
	global $db,$lang,$dv,$restriction,$page,$userid,$membername;
	$id= intval($_POST['id']);
	$groupid= intval($_POST['groupid']);
	$islock= intval($_POST['islock']);
	$intro= strip_tags(trim($_POST['intro']));

	$retStr= "";

	if(($groupid == '') || ($groupid == 0) || ($id == '') || ($id == 0))
	{
		$retStr= $lang['error_url'];
	}
	else if($membername == '')
	{
		$retStr= $lang['error_app'];
	}
	else
	{
		$intro= $db->escape_string($intro);

		$db->query("update {$dv}group_user set islock='".$islock."',intro='".$intro."' where groupid={$groupid} and id={$id}");

	}

	if($retStr == '')
	{
		echo "<script>parent.submitform_suc();</script>";
	}
	else
		echo "<script>parent.information('".$retStr."');</script>";
}
function user_setadmin(){
	global $dv,$db,$lang,$info_list,$page,$total,$page_size,$offset,$page_count,$p,$userid,$membername,$boardmaster;
	$id= intval($_GET['id']);
	$groupid= intval($_GET['groupid']);

	$retStr= "";

	if(($groupid == '') || ($groupid == 0) || ($id == '') || ($id == 0))
	{
		$retStr= $lang['error_url'];
		showmsg($lang['error_url']);
	}
	else
	{
		$intro= $db->escape_string($intro);

		$db->query("update {$dv}group_user set islock='2' where groupid={$groupid} and id={$id}");

		user_list();
	}
}
function user_del(){
	global $dv,$db,$lang,$info_list,$page,$total,$page_size,$offset,$page_count,$p,$userid,$membername,$boardmaster;
	$id= intval($_GET['id']);
	$groupid= intval($_GET['groupid']);

	$retStr= "";

	if(($groupid == '') || ($groupid == 0) || ($id == '') || ($id == 0))
	{
		$retStr= $lang['error_url'];
		showmsg($lang['error_url']);
	}
	else
	{
		$intro= $db->escape_string($intro);

		$db->query("delete from {$dv}group_user where groupid={$groupid} and id={$id}");
		$db->query("update {$dv}group_name set usernum=(usernum-1) where id='".$groupid."'");

		user_list();
	}
}

function update_group(){
	global $dv,$db,$lang,$info_list,$page,$total,$page_size,$offset,$page_count,$p,$userid,$membername,$boardmaster;
	$id= trim($_GET['id']);
	$groupid= intval($_GET['groupid']);

	$retStr= "";

	if(($groupid == '') || ($groupid == 0))
	{
		$retStr= $lang['error_url'];
		showmsg($lang['error_url']);
	}
	else
	{
		$GroupUserNum=0;
		$GroupTopicNum=0;
		$GroupPostNum=0;
		$GroupTodayNum=0;
		$time1 = strtotime(date('Y-m-d 00:00:00'));
		$time2 = $time1 + (24 * 3600 - 1);

$str="";
		$SQL = "Select id From {$dv}group_board where rootid=".$groupid;
		$query=$db->query($SQL);
		while ($arr=$db->fetch_array($query)){
			$TopicNum=intval($db->scalar("Select count(1) From {$dv}group_topic Where groupid={$groupid} And boardid=".$arr["id"]." And istop=0"));
			$GroupTopicNum = $GroupTopicNum + $TopicNum;

			$PostNum=intval($db->scalar("Select count(1) From {$dv}group_bbs1 Where groupid={$groupid} And boardid=".$arr["id"]));
			$GroupPostNum = $GroupPostNum + $PostNum;

			$TodayNum=intval($db->scalar("Select count(1) From {$dv}group_bbs1 Where groupid={$groupid} And boardid=".$arr["id"]." and (dateandtime between ".$time1." and ".$time2.")"));
			$GroupTodayNum = $GroupTodayNum + $TodayNum;

			$stmt="SELECT lastpost FROM {$dv}group_topic WHERE boardid=".$arr["id"]." order by lastposttime Desc";
			$result=$db->query($stmt,array('pageSize'=>1));//'最后回复信息
			$temparr=$db->fetch_array($result);
			$lastpost=addslashes(htmlspecialchars($temparr['lastpost'].""));
			$lpset=explode("$",$lastpost);//'更新版面数据
			if(count($lpset)<7){
				$lastpost.="$";//还要进行脏话的过滤;
			}
			$db->query("UPDATE {$dv}group_board SET postnum=".$PostNum.",topicnum=".$TopicNum.",todaynum=".$TodayNum.",lastpost='".$lastpost."' Where id=".$arr["id"]."");
		}

		$GroupUserNum=intval($db->scalar("Select count(*) From {$dv}group_user Where groupid={$groupid} And islock>0"));

		$db->query("Update {$dv}group_name Set usernum=".$GroupUserNum.",topicnum=".$GroupTopicNum.",postnum=".$GroupPostNum.",todaynum=".$GroupTodayNum." Where id=".$groupid);

		showmsg($lang['new_name_suc']);
	}
}
?>