-
Notifications
You must be signed in to change notification settings - Fork 0
/
FsTree.php
executable file
·226 lines (195 loc) · 5.49 KB
/
FsTree.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
// a sample class to show file system based aciTree
// note: a simple file system helper class is at the end of this file
class FsTree extends Tree {
private $fs = null;
public function __construct(Fs $fs) {
$this->fs = $fs;
}
/*
* $parentId will be the path to the folder.
*/
public function branch($parentId = null) {
$branch = array();
$list = $this->fs->folders($parentId);
foreach ($list as $entry) {
$branch["$parentId/$entry"] = $entry;
}
$list = $this->fs->files($parentId);
foreach ($list as $entry) {
$branch["$parentId/$entry"] = $entry;
}
return $branch;
}
/**
* Return TRUE if it's a TREE folder (have children).
* @param string $path
* @param string $icon - item icon
*/
private function hasChildren($path, &$icon) {
if (is_dir($path)) {
$icon = 'folder';
// here we can return NULL instead of checking for children
// return null;
return !$this->fs->isEmpty($path);
} else {
$icon = 'file';
return false;
}
}
/*
* $itemId will be the path to the file/folder.
*/
public function itemProps($itemId) {
$itemId = trim($itemId, '/\\');
if ($this->fs->allow($itemId, $real)) {
return array_merge(parent::itemProps($itemId), array(
'inode' => $this->hasChildren($real, $icon),
'icon' => $icon,
'size' => (is_file($real) ? formatSizeUnits(filesize($real)) : ''),
'type' => recongnizeType($real),
'random' => mt_rand(0, 99) // just a random property
));
}
return parent::itemProps($itemId);
}
}
// a file system helper for getting file system folders/files
// and for limiting the listings to the base folder
class Fs {
// keep the base folder
private $base = null;
public function __construct($base) {
$this->base($base);
}
/**
* Set/Get the base folder.
* @param string $base
* @return string
*/
public function base($base = null) {
if ($base === null) {
return $this->base;
} else {
$this->base = realpath($base);
}
}
/**
* Check if $path is under $this->base.
* @param string $path - relative path
* @param string $real - absolute path
* @return bool
*/
public function allow($path, &$real = null) {
$real = realpath("$this->base/$path");
return strpos($real, $this->base) === 0;
}
/**
* Get a list of folders.
* @param string $path - relative path
* @return array
*/
public function folders($path) {
$list = array();
if ($this->allow($path, $path) && is_dir($path)) {
$handle = opendir($path);
if ($handle) {
while ($entry = readdir($handle)) {
if (($entry != '.') && ($entry != '..') && is_dir("$path/$entry")) {
$list[] = $entry;
}
}
closedir($handle);
}
}
asort($list);
return $list;
}
/**
* Check if a folder is empty.
* @param string $path - absolute path
* @return boolean
*/
public function isEmpty($path) {
$handle = opendir($path);
if ($handle) {
while ($entry = readdir($handle)) {
if (($entry != '.') && ($entry != '..')) {
closedir($handle);
return false;
}
}
closedir($handle);
}
return true;
}
/**
* Get a list of files.
* @param string $path - relative path
* @return array
*/
public function files($path) {
$list = array();
if ($this->allow($path, $path) && is_dir($path)) {
$handle = opendir($path);
if ($handle) {
while ($entry = readdir($handle)) {
if (($entry != '.') && ($entry != '..') && is_file("$path/$entry")) {
$list[] = $entry;
}
}
closedir($handle);
}
}
asort($list);
return $list;
}
}
function formatSizeUnits($bytes)
{
if ($bytes >= 1073741824)
{
$bytes = number_format($bytes / 1073741824, 2) . ' GB';
}
elseif ($bytes >= 1048576)
{
$bytes = number_format($bytes / 1048576, 2) . ' MB';
}
elseif ($bytes >= 1024)
{
$bytes = number_format($bytes / 1024, 2) . ' KB';
}
elseif ($bytes > 1)
{
$bytes = $bytes . ' bytes';
}
elseif ($bytes == 1)
{
$bytes = $bytes . ' byte';
}
else
{
$bytes = '0 bytes';
}
return $bytes;
}
function recongnizeType($real)
{
$ext = strtoupper( end(explode(".", $real)) );
switch($ext)
{
case "GIF":
case "PNG":
case "JPG":
case "JPEG":
case "TIF":
return "Image";
case "TXT":
case "PDF":
case "RTF":
return "Text";
}
if( ! is_file($real))
return "Folder";
return "Unknown File Type";
}