forked from tbaschak/cisco-config-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browse.php
143 lines (130 loc) · 3.64 KB
/
browse.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
137
138
139
140
141
142
143
<?php
/* Copyright (C) 2005-2014 Adi Linden <adi@adis.ca>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* browse.php Browse the /tftpboot directory structure.
*/
/* Include configuration */
require_once('./config.php');
/* Get path */
if (isset($_GET['path'])) {
$dir = sanitize($_GET['path']);
} else {
$dir = TFTP_FILE_ROOT;
}
/* Download or show files */
if (isset($_GET['download'])) {
/* Download files */
$file = sanitize($_GET['download']);
get_file($file);
} else {
/* Show files */
send_header($dir);
show_files($dir);
send_footer();
}
function sanitize($path)
{
/* Don't get back up the directory structure */
if (preg_match('/\.\./', $path) > 0) {
echo "Illegal path specified";
exit;
}
/* Make sure we are within DIRROOT */
if (preg_match('!^'.TFTP_FILE_ROOT.'!', $path) == 0) {
echo "Illegal access";
exit;
}
return $path;
}
function get_file($file)
{
$fp = @fopen($file, 'r');
if (! $fp) {
echo "Permission denied";
return false;
}
/* Download all files except .txt */
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // some day in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
if (preg_match('/\.txt$/', $file) > 0) {
header("Content-type: text/plain");
} else {
header("Content-type: application/x-download");
header("Content-Disposition: attachment; filename=" . basename($file));
header("Content-Transfer-Encoding: binary");
}
while (! feof($fp)) {
print(fread($fp,1024));
flush();
}
fclose($fp);
}
function do_ls($dir) {
$dh = @opendir($dir);
if ($dh) {
while(false !== ($files[] = readdir($dh))) {
}
return $files;
}
return false;
}
function show_files($dir)
{
if (! $files = do_ls($dir)) {
echo "Failed to access location\n";
return false;
}
asort($files);
foreach ($files as $file) {
/* Ignore dots and empti files */
if ($file == '' || $file == '.' || $file == '..') {
continue;
}
/* Prepend dir */
$path = $dir . '/' . $file;
/* Show file */
if (is_file($path)) {
echo '<img src="./icons/generic.png" alt="file"> ';
echo '<a href="'.$_SERVER['PHP_SELF'].'?download='.$path.'">'.$file."</a><br>\n";
}
/* Show dir */
if (is_dir($path)) {
echo '<img src="./icons/dir.png" alt="dir"> ';
echo '<a href="'.$_SERVER['PHP_SELF'].'?path='.$path.'">'.$file."</a><br>\n";
}
}
}
function send_header($dir)
{
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Browse: <?php echo $dir; ?></title>
</head>
<body>
<h2>Browse: <?php echo $dir; ?></h2>
<p>
<?php
}
function send_footer()
{
?>
</body>
</html>
<?php
}
?>