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'; ?>
<title>Tempbans - <?php echo $name; ?></title>
<?php
require_once './includes/page.php';
$page = new Page();
?>
<title>Tempbans - <?php echo $page->settings->name; ?></title>
<div class="container">
<?php
print_page_header("Bans");
print_check_form("bans");
$page->print_page_header("Bans");
$page->print_check_form("bans");
?>
<div class="row" style="margin-bottom:60px;">
<div class="col-lg-12">
<table class="table table-hover table-bordered table-condensed">
<?php
print_table_headers(array("Name", "Banned By", "Reason", "Banned On", "Banned Until"));
global $table_bans, $conn, $show_inactive_bans;
$result = run_query($table_bans);
$page->print_table_headers(array("Name", "Banned By", "Reason", "Banned On", "Banned Until"));
$result = $page->run_query($page->settings->table_bans);
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;
$until = millis_to_date($row['until']);
$until = $page->millis_to_date($row['until']);
?>
<tr>
<td><?php echo get_avatar($player_name); ?></td>
<td><?php echo get_avatar(get_banner_name($row)); ?></td>
<td style="width: 30%;"><?php echo clean($row['reason']); ?></td>
<td><?php echo millis_to_date($row['time']); ?></td>
<td><?php echo $page->get_avatar($player_name); ?></td>
<td><?php echo $page->get_avatar($page->get_banner_name($row)); ?></td>
<td style="width: 30%;"><?php echo $page->clean($row['reason']); ?></td>
<td><?php echo $page->millis_to_date($row['time']); ?></td>
<td>
<?php if ($row['until'] <= 0) {
$until = 'Permanent Ban';
}
if ($show_inactive_bans && !$row['active']) {
if ($page->settings->show_inactive_bans && !$row['active']) {
$until .= ' (Unbanned)';
}
echo $until;

View File

@ -7,9 +7,10 @@ if (isset($_POST['name'], $_POST['table'])) {
return;
}
require './includes/page.php';
$page = new Page();
$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 ($row = $stmt->fetch()) {
$name = $row['name'];
@ -21,21 +22,21 @@ if (isset($_POST['name'], $_POST['table'])) {
echo($name . ' has not joined before.<br>');
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 (!($row = $stmt->fetch())) {
echo($name . ' is not banned.<br>');
return;
}
$banner = get_banner_name($row);
$banner = $page->get_banner_name($row);
$reason = $row['reason'];
$time = millis_to_date($row['time']);
$until = millis_to_date($row['until']);
$time = $page->millis_to_date($row['time']);
$until = $page->millis_to_date($row['until']);
echo($name . ' is banned!<br>');
echo('Banned by: ' . $banner . '<br>');
echo('Reason: ' . clean($reason) . '<br>');
echo('Reason: ' . $page->clean($reason) . '<br>');
echo('Banned on: ' . $time . '<br>');
if ($row['until'] > 0) {
echo('Banned until: ' . $until . '<br>');

View File

@ -1,4 +1,3 @@
<?php require_once './includes/settings.php'; ?>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">

View File

@ -1,7 +1,11 @@
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<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 class="navbar-collapse collapse">
<ul class="nav navbar-nav">

View File

@ -3,100 +3,103 @@ require './includes/head.php';
require './includes/header.php';
require_once './includes/settings.php';
litebans_connect();
//litebans_connect();
function get_query($table) {
global $active_query, $limit_per_page;
return 'SELECT * FROM ' . $table . $active_query .
' GROUP BY ' . $table . '.id ORDER BY time DESC LIMIT ' . $limit_per_page;
}
function run_query($table) {
global $conn;
$time = microtime(true);
try {
$result = $conn->query(get_query($table));
} catch (PDOException $ex) {
die($ex->getMessage());
class Page {
public function __construct() {
$settings = new Settings();
$this->conn = $settings->conn;
$this->settings = $settings;
$this->uuid_name_cache = array();
}
echo('<!-- Query executed in ' . (microtime(true) - $time) . ' sec -->');
return $result;
}
function get_avatar($name) {
return "<img src='https://cravatar.eu/avatar/" . $name . "/25' style='margin-bottom:5px;margin-right:5px;border-radius:2px;' />" . $name;
}
function get_query($table) {
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 get_name($uuid) {
global $conn, $table_history, $uuid_name_cache;
if (array_key_exists($uuid, $uuid_name_cache)) return $uuid_name_cache[$uuid];
$time = microtime(true);
$stmt = $conn->prepare("SELECT name FROM " . $table_history . " WHERE uuid=? ORDER BY date DESC LIMIT 1");
if ($stmt->execute(array($uuid)) && $row = $stmt->fetch()) {
function run_query($table) {
$time = microtime(true);
try {
$result = $this->conn->query($this->get_query($table));
} catch (PDOException $ex) {
die($ex->getMessage());
}
echo('<!-- Query executed in ' . (microtime(true) - $time) . ' sec -->');
$banner = $row['name'];
$uuid_name_cache[$uuid] = $banner;
return $banner;
return $result;
}
$uuid_name_cache[$uuid] = null;
return null;
}
function get_banner_name($row) {
$uuid = $row['banned_by_uuid'];
$name = get_name($uuid);
if ($name !== null) {
return $name;
function get_avatar($name) {
return "<img src='https://cravatar.eu/avatar/" . $name . "/25' style='margin-bottom:5px;margin-right:5px;border-radius:2px;' />" . $name;
}
$name = $row['banned_by_name'];
return clean($name);
}
function millis_to_date($millis) {
global $date_format;
return date($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);
function get_name($uuid) {
if (array_key_exists($uuid, $this->uuid_name_cache)) return $this->uuid_name_cache[$uuid];
$time = microtime(true);
$stmt = $this->conn->prepare("SELECT name FROM " . $this->settings->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 -->');
$banner = $row['name'];
$uuid_name_cache[$uuid] = $banner;
return $banner;
}
$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) {
echo('
function get_banner_name($row) {
$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="col-lg-12">
<h1 class="' . ($title === "Bans" ? "modal" : "navbar") . '-header">' . $title . '</h1>
</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) {
echo('
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) {
echo('
<div class="row">
<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>
@ -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>
');
}
}
?>

View File

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

View File

@ -1,9 +1,13 @@
<?php include './includes/head.php'; ?>
<?php include './includes/header.php'; ?>
<title>Index - <?php echo $name; ?></title>
<?php
include './includes/head.php';
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="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>

View File

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

View File

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