- Remove namespaces for PHP 5.2 support

- Rename "includes" to "inc"

- Add database error handling and error pages
This commit is contained in:
ruan 2016-01-09 21:24:18 +02:00
parent 57777295bb
commit 5660317403
28 changed files with 114 additions and 65 deletions

View File

@ -1,9 +1,6 @@
<?php <?php
namespace litebans; require_once './inc/page.php';
use PDO;
require_once './includes/page.php';
$page = new Page("bans"); $page = new Page("bans");
$page->print_title(); $page->print_title();

View File

@ -1,9 +1,5 @@
<?php <?php
namespace litebans; require_once './inc/page.php';
use PDOException;
require_once './includes/page.php';
class Check { class Check {
public function run($name, $from) { public function run($name, $from) {
@ -64,7 +60,7 @@ class Check {
} }
*/ */
} catch (PDOException $ex) { } catch (PDOException $ex) {
die($ex->getMessage()); Settings::handle_database_error($page->settings, $ex);
} }
} }

25
error/access-denied.php Normal file
View File

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<title>litebans-php - Database Error</title>
<link href="../inc/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="jumbotron">
<h2>Database Error</h2><br>
<div class="text-warning">
The web interface was unable to connect to the database using the configuration provided.
<br>
Database error: Access denied
</div>
<br>
<a href="../" class="btn btn-default">Try Again</a>
</div>
</div>
</body>
</html>

View File

@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<title>litebans-php - Database Error</title>
<link href="../inc/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="jumbotron">
<h2>Database Error</h2><br>
<div class="text-warning">
The web interface has connected to the database, but could not find any tables generated by LiteBans.
<br>
This means that the plugin has not successfully connected to this database before with the same configuration.
<br><br>
Solutions:
<br>
- Check the startup logs to see if there are any errors.
<br>
- Ensure that the plugin is using the MySQL driver (not SQLite) to connect to the database.
<br>
- Ensure that the table prefix is the same in config.yml and settings.php.
<br>
</div>
<br>
<a href="../" class="btn btn-default">Try Again</a>
</div>
</div>
</body>
</html>

View File

