Added error handler
This commit is contained in:
parent
796148a532
commit
23622bc5a8
@ -7,5 +7,7 @@ class Config
|
|||||||
DB_HOST = 'mariadb',
|
DB_HOST = 'mariadb',
|
||||||
DB_DB = 'lepisi',
|
DB_DB = 'lepisi',
|
||||||
DB_UNAME = 'root',
|
DB_UNAME = 'root',
|
||||||
DB_PWD = 'root';
|
DB_PWD = 'root',
|
||||||
|
|
||||||
|
LOG_ERRORS = false;
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,7 @@ class Home
|
|||||||
public function login()
|
public function login()
|
||||||
{
|
{
|
||||||
if (Session::exists('userid')) {
|
if (Session::exists('userid')) {
|
||||||
|
Session::flash('info', 'You already logged in');
|
||||||
Redirect::to('/');
|
Redirect::to('/');
|
||||||
} else {
|
} else {
|
||||||
View::render('Access/login.html', [
|
View::render('Access/login.html', [
|
||||||
@ -39,7 +40,7 @@ class Home
|
|||||||
'token' => Token::generate()
|
'token' => Token::generate()
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
Redirect::to('/');
|
throw new \Exception("Bad Request", 400);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ class Posts
|
|||||||
['valid_at', '<=', $now],
|
['valid_at', '<=', $now],
|
||||||
['status', '!=', 3]
|
['status', '!=', 3]
|
||||||
]);
|
]);
|
||||||
if ($valid !== false) {
|
if ($valid) {
|
||||||
foreach ($valid as $fields) {
|
foreach ($valid as $fields) {
|
||||||
$id = $fields['id'];
|
$id = $fields['id'];
|
||||||
|
|
||||||
@ -42,7 +42,7 @@ class Posts
|
|||||||
['valid_at', '>', $now],
|
['valid_at', '>', $now],
|
||||||
['status', '!=', 3]
|
['status', '!=', 3]
|
||||||
]);
|
]);
|
||||||
if ($not_valid !== false) {
|
if ($not_valid) {
|
||||||
foreach ($not_valid as $fields) {
|
foreach ($not_valid as $fields) {
|
||||||
$id = $fields['id'];
|
$id = $fields['id'];
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ class Posts
|
|||||||
['expired_at', '<', $now],
|
['expired_at', '<', $now],
|
||||||
['status', '!=', 3]
|
['status', '!=', 3]
|
||||||
]);
|
]);
|
||||||
if ($expired !== false) {
|
if ($expired) {
|
||||||
foreach ($expired as $fields) {
|
foreach ($expired as $fields) {
|
||||||
$id = $fields['id'];
|
$id = $fields['id'];
|
||||||
|
|
||||||
@ -108,7 +108,7 @@ class Posts
|
|||||||
'token' => Token::generate()
|
'token' => Token::generate()
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
Redirect::to('/');
|
throw new \Exception("Page not found", 404);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,7 +148,7 @@ class Posts
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Redirect::to('/');
|
throw new \Exception("Page not found", 404);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,7 +162,7 @@ class Posts
|
|||||||
'token' => Token::generate()
|
'token' => Token::generate()
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
Redirect::to('/');
|
throw new \Exception("Page not found", 404);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@ class Access extends \Core\Model
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
} catch (PDOException $e) {
|
} 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');
|
\Core\Session::flash('info', 'Invalid username/password');
|
||||||
return false;
|
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
echo $e->getMessage();
|
throw new \Exception($e->getMessage(), 444);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,9 +80,8 @@ class Post extends \Core\Model
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
echo $e->getMessage();
|
throw new \Exception($e->getMessage, 444);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,7 +101,7 @@ class Post extends \Core\Model
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
echo $e->getMessage();
|
throw new \Exception($e->getMessage(), 444);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,7 +121,7 @@ class Post extends \Core\Model
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (PDOException $e) {
|
} 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,25 +22,28 @@ abstract class Model
|
|||||||
}
|
}
|
||||||
return self::$conn;
|
return self::$conn;
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
echo $e->getMessage();
|
throw new \Exception($e->getMessage, 444);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createTable($table, $fields = [])
|
public function createTable($table, $fields = [])
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$sql = "CREATE TABLE IF NOT EXISTS {$table} (".implode(',', $fields).") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
|
$sql = "CREATE TABLE IF NOT EXISTS {$table} (".implode(',', $fields).") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
|
||||||
|
|
||||||
$db = static::connectDB();
|
$db = static::connectDB();
|
||||||
$query = $db->prepare($sql);
|
$query = $db->prepare($sql);
|
||||||
|
|
||||||
if ($query->execute()) {
|
$query->execute();
|
||||||
return true;
|
return true;
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
throw new \Exception($e->getMessage(), 444);
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dropTable($table)
|
public function dropTable($table)
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
if (is_array($table)) {
|
if (is_array($table)) {
|
||||||
if (count($table)) {
|
if (count($table)) {
|
||||||
$table = implode(', ', $table);
|
$table = implode(', ', $table);
|
||||||
@ -50,11 +53,11 @@ abstract class Model
|
|||||||
|
|
||||||
$db = static::connectDB();
|
$db = static::connectDB();
|
||||||
$query = $db->prepare($sql);
|
$query = $db->prepare($sql);
|
||||||
|
$query->execute();
|
||||||
if ($query->execute()) {
|
|
||||||
return true;
|
return true;
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
throw new \Exception($e->getMessage(), 444);
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function entry($table, $args, $values = '')
|
public function entry($table, $args, $values = '')
|
||||||
@ -115,12 +118,11 @@ abstract class Model
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($query->execute()) {
|
$query->execute();
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
return false;
|
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
echo $e->getMessage();
|
throw new \Exception($e->getMessage(), 444);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -158,12 +160,10 @@ abstract class Model
|
|||||||
}
|
}
|
||||||
$query->bindValue($x, $id);
|
$query->bindValue($x, $id);
|
||||||
|
|
||||||
if ($query->execute()) {
|
$query->execute();
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
return false;
|
|
||||||
} catch (PDOException $e) {
|
} 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(1, 3);
|
||||||
$query->bindValue(2, $id);
|
$query->bindValue(2, $id);
|
||||||
|
|
||||||
if ($query->execute()) {
|
$query->execute();
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
return false;
|
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
echo $e->getMessage();
|
throw new \Exception($e->getMessage(), 444);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,6 @@ class Redirect
|
|||||||
header("Location:$url");
|
header("Location:$url");
|
||||||
return true;
|
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 = preg_replace('/\{([a-z]+):([^\}]+)\}/', '(?P<\1>\2)', $route);
|
||||||
$route = '/^'.$route.'$/';
|
$route = '/^'.$route.'$/';
|
||||||
|
|
||||||
if ($this->routes[$route] = $params) {
|
$this->routes[$route] = $params;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function match($url)
|
public function match($url)
|
||||||
@ -47,7 +44,6 @@ class Router
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,9 +71,7 @@ class Router
|
|||||||
$action = $var['_method'];
|
$action = $var['_method'];
|
||||||
} else {
|
} else {
|
||||||
// Token invalid
|
// Token invalid
|
||||||
$flash = Session::flash('info', 'Token invalid, try again');
|
throw new \Exception("Token invalid", 498);
|
||||||
$error = Session::flash('info');
|
|
||||||
die($error);
|
|
||||||
}
|
}
|
||||||
unset($var['_token']);
|
unset($var['_token']);
|
||||||
unset($var['_method']);
|
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)
|
private function removeQueryStringVariable($url)
|
||||||
|
@ -1,8 +1,16 @@
|
|||||||
<?php
|
<?php
|
||||||
session_start();
|
session_start();
|
||||||
|
|
||||||
|
ini_set("display_errors", "on");
|
||||||
|
error_reporting(E_ALL);
|
||||||
|
|
||||||
// Autoload
|
// Autoload
|
||||||
require_once dirname(__DIR__).'/vendor/autoload.php';
|
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')) {
|
if (Core\Session::exists('info')) {
|
||||||
echo Core\Session::flash('info');
|
echo Core\Session::flash('info');
|
||||||
echo "<br>";
|
echo "<br>";
|
||||||
|
Loading…
Reference in New Issue
Block a user