Added error handler
This commit is contained in:
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)
|
||||
|
||||
Reference in New Issue
Block a user