From 7817c3308868ae08962a59d1a9e39ddedbdb5f65 Mon Sep 17 00:00:00 2001 From: ruan Date: Fri, 9 Oct 2015 09:34:26 +0200 Subject: [PATCH] Use same selection across all queries --- history.php | 3 ++- includes/page.php | 27 ++++++++++++++++----------- includes/settings.php | 1 + info.php | 3 ++- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/history.php b/history.php index 7103408..611b240 100644 --- a/history.php +++ b/history.php @@ -26,8 +26,9 @@ class History { if ($count_st->execute() && ($row = $count_st->fetch()) !== null) { $counts[$type] = $row['count']; } + $sel = $page->get_selection($table); - $st = $page->conn->prepare("SELECT * FROM $table WHERE $field=:uuid ORDER BY time"); + $st = $page->conn->prepare("SELECT $sel FROM $table WHERE $field=:uuid ORDER BY time"); $st->bindParam(":uuid", $uuid, PDO::PARAM_STR); diff --git a/includes/page.php b/includes/page.php index 6aba6d1..624e369 100644 --- a/includes/page.php +++ b/includes/page.php @@ -88,6 +88,21 @@ class Page { } } + function get_selection($table) { + if (version_compare(PHP_VERSION, '5.4.0') >= 0) { + $selection = "*"; + } else { + // PDO+MySQL under PHP 5.3.x has a bug with BIT columns. + // An empty string is returned no matter what the value is. + // Workaround: cast to unsigned. + $selection = "id,uuid,reason,banned_by_name,banned_by_uuid,time,until,CAST(active AS UNSIGNED) AS active"; + if ($table === $this->settings->table['warnings']) { + $selection .= ",CAST(warned AS UNSIGNED) AS warned"; + } + } + return $selection; + } + function run_query() { try { $table = $this->table; @@ -100,17 +115,7 @@ class Page { $offset = ($limit * $page); } - if (version_compare(PHP_VERSION, '5.4.0') >= 0) { - $sel = "*"; - } else { - // PDO+MySQL under PHP 5.3.x has a bug with BIT columns. - // An empty string is returned no matter what the value is. - // Workaround: cast to unsigned. - $sel = "id,uuid,reason,banned_by_name,banned_by_uuid,time,until,CAST(active AS UNSIGNED) AS active"; - if ($table === $this->settings->table['warnings']) { - $sel .= ",CAST(warned AS UNSIGNED) AS warned"; - } - } + $sel = $this->get_selection($table); $query = "SELECT $sel FROM $table $active_query GROUP BY $table.id ORDER BY time DESC LIMIT :limit OFFSET :offset"; $st = $this->conn->prepare($query); diff --git a/includes/settings.php b/includes/settings.php index 0d8ef67..ccfff1e 100644 --- a/includes/settings.php +++ b/includes/settings.php @@ -99,6 +99,7 @@ final class Settings { try { $this->conn = new PDO($dsn, $username, $password); $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $this->conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); } catch (PDOException $e) { die('Connection failed: ' . $e->getMessage()); } diff --git a/info.php b/info.php index 54dcc44..88e6932 100644 --- a/info.php +++ b/info.php @@ -128,7 +128,8 @@ $id = (int)$id; $type = $page->type; $table = $page->table; -$query = "SELECT * FROM $table WHERE id=? LIMIT 1"; +$sel = $page->get_selection($table); +$query = "SELECT $sel FROM $table WHERE id=? LIMIT 1"; $st = $page->conn->prepare($query);