-
Notifications
You must be signed in to change notification settings - Fork 5
/
router.php
136 lines (128 loc) · 4.33 KB
/
router.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
// Set timezone
date_default_timezone_set("UTC");
// Directory that contains error pages
define("ERRORS", dirname(__FILE__) . "/errors");
// Default index file
$DIRECTORY_INDEX = array(
'index.php',
'index.htm',
'index.html'
);
// Optional array of authorized client IPs for a bit of security
$config["hostsAllowed"] = array();
function logAccess($status = 200) {
file_put_contents("php://stdout", sprintf("[%s] %s:%s [%s]: %s\n",
date("D M j H:i:s Y"), $_SERVER["REMOTE_ADDR"],
$_SERVER["REMOTE_PORT"], $status, $_SERVER["REQUEST_URI"]));
}
// Parse allowed host list
if (!empty($config['hostsAllowed'])) {
if (!in_array($_SERVER['REMOTE_ADDR'], $config['hostsAllowed'])) {
logAccess(403);
http_response_code(403);
exit;
}
}
// if requesting a directory then serve the default index
$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
$ext = pathinfo($path, PATHINFO_EXTENSION);
if (empty($ext)) {
foreach($DIRECTORY_INDEX as $index){
$temp_path = rtrim($path, "/") . "/" . $index;
// If the file index exists then return false and let the server handle it
if (file_exists($_SERVER["DOCUMENT_ROOT"] . $temp_path)) {
return false;
}
}
}
// If the file exists then return false and let the server handle it
if (file_exists($_SERVER["DOCUMENT_ROOT"] . $path) && !is_dir($_SERVER["DOCUMENT_ROOT"] . $path)) {
return false;
}
elseif(!file_exists($_SERVER["DOCUMENT_ROOT"] . $path)){
http_response_code(404);
return false;
}
$this_dir = substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],"/")+1);
$dir = $_SERVER['DOCUMENT_ROOT'].$this_dir;
if(!is_dir($dir)){
http_response_code(404);
return false;
}
$folder = opendir($dir);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<HEAD>
<TITLE>Index of <?=$this_dir?></TITLE>
</HEAD>
<BODY>
<?php
if(file_exists('HEADER.html')){
include 'HEADER.html';
}
?>
<H1>Index of <?=$this_dir?></H1>
<PRE><IMG SRC="/icons/blank.gif" ALT=" "> Name Last modified Size Description
<HR>
<?php
$file_count=0;
if (readdir($folder)) {
while ($file = readdir($folder)) {
$file_count++;
if ($this_dir=="/") $base=""; else $base=$this_dir;
$i=0;
$ispaces = 24-strlen($file);
$spaces[0] = "";
while ($i < $ispaces) {
$spaces[0] .= " ";
$i++;
}
$i=0;
$ispaces = 6-strlen(filesize("$dir/$file"));
$spaces[1] = "";
while ($i < $ispaces) {
$spaces[1] .= " "; $i++;
}
if ($file==".."){
echo '<IMG SRC="/icons/folder.gif" ALT="[DIR]"> <A HREF="../">Parent Directory</A> '.date("d-M-Y H:i", filemtime("$dir/$file")).' - '."\n";
}
elseif (substr($file,0,1)=="."){
continue;
}
elseif (is_dir("$dir/$file")){
echo '<IMG SRC="/icons/folder.gif" ALT="[DIR]"> <A HREF="'. urlencode($file).'/">'.$file.'</A>'.$spaces[0].''.date("d-M-Y H:i", filemtime("$dir/$file")).' - '."\n";
}
elseif (substr($file,-4,4) == ".htm" || substr($file,-5,4) == ".html" || substr($file,-6,4) == ".shtml" || substr($file,-4,4) == ".asp" || substr($file,-4,4) == ".txt"){
echo '<IMG SRC="/icons/text.gif" ALT="[TXT]"> <A HREF="'.urlencode($file).'">'.$file.'</A>'.$spaces[0].''.date("d-M-Y H:i", filemtime("$dir/$file")).''.$spaces[1].''.filesize("$dir/$file").'k '."\n";
}
elseif (substr($file,-4,4) == ".gif" || substr($file,-4,4) == ".jpg" || substr($file,-4,4) == ".png" || substr($file,-4,4) == ".jpeg"){
echo '<IMG SRC="/icons/image2.gif" ALT="[IMG]"> <A HREF="'.urlencode($file).'">'.$file.'</A>'.$spaces[0].''.date("d-M-Y H:i", filemtime("$dir/$file")).''.$spaces[1].''.filesize("$dir/$file").'k '."\n";
}
else
{
echo '<IMG SRC="/icons/unknown.gif" ALT="[IMG]"> <A HREF="'.urlencode($file).'">'.$file.'</A>'.$spaces[0].''.date("d-M-Y H:i", filemtime("$dir/$file")).''.$spaces[1].''.filesize("$dir/$file").'k '."\n";
}
}
if ($file_count==0){
echo '<IMG SRC="/icons/folder.gif" ALT="[DIR]"> <A HREF="../">Parent Directory</A> '.date("d-M-Y H:i", filemtime("$dir/$file")).' - '."\n";
}
}
else
{
echo "<br>Error 404. Not found.";
}
?>
</PRE><HR>
<?php
if (file_exists("$dir/readme.txt")){
echo "<pre>"; include "$dir/readme.txt"; echo "</pre>";
}
else
{
echo "<ADDRESS>" . $_SERVER['SERVER_SOFTWARE'] . " at " . $_SERVER['SERVER_NAME'] . " Port " . $_SERVER['SERVER_PORT'] . "</ADDRESS>";
}
?>
</BODY></HTML>