Refactor, fix active=1 with pgsql

This commit is contained in:
Ruan 2015-08-23 08:22:26 +02:00
parent 6ab6d09db9
commit 636f33205a
3 changed files with 74 additions and 47 deletions

View File

@ -1,48 +1,64 @@
<?php <?php
if (isset($_POST['name'], $_POST['table'])) { require_once './includes/page.php';
$name = $_POST['name'];
// validate user input
if (strlen($name) > 16 || !preg_match("/^[0-9a-zA-Z_]{1,16}$/", $name)) {
echo "Invalid name.";
return;
}
require './includes/page.php';
$page = new Page();
$history = $page->settings->table_history;
$stmt = $page->conn->prepare("SELECT name,uuid FROM $history WHERE name=? ORDER BY date LIMIT 1"); class Check {
if ($stmt->execute(array($name))) { public function run($name) {
if ($row = $stmt->fetch()) { // validate user input
$name = $row['name']; if (strlen($name) > 16 || !preg_match("/^[0-9a-zA-Z_]{1,16}$/", $name)) {
$uuid = $row['uuid']; $this->println("Invalid name.");
}
}
if (!isset($uuid)) {
$name = htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
echo "$name has not joined before.<br>";
return;
}
$table = $page->settings->table_bans;
$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; return;
} }
$banner = $page->get_banner_name($row); $page = new Page(false);
$reason = $page->clean($row['reason']); $history = $page->settings->table_history;
$time = $page->millis_to_date($row['time']);
$until = $page->millis_to_date($row['until']); try {
echo "$name is banned!<br>"; $stmt = $page->conn->prepare("SELECT name,uuid FROM $history WHERE name=? ORDER BY date LIMIT 1");
echo "Banned by: $banner<br>"; if ($stmt->execute(array($name))) {
echo "Reason: $reason<br>"; if ($row = $stmt->fetch()) {
echo "Banned on: $time<br>"; $name = $row['name'];
if ($row['until'] > 0) { $uuid = $row['uuid'];
echo "Banned until: $until<br>"; }
} else { }
echo "Banned permanently.<br>"; if (!isset($uuid)) {
$name = htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
$this->println("$name has not joined before.");
return;
}
$table = $page->settings->table_bans;
$stmt = $page->conn->prepare("SELECT * FROM $table WHERE (uuid=? AND active=" . Settings::$TRUE . ") LIMIT 1");
if ($stmt->execute(array($uuid))) {
if (!($row = $stmt->fetch())) {
$this->println("$name is not banned.");
return;
}
$banner = $page->get_banner_name($row);
$reason = $page->clean($row['reason']);
$time = $page->millis_to_date($row['time']);
$until = $page->millis_to_date($row['until']);
$this->println("$name is banned!");
$this->println("Banned by: $banner");
$this->println("Reason: $reason");
$this->println("Banned on: $time");
if ($row['until'] > 0) {
$this->println("Banned until: $until");
} else {
$this->println("Banned permanently.");
}
}
} catch (PDOException $ex) {
die($ex->getMessage());
} }
} }
function println($line) {
echo "$line<br>";
}
}
if (isset($_POST['name'], $_POST['table'])) {
$check = new Check();
$check->run($_POST['name']);
} }
?> ?>

View File

@ -1,17 +1,18 @@
<?php <?php
require './includes/head.php';
require './includes/header.php';
require_once './includes/settings.php';
class Page { class Page {
public function __construct() { public function __construct($header = true) {
if ($header) {
require_once './includes/head.php';
require_once './includes/header.php';
}
require_once './includes/settings.php';
$settings = new Settings(); $settings = new Settings();
$this->conn = $settings->conn; $this->conn = $settings->conn;
$this->settings = $settings; $this->settings = $settings;
$this->uuid_name_cache = array(); $this->uuid_name_cache = array();
$this->time = microtime(true); $this->time = microtime(true);
$this->page = 1; $this->page = 1;
if (isset($_GET['page'])) { if (isset($_GET['page'])) {
$page = $_GET['page']; // user input $page = $_GET['page']; // user input
if (filter_var($page, FILTER_VALIDATE_INT)) { if (filter_var($page, FILTER_VALIDATE_INT)) {

View File

@ -1,6 +1,8 @@
<?php <?php
final class Settings { final class Settings {
public static $TRUE = "1", $FALSE = "0";
public function __construct($connect = true) { public function __construct($connect = true) {
// Server name, shown on the main page and on the header // Server name, shown on the main page and on the header
$this->name = 'LiteBans'; $this->name = 'LiteBans';
@ -41,11 +43,19 @@ final class Settings {
$driver = 'mysql'; $driver = 'mysql';
/*** End of configuration ***/
$this->active_query = ""; $this->active_query = "";
if (!$this->show_inactive_bans) {
$this->active_query = "WHERE active=1"; if ($driver === "pgsql") {
Settings::$TRUE = "B'1'";
Settings::$FALSE = "B'0'";
} }
if (!$this->show_inactive_bans) {
$this->active_query = "WHERE active=" . Settings::$TRUE;
}
$this->driver = $driver;
if ($connect) { if ($connect) {
$dsn = "$driver:dbname=$database;host=$host;port=$port"; $dsn = "$driver:dbname=$database;host=$host;port=$port";
if ($driver === 'mysql') { if ($driver === 'mysql') {