Added error handler
This commit is contained in:
parent
796148a532
commit
23622bc5a8
@ -7,5 +7,7 @@ class Config
|
||||
DB_HOST = 'mariadb',
|
||||
DB_DB = 'lepisi',
|
||||
DB_UNAME = 'root',
|
||||
DB_PWD = 'root';
|
||||
DB_PWD = 'root',
|
||||
|
||||
LOG_ERRORS = false;
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ class Home
|
||||
public function login()
|
||||
{
|
||||
if (Session::exists('userid')) {
|
||||
Session::flash('info', 'You already logged in');
|
||||
Redirect::to('/');
|
||||
} else {
|
||||
View::render('Access/login.html', [
|
||||
@ -39,7 +40,7 @@ class Home
|
||||
'token' => Token::generate()
|
||||
]);
|
||||
} else {
|
||||
Redirect::to('/');
|
||||
throw new \Exception("Bad Request", 400);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -30,7 +30,7 @@ class Posts
|
||||
['valid_at', '<=', $now],
|
||||
['status', '!=', 3]
|
||||
]);
|
||||
if ($valid !== false) {
|
||||
if ($valid) {
|
||||
foreach ($valid as $fields) {
|
||||
$id = $fields['id'];
|
||||
|
||||
@ -42,7 +42,7 @@ class Posts
|
||||
['valid_at', '>', $now],
|
||||
['status', '!=', 3]
|
||||
]);
|
||||
if ($not_valid !== false) {
|
||||
if ($not_valid) {
|
||||
foreach ($not_valid as $fields) {
|
||||
$id = $fields['id'];
|
||||
|
||||
@ -54,7 +54,7 @@ class Posts
|
||||
['expired_at', '<', $now],
|
||||
['status', '!=', 3]
|
||||
]);
|
||||
if ($expired !== false) {
|
||||
if ($expired) {
|
||||
foreach ($expired as $fields) {
|
||||
$id = $fields['id'];
|
||||
|
||||
@ -108,7 +108,7 @@ class Posts
|
||||
'token' => Token::generate()
|
||||
]);
|
||||
} else {
|
||||
Redirect::to('/');
|
||||
throw new \Exception("Page not found", 404);
|
||||
}
|
||||
}
|
||||
|
||||
@ -148,7 +148,7 @@ class Posts
|
||||
);
|
||||
}
|
||||
} else {
|
||||
Redirect::to('/');
|
||||
throw new \Exception("Page not found", 404);
|
||||
}
|
||||
}
|
||||
|
||||
@ -162,7 +162,7 @@ class Posts
|
||||
'token' => Token::generate()
|
||||
]);
|
||||
} else {
|
||||
Redirect::to('/');
|
||||
throw new \Exception("Page not found", 404);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -55,7 +55,7 @@ class Access extends \Core\Model
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
throw new \Exception($e->getMessage(), 444);
|
||||
}
|
||||
}
|
||||
|
||||
@ -97,9 +97,8 @@ class Access extends \Core\Model
|
||||
}
|
||||
}
|
||||
\Core\Session::flash('info', 'Invalid username/password');
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
throw new \Exception($e->getMessage(), 444);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,9 +80,8 @@ class Post extends \Core\Model
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
throw new \Exception($e->getMessage, 444);
|
||||
}
|
||||
}
|
||||
|
||||
@ -102,7 +101,7 @@ class Post extends \Core\Model
|
||||
}
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
throw new \Exception($e->getMessage(), 444);
|
||||
}
|
||||
}
|
||||
|
||||
@ -122,7 +121,7 @@ class Post extends \Core\Model
|
||||
}
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
throw new \Exception($e->getMessage(), 444);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
11
App/Views/Errors/404.html
Normal file
11
App/Views/Errors/404.html
Normal file
@ -0,0 +1,11 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}Sorry :-({% endblock %}
|
||||
|
||||
{% block body %}
|
||||
|
||||
<h2>URL not found - Error 404</h2>
|
||||
Cannot specify the requested URL. Checking typo ...<br>
|
||||
<span style="font-size: small;">Sshh! No typos.</span>
|
||||
|
||||
{% endblock %}
|
10
App/Views/Errors/498.html
Normal file
10
App/Views/Errors/498.html
Normal file
@ -0,0 +1,10 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}Sorry :-({% endblock %}
|
||||
|
||||
{% block body %}
|
||||
|
||||
<h2>Invalid Token</h2>
|
||||
Your token is no longer valid. Please try again.<br>
|
||||
|
||||
{% endblock %}
|
10
App/Views/Errors/500.html
Normal file
10
App/Views/Errors/500.html
Normal file
@ -0,0 +1,10 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}Sorry :-({% endblock %}
|
||||
|
||||
{% block body %}
|
||||
|
||||
<h2>Aw, crap!</h2>
|
||||
We couldn't get what you want. Please come back home.
|
||||
|
||||
{% endblock %}
|
16
App/Views/Errors/errorHandler.html
Normal file
16
App/Views/Errors/errorHandler.html
Normal file
@ -0,0 +1,16 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}Error/Exception{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
|
||||
<h2>{{ title }}</h2>
|
||||
|
||||
<p>{{ class }}</p>
|
||||
<p>{{ message }}</p>
|
||||
<p>{{ trace_title }}
|
||||
<pre>{{ trace_content }}</pre>
|
||||
</p>
|
||||
<p>{{ file }}</p>
|
||||
|
||||
{% endblock %}
|
99
Core/Error.php
Normal file
99
Core/Error.php
Normal file
@ -0,0 +1,99 @@
|
||||
<?php
|
||||
namespace Core;
|
||||
|
||||
use App\Config;
|
||||
|
||||
class Error
|
||||
{
|
||||
public static function errorHandler($level, $message, $file, $line)
|
||||
{
|
||||
if(error_reporting() !== 0)
|
||||
{
|
||||
throw new \ErrorException($message, 0, $level, $file, $line);
|
||||
}
|
||||
}
|
||||
|
||||
public static function exceptionHandler($e)
|
||||
{
|
||||
/* Trigger error */
|
||||
if (Config::LOG_ERRORS) {
|
||||
$code = $e->getCode();
|
||||
if ($code != 404 || $code != 500 || $code != 498) {
|
||||
$code = 500;
|
||||
}
|
||||
http_response_code($code);
|
||||
|
||||
$file = $e->getFile();
|
||||
$line = $e->getLine();
|
||||
|
||||
$logfile = dirname(__DIR__) . '/.logs/' . date('Y-m-d') . '.log';
|
||||
ini_set('error_log', $logfile);
|
||||
|
||||
$date = new \DateTime();
|
||||
$date->setTimeZone(new \DateTimeZone('Pacific/Chatham'));
|
||||
$now = $date->format("d/M/Y:H:i:s O");
|
||||
|
||||
// $message = "[" . date('d-M-Y H:i:s e') . "]";
|
||||
//
|
||||
// $message .= " Caught exception: " . get_class($e);
|
||||
// $message .= " with message: " . $e->getMessage();
|
||||
// $message .= ". Stack trace: " . $e->getTraceAsString();
|
||||
// $message .= ". Thrown in " . $e->getFile() . "(" . $e->getLine() . ").\n";
|
||||
|
||||
$message = '';
|
||||
$message .= self::getIpAddress();
|
||||
$message .= ' ';
|
||||
$message .= $_SERVER['REMOTE_PORT'];
|
||||
$message .= ' ';
|
||||
$message .= $_SERVER['SERVER_PORT'];
|
||||
$message .= ' ';
|
||||
$message .= (Session::exists('userid')) ? Session::get('userid') : 'anonymous';
|
||||
$message .= ' ';
|
||||
$message .= "[" . $now . "]";
|
||||
$message .= ' "';
|
||||
$message .= $_SERVER['REQUEST_METHOD'];
|
||||
$message .= ' ';
|
||||
$message .= $_SERVER['REQUEST_URI'];
|
||||
$message .= ' ';
|
||||
$message .= $_SERVER['SERVER_PROTOCOL'];
|
||||
$message .= '" ';
|
||||
$message .= $e->getCode();
|
||||
$message .= ' ';
|
||||
$message .= $file;
|
||||
$message .= ' ';
|
||||
$message .= $line;
|
||||
$message .= "\n";
|
||||
|
||||
error_log($message, 3, $logfile);
|
||||
View::render("Errors/$code.html", [
|
||||
'code' => $e->getCode()
|
||||
]);
|
||||
} else {
|
||||
$title = "Fatal Error";
|
||||
$class = "Caught exception: " . get_class($e) . "(" . $e->getCode() . ")";
|
||||
$message = "Message: " . $e->getMessage();
|
||||
$trace_title = "Stack trace:";
|
||||
$trace_content = $e->getTraceAsString();
|
||||
$file = "Thrown in " . $e->getFile() . "(" . $e->getLine() . ")";
|
||||
|
||||
View::render("Errors/errorHandler.html", [
|
||||
'title' => $title,
|
||||
'class' => $class,
|
||||
'message' => $message,
|
||||
'trace_title' => $trace_title,
|
||||
'trace_content' => $trace_content,
|
||||
'file' => $file
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function getIpAddress() {
|
||||
foreach (['HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR'] as $key){
|
||||
if (array_key_exists($key, $_SERVER) === true){
|
||||
foreach (explode(',', $_SERVER[$key]) as $ip){
|
||||
return $ip;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -22,39 +22,42 @@ abstract class Model
|
||||
}
|
||||
return self::$conn;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
throw new \Exception($e->getMessage, 444);
|
||||
}
|
||||
}
|
||||
|
||||
public function createTable($table, $fields = [])
|
||||
{
|
||||
$sql = "CREATE TABLE IF NOT EXISTS {$table} (".implode(',', $fields).") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
|
||||
try {
|
||||
$sql = "CREATE TABLE IF NOT EXISTS {$table} (".implode(',', $fields).") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
|
||||
|
||||
$db = static::connectDB();
|
||||
$query = $db->prepare($sql);
|
||||
$db = static::connectDB();
|
||||
$query = $db->prepare($sql);
|
||||
|
||||
if ($query->execute()) {
|
||||
$query->execute();
|
||||
return true;
|
||||
} catch (PDOException $e) {
|
||||
throw new \Exception($e->getMessage(), 444);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function dropTable($table)
|
||||
{
|
||||
if (is_array($table)) {
|
||||
if (count($table)) {
|
||||
$table = implode(', ', $table);
|
||||
try {
|
||||
if (is_array($table)) {
|
||||
if (count($table)) {
|
||||
$table = implode(', ', $table);
|
||||
}
|
||||
}
|
||||
}
|
||||
$sql = "DROP TABLE IF EXISTS {$table}";
|
||||
$sql = "DROP TABLE IF EXISTS {$table}";
|
||||
|
||||
$db = static::connectDB();
|
||||
$query = $db->prepare($sql);
|
||||
|
||||
if ($query->execute()) {
|
||||
$db = static::connectDB();
|
||||
$query = $db->prepare($sql);
|
||||
$query->execute();
|
||||
return true;
|
||||
} catch (PDOException $e) {
|
||||
throw new \Exception($e->getMessage(), 444);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function entry($table, $args, $values = '')
|
||||
@ -115,12 +118,11 @@ abstract class Model
|
||||
}
|
||||
}
|
||||
|
||||
if ($query->execute()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
$query->execute();
|
||||
return true;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
throw new \Exception($e->getMessage(), 444);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -158,12 +160,10 @@ abstract class Model
|
||||
}
|
||||
$query->bindValue($x, $id);
|
||||
|
||||
if ($query->execute()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
$query->execute();
|
||||
return true;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
throw new \Exception($e->getMessage(), 444);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -179,12 +179,10 @@ abstract class Model
|
||||
$query->bindValue(1, 3);
|
||||
$query->bindValue(2, $id);
|
||||
|
||||
if ($query->execute()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
$query->execute();
|
||||
return true;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
throw new \Exception($e->getMessage(), 444);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,6 @@ class Redirect
|
||||
header("Location:$url");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
throw new \Exception("Bad request", 400);
|
||||
}
|
||||
}
|
||||
|
@ -24,10 +24,7 @@ class Router
|
||||
$route = preg_replace('/\{([a-z]+):([^\}]+)\}/', '(?P<\1>\2)', $route);
|
||||
$route = '/^'.$route.'$/';
|
||||
|
||||
if ($this->routes[$route] = $params) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
$this->routes[$route] = $params;
|
||||
}
|
||||
|
||||
public function match($url)
|
||||
@ -47,7 +44,6 @@ class Router
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -75,9 +71,7 @@ class Router
|
||||
$action = $var['_method'];
|
||||
} else {
|
||||
// Token invalid
|
||||
$flash = Session::flash('info', 'Token invalid, try again');
|
||||
$error = Session::flash('info');
|
||||
die($error);
|
||||
throw new \Exception("Token invalid", 498);
|
||||
}
|
||||
unset($var['_token']);
|
||||
unset($var['_method']);
|
||||
@ -92,8 +86,9 @@ class Router
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new \Exception("Method not found", 400);
|
||||
}
|
||||
return false;
|
||||
throw new \Exception("Page not found", 404);
|
||||
}
|
||||
|
||||
private function removeQueryStringVariable($url)
|
||||
|
@ -1,8 +1,16 @@
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
ini_set("display_errors", "on");
|
||||
error_reporting(E_ALL);
|
||||
|
||||
// Autoload
|
||||
require_once dirname(__DIR__).'/vendor/autoload.php';
|
||||
|
||||
// Errors Handler
|
||||
set_error_handler("Core\Error::errorHandler");
|
||||
set_exception_handler("Core\Error::exceptionHandler");
|
||||
|
||||
if (Core\Session::exists('info')) {
|
||||
echo Core\Session::flash('info');
|
||||
echo "<br>";
|
||||
|
Loading…
Reference in New Issue
Block a user