<?php
/*======================================================================*\
|| #################################################################### ||
|| # vBHosting Version 2                                              # ||
|| # ---------------------------------------------------------------- # ||
|| # All PHP code in this file is ©2004 GameCrash.org.                # ||
|| # http://www.gamecrash.org (soon) | http://www.letzplay.de         # ||
|| # ---------------------------------------------------------------- # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------- VBULLETIN IS NOT FREE SOFTWARE ---------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html # ||
|| #################################################################### ||
\*======================================================================*/

// ######################## SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// ##################### DEFINE IMPORTANT CONSTANTS #######################
define('NO_REGISTER_GLOBALS', 1);

// #################### PRE-CACHE TEMPLATES AND DATA ######################
$phrasegroups = array();
$specialtemplates = array();

// ########################## REQUIRE BACK-END ############################
require_once('./global.php');

// ############################# LOG ACTION ###############################
vbhosting_log();

// ########################################################################
// ######################### START MAIN SCRIPT ############################
// ########################################################################

if (!vbhosting_haspermission('manageadministrators'))
{
	print_cp_no_permission();
}

if (empty($_REQUEST['do']))
{
	$_REQUEST['do'] = 'modify';
}

print_cp_header($vbphrase['vbhosting_manage_administrators']);

if ($_REQUEST['do'] == 'add')
{
	print_form_header('administrator', 'insert');
	construct_hidden_code('hostedid', $hostedid);
	print_table_header($vbphrase['vbhosting_create_new_administrator']);
	print_input_row($vbphrase['username'], 'username');
	print_yes_no_row($vbphrase['vbhosting_can_manage_settings'], 'managesettings');
	print_yes_no_row($vbphrase['vbhosting_can_manage_forums'], 'manageforums');
	print_yes_no_row($vbphrase['vbhosting_can_manage_styles'], 'managestyles');
	print_yes_no_row($vbphrase['vbhosting_can_manage_administrators'], 'manageadministrators', 0);
	print_submit_row($vbphrase['vbhosting_create_new_administrator']);
	print_table_footer();
}

if ($_REQUEST['do'] == 'insert')
{
	globalize($_REQUEST, array(
		'username' => STR_NOHTML,
		'managesettings' => INT,
		'manageforums' => INT,
		'managestyles' => INT,
		'manageadministrators' => INT
	));
	
	$userid = $DB_site->query_first("SELECT userid FROM " . TABLE_PREFIX . "user WHERE username='" . addslashes($username) . "'");
	$userid = intval($userid['userid']);
	
	if ($userid < 1)
	{
		print_cp_message(construct_phrase($vbphrase['vbhosting_could_not_find_user_x'], $username));
	}

	if ($administratorexists = $DB_site->query_first("SELECT administratorid FROM " . TABLE_PREFIX . "vbhosting_administrator WHERE hostedid={$hostedid} AND userid={$userid}"))
	{
		print_cp_message(construct_phrase($vbphrase['vbhosting_x_is_already_administrator'], $username));
	}
	
	$adminperms = 0;
	$adminperms += iif($managesettings, $_VBHOSTINGPERMISSIONS['managesettings'], 0);
	$adminperms += iif($manageforums, $_VBHOSTINGPERMISSIONS['manageforums'], 0);
	$adminperms += iif($managestyles, $_VBHOSTINGPERMISSIONS['managestyles'], 0);
	$adminperms += iif($manageadministrators, $_VBHOSTINGPERMISSIONS['manageadministrators'], 0);
	
	$DB_site->query("INSERT INTO " . TABLE_PREFIX . "vbhosting_administrator (hostedid, userid, permissions) VALUES ({$hostedid}, {$userid}, {$adminperms})");
	
	print_cp_message(construct_phrase($vbphrase['vbhosting_administrator_x_added_successfully'], $username), "administrator.php?hostedid={$hostedid}&do=modify");
}

