Clean up concatenation

This commit is contained in:
Ruan 2015-07-13 09:04:41 +02:00
parent 692629af09
commit 598dc950a9
3 changed files with 32 additions and 32 deletions

View File

@ -3,13 +3,14 @@ if (isset($_POST['name'], $_POST['table'])) {
$name = $_POST['name']; $name = $_POST['name'];
// validate user input // validate user input
if (strlen($name) > 16 || !preg_match("/^[0-9a-zA-Z_]{1,16}$/", $name)) { if (strlen($name) > 16 || !preg_match("/^[0-9a-zA-Z_]{1,16}$/", $name)) {
echo('Invalid name.'); echo "Invalid name.";
return; return;
} }
require './includes/page.php'; require './includes/page.php';
$page = new Page(); $page = new Page();
$history = $page->settings->table_history;
$stmt = $page->conn->prepare("SELECT name,uuid FROM " . $page->settings->table_history . " WHERE name=? ORDER BY date LIMIT 1"); $stmt = $page->conn->prepare("SELECT name,uuid FROM $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'];
@ -18,29 +19,29 @@ if (isset($_POST['name'], $_POST['table'])) {
} }
if (!isset($uuid)) { if (!isset($uuid)) {
$name = htmlspecialchars($name, ENT_QUOTES, 'UTF-8'); $name = htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
echo($name . ' has not joined before.<br>'); echo "$name has not joined before.<br>";
return; return;
} }
$table = $page->settings->table_bans; $table = $page->settings->table_bans;
$stmt = $page->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 = $page->get_banner_name($row); $banner = $page->get_banner_name($row);
$reason = $row['reason']; $reason = $page->clean($row['reason']);
$time = $page->millis_to_date($row['time']); $time = $page->millis_to_date($row['time']);
$until = $page->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: ' . $page->clean($reason) . '<br>'); echo "Reason: $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>";
} else { } else {
echo('Banned permanently.<br>'); echo "Banned permanently.<br>";
} }
} }
} }

View File

@ -12,18 +12,17 @@ class Page {
} }
function get_query($table) { function get_query($table) {
return 'SELECT * FROM ' . $table . $this->settings->active_query . $active_query = $this->settings->active_query;
' GROUP BY ' . $table . '.id ORDER BY time DESC LIMIT ' . $this->settings->limit_per_page; $limit = $this->settings->limit_per_page;
return "SELECT * FROM $table $active_query GROUP BY $table.id ORDER BY time DESC LIMIT $limit";
} }
function run_query($table) { function run_query($table) {
$time = microtime(true);
try { try {
$result = $this->conn->query($this->get_query($table)); $result = $this->conn->query($this->get_query($table));
} catch (PDOException $ex) { } catch (PDOException $ex) {
die($ex->getMessage()); die($ex->getMessage());
} }
echo('<!-- Query executed in ' . (microtime(true) - $time) . ' sec -->');
return $result; return $result;
} }
@ -33,10 +32,9 @@ class Page {
function get_name($uuid) { function get_name($uuid) {
if (array_key_exists($uuid, $this->uuid_name_cache)) return $this->uuid_name_cache[$uuid]; if (array_key_exists($uuid, $this->uuid_name_cache)) return $this->uuid_name_cache[$uuid];
$time = microtime(true); $history = $this->settings->table_history;
$stmt = $this->conn->prepare("SELECT name FROM " . $this->settings->table_history . " WHERE uuid=? ORDER BY date DESC LIMIT 1"); $stmt = $this->conn->prepare("SELECT name FROM $history WHERE uuid=? ORDER BY date DESC LIMIT 1");
if ($stmt->execute(array($uuid)) && $row = $stmt->fetch()) { if ($stmt->execute(array($uuid)) && $row = $stmt->fetch()) {
echo('<!-- Query executed in ' . (microtime(true) - $time) . ' sec -->');
$banner = $row['name']; $banner = $row['name'];
$this->uuid_name_cache[$uuid] = $banner; $this->uuid_name_cache[$uuid] = $banner;
return $banner; return $banner;
@ -78,19 +76,20 @@ class Page {
} }
function print_page_header($title) { function print_page_header($title) {
echo(' $type = $title === "Bans" ? "modal" : "navbar";
<div class="row"> echo("
<div class="col-lg-12"> <div class=\"row\">
<h1 class="' . ($title === "Bans" ? "modal" : "navbar") . '-header">' . $title . '</h1> <div class=\"col-lg-12\">
<h1 class=\"$type-header\">$title</h1>
</div> </div>
</div> </div>
'); ");
} }
function print_table_headers($headers) { function print_table_headers($headers) {
echo("<thead><tr>"); echo("<thead><tr>");
foreach ($headers as $header) { foreach ($headers as $header) {
echo '<th><div style="text-align: center;">', $header, '</div></th>'; echo "<th><div style=\"text-align: center;\">$header</div></th>";
} }
echo("<tbody>"); echo("<tbody>");
} }

View File

@ -22,12 +22,12 @@ final class Settings {
$this->limit_per_page = 20; $this->limit_per_page = 20;
// If you set a table prefix in config.yml, put it here too // If you set a table prefix in config.yml, put it here too
$this->table_prefix = ""; $table_prefix = "";
$this->table_bans = $this->table_prefix . "bans"; $this->table_bans = "{$table_prefix}bans";
$this->table_mutes = $this->table_prefix . "mutes"; $this->table_mutes = "{$table_prefix}mutes";
$this->table_warnings = $this->table_prefix . "warnings"; $this->table_warnings = "{$table_prefix}warnings";
$this->table_history = $this->table_prefix . "history"; $this->table_history = "{$table_prefix}history";
// The date format can be changed here. // The date format can be changed here.
// https://secure.php.net/manual/en/function.date.php // https://secure.php.net/manual/en/function.date.php
@ -36,7 +36,7 @@ final class Settings {
$this->date_format = 'F j, Y, g:i a'; $this->date_format = 'F j, Y, g:i a';
date_default_timezone_set("UTC"); date_default_timezone_set("UTC");
$this->driver = 'mysql'; $driver = 'mysql';
$this->active_query = ""; $this->active_query = "";
if (!$this->show_inactive_bans) { if (!$this->show_inactive_bans) {
@ -44,7 +44,7 @@ final class Settings {
} }
if ($connect) { if ($connect) {
$dsn = $this->driver . ':dbname=' . $database . ';host=' . $dbhost . ';port=' . $dbport . ';charset=utf8'; $dsn = "$driver:dbname=$database;host=$dbhost;port=$dbport;charset=utf8";
try { try {
$this->conn = new PDO($dsn, $username, $password); $this->conn = new PDO($dsn, $username, $password);