-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathxerver.php
More file actions
106 lines (100 loc) · 3.08 KB
/
Copy pathxerver.php
File metadata and controls
106 lines (100 loc) · 3.08 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
<?php
// already loaded ?
if ( defined("XERVER") ) {
exit;
}
// just for internal usage
define("XERVER", true);
// handle exceptions
set_exception_handler(function($e){
die($e);
});
// autoserve from the $root
function serve($root, $options = []) {
// prepare options
$options = array_merge([
"index" => ["index.php", "index.html"],
"directory_listing" => true,
"e404" => function(){
exit("404 not found");
}
], $options);
// set the current working directory
$_SERVER["DOCUMENT_ROOT"] = $root;
// get the requested file extension
$info = pathinfo($_SERVER["REQUEST_PATH"]);
if ( ! isset($info["extension"]) ) {
$info = ["extension" => ""];
}
$ext = $info["extension"];
// prepare the request path
$required = rtrim(str_replace(["/", "\\"], DIRECTORY_SEPARATOR, $root), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . trim($_SERVER["REQUEST_PATH"], "/");
// 1)- a directory ?
if ( is_dir($required) ) {
if ( $_SERVER["REQUEST_PATH"][strlen($_SERVER["REQUEST_PATH"]) - 1] != "/" && $_SERVER["REQUEST_PATH"] != "/"&& $_SERVER["REQUEST_PATH"] != "" ) {
header("Location: " . $_SERVER["REQUEST_PATH"] . "/");
exit;
}
chdir($required);
$_SERVER["REQUEST_URI"] = rtrim($_SERVER["REQUEST_PATH"], "/") . "/" . ($_SERVER["QUERY_STRING"] ? ("?" . $_SERVER["QUERY_STRING"]) : "");
$_SERVER["REQUEST_PATH"] = $_SERVER["PATH_INFO"] .= "/";
foreach ( $options["index"] as $i ) {
if ( is_file($filename = $required . "/" . $i) ) {
$_SERVER["SCRIPT_FILENAME"] = $filename;
$_SERVER["SCRIPT_NAME"] = $_SERVER["PHP_SELF"] = rtrim($_SERVER["REQUEST_PATH"], "/") . "/" . $i;
ksort($_SERVER);
require($filename);
exit;
}
}
if ( $options["directory_listing"] ) {
header("Xerver-Internal-FileServer: " . $required);
exit;
}
exit("You don't have permissions to view this directory");
}
// 2)- is file ?
else if ( is_file($required) ) {
chdir(dirname($required));
$_SERVER["SCRIPT_FILENAME"] = $required;
$_SERVER["SCRIPT_NAME"] = $_SERVER["PHP_SELF"] = $_SERVER["REQUEST_PATH"];
ksort($_SERVER);
if ( $ext != "php" ) {
header("Xerver-Internal-FileServer: " . $required);
exit;
}
require($required);
exit;
}
// 3)- not found ?
else {
$options["e404"]();
}
}
// path rewriter
function rewrite($old, $new) {
$_SERVER["REQUEST_PATH"] = "/" . ltrim(preg_replace("~{$old}~", $new, $_SERVER["REQUEST_PATH"]), "/");
$_SERVER["PATH_INFO"] = $_SERVER["REQUEST_PATH"];
}
// path router
function on(string $pattern, Closure $fn) {
$path = preg_replace("~/+~", "/", "/" . $_SERVER["PATH_INFO"] . "/");
$pattern = preg_replace("~/+~", "/", "/" . $pattern . "/");
if ( preg_match("~^{$pattern}$~", $path, $m) ) {
array_shift($m);
call_user_func_array($fn, $m);
}
}
// vhost router
function vhost($pattern, Closure $fn) {
$host = explode(":", $_SERVER["HTTP_HOST"]);
if ( ! isset($host[0]) ) {
$host = "localhost";
} else {
$host = $host[0];
}
if ( preg_match("~^{$pattern}$~i", $host, $m) ) {
array_shift($m);
call_user_func_array($fn, $m);
}
}