if ($_REQUEST['do'] == 'edit')
{
	globalize($_REQUEST, array('administratorid' => INT));
	
	$administratorinfo = $DB_site->query_first("SELECT u.username, a.permissions FROM " . TABLE_PREFIX . "vbhosting_administrator a, " . TABLE_PREFIX . "user u WHERE a.hostedid={$hostedid} AND a.administratorid={$administratorid} AND a.userid=u.userid");
	
	if ($administratorinfo === false)
	{
		print_cp_message($vbphrase['vbhosting_please_select_an_administrator_to_modify']);
	}
	
	$adminperms = $administratorinfo['permissions'];
	
	print_form_header('administrator', 'update');
	construct_hidden_code('hostedid', $hostedid);
	construct_hidden_code('administratorid', $administratorid);
	print_table_header($vbphrase['vbhosting_create_new_administrator']);
	print_label_row($vbphrase['username'], $administratorinfo['username']);
	print_yes_no_row($vbphrase['vbhosting_can_manage_settings'], 'managesettings', iif($adminperms & $_VBHOSTINGPERMISSIONS['managesettings'], 1, 0));
	print_yes_no_row($vbphrase['vbhosting_can_manage_forums'], 'manageforums', iif($adminperms & $_VBHOSTINGPERMISSIONS['manageforums'], 1, 0));
	print_yes_no_row($vbphrase['vbhosting_can_manage_styles'], 'managestyles', iif($adminperms & $_VBHOSTINGPERMISSIONS['managestyles'], 1, 0));
	print_yes_no_row($vbphrase['vbhosting_can_manage_administrators'], 'manageadministrators', iif($adminperms & $_VBHOSTINGPERMISSIONS['manageadministrators'], 1, 0));
	print_submit_row();
	print_table_footer();
}

if ($_REQUEST['do'] == 'update')
{
	globalize($_REQUEST, array(
		'administratorid' => INT,
		'managesettings' => INT,
		'manageforums' => INT,
		'managestyles' => INT,
		'manageadministrators' => INT
	));
	
	$administratorinfo = $DB_site->query_first("SELECT u.username, u.userid FROM " . TABLE_PREFIX . "vbhosting_administrator a, " . TABLE_PREFIX . "user u WHERE a.hostedid={$hostedid} AND a.administratorid={$administratorid} AND a.userid=u.userid");
	
	if ($administratorinfo === false)
	{
		print_cp_message($vbphrase['vbhosting_please_select_an_administrator_to_modify']);
	}
	
	if ($administratorinfo['userid'] == $bbuserinfo['userid'] && !$manageadministrators)
	{
		$administratorcheck = $DB_site->query_first("SELECT COUNT(*) as count FROM " . TABLE_PREFIX . "vbhosting_administrator WHERE hostedid={$hostedid} AND permissions & {$_VBHOSTINGPERMISSIONS['manageadministrators']}");
		if ($administratorcheck['count'] < 2)
		{
			print_cp_message($vbphrase['vbhosting_you_are_the_last_administrator_with_administrator_management_permissions']);
		}
	}

	$adminperms = 0;
	$adminperms += iif($managesettings, $_VBHOSTINGPERMISSIONS['managesettings'], 0);
	$adminperms += iif($manageforums, $_VBHOSTINGPERMISSIONS['manageforums'], 0);
	$adminperms += iif($managestyles, $_VBHOSTINGPERMISSIONS['managestyles'], 0);
	$adminperms += iif($manageadministrators, $_VBHOSTINGPERMISSIONS['manageadministrators'], 0);
	
	$DB_site->query("UPDATE " . TABLE_PREFIX . "vbhosting_administrator SET permissions={$adminperms} WHERE administratorid={$administratorid} AND hostedid={$hostedid}");
	
	print_cp_message(construct_phrase($vbphrase['vbhosting_administrator_x_updated_successfully'], $username), "administrator.php?hostedid={$hostedid}&do=modify");
}

