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,15 +1,17 @@
<?php
if (isset($_POST['name'], $_POST['table'])) {
$name = $_POST['name'];
require_once './includes/page.php';
class Check {
public function run($name) {
// validate user input
if (strlen($name) > 16 || !preg_match("/^[0-9a-zA-Z_]{1,16}$/", $name)) {
echo "Invalid name.";
$this->println("Invalid name.");
return;
}
require './includes/page.php';
$page = new Page();
$page = new Page(false);
$history = $page->settings->table_history;
try {
$stmt = $page->conn->prepare("SELECT name,uuid FROM $history WHERE name=? ORDER BY date LIMIT 1");
if ($stmt->execute(array($name))) {
if ($row = $stmt->fetch()) {
@ -19,30 +21,44 @@ if (isset($_POST['name'], $_POST['table'])) {
}
if (!isset($uuid)) {
$name = htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
echo "$name has not joined before.<br>";
$this->println("$name has not joined before.");
return;
}
$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=" . Settings::$TRUE . ") LIMIT 1");
if ($stmt->execute(array($uuid))) {
if (!($row = $stmt->fetch())) {
echo "$name is not banned.<br>";
$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']);
echo "$name is banned!<br>";
echo "Banned by: $banner<br>";
echo "Reason: $reason<br>";
echo "Banned on: $time<br>";
$this->println("$name is banned!");
$this->println("Banned by: $banner");
$this->println("Reason: $reason");
$this->println("Banned on: $time");
if ($row['until'] > 0) {
echo "Banned until: $until<br>";
$this->println("Banned until: $until");
} else {
echo "Banned permanently.<br>";
$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
require './includes/head.php';
require './includes/header.php';
require_once './includes/settings.php';
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();
$this->conn = $settings->conn;
$this->settings = $settings;
$this->uuid_name_cache = array();
$this->time = microtime(true);
$this->page = 1;
if (isset($_GET['page'])) {
$page = $_GET['page']; // user input
if (filter_var($page, FILTER_VALIDATE_INT)) {

View File

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