Repository navigation
Expand file tree
/
Copy pathrouter.php
More file actions
107 lines (82 loc) · 2.6 KB
/
Copy pathrouter.php
File metadata and controls
107 lines (82 loc) · 2.6 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
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
<?php
// base code lifted from pixie by parakeet
$dir = __DIR__."/private";
$router = new AltoRouter();
function route($method, $path, $file, bool|string $auth_reroute = true) {
global $router;
$router->map($method, $path, function(...$params) use ($path, $file, $auth_reroute) {
if(is_string($auth_reroute) && !ARLAUTH) {
redirect($auth_reroute."?redirect=".$_SERVER['REQUEST_URI']);
}
if($auth_reroute && str_starts_with($file, "/private/views/") && !ARLAUTH) {
redirect("/?redirect=".$_SERVER['REQUEST_URI']);
}
foreach ($params as $key => $value) {
$$key = $value;
}
if(str_ends_with($file, ".json")) { set_content_type(ARLTYPEJSON); }
elseif(str_ends_with($file, ".txt")) { set_content_type(ARLTYPEPLAIN); }
if(str_ends_with($file, ".json") || str_ends_with($file, ".js")) {
$file = file_get_contents(__DIR__.$file);
$file = str_replace("{domain}", CONFIG->domain, $file);
exit($file);
} else {
if(!file_exists(__DIR__.$file)) {
header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
require __DIR__.'/private/views/errors/404.php';
exit();
}
require __DIR__.$file;
}
});
}
function route_redirect($method, $path, $file, $auth_reroute = true) {
global $router;
$router->map($method, $path, function() use ($file, $auth_reroute) {
if($auth_reroute && str_starts_with($file, "/private/views/") && !SESSION) {
redirect("/");
}
redirect($file);
});
}
function route_api($method, $path) {
global $router;
$file = "/private/api/$path.php";
$router->map($method, "/api/$path", function(...$params) use ($path, $file) {
/*if(SESSION || (str_starts_with($path, "gameserver") && !str_ends_with($path,"/get"))) {
} else {
exit_http(401);
}*/
foreach ($params as $key => $value) {
$$key = $value;
}
if(!file_exists(__DIR__.$file)) {
header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
require __DIR__.'/private/views/errors/404.php';
exit();
}
require __DIR__.$file;
});
}
function load(string $name) {
if(strlen(trim($name)) == 0)
return;
$path = __DIR__."/router.{$name}.php";
if(!file_exists($path))
return;
require $path;
}
route('GET', '/test', '/private/views/test.php', false);
load("main");
load("api");
load("gameapi");
load("reverse");
$match = $router->match();
if (is_array($match) && is_callable($match['target'])) {
call_user_func_array($match['target'], $match['params']);
} else {
header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
require __DIR__.'/private/views/errors/404.php';
exit();
}
?>