if ($_REQUEST['do'] == 'delete')
{
	globalize($_REQUEST, array('administratorid' => INT));
	
	$administratorinfo = $DB_site->query_first("SELECT a.hostedid, a.userid, u.username FROM " . TABLE_PREFIX . "vbhosting_administrator a, " . TABLE_PREFIX . "user u WHERE a.administratorid={$administratorid} AND a.userid=u.userid");
	
	if ($administratorinfo === false)
	{
		print_cp_message($vbphrase['vbhosting_please_select_an_administrator_to_modify']);
	}
	
	if ($administratorinfo['hostedid'] != $hostedid)
	{
		print_cp_message(construct_phrase($vbphrase['vbhosting_x_is_not_administrator_in_this_forum'], $administratorinfo['username']));
	}
	
	if ($administratorinfo['userid'] == $bbuserinfo['userid'])
	{
		$administratorcheck = $DB_site->query_first("SELECT COUNT(*) as count FROM " . TABLE_PREFIX . "vbhosting_administrator WHERE hostedid={$hostedid} AND permissions & {$_VBHOSTINGPERMISSIONS['manageadministrators']}");
		if ($administratorcheck['count'] < 2)
		{
			print_cp_message($vbphrase['vbhosting_you_are_the_last_administrator_with_administrator_management_permissions']);
		}
	}

	print_form_header('administrator', 'remove');
	construct_hidden_code('hostedid', $hostedid);
	construct_hidden_code('administratorid', $administratorid);
	print_table_header($vbphrase['vbhosting_delete_administrator']);
	print_description_row(construct_phrase($vbphrase['vbhosting_are_you_sure_you_want_to_delete_x_from_the_administrators'], iif($administratorinfo['userid'] == $bbuserinfo['userid'], $vbphrase['vbhosting_yourself'], $administratorinfo['username'])));
	print_table_footer(2, '<input type="submit" class="button" tabindex="1" value="' . $vbphrase['vbhosting_delete_administrator'] . '" accesskey="s" />');
}

if ($_REQUEST['do'] == 'remove')
{
	globalize($_REQUEST, array('administratorid' => INT));
	
	$DB_site->query("DELETE FROM " . TABLE_PREFIX . "vbhosting_administrator WHERE hostedid={$hostedid} AND administratorid={$administratorid}");
	
	print_cp_message(construct_phrase($vbphrase['vbhosting_administrator_deleted_successfully'], $username), "administrator.php?hostedid={$hostedid}&do=modify");
}

if ($_REQUEST['do'] == 'modify')
{
	print_form_header('administrator', 'add');
	construct_hidden_code('hostedid', $hostedid);
	print_table_header($vbphrase['vbhosting_manage_administrators']);
	print_description_row($vbphrase['vbhosting_administrators_not_moderators']);
	print_cells_row(array($vbphrase['username'], $vbphrase['vbhosting_possible_actions']),1,'tcat');
	
	$administrators = $DB_site->query("SELECT a.administratorid, u.username FROM " . TABLE_PREFIX . "vbhosting_administrator a, " . TABLE_PREFIX . "user u WHERE a.hostedid={$hostedid} AND a.userid=u.userid ORDER BY a.administratorid");
	while ($administrator = $DB_site->fetch_array($administrators))
	{
		print_label_row("<strong>{$administrator['username']}</strong>", "<a href=\"administrator.php?hostedid={$hostedid}&do=edit&administratorid={$administrator['administratorid']}\">[{$vbphrase['vbhosting_edit_administrator']}]</a> &nbsp; <a href=\"administrator.php?hostedid={$hostedid}&do=delete&administratorid={$administrator['administratorid']}\">[{$vbphrase['vbhosting_delete_administrator']}]</a>");
	}
	
	print_table_footer(2, '<input type="submit" class="button" tabindex="1" value="' . $vbphrase['vbhosting_create_new_administrator'] . '" accesskey="s" />');
}

print_cp_footer();

?>