Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
malwaredllc
GitHub Repository: malwaredllc/byob
Path: blob/master/web-gui/buildyourownbotnet/assets/js/aci-tree/php/FsTree.php
1293 views
1
<?php
2
3
// a sample class to show file system based aciTree
4
// note: a simple file system helper class is at the end of this file
5
6
class FsTree extends Tree {
7
8
private $fs = null;
9
10
public function __construct(Fs $fs) {
11
$this->fs = $fs;
12
}
13
14
/*
15
* $parentId will be the path to the folder.
16
*/
17
18
public function branch($parentId = null) {
19
$branch = array();
20
$list = $this->fs->folders($parentId);
21
foreach ($list as $entry) {
22
$branch["$parentId/$entry"] = $entry;
23
}
24
$list = $this->fs->files($parentId);
25
foreach ($list as $entry) {
26
$branch["$parentId/$entry"] = $entry;
27
}
28
return $branch;
29
}
30
31
/**
32
* Return TRUE if it's a TREE folder (have children).
33
* @param string $path
34
* @param string $icon - item icon
35
*/
36
private function hasChildren($path, &$icon) {
37
if (is_dir($path)) {
38
$icon = 'folder';
39
// here we can return NULL instead of checking for children
40
// return null;
41
return !$this->fs->isEmpty($path);
42
} else {
43
$icon = 'file';
44
return false;
45
}
46
}
47
48
/*
49
* $itemId will be the path to the file/folder.
50
*/
51
52
public function itemProps($itemId) {
53
$itemId = trim($itemId, '/\\');
54
if ($this->fs->allow($itemId, $real)) {
55
56
return array_merge(parent::itemProps($itemId), array(
57
'inode' => $this->hasChildren($real, $icon),
58
'icon' => $icon,
59
'size' => (is_file($real) ? formatSizeUnits(filesize($real)) : ''),
60
'type' => recongnizeType($real),
61
'random' => mt_rand(0, 99) // just a random property
62
));
63
}
64
return parent::itemProps($itemId);
65
}
66
67
}
68
69
// a file system helper for getting file system folders/files
70
// and for limiting the listings to the base folder
71
72
class Fs {
73
74
// keep the base folder
75
private $base = null;
76
77
public function __construct($base) {
78
$this->base($base);
79
}
80
81
/**
82
* Set/Get the base folder.
83
* @param string $base
84
* @return string
85
*/
86
public function base($base = null) {
87
if ($base === null) {
88
return $this->base;
89
} else {
90
$this->base = realpath($base);
91
}
92
}
93
94
/**
95
* Check if $path is under $this->base.
96
* @param string $path - relative path
97
* @param string $real - absolute path
98
* @return bool
99
*/
100
public function allow($path, &$real = null) {
101
$real = realpath("$this->base/$path");
102
return strpos($real, $this->base) === 0;
103
}
104
105
/**
106
* Get a list of folders.
107
* @param string $path - relative path
108
* @return array
109
*/
110
public function folders($path) {
111
$list = array();
112
if ($this->allow($path, $path) && is_dir($path)) {
113
$handle = opendir($path);
114
if ($handle) {
115
while ($entry = readdir($handle)) {
116
if (($entry != '.') && ($entry != '..') && is_dir("$path/$entry")) {
117
$list[] = $entry;
118
}
119
}
120
closedir($handle);
121
}
122
}
123
asort($list);
124
return $list;
125
}
126
127
/**
128
* Check if a folder is empty.
129
* @param string $path - absolute path
130
* @return boolean
131
*/
132
public function isEmpty($path) {
133
$handle = opendir($path);
134
if ($handle) {
135
while ($entry = readdir($handle)) {
136
if (($entry != '.') && ($entry != '..')) {
137
closedir($handle);
138
return false;
139
}
140
}
141
closedir($handle);
142
}
143
return true;
144
}
145
146
/**
147
* Get a list of files.
148
* @param string $path - relative path
149
* @return array
150
*/
151
public function files($path) {
152
$list = array();
153
if ($this->allow($path, $path) && is_dir($path)) {
154
$handle = opendir($path);
155
if ($handle) {
156
while ($entry = readdir($handle)) {
157
if (($entry != '.') && ($entry != '..') && is_file("$path/$entry")) {
158
$list[] = $entry;
159
}
160
}
161
closedir($handle);
162
}
163
}
164
asort($list);
165
return $list;
166
}
167
168
}
169
170
171
function formatSizeUnits($bytes)
172
{
173
if ($bytes >= 1073741824)
174
{
175
$bytes = number_format($bytes / 1073741824, 2) . ' GB';
176
}
177
elseif ($bytes >= 1048576)
178
{
179
$bytes = number_format($bytes / 1048576, 2) . ' MB';
180
}
181
elseif ($bytes >= 1024)
182
{
183
$bytes = number_format($bytes / 1024, 2) . ' KB';
184
}
185
elseif ($bytes > 1)
186
{
187
$bytes = $bytes . ' bytes';
188
}
189
elseif ($bytes == 1)
190
{
191
$bytes = $bytes . ' byte';
192
}
193
else
194
{
195
$bytes = '0 bytes';
196
}
197
198
return $bytes;
199
}
200
201
202
function recongnizeType($real)
203
{
204
$ext = strtoupper( end(explode(".", $real)) );
205
206
switch($ext)
207
{
208
case "GIF":
209
case "PNG":
210
case "JPG":
211
case "JPEG":
212
case "TIF":
213
return "Image";
214
215
216
case "TXT":
217
case "PDF":
218
case "RTF":
219
return "Text";
220
}
221
222
if( ! is_file($real))
223
return "Folder";
224
225
return "Unknown File Type";
226
}
227