diff --git a/public/errors.css b/public/errors.css new file mode 100644 index 0000000..07687fa --- /dev/null +++ b/public/errors.css @@ -0,0 +1,89 @@ +* { + margin: 0; + padding: 0; + box-sizing: border-box; + position: relative; + outline-style: none; +} + +html, body { + width: 100%; + height: 100%; +} + +body { + background-color: #7b96c4; + background-color: var(--error-colour, #7b96c4); +} + +.http-error { + padding: 10px 0; +} + +.http-error-container { + max-width: 700px; + width: 100%; + height: 100%; + font-family: IPAMonaPGothic, 'IPA モナー Pゴシック', Monapo, Mona, 'MS PGothic', 'MS Pゴシック', 'Hiragino Kaku Gothic Pro', 'Yu Gothic', monospace; + margin: 0 auto; + background-color: #fff; + border: 4px solid #060300; + border-top-width: 0; +} + +a, a:visited { color: #66f; text-decoration: none; } +a:hover, a:focus { text-decoration: underline; } + +.http-error-top { + display: flex; + justify-content: space-between; + background-color: #000; + color: #fff; +} +.http-error-home a { + padding: 2px; + display: inline-block; + color: #7b96c4; + color: var(--error-colour, #7b96c4); + font-weight: 700; +} + +.http-error-nav a { + padding: 2px; + display: inline-block; +} + +.http-error-body { + margin: 10px; + color: #7b96c4; + color: var(--error-colour, #7b96c4); +} + +.http-error-header { + font-size: 2em; + line-height: 1.2em; +} + +.http-error-image { + text-align: center; +} +.http-error-image img { + max-width: 512px; + width: 100%; + vertical-align: middle; +} + +.http-error-blerb { + text-align: center; + font-size: 1.2em; + line-height: 1.5em; +} + +.http-error-footer { + font-size: .7em; + line-height: 1.3em; + text-align: center; + color: #999; + padding: 4px 0; + border-top: 2px solid #000; +} diff --git a/public/images/k401.jpg b/public/images/k401.jpg new file mode 100644 index 0000000..7a246b3 Binary files /dev/null and b/public/images/k401.jpg differ diff --git a/public/images/k403.jpg b/public/images/k403.jpg new file mode 100644 index 0000000..3d76c66 Binary files /dev/null and b/public/images/k403.jpg differ diff --git a/public/images/k404.jpg b/public/images/k404.jpg new file mode 100644 index 0000000..52c6bd6 Binary files /dev/null and b/public/images/k404.jpg differ diff --git a/public/images/k500.jpg b/public/images/k500.jpg new file mode 100644 index 0000000..0d738a0 Binary files /dev/null and b/public/images/k500.jpg differ diff --git a/public/images/k503.jpg b/public/images/k503.jpg new file mode 100644 index 0000000..1e2a864 Binary files /dev/null and b/public/images/k503.jpg differ diff --git a/public/index.php b/public/index.php index ad0e293..8ff716a 100644 --- a/public/index.php +++ b/public/index.php @@ -14,10 +14,16 @@ set_exception_handler(function(\Throwable $ex) { } header('Content-Type: text/html; charset=utf-8'); - echo file_get_contents(MKI_DIR_TEMPLATES . '/500.html'); + echo file_get_contents(MKI_DIR_TEMPLATES . '/errors/500.html'); exit; }); +if(file_exists(MKI_ROOT . '/.migrating')) { + http_response_code(503); + echo file_get_contents(MKI_DIR_TEMPLATES . '/errors/503.html'); + exit; +} + $makai->startCSRFP( $cfg['csrfp'] ?? 'meow', (string)filter_input(INPUT_SERVER, 'REMOTE_ADDR') diff --git a/src/RoutingContext.php b/src/RoutingContext.php index f88842c..4c37e6d 100644 --- a/src/RoutingContext.php +++ b/src/RoutingContext.php @@ -21,19 +21,18 @@ class RoutingContext { return $this->router; } - private function defaultErrorHandler($response, $request, $code, $text): void { - $name = sprintf('errors/%s', $code); - if(!is_file(sprintf('%s/%s.twig', MKI_DIR_TEMPLATES, $name))) - $name = 'errors/master'; - - $response->setContent($this->templating->render($name, [ - 'http_error_code' => $code, - 'http_error_title' => $text, - ])); - } - public function registerDefaultErrorPages(): void { - $this->router->setDefaultErrorHandler($this->defaultErrorHandler(...)); + $this->router->get('/error-401.html', fn() => $this->templating->render('errors/401')); + $this->router->get('/error-403.html', fn() => $this->templating->render('errors/403')); + $this->router->get('/error-404.html', fn() => $this->templating->render('errors/504')); + $this->router->get('/error-500.html', fn() => $this->templating->render('errors/500')); + $this->router->get('/error-503.html', fn() => $this->templating->render('errors/503')); + + $this->router->addErrorHandler(401, fn($resp) => $resp->setContent($this->templating->render('errors/401'))); + $this->router->addErrorHandler(403, fn($resp) => $resp->setContent($this->templating->render('errors/403'))); + $this->router->addErrorHandler(404, fn($resp) => $resp->setContent($this->templating->render('errors/404'))); + $this->router->addErrorHandler(500, fn($resp) => $resp->setContent(file_get_contents(MKI_DIR_TEMPLATES . '/errors/500.html'))); + $this->router->addErrorHandler(503, fn($resp) => $resp->setContent(file_get_contents(MKI_DIR_TEMPLATES . '/errors/503.html'))); } public function register(IRouteHandler $handler): void { diff --git a/templates/500.html b/templates/500.html deleted file mode 100644 index 2f36815..0000000 --- a/templates/500.html +++ /dev/null @@ -1,16 +0,0 @@ - - - - - Error 500 - - - -

Error 500

-

Something went very wrong. Please let me know about this.

- - diff --git a/templates/errors/401.twig b/templates/errors/401.twig new file mode 100644 index 0000000..e0637f7 --- /dev/null +++ b/templates/errors/401.twig @@ -0,0 +1,7 @@ +{% extends 'errors/master.twig' %} + +{% set error_code = 401 %} +{% set error_text = 'Unauthorized' %} +{% set error_image = '/images/k401.jpg' %} +{% set error_blerb = 'You must be logged in to view this page.' %} +{% set error_colour = '#87448d' %} diff --git a/templates/errors/403.twig b/templates/errors/403.twig index a5d071c..4907de7 100644 --- a/templates/errors/403.twig +++ b/templates/errors/403.twig @@ -1,4 +1,7 @@ {% extends 'errors/master.twig' %} -{% set http_error_image = '/images/403.jpg' %} -{% set http_error_desc = 'You are not supposed to be here.' %} +{% set error_code = 403 %} +{% set error_text = 'Forbidden' %} +{% set error_image = '/images/k403.jpg' %} +{% set error_blerb = 'You are not supposed to be here.' %} +{% set error_colour = '#387982' %} diff --git a/templates/errors/404.twig b/templates/errors/404.twig index a108053..29aaee5 100644 --- a/templates/errors/404.twig +++ b/templates/errors/404.twig @@ -1,4 +1,7 @@ {% extends 'errors/master.twig' %} -{% set http_error_image = '/images/404.jpg' %} -{% set http_error_desc = 'Whatever you\'re looking for is no longer here, or might not have been here in the first place.' %} +{% set error_code = 404 %} +{% set error_text = 'Not Found' %} +{% set error_image = '/images/k404.jpg' %} +{% set error_blerb = 'What you\'re looking for is no longer here or might not have been here at all.' %} +{% set error_colour = '#7854c1' %} diff --git a/templates/errors/405.twig b/templates/errors/405.twig deleted file mode 100644 index 9ce9522..0000000 --- a/templates/errors/405.twig +++ /dev/null @@ -1,4 +0,0 @@ -{% extends 'errors/master.twig' %} - -{% set http_error_image = '/images/405.jpg' %} -{% set http_error_desc = 'You\'re up to something, aren\'t you?' %} diff --git a/templates/errors/500.html b/templates/errors/500.html new file mode 100644 index 0000000..66c7d79 --- /dev/null +++ b/templates/errors/500.html @@ -0,0 +1,35 @@ + + + + + HTTP 500 Internal Server Error // flash.moe + + + + + +
+
+ +
+

HTTP 500 Internal Server Error

+
+ 500 +
+

Something happened that caused the server to be unable to complete the request.

+
+ +
+
+ + diff --git a/templates/errors/500.twig b/templates/errors/500.twig new file mode 100644 index 0000000..c9b56b0 --- /dev/null +++ b/templates/errors/500.twig @@ -0,0 +1,7 @@ +{% extends 'errors/master.twig' %} + +{% set error_code = 500 %} +{% set error_text = 'Internal Server Error' %} +{% set error_image = '/images/k500.jpg' %} +{% set error_blerb = 'Something happened that caused the server to be unable to complete the request.' %} +{% set error_colour = '#3a5794' %} diff --git a/templates/errors/503.html b/templates/errors/503.html new file mode 100644 index 0000000..1b614f9 --- /dev/null +++ b/templates/errors/503.html @@ -0,0 +1,35 @@ + + + + + HTTP 503 Service Temporarily Unavailable // flash.moe + + + + + +
+
+ +
+

HTTP 503 Service Temporarily Unavailable

+
+ 503 +
+

The website is currently unavailable, it will likely be back soon!

+
+ +
+
+ + diff --git a/templates/errors/503.twig b/templates/errors/503.twig new file mode 100644 index 0000000..6789c19 --- /dev/null +++ b/templates/errors/503.twig @@ -0,0 +1,7 @@ +{% extends 'errors/master.twig' %} + +{% set error_code = 503 %} +{% set error_text = 'Service Temporarily Unavailable' %} +{% set error_image = '/images/k503.jpg' %} +{% set error_blerb = 'The website is currently unavailable, it will likely be back soon!' %} +{% set error_colour = '#4c4cad' %} diff --git a/templates/errors/master.twig b/templates/errors/master.twig index 8551693..1758dbd 100644 --- a/templates/errors/master.twig +++ b/templates/errors/master.twig @@ -1,11 +1,38 @@ -{% extends 'master-2021.twig' %} +{% extends 'master.twig' %} -{% block container %} +{% set error_string = 'HTTP %03d %s'|format(error_code, error_text) %} +{% set master_title = error_string ~ ' // flash.moe' %} + +{% block master_head %} + + +{% endblock %} + +{% set styles = ['/errors.css'] %} +{% set master_body_attrs = {'style': '--error-colour: ' ~ error_colour} %} + +{% block master_body %}
-

{{ http_error_title|default('Unknown Error #' ~ http_error_code) }}

- {% if http_error_image is defined %} - {{ http_error_image }} - {% endif %} -
{{ http_error_desc|default('No additional information is available.') }}
+
+ +
+

{{ error_string }}

+
+ {{ '%03d'|format(error_code) }} +
+

{{ error_blerb }}

+
+ +
{% endblock %}