diff options
author | Thedro Neely <thedroneely@gmail.com> | 2018-12-16 11:56:57 -0500 |
---|---|---|
committer | Thedro Neely <thedroneely@gmail.com> | 2018-12-16 11:56:57 -0500 |
commit | 33b986037b149c0d9a7bb7cc3673f3248d72a3bc (patch) | |
tree | a73ddd60e4af6814e7729e5f99cc127256844465 | |
parent | ffc25e850ed495be7466eb3a9302fdab186c4333 (diff) | |
download | edwinmattiacci.com-33b986037b149c0d9a7bb7cc3673f3248d72a3bc.tar.gz edwinmattiacci.com-33b986037b149c0d9a7bb7cc3673f3248d72a3bc.tar.bz2 edwinmattiacci.com-33b986037b149c0d9a7bb7cc3673f3248d72a3bc.zip |
public/error: Add error pages and experimental tester
-rw-r--r-- | public/error/403.html | 18 | ||||
-rw-r--r-- | public/error/404.html | 18 | ||||
-rw-r--r-- | public/error/error.html | 225 | ||||
-rw-r--r-- | public/error/error.json | 1 |
4 files changed, 262 insertions, 0 deletions
diff --git a/public/error/403.html b/public/error/403.html new file mode 100644 index 0000000..f469f0b --- /dev/null +++ b/public/error/403.html @@ -0,0 +1,18 @@ +<?php require $_SERVER['DOCUMENT_ROOT'] . '/..' . '/views/partials/header.error.php'; ?> + + <main> + <div class="intro"> + + <h1 class="error__header">403</h1> + + <br> + <h1>¯\_(ツ)_/¯</h1> + <br> + + <h2 class="error__lineHeight">Error 403 - Forbidden.</h2> + <h2>You can head back to the <a class="link__underline" href="/">homepage</a>.</h2> + + </div> + </main> + +<?php require $_SERVER['DOCUMENT_ROOT'] . '/..' . '/views/partials/footer.php'; ?> diff --git a/public/error/404.html b/public/error/404.html new file mode 100644 index 0000000..575149e --- /dev/null +++ b/public/error/404.html @@ -0,0 +1,18 @@ +<?php require $_SERVER['DOCUMENT_ROOT'] . '/..' . '/views/partials/header.error.php'; ?> + + <main> + <div class="intro"> + + <h1 class="error__header">404</h1> + + <br> + <h1>¯\_(ツ)_/¯</h1> + <br> + + <h2 class="error__lineHeight">Error 404 - Page not found.</h2> + <h2>You can head back to the <a class="link__underline" href="/">homepage</a>.</h2> + + </div> + </main> + +<?php require $_SERVER['DOCUMENT_ROOT'] . '/..' . '/views/partials/footer.php'; ?> diff --git a/public/error/error.html b/public/error/error.html new file mode 100644 index 0000000..14467d7 --- /dev/null +++ b/public/error/error.html @@ -0,0 +1,225 @@ +<?php + +/** + * Web Server HTTP status code response test + * + * For PHP applications that run on a web server that intercepts all + * fast-cgi errors with the appropriate Http error template response. This program + * will run through given HTTP error response codes. + * + * @author Thedro Neely <thedroneely@gmail.com> + * + * @copyright Copyright 2018, Thedro Neely. + * @license https://github.com/tdro + * @link https://github.com/tdro + */ + +declare(strict_types=1); + +$path = $_SERVER['DOCUMENT_ROOT']; +$errorJsonFile = $path . '/error/error.json'; +$errorFile = $path . '/error/error.html'; +$debug = true; + +$errorList = [ + 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, + 410, 411, 412, 413, 414, 415, 416, 417, 418, 421, + 422, 423, 424, 426, 428, 429, 431, 451, 500, 501, + 502, 503, 504, 505, 511, 520, 522, 524 +]; + +$parameters = [ + 'HttpResponseCode' => (int) $errorList[0], + 'Run' => (int) 1, +]; + +/** + * Functions + */ + +function read($file) { + return json_decode(file_get_contents($file), true); +} + +function write($file, $json) { + file_put_contents($file, json_encode($json), 0); +} + +function debug ($message, $debug) { + if ($debug) { + echo '<pre>' . $message . '</pre>'; + } +} + +/** + * Inline CSS styling + */ + +echo ' + <style> + + pre { + background-color: black; + color: white; + max-width: 20em; + padding: 0.5em; + z-index: 10; + } + + form { z-index: 10; } + + </style> +'; + +/** + * Forms + */ + +if (isset($_POST["stop"])) { + $parameters['Run'] = 0; +} + +if (isset($_POST["start"])) { + $parameters['Run'] = 1; +} + +if ($parameters['Run'] === 1) { + echo ' + <form action="/error/error.html" method="post" id="stop"> + <input type="hidden" name="stop" value=stop> + </form> + + <button type="submit" form="stop">Stop</button> + '; +} + +if ($parameters['Run'] === 0) { + echo ' + <form action="/error/error.html" method="post" id="start"> + <input type="hidden" name="start" value=start> + </form> + + <form action="/error/error.html" method="post" id="previous"> + <input type="hidden" name="previous" value=previous> + </form> + + <form action="/error/error.html" method="post" id="next"> + <input type="hidden" name="next" value=next> + </form> + + <button type="submit" form="start">Start</button> + <button type="submit" form="previous"><</button> + <button type="submit" form="next">></button> + '; +} + +/** + * Json File + */ + +if (!file_exists($errorJsonFile)) { + + debug('Socket file does not exist. Creating...', $debug); + write($errorJsonFile, $parameters); + + if (!file_exists($errorJsonFile)) { + debug('Socket file creation failed!', $debug); + return; + } +} + +/** + * Error Code and Array Keys + */ + +$getError = read($errorJsonFile)['HttpResponseCode']; +debug('Current Error Code: ' . $getError . '<br>', $debug); + +$currentErrorKey = array_search($getError, $errorList); +$nextErrorKey = $currentErrorKey + 1; + +$getErrorTotal = count($errorList) - 1; +debug('Error List Count: ' . ($currentErrorKey + 1) . '/' . ($getErrorTotal + 1), $debug); + +/** + * Error Run + */ + +if ($nextErrorKey <= $getErrorTotal) { + + if ($parameters['Run'] === 1) { + echo '<meta http-equiv="refresh" content="1; url=/error/error.html">'; + } + + foreach ($parameters as $parameter) { + $parameters['HttpResponseCode'] = $errorList[$nextErrorKey]; + } + + write($errorJsonFile, $parameters); + var_dump(read($errorJsonFile)); + var_dump($_POST); + + /** + * Send HTTP response code + */ + + http_response_code((int) $getError); + return; + +} + +write($errorJsonFile, $parameters); +debug('Error run completed.', $debug); +var_dump($parameters); + +/** + * Http Response Template Files + */ + +$http_response_status = [ + 400 => "Bad Request", + 401 => "Unauthorized", + 402 => "Payment Required", + 403 => "Forbidden", + 404 => "Not Found", + 405 => "Method Not Allowed", + 406 => "Not Acceptable", + 407 => "Proxy Authentication Required", + 408 => "Request Timeout", + 409 => "Conflict", + 410 => "Gone", + 411 => "Length Required", + 412 => "Precondition Failed", + 413 => "Payload Too Large", + 414 => "Requested URI Too Long", + 415 => "Unsupported Media Type", + 416 => "Requested Range Not Satisfiable", + 417 => "Expectation Failed", + 418 => "I'm a teapot", + 421 => "Misdirected Request", + 422 => "Unprocessable Entity", + 423 => "Locked", + 424 => "Failed Dependency", + 426 => "Upgrade Required", + 428 => "Precondition Required", + 429 => "Too Many Requests", + 431 => "Request Header Fields Too Large", + 444 => "Connection Closed Without Response", + 451 => "Unavailable For Legal Reasons", + 500 => "Internal Server Error", + 501 => "Not Implemented", + 502 => "Bad Gateway", + 503 => "Service Unavailable", + 504 => "Gateway Timeout", + 505 => "HTTP Version Not Supported", + 506 => "Variant Also Negotiates", + 507 => "Insufficient Storage", + 508 => "Loop Detected", + 510 => "Not Extended", + 511 => "Network Authentication Required", +]; + +// var_dump($http_response_status); +// echo key($http_response_status); + +?> diff --git a/public/error/error.json b/public/error/error.json new file mode 100644 index 0000000..a4e3df8 --- /dev/null +++ b/public/error/error.json @@ -0,0 +1 @@ +{"HttpResponseCode":400,"Run":1} |