1 | <?php |
---|
2 | |
---|
3 | // /home/username/foo/public_html/ |
---|
4 | $dir = dirname(__FILE__)."/../.."; |
---|
5 | $include = '/\.(php|shtml|html|htm|shtm|cgi|txt|doc|pdf|rtf|xls|csv)$/'; |
---|
6 | $exclude = ''; |
---|
7 | $dirinclude = ''; |
---|
8 | $direxclude = '/(^|\/)[._]|htmlarea/'; // Exclude the htmlarea tree by default |
---|
9 | |
---|
10 | $hash = ''; |
---|
11 | foreach(explode(',', 'dir,include,exclude,dirinclude,direxclude') as $k) |
---|
12 | { |
---|
13 | if(isset($_REQUEST[$k])) |
---|
14 | { |
---|
15 | if(get_magic_quotes_gpc()) |
---|
16 | { |
---|
17 | $_REQUEST[$k] = stripslashes($_REQUEST[$k]); |
---|
18 | } |
---|
19 | $hash .= $k . '=' . $_REQUEST[$k]; |
---|
20 | $$k = $_REQUEST[$k]; |
---|
21 | } |
---|
22 | } |
---|
23 | |
---|
24 | if($hash) |
---|
25 | { |
---|
26 | session_start(); |
---|
27 | if(!isset($_SESSION[sha1($hash)])) |
---|
28 | { |
---|
29 | ?> |
---|
30 | [ ]; |
---|
31 | <?php |
---|
32 | exit; |
---|
33 | } |
---|
34 | } |
---|
35 | |
---|
36 | function scan($dir, $durl = '') |
---|
37 | { |
---|
38 | global $include, $exclude, $dirinclude, $direxclude; |
---|
39 | static $seen = array(); |
---|
40 | |
---|
41 | $files = array(); |
---|
42 | |
---|
43 | $dir = realpath($dir); |
---|
44 | if(isset($seen[$dir])) |
---|
45 | { |
---|
46 | return $files; |
---|
47 | } |
---|
48 | $seen[$dir] = TRUE; |
---|
49 | $dh = @opendir($dir); |
---|
50 | |
---|
51 | |
---|
52 | while($dh && ($file = readdir($dh))) |
---|
53 | { |
---|
54 | if($file !== '.' && $file !== '..') |
---|
55 | { |
---|
56 | $path = realpath($dir . '/' . $file); |
---|
57 | $url = $durl . '/' . $file; |
---|
58 | |
---|
59 | if(($dirinclude && !preg_match($dirinclude, $url)) || ($direxclude && preg_match($direxclude, $url))) continue; |
---|
60 | if(is_dir($path)) |
---|
61 | { |
---|
62 | if($subdir = scan($path, $url)) |
---|
63 | { |
---|
64 | $files[] = array('url'=>$url, 'children'=>$subdir); |
---|
65 | } |
---|
66 | } |
---|
67 | elseif(is_file($path)) |
---|
68 | { |
---|
69 | if(($include && !preg_match($include, $url)) || ($exclude && preg_match($exclude, $url))) continue; |
---|
70 | $files[] = array('url'=>$url); |
---|
71 | } |
---|
72 | |
---|
73 | } |
---|
74 | } |
---|
75 | @closedir($dh); |
---|
76 | return dirsort($files); |
---|
77 | } |
---|
78 | |
---|
79 | function dirsort($files) |
---|
80 | { |
---|
81 | usort($files, 'dircomp'); |
---|
82 | return $files; |
---|
83 | } |
---|
84 | |
---|
85 | function dircomp($a, $b) |
---|
86 | { |
---|
87 | if(is_array($a)) $a = $a[0]; |
---|
88 | if(is_array($b)) $b = $b[0]; |
---|
89 | return strcmp(strtolower($a), strtolower($b)); |
---|
90 | } |
---|
91 | |
---|
92 | function to_js($var, $tabs = 0) |
---|
93 | { |
---|
94 | if(is_numeric($var)) |
---|
95 | { |
---|
96 | return $var; |
---|
97 | } |
---|
98 | |
---|
99 | if(is_string($var)) |
---|
100 | { |
---|
101 | return "'" . js_encode($var) . "'"; |
---|
102 | } |
---|
103 | |
---|
104 | if(is_array($var)) |
---|
105 | { |
---|
106 | $useObject = false; |
---|
107 | foreach(array_keys($var) as $k) { |
---|
108 | if(!is_numeric($k)) $useObject = true; |
---|
109 | } |
---|
110 | $js = array(); |
---|
111 | foreach($var as $k => $v) |
---|
112 | { |
---|
113 | $i = ""; |
---|
114 | if($useObject) { |
---|
115 | if(preg_match('#[a-zA-Z]+[a-zA-Z0-9]*#', $k)) { |
---|
116 | $i .= "$k: "; |
---|
117 | } else { |
---|
118 | $i .= "'$k': "; |
---|
119 | } |
---|
120 | } |
---|
121 | $i .= to_js($v, $tabs + 1); |
---|
122 | $js[] = $i; |
---|
123 | } |
---|
124 | if($useObject) { |
---|
125 | $ret = "{\n" . tabify(implode(",\n", $js), $tabs) . "\n}"; |
---|
126 | } else { |
---|
127 | $ret = "[\n" . tabify(implode(",\n", $js), $tabs) . "\n]"; |
---|
128 | } |
---|
129 | return $ret; |
---|
130 | } |
---|
131 | |
---|
132 | return 'null'; |
---|
133 | } |
---|
134 | |
---|
135 | function tabify($text, $tabs) |
---|
136 | { |
---|
137 | if($text) |
---|
138 | { |
---|
139 | return str_repeat(" ", $tabs) . preg_replace('/\n(.)/', "\n" . str_repeat(" ", $tabs) . "\$1", $text); |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | function js_encode($string) |
---|
144 | { |
---|
145 | static $strings = "\\,\",',%,&,<,>,{,},@,\n,\r"; |
---|
146 | |
---|
147 | if(!is_array($strings)) |
---|
148 | { |
---|
149 | $tr = array(); |
---|
150 | foreach(explode(',', $strings) as $chr) |
---|
151 | { |
---|
152 | $tr[$chr] = sprintf('\x%02X', ord($chr)); |
---|
153 | } |
---|
154 | $strings = $tr; |
---|
155 | } |
---|
156 | |
---|
157 | return strtr($string, $strings); |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | echo to_js(scan($dir)); |
---|
162 | ?> |
---|