-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.php
77 lines (63 loc) · 2.25 KB
/
index.php
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
<?php
namespace VoicesOfWynn;
use VoicesOfWynn\Controllers\Router;
//Set autoloader for dependencies
require __DIR__.'/vendor/autoload.php';
//Define and set autoloader for custom classes
function autoloader(string $name): void
{
//Replace '\' (used in namespaces) with '/' (used to navigate through directories)
$name = str_replace('\\', '/', $name);
//Remove the root folder from the path (this file is already in it)
if (strpos($name, '/') !== false) {
$name = substr($name, strpos($name, '/') + 1);
}
$name .= '.php';
require $name;
}
spl_autoload_register('VoicesOfWynn\autoloader');
// Create "sessions" folder if it does not exist
$sessionsPath = __DIR__.'/sessions';
if (!is_dir($sessionsPath)) {
mkdir($sessionsPath, 0777, true);
}
//Resume session and set character encoding
session_start();
mb_internal_encoding('UTF-8');
/* KEEP THIS COMMENTED ON LOCAL SERVERS - IT'S NOT POSSIBLE TO USE SSL ON THEM
//Check if HTTPS connection was used and if not (HTTP), redirect the client.
if (!(isset($_SERVER["HTTP_X_FORWARDED_PROTO"]) && $_SERVER["HTTP_X_FORWARDED_PROTO"] === "https") && substr($_SERVER['REQUEST_URI'], 0, 5 ) !== "/api/") {
header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
header('Connection: close');
exit();
}
*/
$requestedUrl = $_SERVER['REQUEST_URI'];
//Process the request
$router = new Router();
$result = $router->process(array($requestedUrl));
$website = '';
if ($result >= 400) {
//Display the error webpage, overwrite the page headers (title, description, keywords)
$errorControllerName = "VoicesOfWynn\Controllers\Errors\Error".$result;
$errorController = new $errorControllerName();
$errorController->process(array($router->isWebpageRequest));
if ($router->isWebpageRequest) {
$website = $errorController->getResult();
} else {
$website = $router->getResult();
}
}
else if ($result === 204) {
header('HTTP/1.1 204 No Content');
//Don't render any views, simply don't echo anything into the response body
//This is mostly used for AJAX calls
}
else if ($result < 300) {
if ($result >= 100) {
http_response_code($result);
}
$website = $router->getResult();
}
//Display the generated website
echo $website;