aboutsummaryrefslogtreecommitdiff
path: root/public/error/error.html
blob: 14467d7f94cd593b48af2265c297ca6b6f1c2173 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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);

?>