-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
69 lines (60 loc) · 2.27 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
<?php
namespace AnkiDeckUpdateChecker;
use AnkiDeckUpdateChecker\Controllers\Controller;
use AnkiDeckUpdateChecker\Controllers\Router;
use AnkiDeckUpdateChecker\Models\ErrorProcessor;
use AnkiDeckUpdateChecker\Models\UserException;
use Throwable;
//Renew session and set encoding
session_start();
mb_internal_encoding('UTF-8');
//Check for secure connection and redirect if necessary (DOESN'T WORK ON LOCALHOST)
/*if (!(isset($_SERVER["HTTP_X_FORWARDED_PROTO"]) && $_SERVER["HTTP_X_FORWARDED_PROTO"] === "https")) {
array('uri' => $_SERVER['REQUEST_URI'], 'ip' => $_SERVER['REMOTE_ADDR']));
header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
header('Connection: close');
exit();
}*/
//Define and set the autoloader
function autoloader(string $name): void
{
//Replace backslashes (used in namespace path) with forward slashes (used in directory path)
$name = str_replace('\\', '/', $name);
//Remove root directory (in which this file is located)
if (strpos($name, '/') !== false) {
$folders = explode('/', $name);
unset($folders[0]);
$name = implode('/', $folders);
}
$name .= '.php';
require $name;
}
spl_autoload_register('AnkiDeckUpdateChecker\\autoloader');
//Define and set uncaught exceptions handler
function fatalExceptionHandler(Throwable $e) : void
{
if ($e instanceof UserException) {
$errorCode = $e->getCode();
$errorMsg = $e->getMessage();
$errProc = new ErrorProcessor();
$errorFound = $errProc->processError($errorCode, $errorMsg);
} else {
$errorFound = false;
}
if (!$errorFound) {
//Unknown error – just display it hard TURN THIS OFF ON PRODUCTION IF ERROR OUTPUT IS ON
throw $e;
}
$headerCode = $errProc->httpHeaderCode;
$headerMessage = $errProc->httpHeaderMessage;
$errorView = $errProc->errorWebpageView;
extract($errProc->errorWebpageData);
header("HTTP/1.0 $headerCode $headerMessage");
require Controller::VIEWS_DIRECTORY.'/'.$errorView.'.phtml';
}
set_exception_handler('AnkiDeckUpdateChecker\\fatalExceptionHandler');
//Load the URL and process the request (get data for the views)
$rooter = new Router();
$rooter->process(array($_SERVER['REQUEST_URI']));
//Load the views
$rooter->generate();