@ -1,11 +1,5 @@
<?php <?php
namespace litebans; require_once './inc/page.php';
use PDO;
use PDOException;
use PDORow;
require_once './includes/page.php';
require_once './info.php'; require_once './info.php';
class History { class History {
@ -125,7 +119,7 @@ try {
$total += $count; $total += $count;
} }
usort($all, array("litebans\\History", "cmp_row_date")); usort($all, array("History", "cmp_row_date"));
if (!empty($all)) { if (!empty($all)) {
$page->table_begin(); $page->table_begin();
@ -211,5 +205,5 @@ try {
$page->print_footer(); $page->print_footer();
} catch (PDOException $ex) { } catch (PDOException $ex) {
die($ex->getMessage()); Settings::handle_database_error($page->settings, $ex);
} }

View File

Before

Width:  |  Height:  |  Size: 61 KiB

After

Width:  |  Height:  |  Size: 61 KiB

3
inc/footer.php Normal file
View File

@ -0,0 +1,3 @@
<script src="inc/js/jquery.min.js"></script>
<script src="inc/js/bootstrap.min.js"></script>
<?php echo "</html>"; ?>

View File

@ -1,8 +1,4 @@
<?php <?php
namespace litebans;
use PDO;
use PDOException;
class Header { class Header {
/** /**
@ -30,7 +26,7 @@ function __construct($page) {
'kicks.php' => $row['c_kicks'], 'kicks.php' => $row['c_kicks'],
); );
} catch (PDOException $ex) { } catch (PDOException $ex) {
die($ex->getMessage()); Settings::handle_database_error($page->settings, $ex);
} }
} }
} }
@ -62,10 +58,10 @@ $settings = $this->page->settings;
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content=""> <meta name="description" content="">
<meta name="author" content="LiteBans"> <meta name="author" content="LiteBans">
<link rel="shortcut icon" href="includes/img/minecraft.ico"> <link rel="shortcut icon" href="inc/img/minecraft.ico">
<!-- CSS --> <!-- CSS -->
<link href="includes/css/bootstrap.min.css" rel="stylesheet"> <link href="inc/css/bootstrap.min.css" rel="stylesheet">
<link href="includes/css/custom.css" rel="stylesheet"> <link href="inc/css/custom.css" rel="stylesheet">
<script type="text/javascript"> <script type="text/javascript">
function withjQuery(f) { function withjQuery(f) {
if (window.jQuery) f(); if (window.jQuery) f();
@ -110,5 +106,4 @@ $settings = $this->page->settings;
<?php <?php
} }
} }
?> ?>

View File

Before

Width:  |  Height:  |  Size: 288 B

After

Width:  |  Height:  |  Size: 288 B

View File

Before

Width:  |  Height:  |  Size: 168 KiB

After

Width:  |  Height:  |  Size: 168 KiB

View File

@ -1,8 +1,4 @@
<?php <?php
namespace litebans;
use PDO;
use PDOException;
class Page { class Page {
public function __construct($name, $header = true) { public function __construct($name, $header = true) {
@ -12,9 +8,9 @@ class Page {
$this->time = microtime(true); $this->time = microtime(true);
if ($header) { if ($header) {
require_once './includes/header.php'; require_once './inc/header.php';
} }
require_once './includes/settings.php'; require_once './inc/settings.php';
$settings = new Settings(); $settings = new Settings();
$this->conn = $settings->conn; $this->conn = $settings->conn;
$this->settings = $settings; $this->settings = $settings;
@ -129,7 +125,7 @@ class Page {
return $st; return $st;
} catch (PDOException $ex) { } catch (PDOException $ex) {
die($ex->getMessage()); Settings::handle_database_error($this->settings, $ex);
} }
} }
@ -381,7 +377,7 @@ class Page {
$time = microtime(true) - $this->time; $time = microtime(true) - $this->time;
echo "<!-- Page generated in $time seconds. -->"; echo "<!-- Page generated in $time seconds. -->";
include_once './includes/footer.php'; include_once './inc/footer.php';
} }
function table_begin() { function table_begin() {

View File

@ -1,12 +1,11 @@
<?php <?php
namespace litebans;
use PDO;
use PDOException;
final class Settings { final class Settings {
public static $TRUE = "1", $FALSE = "0"; public static $TRUE = "1", $FALSE = "0";
/**
* @param bool|true $connect
*/
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';
@ -44,7 +43,7 @@ final class Settings {
"CONSOLE", "Console", "CONSOLE", "Console",
); );
$this->console_name = "Console"; $this->console_name = "Console";
$this->console_image = "includes/img/console.png"; $this->console_image = "inc/img/console.png";
// Avatar images for all players will be fetched from this URL. // Avatar images for all players will be fetched from this URL.
// Examples: // Examples:
@ -69,6 +68,8 @@ final class Settings {
// Enable PHP error reporting. // Enable PHP error reporting.
$error_reporting = true; $error_reporting = true;
// Enable error pages.
$this->error_pages = true;
/*** End of configuration ***/ /*** End of configuration ***/
@ -108,11 +109,38 @@ final class Settings {
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $this->conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $e) { } catch (PDOException $e) {
die('Connection failed: ' . $e->getMessage()); Settings::handle_database_error($this, $e);
} }
if ($driver === 'pgsql') { if ($driver === 'pgsql') {
$this->conn->query("SET NAMES 'UTF8';"); $this->conn->query("SET NAMES 'UTF8';");
} }
} }
} }
/**
* @param $settings Settings
* @param $e Exception
*/
static function handle_database_error($settings, $e) {
$message = $e->getMessage();
if ($settings->error_pages) {
if (strstr($message, "Access denied for user")) {
$settings->redirect("error/access-denied.php");
}
if (strstr($message, "Base table or view not found:")) {
$settings->redirect("error/tables-not-found.php");
}
}
die('Database error: ' . $message);
}
function redirect($url) {
echo "<a href=\"$url\">Redirecting...</a>";
echo "<script type=\"text/javascript\">document.location=\"$url\";</script>";
die;
}
} }

View File

@ -1,3 +0,0 @@
<script src="includes/js/jquery.min.js"></script>
<script src="includes/js/bootstrap.min.js"></script>
<?php echo "</html>"; ?>

View File

@ -1,10 +1,8 @@
<?php <?php
namespace litebans; require_once './inc/page.php';
require_once './includes/page.php';
$page = new Page("index"); $page = new Page("index");
$page->print_title(); $page->print_title();
?> ?>
<div class="container"> <div class="container">
<div class="jumbotron"> <div class="jumbotron">

View File

@ -1,9 +1,5 @@
<?php <?php
namespace litebans; require_once './inc/page.php';
use PDO;
require_once './includes/page.php';
abstract class Info { abstract class Info {
/** /**

View File

@ -1,9 +1,6 @@
<?php <?php
namespace litebans; require_once './inc/page.php';
use PDO;
require_once './includes/page.php';
$page = new Page("kicks"); $page = new Page("kicks");
$page->print_title(); $page->print_title();

View File

@ -1,9 +1,6 @@
<?php <?php
namespace litebans; require_once './inc/page.php';
use PDO;
require_once './includes/page.php';
$page = new Page("mutes"); $page = new Page("mutes");
$page->print_title(); $page->print_title();

View File

@ -1,9 +1,6 @@
<?php <?php
namespace litebans; require_once './inc/page.php';
use PDO;
require_once './includes/page.php';
$page = new Page("warnings"); $page = new Page("warnings");
$page->print_title(); $page->print_title();