-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathphp_file_tree.php
136 lines (115 loc) · 4.91 KB
/
php_file_tree.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
/*
== PHP FILE TREE ==
Let's call it...oh, say...version 1?
== AUTHOR ==
Cory S.N. LaViska
http://abeautifulsite.net/
== DOCUMENTATION ==
For documentation and updates, visit http://abeautifulsite.net/notebook.php?article=21
2019/7/20 - made it so empty directories don't show. - Andrew Calcutt
2019/7/24 - made "Home" link show at the top. - Andrew Calcutt
2019/7/28 - Added file array output. - Andrew Calcutt
*/
function php_file_tree($directory, $return_link, $extensions = array(), $excludedfiles = array(), $includedfiles = array(), $file_list = array()) {
// Generates a valid XHTML list of all directories, sub-directories, and files in $directory
// Remove trailing slash
if( substr($directory, -1) == "/" ) $directory = substr($directory, 0, strlen($directory) - 1);
$arr_ret = php_file_tree_dir($directory, $return_link, $extensions, $excludedfiles, $includedfiles, true, $file_list);
$code .= $arr_ret["data"];
$file_list = $arr_ret["file_list"];
return array("data"=>$code,"file_list"=>$file_list);
}
function php_file_tree_dir($directory, $return_link, $extensions = array(), $excludedfiles = array(), $includedfiles = array(), $first_call = true, $file_list = array()) {
// Recursive function called by php_file_tree() to list directories/files
// Get and sort directories/files
if( function_exists("scandir") ) $file = scandir($directory); else $file = php4_scandir($directory);
natcasesort($file);
// Make directories first
$files = $dirs = array();
foreach($file as $this_file) {
if( is_dir("$directory/$this_file" ) ) $dirs[] = $this_file; else $files[] = $this_file;
}
$file = array_merge($dirs, $files);
// PRE-FILTERING: Include explicitly included files, regardless of extension
if (!empty($includedfiles)) {
foreach ($includedfiles as $included_file) {
$full_included_path = $directory . "/" . $included_file;
if (file_exists($full_included_path) && !is_dir($full_included_path)) {
// Check if the included file is already in the $file array
if (!in_array($included_file, $file)) {
$file[] = $included_file; // Add the included file if it's not already there
}
}
}
}
// Filter unwanted extensions
$filtered = array(); // Initialize the $filtered array
if( !empty($extensions) ) {
foreach( array_keys($file) as $key ) {
if( !is_dir("$directory/$file[$key]") ) {
$ext = strtolower(substr($file[$key], strrpos($file[$key], ".") + 1));
if( !in_array($ext, $extensions) ) {
$filtered[] = $key; // Mark the key for filtering
}
}
}
}
$has_content = false;
if( count($file) > 2 ) {
$id = base64_encode("ul/$directory");
$php_file_tree = "<ul id=\"".$id."\"";
if( $first_call ) { $php_file_tree .= " class=\"php-file-tree\""; }
$php_file_tree .= ">";
if( $first_call ) { $php_file_tree .= "<li class=\"pft-home\"><a href=\".\">Home</a></li>"; $first_call = false; }
foreach( $file as $this_file ) {
if( $this_file != "." && $this_file != ".." ) {
if( is_dir("$directory/$this_file") ) {
$arr_ret = php_file_tree_dir("$directory/$this_file", $return_link ,$extensions, $excludedfiles, $includedfiles, false, $file_list);
$subdir = $arr_ret["data"];
$file_list = $arr_ret["file_list"];
if($subdir)
{
$has_content = true;
// Directory
$link = str_replace("[link]", "$directory/$this_file", $return_link);
$id = base64_encode("li/$directory/".$this_file);
// Removed the <a> tag here
$php_file_tree .= "<li id=\"".$id."\" class=\"pft-directory\">" . htmlspecialchars($this_file);
$php_file_tree .= $subdir;
$php_file_tree .= "</li>";
}
} else {
// Inclusion logic - AFTER Extension Filtering
$included = in_array($this_file, $includedfiles);
$is_filtered = in_array(array_search($this_file, $file), $filtered); // Check if the file is marked as filtered
if((!in_array ($this_file , $excludedfiles) && !$is_filtered) || $included)
{
$has_content = true;
// File
// Get extension (prepend 'ext-' to prevent invalid classes from extensions that begin with numbers)
$ext = "ext-" . substr($this_file, strrpos($this_file, ".") + 1);
$link = str_replace("[link]", "$directory/" . urlencode($this_file), $return_link);
$id = base64_encode("li/$directory/".$this_file);
$php_file_tree .= "<li id=\"".$id."\" class=\"pft-file " . strtolower($ext) . "\"><a href=\"$link\">" . htmlspecialchars($this_file) . "</a></li>";
$file_list[] = "$directory/" . urlencode($this_file);
}
}
}
}
$php_file_tree .= "</ul>";
}
if($has_content)
return array("data"=>$php_file_tree,"file_list"=>$file_list);
else
return array("data"=>'',"file_list"=>$file_list);
}
// For PHP4 compatibility
function php4_scandir($dir) {
$dh = opendir($dir);
while( false !== ($filename = readdir($dh)) ) {
$files[] = $filename;
}
sort($files);
return($files);
}