litebans-php/history.php
2018-05-31 09:01:20 +02:00

221 lines
6.4 KiB
PHP

<?php
require_once './inc/page.php';
require_once './info.php';
class History {
/**
* Appends all rows from $table matching $uuid to $array
* @param Page $page
* @param array $array
* @param string $type
* @param string $uuid
* @param string $field
*/
static function push($page, &$array, $type, $uuid, $field) {
$table = $page->settings->table[$type];
$sel = $page->get_selection($table);
$limit = $page->settings->limit_per_page;
$offset = History::get_offset($type);
$st = $page->conn->prepare("SELECT $sel FROM $table WHERE $field=:uuid ORDER BY time DESC LIMIT :limit OFFSET :offset");
$st->bindParam(":uuid", $uuid, PDO::PARAM_STR);
$st->bindParam(":limit", $limit, PDO::PARAM_INT);
$st->bindParam(":offset", $offset, PDO::PARAM_INT);
if ($st->execute()) {
while ($row = $st->fetch(PDO::FETCH_ASSOC)) {
$row['__table__'] = $type;
array_push($array, $row);
}
}
$st->closeCursor();
}
/**
* 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;
}
static function get_offset($table) {
$v = $table[0];
if (isset($_GET[$v]) && is_string($_GET[$v])) {
if (filter_var($_GET[$v], FILTER_VALIDATE_INT)) {
return (int)$_GET[$v];
}
}
return 0;
}
}
$page = new Page("history");
isset($_GET['uuid']) && is_string($_GET['uuid']) or die($page->t("error.missing-args"));
$staffhistory = (isset($_GET['staffhistory']) && $_GET['staffhistory'] === "1");
$uuid = $_GET['uuid'];
$name = $page->get_name($uuid);
$name !== null or die(str_replace("{name}", $name, $page->t("error.name.unseen")));
if ($staffhistory) {
$page->title = $page->t("title.staff-history");
} else {
$page->title = $page->t("title.player-history");
}
$page->title = str_replace("{name}", $name, $page->title);
$page->print_title();
$page->print_header();
$from = null;
$from_title = null;
$from_href = null;
if (isset($_GET['from'])) {
// sanitize $_GET['from']
$info = $page->type_info($_GET['from']);
if ($info['type'] !== null) {
$from_title = $info['title'];
$from = Page::lc_first($from_title);
$from_href = "$from.php";
}
}
try {
$all = array();
$field = "uuid";
if ($staffhistory) {
$field = "banned_by_uuid";
}
$t = $page->settings->table;
$t_bans = $t['bans'];
$t_mutes = $t['mutes'];
$t_warnings = $t['warnings'];
$t_kicks = $t['kicks'];
$total = 0;
$count_st = $page->conn->prepare("SELECT
(SELECT COUNT(*) FROM $t_bans WHERE $field=:uuid0) +
(SELECT COUNT(*) FROM $t_mutes WHERE $field=:uuid1) +
(SELECT COUNT(*) FROM $t_warnings WHERE $field=:uuid2) +
(SELECT COUNT(*) FROM $t_kicks WHERE $field=:uuid3) as total
");
for ($i = 0; $i <= 3; $i++) $count_st->bindParam(":uuid$i", $uuid, PDO::PARAM_STR);
if ($count_st->execute() && ($row = $count_st->fetch()) !== null) {
$total = $row['total'];
}
$count_st->closeCursor();
History::push($page, $all, 'bans', $uuid, $field);
History::push($page, $all, 'mutes', $uuid, $field);
History::push($page, $all, 'warnings', $uuid, $field);
History::push($page, $all, 'kicks', $uuid, $field);
usort($all, array("History", "cmp_row_date"));
if (!empty($all)) {
$page->table_begin();
$limit = $page->settings->limit_per_page;
/*$offset = 0;
if ($page->settings->show_pager) {
$current_page = $page->page - 1;
$offset = ($limit * $current_page);
$limit += $offset;
}*/
$totalb = 0;
$totalm = 0;
$totalw = 0;
$totalk = 0;
$i = 0;
foreach ($all as $row) {
$i++;
/*if ($page->settings->show_pager && $i < $offset) {
continue;
}*/
if ($i > $limit) break;
$type = $row['__table__'];
if ($type == 'bans') $totalb++;
elseif ($type == 'mutes') $totalm++;
elseif ($type == 'warnings') $totalw++;
elseif ($type == 'kicks') $totalk++;
$page->set_info($page->type_info($type));
$label_type = $page->type;
$label_name = Info::create($row, $page, $label_type)->name(); //ucfirst($label_type);
$label = "<span class='badge litebans-label-history litebans-label-$label_type'>$label_name</span>";
$page->print_table_rows($row, array(
"type" => $label,
"player" => $page->get_avatar($page->get_name($row['uuid']), $row['uuid']),
"executor" => $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),
"server.name" => $page->server($row),
//'i' => $i . "/" . $limit . "/" . $total,
));
}
$page->table_end();
// print pager
if ($page->settings->show_pager) {
$page->name = "history";
$args = "&uuid=$uuid";
if ($from !== null) {
$args .= "&from=$from";
}
if ($staffhistory) {
$args .= "&staffhistory=1";
}
$prevargs = $args;
$offb = History::get_offset("b") + $totalb;
$offm = History::get_offset("m") + $totalm;
$offw = History::get_offset("w") + $totalw;
$offk = History::get_offset("k") + $totalk;
$args .= "&b=$offb&m=$offm&w=$offw&k=$offk";
$page->print_pager($total, $args, $prevargs);
}
} else {
echo $page->t("history.error.uuid.no-result") . "<br>";
}
if ($from_href !== null) {
$btnlabel = str_replace("{origin}", $from_title, $page->t("action.return"));
echo "<br><a class=\"btn btn-primary\" href=\"$from_href\">$btnlabel</a> ";
}
$page->print_footer();
} catch (PDOException $ex) {
Settings::handle_error($page->settings, $ex);
}