Add history.php

This commit is contained in:
ruan 2015-09-14 18:49:19 +02:00
parent 32e544d431
commit 6597a423e6
8 changed files with 287 additions and 41 deletions

View File

@ -6,7 +6,7 @@ use PDOException;
require_once './includes/page.php';
class Check {
public function run($name) {
public function run($name, $from) {
// validate user input
if (strlen($name) > 16 || !preg_match("/^[0-9a-zA-Z_]{1,16}$/", $name)) {
$this->println("Invalid name.");
@ -28,6 +28,17 @@ class Check {
$this->println("$name has not joined before.");
return;
}
$href = "history.php?uuid=$uuid";
// sanitize $_POST['table'] ($from)
$from_type = $page->type_info($from);
$type = $from_type['type'];
if ($type !== null) {
$href .= "&from=" . lcfirst($from_type['title']);
}
echo "<script type=\"text/javascript\">document.location=\"$href\";</script>";
/*
$table = $page->settings->table['bans'];
$stmt = $page->conn->prepare("SELECT * FROM $table WHERE (uuid=? AND active=" . Settings::$TRUE . ") LIMIT 1");
@ -51,6 +62,7 @@ class Check {
$this->println("Banned permanently.");
}
}
*/
} catch (PDOException $ex) {
die($ex->getMessage());
}
@ -61,7 +73,7 @@ class Check {
}
}
if (isset($_POST['name'], $_POST['table'])) {
if (isset($_GET['name'], $_GET['table'])) {
$check = new Check();
$check->run($_POST['name']);
$check->run($_GET['name'], $_GET['table']);
}

154
history.php Normal file
View File

@ -0,0 +1,154 @@
<?php
namespace litebans;
use PDO;
use PDOException;
use PDORow;
require_once './includes/page.php';
require_once './info.php';
class History {
/**
* Appends COUNT(*) from $table matching $uuid to $counts,
* then appends all rows from $table matching $uuid to $array
* @param Page $page
* @param array $array
* @param string $table
* @param string $uuid
* @param array $counts
*/
static function push($page, &$array, $table, $uuid, &$counts) {
$count_st = $page->conn->prepare("SELECT COUNT(*) AS count FROM $table WHERE uuid=:uuid");
$count_st->bindParam(":uuid", $uuid, PDO::PARAM_STR);
if ($count_st->execute() && ($row = $count_st->fetch()) !== null) {
$counts[$table] = $row['count'];
}
$st = $page->conn->prepare("SELECT * FROM $table WHERE uuid=:uuid ORDER BY time");
$st->bindParam(":uuid", $uuid, PDO::PARAM_STR);
// Incompatible with pager as rows are usort()'d
// $st->bindParam(':limit', $limit, PDO::PARAM_INT);
if ($st->execute()) {
while ($row = $st->fetch(PDO::FETCH_ASSOC)) {
$row['__table__'] = $table;
array_push($array, $row);
}
}
}
/**
* usort() function for rows in the database
* @param PDORow $a
* @param PDORow $b
* @return int
*/
static function cmp_row_date($a, $b) {
$a = $a['time'];
$b = $b['time'];
if ($a === $b) {
return 0;
}
return ($a < $b) ? 1 : -1;
}
}
$page = new Page("history");
if (!isset($_GET['uuid'])) {
die("Missing arguments (uuid).");
}
$uuid = $_GET['uuid'];
$name = $page->get_name($uuid);
if ($name === null) {
die("Player not found in database.");
}
$page->name = "Recent Punishments for $name";
$page->print_title();
$page->print_page_header();
try {
$all = array();
$counts = array();
History::push($page, $all, 'bans', $uuid, $counts);
History::push($page, $all, 'mutes', $uuid, $counts);
History::push($page, $all, 'warnings', $uuid, $counts);
History::push($page, $all, 'kicks', $uuid, $counts);
$total = 0;
foreach ($counts as $count) {
$total += $count;
}
usort($all, array("litebans\\History", "cmp_row_date"));
if (!empty($all)) {
$page->table_begin();
$page->table_print_headers(array("Type", "Player", "Moderator", "Reason", "Date", "Expires"));
$offset = 0;
$limit = $page->settings->limit_per_page;
if ($page->settings->show_pager) {
$current_page = $page->page - 1;
$offset = ($limit * $current_page);
$limit += $offset;
}
$i = 0;
foreach ($all as $row) {
$i++;
if ($page->settings->show_pager && $i < $offset) {
continue;
}
if ($i > $limit) break;
$type = $row['__table__'];
$page->set_info($page->type_info($type));
$style = 'style="font-size: 13px;"';
$label_type = $page->type;
$label_name = Info::create($row, $page, $label_type)->name(); //ucfirst($label_type);
$label = "<span $style class='label label-$label_type'>$label_name</span>";
$page->print_table_rows($row, array(
'Type' => $label,
'Player' => $page->get_avatar($name, $row['uuid']),
'Moderator' => $page->get_avatar($page->get_banner_name($row), $row['banned_by_uuid']),
'Reason' => $page->clean($row['reason']),
'Date' => $page->millis_to_date($row['time']),
'Expires' => $page->expiry($row),
));
}
$page->table_end();
// print pager
if ($page->settings->show_pager) {
$page->name = "history";
$page->print_pager($total, "&uuid=$uuid");
}
} else {
echo "No punishments found.<br><br>";
}
if (isset($_GET['from'])) {
// sanitize $_GET['from']
$info = $page->type_info($_GET['from']);
if ($info['type'] !== null) {
$title = $info['title'];
$href = lcfirst($title) . ".php";
echo "<a class=\"btn\" href=\"$href\">Return to $title</a>";
}
}
$page->print_footer();
} catch (PDOException $ex) {
die($ex->getMessage());
}

View File

@ -41,3 +41,19 @@ a:focus {
tr.hover {
cursor: pointer;
}
.label-ban {
background-color: darkred;
}
.label-mute {
background-color: dimgrey;
}
.label-warn {
background-color: darkorange;
}
.label-kick {
background-color: tomato;
}

View File

@ -24,28 +24,14 @@ class Page {
}
}
$this->name = $name;
switch ($name) {
case "ban":
case "bans":
$this->type = "ban";
$this->table = $settings->table['bans'];
break;
case "mute":
case "mutes":
$this->type = "mute";
$this->table = $settings->table['mutes'];
break;
case "warn":
case "warnings":
$this->type = "warn";
$this->table = $settings->table['warnings'];
break;
case "kick":
case "kicks":
$this->type = "kick";
$this->table = $settings->table['kicks'];
break;
}
$this->type = null;
$this->table = null;
$this->title = null;
$info = $this->type_info($name);
$this->set_info($info);
$this->permanent = array(
'ban' => 'Permanent Ban',
'mute' => 'Permanent Mute',
@ -60,6 +46,46 @@ class Page {
);
}
public function type_info($type) {
$settings = $this->settings;
switch ($type) {
case "ban":
case "bans":
return array(
"type" => "ban",
"table" => $settings->table['bans'],
"title" => "Bans",
);
case "mute":
case "mutes":
return array(
"type" => "mute",
"table" => $settings->table['mutes'],
"title" => "Mutes",
);
case "warn":
case "warnings":
return array(
"type" => "warn",
"table" => $settings->table['warnings'],
"title" => "Warnings",
);
case "kick":
case "kicks":
return array(
"type" => "kick",
"table" => $settings->table['kicks'],
"title" => "Kicks",
);
default:
return array(
"type" => null,
"table" => null,
"title" => null,
);
}
}
function run_query() {
try {
$table = $this->table;
@ -86,12 +112,14 @@ class Page {
}
/**
* Returns an <img> tag representing the Minecraft avatar for a specific name or UUID.
* Returns HTML representing the Minecraft avatar for a specific name or UUID.
* @param $name
* @param $uuid
* @param bool $name_under
* @param string $name_repl
* @return string
*/
function get_avatar($name, $uuid) {
function get_avatar($name, $uuid, $name_under = true, $name_repl = null) {
if (strlen($uuid) === 36 && $uuid[14] === '3') {
// Avatars cannot be associated with offline mode UUIDs (version 3)
$uuid = $name;
@ -101,6 +129,12 @@ class Page {
$src = $this->settings->console_image;
$name = $this->settings->console_name;
}
if ($name_repl !== null) {
$name = $name_repl;
}
if ($name_under) {
return "<p align='center'><img class='avatar noselect' src='$src'/><br>$name</p>";
}
return "<img class='avatar noselect' src='$src'/>$name";
}
@ -176,6 +210,9 @@ class Page {
* @return string
*/
public function expiry($row) {
if ($this->type === "kick") {
return "N/A";
}
if ($row['until'] <= 0) {
return $this->permanent[$this->type];
}
@ -243,19 +280,21 @@ class Page {
<div style="margin-left: 15px;">
<form onsubmit="captureForm(event);" class="form-inline"><div class="form-group"><input type="text" class="form-control" id="user" placeholder="Player"></div><button type="submit" class="btn btn-default" style="margin-left: 5px;">Check</button></form>
</div>
<script type="text/javascript">function captureForm(b){o=$("#output");o.removeClass("in");x=setTimeout(function(){o.html("<br>")}, 150);$.ajax({type:"POST",url:"check.php",data:{name:$("#user").val(),table:"' . $table . '"}}).done(function(c){clearTimeout(x);o.html(c);o.addClass("in")});b.preventDefault();return false};</script>
<script type="text/javascript">function captureForm(b){o=$("#output");o.removeClass("in");x=setTimeout(function(){o.html("<br>")}, 150);$.ajax({type:"GET",url:"check.php?name="+$("#user").val()+"&table=' . $table . '"}).done(function(c){clearTimeout(x);o.html(c);o.addClass("in")});b.preventDefault();return false};</script>
<div id="output" class="success fade" data-alert="alert" style="margin-left: 15px;"><br></div>
</div>
');
}
function print_pager() {
function print_pager($total = -1, $args = "") {
$table = $this->table;
$page = $this->name . ".php";
if (!$this->settings->show_pager) return;
$result = $this->conn->query("SELECT COUNT(*) AS count FROM $table")->fetch(PDO::FETCH_ASSOC);
$total = $result['count'];
if ($total === -1) {
$result = $this->conn->query("SELECT COUNT(*) AS count FROM $table")->fetch(PDO::FETCH_ASSOC);
$total = $result['count'];
}
$pages = (int)($total / $this->settings->limit_per_page) + 1;
@ -271,12 +310,12 @@ class Page {
$pager_prev = "<div class=\"$prev_class\" style=\"float:left; font-size:30px;\">«</div>";
if ($prev_active) {
$pager_prev = "<a href=\"$page?page=$prev\">$pager_prev</a>";
$pager_prev = "<a href=\"$page?page={$prev}{$args}\">$pager_prev</a>";
}
$pager_next = "<div class=\"$next_class\" style=\"float: right; font-size:30px;\">»</div>";
if ($next_active) {
$pager_next = "<a href=\"$page?page=$next\">$pager_next</a>";
$pager_next = "<a href=\"$page?page={$next}{$args}\">$pager_next</a>";
}
$pager_count = "<div style=\"margin-top: 32px;\"><div style=\"text-align: center; font-size:15px;\">Page $cur/$pages</div></div>";
echo "$pager_prev $pager_next $pager_count";
@ -301,4 +340,13 @@ class Page {
echo "<script type=\"text/javascript\">$('tr').click(function(){window.location=$(this).find('a').attr('href');}).hover(function(){\$(this).toggleClass('hover');});</script>";
}
}
/**
* @param $info
*/
public function set_info($info) {
$this->type = $info['type'];
$this->table = $info['table'];
$this->title = $info['title'];
}
}

View File

@ -38,6 +38,11 @@ abstract class Info {
return $this->row['until'] <= 0;
}
function history_link($player_name, $row) {
$uuid = $row['uuid'];
return "<a href=\"history.php?uuid=$uuid\">$player_name</a>";
}
abstract function basic_info($row, $player_name);
}
@ -45,8 +50,8 @@ class BanInfo extends Info {
function basic_info($row, $player_name) {
$page = $this->page;
return array(
'Banned Player' => $page->get_avatar($player_name, $row['uuid']),
'Banned By' => $page->get_avatar($page->get_banner_name($row), $row['banned_by_uuid']),
'Banned Player' => $page->get_avatar($player_name, $row['uuid'], false, $this->history_link($player_name, $row)),
'Banned By' => $page->get_avatar($page->get_banner_name($row), $row['banned_by_uuid'], false),
'Ban Reason' => $page->clean($row['reason']),
'Ban Placed' => $page->millis_to_date($row['time']),
'Expires' => $page->expiry($row),
@ -58,8 +63,8 @@ class MuteInfo extends Info {
function basic_info($row, $player_name) {
$page = $this->page;
return array(
'Muted Player' => $page->get_avatar($player_name, $row['uuid']),
'Muted By' => $page->get_avatar($page->get_banner_name($row), $row['banned_by_uuid']),
'Muted Player' => $page->get_avatar($player_name, $row['uuid'], false, $this->history_link($player_name, $row)),
'Muted By' => $page->get_avatar($page->get_banner_name($row), $row['banned_by_uuid'], false),
'Mute Reason' => $page->clean($row['reason']),
'Mute Placed' => $page->millis_to_date($row['time']),
'Expires' => $page->expiry($row),
@ -75,8 +80,8 @@ class WarnInfo extends Info {
function basic_info($row, $player_name) {
$page = $this->page;
return array(
'Warned Player' => $page->get_avatar($player_name, $row['uuid']),
'Warned By' => $page->get_avatar($page->get_banner_name($row), $row['banned_by_uuid']),
'Warned Player' => $page->get_avatar($player_name, $row['uuid'], false, $this->history_link($player_name, $row)),
'Warned By' => $page->get_avatar($page->get_banner_name($row), $row['banned_by_uuid'], false),
'Warning Reason' => $page->clean($row['reason']),
'Warning Placed' => $page->millis_to_date($row['time']),
'Expires' => $page->expiry($row),
@ -88,14 +93,19 @@ class KickInfo extends Info {
function basic_info($row, $player_name) {
$page = $this->page;
return array(
'Kicked Player' => $page->get_avatar($player_name, $row['uuid']),
'Kicked By' => $page->get_avatar($page->get_banner_name($row), $row['banned_by_uuid']),
'Kicked Player' => $page->get_avatar($player_name, $row['uuid'], false, $this->history_link($player_name, $row)),
'Kicked By' => $page->get_avatar($page->get_banner_name($row), $row['banned_by_uuid'], false),
'Kick Reason' => $page->clean($row['reason']),
'Kick Date' => $page->millis_to_date($row['time']),
);
}
}
// check if info.php is requested, otherwise it's included
if ((substr($_SERVER['SCRIPT_NAME'], -strlen("info.php"))) !== "info.php") {
return;
}
if (!isset($_GET['type'], $_GET['id'])) {
die("Missing arguments (type, id).");
}

View File

@ -10,6 +10,8 @@ $headers = array("Name", "Kicked By", "Reason", "Date");
$page->print_page_header();
$page->print_check_form();
$page->table_begin();
$page->table_print_headers($headers);

View File

@ -10,6 +10,8 @@ $headers = array("Name", "Muted By", "Reason", "Muted On", "Muted Until");
$page->print_page_header();
$page->print_check_form();
$page->table_begin();
$page->table_print_headers($headers);

View File

@ -10,6 +10,8 @@ $headers = array("Name", "Warned By", "Reason", "Warned Until", "Received Warnin
$page->print_page_header();
$page->print_check_form();
$page->table_begin();
$page->table_print_headers($headers);