diff --git a/App/Config.php b/App/Config.php index ad051d7..1b2c322 100644 --- a/App/Config.php +++ b/App/Config.php @@ -7,5 +7,7 @@ class Config DB_HOST = 'mariadb', DB_DB = 'lepisi', DB_UNAME = 'root', - DB_PWD = 'root'; + DB_PWD = 'root', + + LOG_ERRORS = false; } diff --git a/App/Controllers/Home.php b/App/Controllers/Home.php index 87ce0fe..f763266 100644 --- a/App/Controllers/Home.php +++ b/App/Controllers/Home.php @@ -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); } } diff --git a/App/Controllers/Posts.php b/App/Controllers/Posts.php index 57e8993..a5d017f 100644 --- a/App/Controllers/Posts.php +++ b/App/Controllers/Posts.php @@ -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); } } diff --git a/App/Models/Access.php b/App/Models/Access.php index 573bcad..b402a2c 100644 --- a/App/Models/Access.php +++ b/App/Models/Access.php @@ -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); } } diff --git a/App/Models/Post.php b/App/Models/Post.php index cca6c52..e6e5422 100644 --- a/App/Models/Post.php +++ b/App/Models/Post.php @@ -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); } } } diff --git a/App/Views/Errors/404.html b/App/Views/Errors/404.html new file mode 100644 index 0000000..6548b14 --- /dev/null +++ b/App/Views/Errors/404.html @@ -0,0 +1,11 @@ +{% extends "base.html" %} + +{% block title %}Sorry :-({% endblock %} + +{% block body %} + +
{{ class }}
+{{ message }}
+{{ trace_title }} +
{{ trace_content }}+ +
{{ file }}
+ +{% endblock %} diff --git a/Core/Error.php b/Core/Error.php new file mode 100644 index 0000000..bf1d799 --- /dev/null +++ b/Core/Error.php @@ -0,0 +1,99 @@ +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; + } + } + } + } +} diff --git a/Core/Model.php b/Core/Model.php index 61a2377..144762c 100644 --- a/Core/Model.php +++ b/Core/Model.php @@ -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); } } } diff --git a/Core/Redirect.php b/Core/Redirect.php index 0f8fa63..a4f3b1d 100644 --- a/Core/Redirect.php +++ b/Core/Redirect.php @@ -12,6 +12,6 @@ class Redirect header("Location:$url"); return true; } - return false; + throw new \Exception("Bad request", 400); } } diff --git a/Core/Router.php b/Core/Router.php index c139dc4..f1802ae 100644 --- a/Core/Router.php +++ b/Core/Router.php @@ -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) diff --git a/public/index.php b/public/index.php index 7e550af..5909807 100644 --- a/public/index.php +++ b/public/index.php @@ -1,8 +1,16 @@ ";