Snippets

Anthony Jordan Staff Panel - Sniko's Chat

Created by Bear Jordan last modified
<?php
/*
Written and released by Magictallguy under the DBAD License - http://www.dbad-license.org
DON'T BE A DICK PUBLIC LICENSE
    Version 1, December 2009
    Copyright (C) 2009 Philip Sturgeon me@philsturgeon.uk

Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed.
    DON'T BE A DICK PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

    Do whatever you like with the original work, just don't be a dick.

    Being a dick includes - but is not limited to - the following instances:

    1a. Outright copyright infringement - Don't just copy this and change the name.
    1b. Selling the unmodified original with no work done what-so-ever, that's REALLY being a dick.
    1c. Modifying the original work to contain hidden harmful content. That would make you a PROPER dick.

    If you become rich through modifications, related works/services, or supporting the original work, share the love. Only a dick would make loads off this work and not buy the original works creator(s) a pint.

    Code is provided with no warranty. Using somebody else's code and bitching when it goes wrong makes you a DONKEY dick. Fix the problem yourself. A non-dick would submit the fix back.
*/
require_once __DIR__ . '/sglobals.php';
if(!function_exists('error')) {
	function error($msg) {
		global $h;
		echo '<div class="error"><strong>ERROR</strong><br />',$msg,'</div>';
		exit($h->endpage());
	}
}
if(!function_exists('format')) {
	function format($str, $dec = 0) {
		$ret = is_numeric($str) ? number_format($str, $dec) : stripslashes(strip_tags($str));
		if(!is_numeric($str) && $dec)
			$ret = nl2br($ret);
		return $ret;
	}
}
if(!function_exists('username')) {
	function username($id = 0, $showID = false) {
		global $db, $ir;
		if(!$id || !ctype_digit($id))
			return 'SYSTEM';
		if($id == $ir['userid'])
			return '<a href="viewuser.php?u='.$id.'">'.format($ir['username']).'</a>'.($showID ? ' ['.$id.']' : '');
		$select = $db->query('SELECT `username` FROM `users` WHERE `userid` = '.$id);
		if(!$db->num_rows($select))
			return 'SYSTEM';
		return '<a href="viewuser.php?u='.$id.'">'.format($db->fetch_single($select)).'</a>'.($showID ? ' ['.$id.']' : '');
	}
}
if($ir['user_level'] != 2)
	error('You don\'t have access');
$_GET['action'] = array_key_exists('action', $_GET) && ctype_alpha($_GET['action']) ? strtolower(trim($_GET['action'])) : null;
switch($_GET['action']) {
	case 'ban':
		banUser();
		break;
	case 'unban':
		unbanUser();
		break;
	case 'add':
		addRoom();
		break;
	case 'edit':
		editRoom();
		break;
	case 'del':
		deleteRoom();
		break;
	default:
		staffChatIndex();
		break;
}

function staffChatIndex() {
	?><table class="table" width="75%">
		<tr>
			<th width="50%">Rooms</th>
			<th width="50%">Bans</th>
		</tr>
		<tr>
			<td>
				<a href="staff_chat.php?action=add">Add Room</a><br />
				<a href="staff_chat.php?action=edit">Edit Room</a><br />
				<a href="staff_chat.php?action=del">Delete Room</a>
			</td>
			<td>
				<a href="staff_chat.php?action=ban">Ban Player (temp/perm)</a><br />
				<a href="staff_chat.php?action=unban">Unban Player</a>
			</td>
		</tr>
	</table><?php
}

