Get rid of globals

This commit is contained in:
Ruan 2015-07-13 07:44:34 +02:00
parent 58b4f1c4aa
commit 992370b447
9 changed files with 191 additions and 177 deletions

View File

@ -1,32 +1,34 @@
<?php require './includes/page.php'; ?> <?php
<title>Tempbans - <?php echo $name; ?></title> require_once './includes/page.php';
$page = new Page();
?>
<title>Tempbans - <?php echo $page->settings->name; ?></title>
<div class="container"> <div class="container">
<?php <?php
print_page_header("Bans"); $page->print_page_header("Bans");
print_check_form("bans"); $page->print_check_form("bans");
?> ?>
<div class="row" style="margin-bottom:60px;"> <div class="row" style="margin-bottom:60px;">
<div class="col-lg-12"> <div class="col-lg-12">
<table class="table table-hover table-bordered table-condensed"> <table class="table table-hover table-bordered table-condensed">
<?php <?php
print_table_headers(array("Name", "Banned By", "Reason", "Banned On", "Banned Until")); $page->print_table_headers(array("Name", "Banned By", "Reason", "Banned On", "Banned Until"));
global $table_bans, $conn, $show_inactive_bans; $result = $page->run_query($page->settings->table_bans);
$result = run_query($table_bans);
while ($row = $result->fetch(PDO::FETCH_ASSOC)) { while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$player_name = get_name($row['uuid']); $player_name = $page->get_name($row['uuid']);
if ($player_name === null) continue; if ($player_name === null) continue;
$until = millis_to_date($row['until']); $until = $page->millis_to_date($row['until']);
?> ?>
<tr> <tr>
<td><?php echo get_avatar($player_name); ?></td> <td><?php echo $page->get_avatar($player_name); ?></td>
<td><?php echo get_avatar(get_banner_name($row)); ?></td> <td><?php echo $page->get_avatar($page->get_banner_name($row)); ?></td>
<td style="width: 30%;"><?php echo clean($row['reason']); ?></td> <td style="width: 30%;"><?php echo $page->clean($row['reason']); ?></td>
<td><?php echo millis_to_date($row['time']); ?></td> <td><?php echo $page->millis_to_date($row['time']); ?></td>
<td> <td>
<?php if ($row['until'] <= 0) { <?php if ($row['until'] <= 0) {
$until = 'Permanent Ban'; $until = 'Permanent Ban';
} }
if ($show_inactive_bans && !$row['active']) { if ($page->settings->show_inactive_bans && !$row['active']) {
$until .= ' (Unbanned)'; $until .= ' (Unbanned)';
} }
echo $until; echo $until;
@ -38,4 +40,4 @@
</div> </div>
</div> </div>
<?php include './includes/footer.php'; ?> <?php include './includes/footer.php'; ?>
</div> </div>

View File

@ -7,9 +7,10 @@ if (isset($_POST['name'], $_POST['table'])) {
return; return;
} }
require './includes/page.php'; require './includes/page.php';
$page = new Page();
$name = $_POST['name']; $name = $_POST['name'];
$stmt = $conn->prepare("SELECT name,uuid FROM " . $table_history . " WHERE name=? ORDER BY date LIMIT 1"); $stmt = $page->conn->prepare("SELECT name,uuid FROM " . $page->settings->table_history . " WHERE name=? ORDER BY date LIMIT 1");
if ($stmt->execute(array($name))) { if ($stmt->execute(array($name))) {
if ($row = $stmt->fetch()) { if ($row = $stmt->fetch()) {
$name = $row['name']; $name = $row['name'];
@ -21,21 +22,21 @@ if (isset($_POST['name'], $_POST['table'])) {
echo($name . ' has not joined before.<br>'); echo($name . ' has not joined before.<br>');
return; return;
} }
$table = $table_bans; $table = $page->settings->table_bans;
$stmt = $conn->prepare("SELECT * FROM " . $table . " WHERE (uuid=? AND active=1) LIMIT 1"); $stmt = $page->conn->prepare("SELECT * FROM " . $table . " WHERE (uuid=? AND active=1) LIMIT 1");
if ($stmt->execute(array($uuid))) { if ($stmt->execute(array($uuid))) {
if (!($row = $stmt->fetch())) { if (!($row = $stmt->fetch())) {
echo($name . ' is not banned.<br>'); echo($name . ' is not banned.<br>');
return; return;
} }
$banner = get_banner_name($row); $banner = $page->get_banner_name($row);
$reason = $row['reason']; $reason = $row['reason'];
$time = millis_to_date($row['time']); $time = $page->millis_to_date($row['time']);
$until = millis_to_date($row['until']); $until = $page->millis_to_date($row['until']);
echo($name . ' is banned!<br>'); echo($name . ' is banned!<br>');
echo('Banned by: ' . $banner . '<br>'); echo('Banned by: ' . $banner . '<br>');
echo('Reason: ' . clean($reason) . '<br>'); echo('Reason: ' . $page->clean($reason) . '<br>');
echo('Banned on: ' . $time . '<br>'); echo('Banned on: ' . $time . '<br>');
if ($row['until'] > 0) { if ($row['until'] > 0) {
echo('Banned until: ' . $until . '<br>'); echo('Banned until: ' . $until . '<br>');
@ -44,4 +45,4 @@ if (isset($_POST['name'], $_POST['table'])) {
} }
} }
} }
?> ?>

View File

@ -1,4 +1,3 @@
<?php require_once './includes/settings.php'; ?>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content=""> <meta name="description" content="">
@ -10,4 +9,4 @@
html { html {
background-image: url('includes/img/377759.png'); background-image: url('includes/img/377759.png');
} }
</style> </style>

View File

@ -1,7 +1,11 @@
<div class="navbar navbar-default navbar-fixed-top" role="navigation"> <div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container"> <div class="container">
<div class="navbar-header"> <div class="navbar-header">
<a class="navbar-brand" href="#"><?php echo $name; ?></a> <a class="navbar-brand" href="#"><?php
require_once './includes/settings.php';
$settings = new Settings(false);
echo $settings->name;
?></a>
</div> </div>
<div class="navbar-collapse collapse"> <div class="navbar-collapse collapse">
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
@ -12,4 +16,4 @@
</ul> </ul>
</div> </div>
</div> </div>
</div> </div>

View File

@ -3,100 +3,103 @@ require './includes/head.php';
require './includes/header.php'; require './includes/header.php';
require_once './includes/settings.php'; require_once './includes/settings.php';
litebans_connect(); //litebans_connect();
function get_query($table) { class Page {
global $active_query, $limit_per_page; public function __construct() {
return 'SELECT * FROM ' . $table . $active_query . $settings = new Settings();
' GROUP BY ' . $table . '.id ORDER BY time DESC LIMIT ' . $limit_per_page; $this->conn = $settings->conn;
} $this->settings = $settings;
$this->uuid_name_cache = array();
function run_query($table) {
global $conn;
$time = microtime(true);
try {
$result = $conn->query(get_query($table));
} catch (PDOException $ex) {
die($ex->getMessage());
} }
echo('<!-- Query executed in ' . (microtime(true) - $time) . ' sec -->');
return $result;
}
function get_avatar($name) { function get_query($table) {
return "<img src='https://cravatar.eu/avatar/" . $name . "/25' style='margin-bottom:5px;margin-right:5px;border-radius:2px;' />" . $name; return 'SELECT * FROM ' . $table . $this->settings->active_query .
} ' GROUP BY ' . $table . '.id ORDER BY time DESC LIMIT ' . $this->settings->limit_per_page;
}
$uuid_name_cache = array(); function run_query($table) {
$time = microtime(true);
function get_name($uuid) { try {
global $conn, $table_history, $uuid_name_cache; $result = $this->conn->query($this->get_query($table));
if (array_key_exists($uuid, $uuid_name_cache)) return $uuid_name_cache[$uuid]; } catch (PDOException $ex) {
$time = microtime(true); die($ex->getMessage());
$stmt = $conn->prepare("SELECT name FROM " . $table_history . " WHERE uuid=? ORDER BY date DESC LIMIT 1"); }
if ($stmt->execute(array($uuid)) && $row = $stmt->fetch()) {
echo('<!-- Query executed in ' . (microtime(true) - $time) . ' sec -->'); echo('<!-- Query executed in ' . (microtime(true) - $time) . ' sec -->');
$banner = $row['name']; return $result;
$uuid_name_cache[$uuid] = $banner;
return $banner;
} }
$uuid_name_cache[$uuid] = null;
return null;
}
function get_banner_name($row) { function get_avatar($name) {
$uuid = $row['banned_by_uuid']; return "<img src='https://cravatar.eu/avatar/" . $name . "/25' style='margin-bottom:5px;margin-right:5px;border-radius:2px;' />" . $name;
$name = get_name($uuid);
if ($name !== null) {
return $name;
} }
$name = $row['banned_by_name'];
return clean($name);
}
function millis_to_date($millis) {
global $date_format;
return date($date_format, $millis / 1000);
}
/** function get_name($uuid) {
* Prepares text to be displayed on the web interface. if (array_key_exists($uuid, $this->uuid_name_cache)) return $this->uuid_name_cache[$uuid];
* Removes chat colours, replaces newlines with proper HTML, and sanitizes the text. $time = microtime(true);
* @param $text $stmt = $this->conn->prepare("SELECT name FROM " . $this->settings->table_history . " WHERE uuid=? ORDER BY date DESC LIMIT 1");
* @return mixed|string if ($stmt->execute(array($uuid)) && $row = $stmt->fetch()) {
*/ echo('<!-- Query executed in ' . (microtime(true) - $time) . ' sec -->');
function clean($text) { $banner = $row['name'];
if (strstr($text, "\xa7") || strstr($text, "&")) { $uuid_name_cache[$uuid] = $banner;
$regex = "/(?i)(\xa7|&)[0-9A-FK-OR]/"; return $banner;
$text = preg_replace($regex, "", $text); }
$uuid_name_cache[$uuid] = null;
return null;
} }
$text = htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
if (strstr($text, "\n")) {
$text = preg_replace("/\n/", "<br>", $text);
}
return $text;
}
function print_page_header($title) { function get_banner_name($row) {
echo(' $uuid = $row['banned_by_uuid'];
$name = $this->get_name($uuid);
if ($name !== null) {
return $name;
}
$name = $row['banned_by_name'];
return $this->clean($name);
}
function millis_to_date($millis) {
return date($this->settings->date_format, $millis / 1000);
}
/**
* Prepares text to be displayed on the web interface.
* Removes chat colours, replaces newlines with proper HTML, and sanitizes the text.
* @param $text
* @return mixed|string
*/
function clean($text) {
if (strstr($text, "\xa7") || strstr($text, "&")) {
$regex = "/(?i)(\xa7|&)[0-9A-FK-OR]/";
$text = preg_replace($regex, "", $text);
}
$text = htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
if (strstr($text, "\n")) {
$text = preg_replace("/\n/", "<br>", $text);
}
return $text;
}
function print_page_header($title) {
echo('
<div class="row"> <div class="row">
<div class="col-lg-12"> <div class="col-lg-12">
<h1 class="' . ($title === "Bans" ? "modal" : "navbar") . '-header">' . $title . '</h1> <h1 class="' . ($title === "Bans" ? "modal" : "navbar") . '-header">' . $title . '</h1>
</div> </div>
</div> </div>
'); ');
}
function print_table_headers($headers) {
echo("<thead><tr>");
foreach ($headers as $header) {
echo '<th><div style="text-align: center;">', $header, '</div></th>';
} }
echo("<tbody>");
}
function print_check_form($table) { function print_table_headers($headers) {
echo(' echo("<thead><tr>");
foreach ($headers as $header) {
echo '<th><div style="text-align: center;">', $header, '</div></th>';
}
echo("<tbody>");
}
function print_check_form($table) {
echo('
<div class="row"> <div class="row">
<div style="margin-left: 15px;"> <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> <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>
@ -105,6 +108,7 @@ function print_check_form($table) {
<div id="output" class="success fade" data-alert="alert" style="margin-left: 15px;"><br></div> <div id="output" class="success fade" data-alert="alert" style="margin-left: 15px;"><br></div>
</div> </div>
'); ');
}
} }
?> ?>

View File

@ -1,62 +1,58 @@
<?php <?php
/** Server settings **/
$name = 'LiteBans';
/** MySQL settings **/ final class Settings {
// Server host public function __construct($connect = true) {
$dbhost = 'localhost'; // Server name
$dbport = 3306; $this->name = 'LiteBans';
$username = 'root'; // Server host
$password = 'password'; $dbhost = 'localhost';
$dbport = 3306;
// Database name $username = 'root';
$database = 'litebans'; $password = 'password';
// Show inactive bans? Removed bans will show (Unbanned), mutes will show (Unmuted), warnings will show (Expired). // Database name
$show_inactive_bans = true; $database = 'litebans';
// Amount of bans/mutes/warnings to show on each page // Show inactive bans? Removed bans will show (Unbanned), mutes will show (Unmuted), warnings will show (Expired).
$limit_per_page = 20; $this->show_inactive_bans = true;
// If you set a table prefix in config.yml, put it here too // Amount of bans/mutes/warnings to show on each page
$table_prefix = ""; $this->limit_per_page = 20;
// The date format can be changed here. // If you set a table prefix in config.yml, put it here too
// https://secure.php.net/manual/en/function.date.php $this->table_prefix = "";
// Example of default:
// July 2, 2015, 9:19 pm
$date_format = 'F j, Y, g:i a';
date_default_timezone_set("UTC");
$driver = 'mysql'; $this->table_bans = $this->table_prefix . "bans";
$this->table_mutes = $this->table_prefix . "mutes";
$this->table_warnings = $this->table_prefix . "warnings";
$this->table_history = $this->table_prefix . "history";
/*****************************************************************************/ // The date format can be changed here.
function litebans_connect() { // https://secure.php.net/manual/en/function.date.php
// imported // Example of default:
global $dbhost, $dbport, $username, $password, $database, $table_prefix, $show_inactive_bans, $driver; // July 2, 2015, 9:19 pm
$this->date_format = 'F j, Y, g:i a';
date_default_timezone_set("UTC");
// exported $this->driver = 'mysql';
global $conn, $active_query;
global $table_bans, $table_mutes, $table_warnings, $table_history;
$dsn = $driver . ':dbname=' . $database . ';host=' . $dbhost . ';port=' . $dbport . ';charset=utf8'; $this->active_query = "";
if (!$this->show_inactive_bans) {
$this->active_query = "WHERE active=1";
}
try { if ($connect) {
$conn = new PDO($dsn, $username, $password); $dsn = $this->driver . ':dbname=' . $database . ';host=' . $dbhost . ';port=' . $dbport . ';charset=utf8';
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$table_bans = $table_prefix . "bans"; try {
$table_mutes = $table_prefix . "mutes"; $this->conn = new PDO($dsn, $username, $password);
$table_warnings = $table_prefix . "warnings"; } catch (PDOException $e) {
$table_history = $table_prefix . "history"; echo 'Connection failed: ' . $e->getMessage();
}
$active_query = "WHERE active=1"; }
if ($show_inactive_bans) {
$active_query = "";
} }
} }
?> ?>

View File

@ -1,9 +1,13 @@
<?php include './includes/head.php'; ?> <?php
<?php include './includes/header.php'; ?> include './includes/head.php';
<title>Index - <?php echo $name; ?></title> include './includes/header.php';
include_once './includes/settings.php';
$settings = new Settings(false);
?>
<title>Index - <?php echo $settings->name; ?></title>
<div class="container"> <div class="container">
<div class="jumbotron"> <div class="jumbotron">
<div style="text-align: center;"><h2>Welcome to <?php echo $name; ?>'s Ban List.</h2></div> <div style="text-align: center;"><h2>Welcome to <?php echo $settings->name; ?>'s Ban List.</h2></div>
<div style="text-align: center;"><p>Here is where our Bans, Mutes, and Warnings are listed.</p></div> <div style="text-align: center;"><p>Here is where our Bans, Mutes, and Warnings are listed.</p></div>
</div> </div>

View File

@ -1,31 +1,33 @@
<?php require './includes/page.php'; ?> <?php
<title>TempMutes - <?php echo $name; ?></title> require_once './includes/page.php';
$page = new Page();
?>
<title>TempMutes - <?php echo $page->settings->name; ?></title>
<div class="container"> <div class="container">
<?php <?php
print_page_header("Mutes"); $page->print_page_header("Mutes");
?> ?>
<div class="row" style="margin-bottom:60px;"> <div class="row" style="margin-bottom:60px;">
<div class="col-lg-12"> <div class="col-lg-12">
<table class="table table-hover table-bordered table-condensed"> <table class="table table-hover table-bordered table-condensed">
<?php <?php
print_table_headers(array("Name", "Muted By", "Reason", "Muted On", "Muted Until")); $page->print_table_headers(array("Name", "Muted By", "Reason", "Muted On", "Muted Until"));
global $table_mutes, $conn, $show_inactive_bans; $result = $page->run_query($page->settings->table_mutes);
$result = run_query($table_mutes);
while ($row = $result->fetch(PDO::FETCH_ASSOC)) { while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$player_name = get_name($row['uuid']); $player_name = $page->get_name($row['uuid']);
if ($player_name === null) continue; if ($player_name === null) continue;
$until = millis_to_date($row['until']); $until = $page->millis_to_date($row['until']);
?> ?>
<tr> <tr>
<td><?php echo get_avatar($player_name); ?></td> <td><?php echo $page->get_avatar($player_name); ?></td>
<td><?php echo get_avatar(get_banner_name($row)); ?></td> <td><?php echo $page->get_avatar($page->get_banner_name($row)); ?></td>
<td style="width: 30%;"><?php echo clean($row['reason']); ?></td> <td style="width: 30%;"><?php echo $page->clean($row['reason']); ?></td>
<td><?php echo millis_to_date($row['time']); ?></td> <td><?php echo $page->millis_to_date($row['time']); ?></td>
<td> <td>
<?php if ($row['until'] <= 0) { <?php if ($row['until'] <= 0) {
$until = 'Permanent Mute'; $until = 'Permanent Mute';
} }
if ($show_inactive_bans && !$row['active']) { if ($page->settings->show_inactive_bans && !$row['active']) {
$until .= ' (Unmuted)'; $until .= ' (Unmuted)';
} }
echo $until; echo $until;
@ -37,4 +39,4 @@
</div> </div>
</div> </div>
<?php include './includes/footer.php'; ?> <?php include './includes/footer.php'; ?>
</div> </div>

View File

@ -1,30 +1,32 @@
<?php require './includes/page.php'; ?> <?php
<title>Warnings - <?php echo $name; ?></title> require_once './includes/page.php';
$page = new Page();
?>
<title>Warnings - <?php echo $page->settings->name; ?></title>
<div class="container"> <div class="container">
<?php <?php
print_page_header("Warnings"); $page->print_page_header("Warnings");
?> ?>
<div class="row" style="margin-bottom:60px;"> <div class="row" style="margin-bottom:60px;">
<div class="col-lg-12"> <div class="col-lg-12">
<table class="table table-hover table-bordered table-condensed"> <table class="table table-hover table-bordered table-condensed">
<?php <?php
print_table_headers(array("Name", "Warned By", "Reason", "Warned Until", "Received Warning?")); $page->print_table_headers(array("Name", "Warned By", "Reason", "Warned Until", "Received Warning?"));
global $table_warnings, $conn, $show_inactive_bans; $result = $page->run_query($page->settings->table_warnings);
$result = run_query($table_warnings);
while ($row = $result->fetch(PDO::FETCH_ASSOC)) { while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$player_name = get_name($row['uuid']); $player_name = $page->get_name($row['uuid']);
if ($player_name === null) continue; if ($player_name === null) continue;
$until = millis_to_date($row['until']); $until = $page->millis_to_date($row['until']);
?> ?>
<tr> <tr>
<td><?php echo get_avatar($player_name); ?></td> <td><?php echo $page->get_avatar($player_name); ?></td>
<td><?php echo get_avatar(get_banner_name($row)); ?></td> <td><?php echo $page->get_avatar($page->get_banner_name($row)); ?></td>
<td style="width: 30%;"><?php echo clean($row['reason']); ?></td> <td style="width: 30%;"><?php echo $page->clean($row['reason']); ?></td>
<td> <td>
<?php if ($row['until'] <= 0) { <?php if ($row['until'] <= 0) {
$until = 'Permanent Warning'; $until = 'Permanent Warning';
} }
if ($show_inactive_bans && !$row['active']) { if ($page->settings->show_inactive_bans && !$row['active']) {
$until .= ' (Expired)'; $until .= ' (Expired)';
} }
echo $until; echo $until;
@ -39,4 +41,4 @@
</div> </div>
</div> </div>
<?php include './includes/footer.php'; ?> <?php include './includes/footer.php'; ?>
</div> </div>