Location: PHPKode > projects > Zitro-Stats > include/out_admin_clans.inc.php
<?php

	// ANTIHACK
	defined('INSTATS') AND defined('OUT') OR die();

	////////////////////////////////////////////////////////////////
	// ADMINCHECK
	if(!isset($_COOKIE['ZITROSTATS']['ADMIN_PASSWORD']) || $_COOKIE['ZITROSTATS']['ADMIN_PASSWORD']!=md5($options['ADMIN_PASSWORD'])) {
		header("Location: ?inc=admin");
		die();
	}

	////////////////////////////////////////////////////////////////
	// INSERT
	if(@$_POST['mode']=='insert') {
		if($_POST['clan'] || $_POST['clan']=$_POST['clan2'])
			$sql->query("
				REPLACE `$options[SQL_TABPREFIX]clans`
				SET
					`clan`='".$sql->escape($_POST['clan'])."',
					`name`='".$sql->escape($_POST['name'])."',
					`channel`='".$sql->escape($_POST['channel'])."',
					`homepage`='".$sql->escape($_POST['homepage'])."'
				");
		header("Location: $_SERVER[REQUEST_URI]");
		die();
	}
	////////////////////////////////////////////////////////////////
	// EDIT
	elseif(@$_POST['mode']=='edit' && is_array($_POST['clans'])) {
		$deleteclans = array();
		foreach($_POST['clans'] as $oldclan=>$clan) {
			$oldclan = urldecode($oldclan);
			if(@$clan['delete'])
				// DELETE
				$deleteclans[] = $sql->escape($oldclan);
			elseif($clan['name']!=$clan['old_name'] || $clan['channel']!=$clan['old_channel'] || $clan['homepage']!=$clan['old_homepage'])
				// UPDATE
				$sql->query("
					UPDATE `$options[SQL_TABPREFIX]clans`
					SET
						`name`='".$sql->escape($clan['name'])."',
						`channel`='".$sql->escape($clan['channel'])."',
						`homepage`='".$sql->escape($clan['homepage'])."'
					WHERE `clan`='".$sql->escape($oldclan)."'
					LIMIT 1
					");
		}
		if(count($deleteclans))
			$sql->query("
				DELETE FROM `$options[SQL_TABPREFIX]clans`
				WHERE `clan` IN('".implode("','",$deleteclans)."')
				LIMIT ".count($deleteclans)."
				");
		header("Location: $_SERVER[REQUEST_URI]");
		die();
	}

	////////////////////////////////////////////////////////////////
	// OPTIONS
	if(isset($_COOKIE['ZITROSTATS']['CLANS_LIMIT']))
		$options['CLANS_LIMIT'] =
			min(max(1,intval($_COOKIE['ZITROSTATS']['CLANS_LIMIT'])),$options['CLANS_LIMIT_MAX']);
	$filter['rank']		= ($options['CLANS_MIN_KILLS'] ? "`kills`>=$options[CLANS_MIN_KILLS]" : "").($options['CLANS_MIN_PLAYERS'] ? ($options['CLANS_MIN_PLAYERS'] ? " AND " : "")."COUNT(*)>=$options[CLANS_MIN_PLAYERS]" : "");
	$filter['page']		= intval(@$_GET['page']);
	$filter['offset']	= $filter['page'] * $options['CLANS_LIMIT'];
	$filter['clan']		= @$_GET['clan'];
	$filter['name']		= @$_GET['name'];
	$filter['channel']	= @$_GET['channel'];
	$filter['homepage']	= @$_GET['homepage'];
	$filter['sort']		= @$_GET['sort'] && in_array($_GET['sort'],array('name','channel','homepage')) ? $_GET['sort'] : 'clan';
	$filter['user'] 	= array();
	if($filter['clan'])		$filter['user'][] = "LCASE(`clan`) LIKE '%".str_replace(array('%','_'),array('\%','\_'),strtolower($sql->escape($filter['clan'])))."%'";
	if($filter['name'])		$filter['user'][] = "LCASE(`name`) LIKE '%".str_replace(array('%','_'),array('\%','\_'),strtolower($sql->escape($filter['name'])))."%'";
	if($filter['channel'])	$filter['user'][] = "LCASE(`channel`) LIKE '%".str_replace(array('%','_'),array('\%','\_'),strtolower($sql->escape($filter['channel'])))."%'";
	if($filter['homepage'])	$filter['user'][] = "LCASE(`homepage`) LIKE '%".str_replace(array('%','_'),array('\%','\_'),strtolower($sql->escape($filter['homepage'])))."%'";
	$filter['user']		= implode(" AND ",$filter['user']);

	////////////////////////////////////////////////////////////////
	// OUTPUT INSERT
	$result = $sql->query("
		SELECT `clan`
		FROM `$options[SQL_TABPREFIX]clans`
		ORDER BY `clan`
		");
	while(list($not[]) = $sql->fetchrow($result));
	$result = $sql->query("
		SELECT
			`clan`,
			COUNT(*) as `players`,
			ROUND(AVG(`points`)) as `points`,
			SUM(`kills`) as `kills`,
			SUM(`deaths`) as `deaths`,
			SUM(`selfkills`) as `selfkills`,
			IF($filter[rank],1,0) as `ranked`
		FROM `$options[SQL_TABPREFIX]players`
		WHERE `clan`!=''
		GROUP BY `clan`
		ORDER BY
			`ranked` DESC,
			`players` DESC,
			`kills` DESC,
			`clan` ASC
		");
	$out['insert'] = "";
	if($sql->numrows($result)) {
		while($clan = $sql->fetcharray($result)) {
			if(!in_array($clan['clan'],$not)) {
				if(!isset($last))
					$out['insert'] .= "<optgroup label=\"".($clan['ranked']?"R":"Unr")."anked\">";
				elseif($last!=$clan['ranked'])
					$out['insert'] .= "</optgroup><optgroup label=\"".($clan['ranked']?"R":"Unr")."anked\">";
				$out['insert'] .= "<option>".htmlentities($clan['clan'])."</option>";
				$last = $clan['ranked'];
			}
		}
		$out['insert'] .= "</optgroup>";
	}
	$i = 0;
	$out['insert'] =
		"<form method=\"post\" action=\"".htmlentities($_SERVER['REQUEST_URI'])."\">\n".
		out_table(
			out_table_caption("Insert Clan").
			out_table_row(
				out_table_cell(0,1,1,"Clan",H).
				out_table_cell(0,1,1,"<input type=\"text\" name=\"clan\" size=\"10\" maxlength=\"12\" value=\"\"/> or <select name=\"clan2\" size=\"1\"><option></option>".$out['insert']."</select>",L),
				++$i&1 ? "light" : "dark").
			out_table_row(
				out_table_cell(0,1,1,"Name",H).
				out_table_cell(0,1,1,"<input type=\"text\" name=\"name\" size=\"27\" maxlength=\"64\" value=\"\"/>",L),
				++$i&1 ? "light" : "dark").
			out_table_row(
				out_table_cell(0,1,1,"Channel",H).
				out_table_cell(0,1,1,"# <input type=\"text\" name=\"channel\" size=\"25\" maxlength=\"64\" value=\"\"/>",L),
				++$i&1 ? "light" : "dark").
			out_table_row(
				out_table_cell(0,1,1,"Homepage",H).
				out_table_cell(0,1,1,"http:// <input type=\"text\" name=\"homepage\" size=\"19\" maxlength=\"64\" value=\"\"/>",L),
				++$i&1 ? "light" : "dark")
			).
		"<div>\n".
		"	<input type=\"hidden\" name=\"mode\" value=\"insert\" /><input class=\"button\" type=\"submit\" value=\"Insert\" />\n".
		"</div>\n".
		"</form>\n";

	////////////////////////////////////////////////////////////////
	// OUTPUT FILTER
	$i = 0;
	$out['filter'] =
		"<form method=\"get\" action=\"\">\n".
		out_table(
			out_table_caption("Filter Options").
			out_table_row(
				out_table_cell(0,1,1,"Clan",H).
				out_table_cell(0,1,1,"<input type=\"text\" name=\"clan\" size=\"27\" maxlength=\"12\" value=\"".htmlentities($filter['clan'])."\"/>",L),
				++$i&1 ? "light" : "dark").
			out_table_row(
				out_table_cell(0,1,1,"Name",H).
				out_table_cell(0,1,1,"<input type=\"text\" name=\"name\" size=\"27\" maxlength=\"64\" value=\"".htmlentities($filter['name'])."\"/>",L),
				++$i&1 ? "light" : "dark").
			out_table_row(
				out_table_cell(0,1,1,"Channel",H).
				out_table_cell(0,1,1,"<input type=\"text\" name=\"channel\" size=\"27\" maxlength=\"64\" value=\"".htmlentities($filter['channel'])."\"/>",L),
				++$i&1 ? "light" : "dark").
			out_table_row(
				out_table_cell(0,1,1,"Homepage",H).
				out_table_cell(0,1,1,"<input type=\"text\" name=\"homepage\" size=\"27\" maxlength=\"255\" value=\"".htmlentities($filter['homepage'])."\"/>",L),
				++$i&1 ? "light" : "dark").
			out_table_row(
				out_table_cell(0,1,1,"Order",H).
				out_table_cell(0,1,1,"<select name=\"sort\" size=\"1\">".
					"<option value=\"clan\"".($filter['sort']=='clan'?" selected=\"selected\"":"").">Clan</option>".
					"<option value=\"name\"".($filter['sort']=='name'?" selected=\"selected\"":"").">Name</option>".
					"<option value=\"channel\"".($filter['sort']=='channel'?" selected=\"selected\"":"").">Channel</option>".
					"<option value=\"homepage\"".($filter['sort']=='homepage'?" selected=\"selected\"":"").">Homepage</option>".
					"</select>",L),
				++$i&1 ? "light" : "dark")
			).
		"<div>\n".
		"	<input type=\"hidden\" name=\"inc\" value=\"$include\"/>\n".
		"	<input class=\"button\" type=\"submit\" value=\"Filter\"/>\n".
		"</div>\n".
		"</form>\n";

	////////////////////////////////////////////////////////////////
	// OUTPUT EDIT
	$stats = $sql->fetcharray($sql->query("
		SELECT
			COUNT(*) as `clans`
		FROM `$options[SQL_TABPREFIX]clans`
		".($filter['user'] ? "WHERE $filter[user]" : "")."
		"));
	$result = $sql->query("
		SELECT
			`clan`,
			`name`,
			`channel`,
			`homepage`
		FROM `$options[SQL_TABPREFIX]clans`
		".($filter['user'] ? "WHERE $filter[user]" : "")."
		ORDER BY
			`$filter[sort]` ASC,
			`clan`
		".($options['CLANS_LIMIT'] ? "LIMIT $filter[offset],$options[CLANS_LIMIT]" : "")."
		");
	if($sql->numrows($result)) {
		$out['edit'] = "";
		$i = 0;
		while($clan = $sql->fetcharray($result)) {
			$code = urlencode($clan['clan']);
			$out['edit'] .= out_table_row(
				out_table_cells_clan($filter['sort']=='clan'?1:0,1,$clan['clan']).
				out_table_cell($filter['sort']=='name'?1:0,1,1,"<input type=\"hidden\" name=\"clans[$code][old_name]\" value=\"".htmlentities($clan['name'])."\"/><input type=\"text\" name=\"clans[$code][name]\" size=\"30\" maxlength=\"64\" value=\"".htmlentities($clan['name'])."\"/>").
				out_table_cell($filter['sort']=='channel'?1:0,1,1,"<input type=\"hidden\" name=\"clans[$code][old_channel]\" value=\"".htmlentities($clan['channel'])."\"/># <input type=\"text\" name=\"clans[$code][channel]\" size=\"20\" maxlength=\"64\" value=\"".htmlentities($clan['channel'])."\"/>").
				out_table_cell($filter['sort']=='homepage'?1:0,1,1,"<input type=\"hidden\" name=\"clans[$code][old_homepage]\" value=\"".htmlentities($clan['homepage'])."\"/>http:// <input type=\"text\" name=\"clans[$code][homepage]\" size=\"30\" maxlength=\"255\" value=\"".htmlentities($clan['homepage'])."\"/>").
				out_table_cell(0,1,1,"<input type=\"checkbox\" name=\"clans[$code][delete]\"/>"),
				++$i&1 ? "light" : "dark");
		}
		// GENERATE LINK FOR OTHER PLAYER PAGES
		$p3 = ceil($stats['clans']/$options['CLANS_LIMIT'])-1;
		$p1 = max(min($filter['page']-floor($options['CLANS_PAGES']/2),$p3-$options['CLANS_PAGES']),0);
		$p2 = min($p1+$options['CLANS_PAGES'],$p3);
		if($filter['page']>0) {
			$links[] = "<link rel=\"first\" href=\"?inc=$include&amp;page=0".($filter['clan']?"&amp;clan=".urlencode($filter['clan']):"").($filter['name']?"&amp;name=".urlencode($filter['name']):"").($filter['channel']?"&amp;channel=".urlencode($filter['channel']):"").($filter['homepage']?"&amp;homepage=".urlencode($filter['homepage']):"").($filter['sort']?"&amp;sort=$filter[sort]":"")."\" />";
			$links[] = "<link rel=\"previous\" href=\"?inc=$include&amp;page=".($filter['page']-1).($filter['clan']?"&amp;clan=".urlencode($filter['clan']):"").($filter['name']?"&amp;name=".urlencode($filter['name']):"").($filter['channel']?"&amp;channel=".urlencode($filter['channel']):"").($filter['homepage']?"&amp;homepage=".urlencode($filter['homepage']):"").($filter['sort']?"&amp;sort=$filter[sort]":"")."\" />";
		}
		if($filter['page']<$p3) {
			$links[] = "<link rel=\"last\" href=\"?inc=$include&amp;page=$p3".($filter['clan']?"&amp;clan=".urlencode($filter['clan']):"").($filter['name']?"&amp;name=".urlencode($filter['name']):"").($filter['channel']?"&amp;channel=".urlencode($filter['channel']):"").($filter['homepage']?"&amp;homepage=".urlencode($filter['homepage']):"").($filter['sort']?"&amp;sort=$filter[sort]":"")."\" />";
			$links[] = "<link rel=\"next\" href=\"?inc=$include&amp;page=".($filter['page']+1).($filter['clan']?"&amp;clan=".urlencode($filter['clan']):"").($filter['name']?"&amp;name=".urlencode($filter['name']):"").($filter['channel']?"&amp;channel=".urlencode($filter['channel']):"").($filter['homepage']?"&amp;homepage=".urlencode($filter['homepage']):"").($filter['sort']?"&amp;sort=$filter[sort]":"")."\" />";
		}
		$pagelinks = array();
		for($i=$p1; $i<=$p2; $i++) {
			$pagelinks[] = ($i==$filter['page'] ? "<span class=\"larger\">" : "<a href=\"?inc=$include&amp;page=$i".($filter['clan']?"&amp;clan=".urlencode($filter['clan']):"").($filter['name']?"&amp;name=".urlencode($filter['name']):"").($filter['channel']?"&amp;channel=".urlencode($filter['channel']):"").($filter['homepage']?"&amp;homepage=".urlencode($filter['homepage']):"").($filter['sort']?"&amp;sort=$filter[sort]":"")."\">").
				" ".($i+1)." ".
				($i==$filter['page'] ? "</span>" : "</a>");
		}
		$out['edit'] = out_table_foot(
			out_table_row(
				out_table_cell(0,2,1,$sql->numrows($result)." of $stats[clans] Clan".($stats['clans']==1?"":"s"),L).
				out_table_cell(0,4,1,
					"Pages: ".
					($p1>0 ? "<a href=\"?inc=$include&amp;page=0".($filter['clan']?"&amp;clan=".urlencode($filter['clan']):"").($filter['name']?"&amp;name=".urlencode($filter['name']):"").($filter['channel']?"&amp;channel=".urlencode($filter['channel']):"").($filter['homepage']?"&amp;homepage=".urlencode($filter['homepage']):"").($filter['sort']?"&amp;sort=$filter[sort]":"")."\"> 1 </a>".($p1>1?"...":",") : "").
					implode(",",$pagelinks).
					($p2<$p3 ? ($p2<$p3-1?"...":",")."<a href=\"?inc=$include&amp;page=$p3".($filter['clan']?"&amp;clan=".urlencode($filter['clan']):"").($filter['name']?"&amp;name=".urlencode($filter['name']):"").($filter['channel']?"&amp;channel=".urlencode($filter['channel']):"").($filter['homepage']?"&amp;homepage=".urlencode($filter['homepage']):"").($filter['sort']?"&amp;sort=$filter[sort]":"")."\"> ".($p3+1)." </a>" : ""),
					R)
				)
			).
			out_table_body($out['edit']);
	} else {
		$out['edit'] = out_table_body(
			out_table_row(
				out_table_cell(0,6,1,"<span class=\"smaller\">no Clantags listed here</span>",C),
				"light")
			);
	}
	$out['edit'] =
		"<form method=\"post\" action=\"".htmlentities($_SERVER['REQUEST_URI'])."\">\n".
		out_table(
			out_table_caption("Edit Clans").
			out_table_head(
				out_table_row(
					out_table_cell($filter['sort']=='clan'?1:0,2,1,"Clan").
					out_table_cell($filter['sort']=='name'?1:0,1,1,"Name").
					out_table_cell($filter['sort']=='channel'?1:0,1,1,"Channel").
					out_table_cell($filter['sort']=='homepage'?1:0,1,1,"Homepage").
					out_table_cell(0,1,1,"Delete")
					)
				).
				$out['edit']
			).
		"<div>\n".
		"	<input type=\"hidden\" name=\"mode\" value=\"edit\" /><input class=\"button\" type=\"submit\" value=\"Edit\"/>\n".
		"</div>\n".
		"</form>\n";

	////////////////////////////////////////////////////////////////
	// OUTPUT
	$navi[] = "<a href=\"?inc=admin\">Admin</a>";
	$navi[] = "<a href=\"?inc=admin_clantags\">Clans</a>";
	$links[] = "<link rel=\"up\" href=\"?inc=admin\" />";
	$out =
		"<h1>Clans Administration<br /></h1>\n".
		"<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n".
		"	<tr>\n".
		"		<td align=\"center\" valign=\"top\">\n".
		out_indent($out['insert'],3).
		"			<br />\n".
		"			<br />\n".
		"			<br />\n".
		out_indent($out['filter'],3).
		"		</td>\n".
		"		<td class=\"line\">\n".
		"		</td>\n".
		"		<td align=\"center\" valign=\"top\" style=\"width:700px\">\n".
		out_indent($out['edit'],3).
		"		</td>\n".
		"	</tr>\n".
		"</table>\n";

?>
Return current item: Zitro-Stats