function banUser() {
	global $db;
	$types = [
		'temp' => 'Temporary',
		'perm' => 'Permanent'
	];
	if(array_key_exists('submit', $_POST)) {
		$_POST['user1'] = array_key_exists('user1', $_POST) && ctype_digit($_POST['user1']) ? $_POST['user1'] : null;
		$_POST['user2'] = array_key_exists('user2', $_POST) && ctype_digit($_POST['user2']) ? $_POST['user2'] : null;
		if((empty($_POST['user1']) && empty($_POST['user2'])) || (!empty($_POST['user1']) && !empty($_POST['user2'])))
			error('You must use only one of the options available');
		$who = empty($_POST['user1']) ? $_POST['user2'] : $_POST['user1'];
		$selectUser = $db->query('SELECT `userid` FROM `users` WHERE `userid` = '.$who);
		if(!$db->num_rows($selectUser))
			error('That player doesn\'t exist');
		$name = $db->fetch_single($selectUser);
		$selectBan = $db->query('SELECT `ban_type`, `ban_until`, `banned_by` FROM `pusher_chat_bans` WHERE `userid` = '.$who);
		if($db->num_rows($selectBan)) {
			$row = $db->fetch_row($selectBan);
			error(username($who).' has already been '.$types2[$row['ban_type']].' banned from the chat by '.username($row['banned_by']).'. The ban will lift on '.date('H:i:s d/m/Y', $row['ban_until']));
		}
		$_POST['type'] = array_key_exists('type', $_POST) && in_array($_POST['type'], ['temp', 'perm']) ? $_POST['type'] : null;
		if(empty($_POST['type']))
			error("You didn't select a valid ban type");
		$db->query('INSERT INTO `pusher_chat_bans` (`userid`, `ban_type`, `ban_until`, `banned_by`) VALUES ('.$who.', '.$_POST['type'].', '.(time() + 60).', '.$ir['userid'].')');
		stafflog_add('Imposed a '.strtolower($types[$_POST['type']]).' chat ban on '.username($who));
		?>You've imposed a <?php echo strtolower($types[$_POST['type']]);?> ban on <?php echo username($who);
	}
	?><form action="staff_chat.php?action=ban" method="post">
		<table class="table" width="75%">
			<tr>
				<th width="25%">Select a player</th>
				<td width="75%"><select name="user1">
					<option value="0">--- SELECT ---</option><?php
					$selectUsers = $db->query('SELECT `userid`, `username` FROM `users` ORDER BY `username` ASC');
					while($row = $db->fetch_row($selectUsers))
						printf('<option value="%u">%s [%u]</option>', $row['userid'], format($row['username']), $row['userid']);
				?></select></td>
			</tr>
			<tr>
				<th><strong style="text-decoration:underline;">OR</strong> enter their ID</th>
				<td><input type="number" name="user2" /></td>
			</tr>
			<tr>
				<th>Type of ban</th>
				<td><select name="type"><?php
					foreach($types as $type => $disp)
						printf('<option value="%s">%s</option>', $type, $disp);
				?></select></td>
			</tr>
			<tr>
				<td colspan="2" class="center"><input type="submit" name="submit" value="Impose ban" /></td>
			</tr>
		</table>
	</form><?php
}

