-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
68 lines (56 loc) · 1.62 KB
/
index.php
File metadata and controls
68 lines (56 loc) · 1.62 KB
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
<?php
spl_autoload_register(function ($class) {
$file = "class/" . str_replace('\\', '/', $class) . '.php';
// if the file exists, require it
if (file_exists($file)) {
require $file;
}
});
$url = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$PROXIED_FUNCTIONS = [];
function p($fn) {
global $PROXIED_FUNCTIONS;
global $url;
$id = count($PROXIED_FUNCTIONS);
$PROXIED_FUNCTIONS[$id] = $fn;
echo "((...params) => fetch('$url', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ fn: $id, params }) }).then(r => r.json()))";
}
class Route {
public function __construct($pattern, $script) {
$this->pattern = $pattern;
$this->script = $script;
}
}
$routes = [];
require 'routes.php';
// route the request
$script = 'view/notfound.php';
foreach($routes as $route) {
if($url == $route->pattern) {
$script = $route->script;
break;
}
}
ob_start();
require $script;
$html = ob_get_contents();
ob_end_clean();
switch($_SERVER['REQUEST_METHOD']) {
case 'GET':
echo $html;
break;
case 'POST':
try {
$input = json_decode(file_get_contents('php://input'), true);
if(!isset(
$input['fn'],
$input['params'],
$PROXIED_FUNCTIONS[$input['fn']]
)) throw new Exception();
$data = json_encode($PROXIED_FUNCTIONS[$input['fn']](...$input['params']));
header('Content-Type: application/json');
echo $data;
} catch(Exception $e) {
http_response_code(500);
}
}