<?php
/**
 * @version $Id: group.inc.php v2.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

if(!defined('IN_ADMIN') || !isset($_PBDEV['PHP_SELF']) || !preg_match('/[\/\\\\]admincp\.php$/i', $_PBDEV['PHP_SELF']))
{
	exit('Access Denied');
}
define('PB_CSCRIPT', 'group');
allowdo('setgroup');
$baseurl = 'admincp.php?action=group&type=';

if ($type == 'count' || empty($type))
{
	$DB->db_query("SELECT COUNT(*) amount, groupid FROM {$db_prefix}members GROUP BY groupid");
	while ($rs = $DB->db_fetch_array())
	{
		if ($rs['groupid'] == -1)
		{
			$rs['groupname'] = $grouplevel['7']['grouptitle'];
			$rs['groupid'] = 7;
		}
		else
		{
			$rs['groupname'] = $grouplevel[$rs['groupid']]['grouptitle'];
		}
		$volume[] = $rs['groupid'];
		$group[] = $rs;
	}
	array_multisort($volume, SORT_ASC, $group);
	$TPL->assign('group', $group);
	$type = 'count';
}
elseif ($type == 'admin')
{
	$DB->db_query("SELECT adminid FROM {$db_prefix}admingroups");
	while ($rs = $DB->db_fetch_array())
	{
		$rs['groupname'] = $grouplevel[$rs['adminid']]['grouptitle'];
		$group[] = $rs;
	}
	$TPL->assign('group', $group);
}
elseif ($type == 'user')
{
	$systemgroup = $membergroup = array();
	$DB->db_query("SELECT groupid, grouptitle FROM {$db_prefix}usergroups");
	while ($rs = $DB->db_fetch_array())
	{
		if ($rs['groupid'] > 6)
		{
			$membergroup[] = $rs;
		}
		else
		{
			$systemgroup[] = $rs;
		}
	}
	$TPL->assign('systemgroup', $systemgroup);
	$TPL->assign('membergroup', $membergroup);
}
elseif ($type == 'mod')
{
	if ($edit == 'admin')
	{
		$baseurl .= 'admin';
		if (!$_SUPERMANAGER)
		{
			showMsg($a_message['admin_nopermission'], $baseurl);
		}
		if (empty($adminid) || !is_numeric($adminid))
		{
			showMsg($a_message['parameter_error'], $baseurl);
		}
		$adminid = (int)$adminid;
		$actionurl .= "&edit=admin&adminid=$adminid";
		if ($submitted)
		{
			$field = array();
			$field['alloweditatc'] = isset($alloweditatc) ? 1 : 0;
			$field['allowdelatc'] = isset($allowdelatc) ? 1 : 0;
			$field['allowlockatc'] = isset($allowlockatc) ? 1 : 0;
			$field['allowmoveatc'] = isset($allowmoveatc) ? 1 : 0;
			$field['allowcopyatc'] = isset($allowcopyatc) ? 1 : 0;
			$field['allowtopatc'] = isset($allowtopatc) ? 1 : 0;
			$field['allowcommend'] = isset($allowcommend) ? 1 : 0;
			$field['allowshield'] = isset($allowshield) ? 1 : 0;
			
			$field['allowadmincp'] = isset($allowadmincp) ? 1 : 0;
			$field['setsys'] = isset($setsys) ? 1 : 0;
			$field['setcate'] = isset($setcate) ? 1 : 0;
			$field['settag'] = isset($settag) ? 1 : 0;
			$field['setmember'] = isset($setmember) ? 1 : 0;
			$field['setgroup'] = isset($setgroup) ? 1 : 0;
			$field['setcheck'] = isset($setcheck) ? 1 : 0;
			$field['setbatch'] = isset($setbatch) ? 1 : 0;
			$field['setplugins'] = isset($setplugins) ? 1 : 0;
			$field['settpl'] = isset($settpl) ? 1 : 0;
			$field['setlink'] = isset($setlink) ? 1 : 0;
			$field['setdb'] = isset($setdb) ? 1 : 0;
			$field['settool'] = isset($settool) ? 1 : 0;
			$field['setlog'] = isset($setlog) ? 1 : 0;
			$field['setspecial'] = isset($setspecial) ? 1 : 0;
			updateRecords($field, 'admingroups', "adminid = $adminid");
			$Cache->adminGroupCache();
			redirect($a_message['edit_group_success'], $actionurl);
		}
		else
		{
			$rs = $DB->db_fetch_one_array("SELECT * FROM {$db_prefix}admingroups WHERE adminid = $adminid");
			if ($rs)
			{
				@extract($rs);
				$adminRight = array();
				$adminRight['alloweditatc'] = $alloweditatc ? 'checked="checked"' : '';
				$adminRight['allowdelatc'] = $allowdelatc ? 'checked="checked"' : '';
				$adminRight['allowlockatc'] = $allowlockatc ? 'checked="checked"' : '';
				$adminRight['allowmoveatc'] = $allowmoveatc ? 'checked="checked"' : '';
				$adminRight['allowcopyatc'] = $allowcopyatc ? 'checked="checked"' : '';
				$adminRight['allowtopatc'] = $allowtopatc ? 'checked="checked"' : '';
				$adminRight['allowcommend'] = $allowcommend ? 'checked="checked"' : '';
				$adminRight['allowshield'] = $allowshield ? 'checked="checked"' : '';
				
				$adminRight['allowadmincp'] = $allowadmincp ? 'checked="checked"' : '';
				$adminRight['setsys'] = $setsys ? 'checked="checked"' : '';
				$adminRight['setcate'] = $setcate ? 'checked="checked"' : '';
				$adminRight['settag'] = $settag ? 'checked="checked"' : '';
				$adminRight['setmember'] = $setmember ? 'checked="checked"' : '';
				$adminRight['setgroup'] = $setgroup ? 'checked="checked"' : '';
				$adminRight['setcheck'] = $setcheck ? 'checked="checked"' : '';
				$adminRight['setbatch'] = $setbatch ? 'checked="checked"' : '';
				$adminRight['setplugins'] = $setplugins ? 'checked="checked"' : '';
				$adminRight['settpl'] = $settpl ? 'checked="checked"' : '';
				$adminRight['setlink'] = $setlink ? 'checked="checked"' : '';
				$adminRight['setdb'] = $setdb ? 'checked="checked"' : '';
				$adminRight['settool'] = $settool ? 'checked="checked"' : '';
				$adminRight['setlog'] = $setlog ? 'checked="checked"' : '';
				$adminRight['setspecial'] = $setspecial ? 'checked="checked"' : '';
				$system['grouptitle'] = $grouplevel[$adminid]['grouptitle'];
			}
			else
			{
				showMsg($a_message['group_not_exist'], $baseurl);
			}
			$TPL->assign('admin_right', $adminRight);
			$TPL->assign('edit', 'admin');
		}
	}
	else
	{
		$baseurl .= 'user';
		if (empty($groupid) || !is_numeric($groupid))
		{
			showMsg($a_message['parameter_error'], $baseurl);
		}
		$groupid = (int)$groupid;
		$actionurl .= "&edit=user&groupid=$groupid";
		if ($submitted)
		{
			$field = array();
			$field['allowvisit'] = isset($allowvisit) ? 1 : 0;
			$field['allowsort'] = isset($allowsort) ? 1 : 0;
			$field['allowread'] = isset($allowread) ? 1 : 0;
			$field['allowpost'] = isset($allowpost) ? 1 : 0;
			$field['allowcomment'] = isset($allowcomment) ? 1 : 0;
			$field['allowdigg'] = isset($allowdigg) ? 1 : 0;
			$field['allowbury'] = isset($allowbury) ? 1 : 0;
			$field['allowbsearch'] = isset($allowbsearch) ? 1 : 0;
			$field['allowasearch'] = isset($allowasearch) ? 1 : 0;
			$field['searchmax'] = ($searchmax > 0) ? (int)$searchmax : 0;
			$field['allowreport'] = 1;
			$field['allowhtml'] = isset($allowhtml) ? 1 : 0;
			$field['allowmsg'] = isset($allowmsg) ? 1 : 0;
			$field['msgmax'] = ($msgmax > 0) ? (int)$msgmax : 0;
			$field['allowfavors'] = isset($allowfavors) ? 1 : 0;
			$field['favorsmax'] = ($favorsmax > 0) ? (int)$favorsmax : 0;
			$field['allowavatar'] = isset($allowavatar) ? 1 : 0;
			$field['allowaupload'] = isset($allowaupload) ? 1 : 0;
			$field['uploadmax'] = (int)$uploadmax * 1024;
			if ($field['uploadmax'] > ini_bytes('upload_max_filesize'))
			{
				showMsg($a_message['uploadThanMax'], $actionurl);
			}
			$field['uploadtype'] = strtolower(trim($uploadtype));
			if (!preg_match('/[a-z0-9,\.]*/i', $field['uploadtype']))
			{
				showMsg($a_message['forbiddenExt'], $actionurl);
			}
			$ext = array('exe', 'jsp', 'asp', 'asa', 'htr', 'stm', 'shtml', 'php3', 'aspx', 'cgi', 'fcgi', 'pl', 'php', 'bat');
			if ($field['uploadtype'])
			{
				foreach ($ext as $value)
				{
					if (strpos($field['uploadtype'], $value) !== FALSE)
					{
						showMsg($a_message['forbiddenExt'], $actionurl);
					}
				}
				if (strpos($field['uploadtype'], '£¬') !== FALSE)
				{
					$field['uploadtype'] = str_replace('£¬', ',', $field['uploadtype']);
				}
			}
			updateRecords($field, 'usergroups', "groupid = $groupid");
			$Cache->userGroupCache();
			redirect($a_message['edit_group_success'], $actionurl);
		}
		else
		{
			$rs = $DB->db_fetch_one_array("SELECT * FROM {$db_prefix}usergroups WHERE groupid = $groupid");
			if ($rs)
			{
				@extract($rs);
				$groupRight = array();
				$groupRight['allowvisit'] = $allowvisit ? 'checked="checked"' : '';
				$groupRight['allowsort'] = $allowsort ? 'checked="checked"' : '';
				$groupRight['allowread'] = $allowread ? 'checked="checked"' : '';
				$groupRight['allowpost'] = $allowpost ? 'checked="checked"' : '';
				$groupRight['allowcomment'] = $allowcomment ? 'checked="checked"' : '';
				$groupRight['allowdigg'] = $allowdigg ? 'checked="checked"' : '';
				$groupRight['allowbury'] = $allowbury ? 'checked="checked"' : '';
				$groupRight['allowbsearch'] = $allowbsearch ? 'checked="checked"' : '';
				$groupRight['allowasearch'] = $allowasearch ? 'checked="checked"' : '';
				$groupRight['searchmax'] = $searchmax;
				$groupRight['allowhtml'] = $allowhtml ? 'checked="checked"' : '';
				$groupRight['allowmsg'] = $allowmsg ? 'checked="checked"' : '';
				$groupRight['msgmax'] = $msgmax;
				$groupRight['allowfavors'] = $allowfavors ? 'checked="checked"' : '';
				$groupRight['favorsmax'] = $favorsmax;
				$groupRight['allowavatar'] = $allowavatar ? 'checked="checked"' : '';
				$groupRight['allowaupload'] = $allowaupload ? 'checked="checked"' : '';
				$groupRight['uploadmax'] = ceil($uploadmax / 1024);
				$groupRight['uploadtype'] = $uploadtype;

				$system['grouptitle'] = $grouplevel[$groupid]['grouptitle'];
			}
			else
			{
				showMsg($a_message['group_not_exist'], $baseurl);
			}
			$TPL->assign('group_right', $groupRight);
		}
	}
	$TPL->assign('system', $system);
}
else
{
	redirect($a_message['unknow_operate'], 'admincp.php?action=main');
}

$TPL->assign('menu', $m_group);

?>