function unbanUser() {
	global $db;
	if(array_key_exists('submit', $_POST)) {
		$_POST['user1'] = array_key_exists('user1', $_POST) && ctype_digit($_POST['user1']) ? $_POST['user1'] : null;
		$_POST['user2'] = array_key_exists('user2', $_POST) && ctype_digit($_POST['user2']) ? $_POST['user2'] : null;
		if((empty($_POST['user1']) && empty($_POST['user2'])) || (!empty($_POST['user1']) && !empty($_POST['user2'])))
			error("You must use only one of the options available");
		$who = empty($_POST['user1']) ? $_POST['user2'] : $_POST['user1'];
		$selectUser = $db->query('SELECT `userid` FROM `users` WHERE `userid` = '.$who);
		if(!$db->num_rows($selectUser))
			error('That player doesn\'t exist');
		$name = $db->fetch_single($selectUser);
		$selectBan = $db->query('SELECT `ban_type`, `ban_until`, `banned_by` FROM `pusher_chat_bans` WHERE `userid` = '.$who);
		if(!$db->num_rows($selectBan))
			error(username($who).' isn\'t banned from the chat');
		$db->query('DELETE FROM `pusher_chat_bans` WHERE `userid` = '.$who);
		stafflog_add('Revoked the chat ban on '.username($who));
		?>You've revoked the ban on <?php echo username($who);
	}
	?><form action="staff_chat.php?action=unban" method="post">
		<table class="table" width="75%">
			<tr>
				<th width="25%">Select a player</th>
				<td width="75%"><select name="user1">
					<option value="0">--- SELECT ---</option><?php
					$selectUsers = $db->query('SELECT `userid`, `username` FROM `users` ORDER BY `username` ASC');
					while($row = $db->fetch_row($selectUsers))
						printf('<option value="%u">%s [%u]</option>', $row['userid'], format($row['username']), $row['userid']);
				?></select></td>
			</tr>
			<tr>
				<th><strong style="text-decoration:underline;">OR</strong> enter their ID</th>
				<td><input type="number" name="user2" /></td>
			</tr>
			<tr>
				<td colspan="2" class="center"><input type="submit" name="submit" value="Revoke ban" /></td>
			</tr>
		</table>
	</form><?php
}
function addRoom() {
	global $db;
	$status = [
		0 => 'open',
		1 => 'closed'
	];
	if(array_key_exists('submit', $_POST)) {
		$_POST['name'] = array_key_exists('name', $_POST) && is_string($_POST['name']) ? $db->escape(trim($_POST['name'])) : null;
		if(empty($_POST['name']))
			error('You didn\'t enter a valid name');
		$select = $db->query('SELECT `id` FROM `pusher_chat_rooms` WHERE `chat_name` = "'.$_POST['name'].'"');
		if($db->num_rows($select))
			error('That room already exists');
		$_POST['status'] = array_key_exists('status', $_POST) && in_array($_POST['status'], [0, 1]) ? $_POST['status'] : null;
		if(empty($_POST['status']))
			error('You didn\'t select a valid room status');
		$db->query('INSERT INTO `pusher_chat_rooms` (`chat_name`, `closed`) VALUES ("'.$_POST['name'].'", '.$_POST['status'].')');
		stafflog_add('Added chat room: '.stripslashes($_POST['name']));
		?>You've added the new chat room: <?php echo stripslashes($_POST['name']);?><br /><?php
	}
	?><form action="staff_chat.php?action=add" method="post">
		<table class="table" width="75%">
			<tr>
				<th width="25%">Name</th>
				<td width="75%"><input type="text" name="name" /></td>
			</tr>
			<tr>
				<th>Status</th>
				<td><select name="status"><?php
					foreach($status as $val => $disp)
						printf('<option value="%u">%s</option>', $val, ucfirst($disp));
				?></select></td>
			</tr>
			<tr>
				<td colspan="2" class="center"><input type="submit" name="submit" value="Add Room" /></td>
			</tr>
		</table>
	</form><?php
}
function editRoom() {
	global $db;
	$_GET['step'] = array_key_exists('step', $_GET) && ctype_digit($_GET['step']) ? $_GET['step'] : null;
	switch($_GET['step']) {
		case 2:
			$_POST['id'] = array_key_exists('id', $_POST) && ctype_digit($_POST['id']) ? $_POST['id'] : null;
			if(empty($_POST['id']))
				error('You didn\'t select a valid room');
			$select = $db->query('SELECT `chat_name` FROM `pusher_chat_rooms` WHERE `id` = '.$_POST['id']);
			if(!$db->num_rows($select))
				error('That room doesn\'t exist');
			$_POST['name'] = array_key_exists('name', $_POST) && is_string($_POST['name']) ? $db->escape(trim($_POST['name'])) : null;
			if(empty($_POST['name']))
				$mtg->error('You didn\'t enter a valid name');
			$select = $db->query('SELECT `id` FROM `pusher_chat_rooms` WHERE `chat_name` = "'.$_POST['name'].'" AND `id` != '.$_POST['id']);
			if($db->num_rows($select))
				error('Another room with that name already exists');
			$_POST['status'] = array_key_exists('status', $_POST) && in_array($_POST['status'], [0, 1]) ? $_POST['status'] : null;
			if(empty($_POST['status']))
				error('You didn\'t select a valid room status');
			$db->query('REPLACE INTO `pusher_chat_rooms` (`id`, `chat_name`, `closed`) VALUES ('.$_POST['id'].', "'.$_POST['name'].'", '.$_POST['status'].')');
			stafflog_add('Edited chat room: '.stripslashes($_POST['name']));
			?>You've edited the chat room: <?php echo stripslashes($_POST['name']);?><br /><?php
			break;
		case 1:
			$status = [
				0 => 'open',
				1 => 'closed'
			];
			$_POST['id'] = array_key_exists('id', $_POST) && ctype_digit($_POST['id']) ? $_POST['id'] : null;
			if(empty($_POST['id']))
				error('You didn\'t select a valid room');
			$select = $db->query('SELECT `chat_name`, `closed` FROM `pusher_chat_rooms` WHERE `id` = '.$_POST['id']);
			if(!$db->num_rows($select))
				error('That room doesn\'t exist');
			$row = $db->fetch_row($select);
			?><form action="staff_chat.php?action=edit&amp;step=2" method="post">
				<input type="hidden" name="id" value="<?php echo $_POST['id'];?>" />
				<table class="table" width="75%">
					<tr>
						<th width="25%">Name</th>
						<td width="75%"><input type="text" name="name" value="<?php echo $row['chat_name'];?>" /></td>
					</tr>
					<tr>
						<th>Status</th>
						<td><select name="status"><?php
							foreach($status as $val => $disp)
								printf('<option value="%u"%s>%s</option>', $val, $val == $row['closed'] ? ' selected="selected"' : '', ucfirst($disp));
						?></select></td>
					</tr>
					<tr>
						<td colspan="2" class="center"><input type="submit" name="submit" value="Edit Room" /></td>
					</tr>
				</table>
			</form><?php
			break;
		default:
			?><form action="staff_chat.php?action=edit&amp;step=1" method="post">
				<table class="table" width="75%">
					<tr>
						<th width="25%">Room to delete</th>
						<td width="75%"><select name="id"><?php
							$select = $db->query('SELECT `id`, `chat_name` FROM `pusher_chat_rooms` ORDER BY `id` ASC');
							if(!$db->num_rows($select))
								echo '<option value="0">There are no rooms</option>';
							else
								while($row = $db->fetch_row($select))
									printf('<option value="%u">%s</option>', $row['id'], $row['chat_name']);
						?></select></td>
					</tr>
					<tr>
						<td colspan="2" class="center"><input type="submit" name="submit" value="Edit room" /></td>
					</tr>
				</table>
			</form><?php
			break;
	}
}
function deleteRoom() {
	global $db;
	if(array_key_exists('submit', $_POST)) {
		$_POST['id'] = array_key_exists('id', $_POST) && ctype_digit($_POST['id']) ? $_POST['id'] : null;
		if(empty($_POST['id']))
			error('You didn\'t select a valid room');
		$select = $db->query('SELECT `chat_name` FROM `pusher_chat_rooms` WHERE `id` = '.$_POST['id']);
		if(!$db->num_rows($select))
			error('That room doesn\'t exist');
		$name = $db->fetch_single($select);
		if(!array_key_exists('ans', $_POST)) {
			?>Are you sure you want to delete the Chat Room: &ldquo;<?php echo $name;?>&rdquo;?<br />
			<form action="staff_chat.php?action=del" method="post">
				<input type="hidden" name="id" value="<?php echo $_POST['id'];?>" />
				<input type="hidden" name="submit" value="1" />
				<input type="submit" name="ans" value="Yes, delete that room!" />
			</form><?php
		} else {
			$db->query('DELETE FROM `pusher_chat_rooms` WHERE `id` = '.$_POST['id']);
			stafflog_add('Deleted chat room: '.$name);
			?>You've deleted the Chat Room: &ldquo;<?php echo $name;?>&rdquo;<?php
		}
	} else {
		?><form action="staff_chat.php?action=del" method="post">
			<table class="table" width="75%">
				<tr>
					<th width="25%">Room to delete</th>
					<td width="75%"><select name="id"><?php
						$select = $db->query('SELECT `id`, `chat_name` FROM `pusher_chat_rooms` ORDER BY `id` ASC');
						if(!$db->num_rows($select))
							echo '<option value="0">There are no rooms</option>';
						else
							while($row = $db->fetch_row($select))
								printf('<option value="%u">%s</option>', $row['id'], $row['chat_name']);
					?></select></td>
				</tr>
				<tr>
					<td colspan="2" class="center"><input type="submit" name="submit" value="Delete room" /></td>
				</tr>
			</table>
		</form><?php
	}
}
$h->endpage();

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.