file.php
8.14 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
/**
* Author: Hoang Ngo
*/
declare( strict_types=1 );
namespace LearnDash\Hub\Framework;
class File {
/**
* Engine use to create a dir tree
*
* @var string
*/
public $engine = '';
/**
* Absolute path to a folder need to create a dir tre
*
* @var string
*/
public $path = '';
/**
* Is the result including file?
*
* @var bool
*/
public $include_file = true;
/**
* Is the result include dir
*
* @var bool
*/
public $include_dir = true;
/**
* @var bool
*/
public $include_hidden = false;
/**
* This is where to define the rules for exclude files out of the result
*
* 'ext'=>array('jpg','gif') file extension you don't want appear in the result
* 'path'=>array('/tmp/file1.txt','/tmp/file2') absolute path to files
* 'dir'=>array('/tmp/','/dir/') absolute path to the directory you dont want to include files
* 'filename'=>array('abc*') file name you don't want to include, can be regex,
*
* @var array
*/
public $exclude = array();
/**
* This is where to define the rules for include files, please note that if $include is provided, the $exclude
* will get ignored
*
* 'ext'=>array('jpg','gif') file extension you don't want appear in the result
* 'path'=>array('/tmp/file1.txt','/tmp/file2') absolute path to files
* 'dir'=>array('/tmp/','/dir/') absolute path to the directory you dont want to include files
* 'filename'=>array('abc*') file name you don't want to include, can be regex,
*
* @var array
*/
public $include = array();
/**
* Does this search recursive
*
* @var bool
*/
public $is_recursive = true;
/**
* if provided, only search file smaller than this
*
* @var int
*/
public $max_filesize = 0;
/**
* @param $path
* @param bool|true $include_file
* @param bool|false $include_dir
* @param array $include
* @param array $exclude
* @param bool|true $is_recursive
* @param int $max_filesize
*/
public function __construct(
$path,
bool $include_file = true,
bool $include_dir = false,
array $include = array(),
array $exclude = array(),
bool $is_recursive = true,
$include_hidden = false,
int $max_filesize = 0
) {
$this->path = $path;
$this->include_file = $include_file;
$this->include_dir = $include_dir;
$this->include = $include;
$this->exclude = $exclude;
$this->is_recursive = $is_recursive;
$this->include_hidden = $include_hidden;
$this->max_filesize = $max_filesize;
}
/**
* @return array
*/
public function get_dir_tree(): array {
$result = array();
if ( ! is_dir( $this->path ) ) {
return $result;
}
return $this->get_dir_tree_by_scandir();
}
/**
* @param null $path
*
* @return array
*/
private function get_dir_tree_by_scandir( string $path = null ): array {
if ( is_null( $path ) ) {
$path = $this->path;
}
$path = rtrim( $path, DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR;
$rfiles = scandir( $path );
$data = array();
foreach ( $rfiles as $rfile ) {
if ( $rfile === '.' || $rfile === '..' ) {
continue;
}
if ( substr( pathinfo( $rfile, PATHINFO_BASENAME ), 0, 1 ) === '.'
&& false === $this->include_hidden ) {
// hidden files, move on.
continue;
}
$real_path = $path . $rfile;
$type = filetype( $real_path );
if ( ( ! empty( $this->include ) || ! empty( $this->exclude ) ) && ( false === $this->filter_directory(
$real_path,
$type
) ) ) {
continue;
}
if ( 'file' === $type && true === $this->include_file ) {
if ( is_numeric( $this->max_filesize ) && 0 < $this->max_filesize ) {
$max_size = $this->max_filesize * ( pow( 1024, 2 ) );
if ( filesize( $real_path ) > $max_size ) {
continue;
} else {
$data[] = $real_path;
}
} else {
$data[] = $real_path;
}
}
if ( 'dir' === $type ) {
if ( $this->include_dir ) {
$data[] = $real_path;
}
if ( $this->is_recursive ) {
$tdata = $this->get_dir_tree_by_scandir( $real_path );
$data = array_merge( $data, $tdata );
}
}
}
return $data;
}
/**
* Filter for recursive directory tree
*
* @param $current
* @param null $filetype
*
* @return bool
*/
public function filter_directory( $current, $filetype = null ): bool {
if ( ! empty( $this->include ) ) {
return $this->_filter_include( $current, $filetype );
} elseif ( ! empty( $this->exclude ) ) {
return $this->_filter_exclude( $current, $filetype );
}
return true;
}
/**
* @param $path
* @param null $filetype
*
* @return bool
*/
private function _filter_include( $path, $filetype = null ): bool {
$include = $this->include;
$exclude = $this->exclude;
$applied = 0;
$dir_include = isset( $include['dir'] ) ? $include['dir'] : array();
$dir_exclude = isset( $exclude['dir'] ) ? $exclude['dir'] : array();
if ( ! is_null( $filetype ) ) {
$type = $filetype;
} else {
$type = filetype( $path );
}
if ( is_array( $dir_include ) && count( $dir_include ) ) {
if ( is_array( $dir_exclude ) ) {
foreach ( $dir_exclude as $dir ) {
if ( strpos( $path, $dir ) === 0 ) {
// this mean, exlucde matched, we wont list this
// move to next loop
continue;
}
}
}
foreach ( $dir_include as $dir ) {
if ( strpos( $path, $dir ) === 0 ) {
return true;
}
}
$applied ++;
}
// next extension
$ext_include = isset( $include['ext'] ) ? $include['ext'] : array();
if ( is_array( $ext_include ) && count( $ext_include ) && $type == 'file' ) {
// we will uses foreach and strcasecmp instead of regex cause it faster
foreach ( $ext_include as $ext ) {
if ( strcasecmp( pathinfo( $path, PATHINFO_EXTENSION ), $ext ) === 0 ) {
// match
return true;
}
}
$applied ++;
}
// now filename
$filename_include = isset( $include['filename'] ) ? $include['filename'] : array();
if ( is_array( $filename_include ) && count( $filename_include ) && $type == 'file' ) {
foreach ( $filename_include as $filename ) {
if ( preg_match( '/' . $filename . '/', pathinfo( $path, PATHINFO_BASENAME ) ) ) {
return true;
}
}
$applied ++;
}
// now abs path
$path_include = isset( $include['path'] ) ? $include['path'] : array();
if ( is_array( $path_include ) && count( $path_include ) && $type == 'file' ) {
foreach ( $path_include as $p ) {
if ( strcmp( $p, $path ) === 0 ) {
return true;
}
}
$applied ++;
}
if ( 0 === $applied ) {
return true;
}
return false;
}
/**
* Run the filter for a file/dir
*
* @param $path
* @param null $filetype
*
* @return bool
*/
private function _filter_exclude( $path, $filetype = null ): bool {
$exclude = $this->exclude;
// first filer dir, or file inside dir
if ( ! is_null( $filetype ) ) {
$type = $filetype;
} else {
$type = filetype( $path );
}
$dir_exclude = isset( $exclude['dir'] ) ? $exclude['dir'] : array();
if ( is_array( $dir_exclude ) && count( $dir_exclude ) ) {
foreach ( $dir_exclude as $dir ) {
if ( strpos( $path, $dir ) === 0 ) {
return false;
}
}
}
// next extension
$ext_exclude = isset( $exclude['ext'] ) ? $exclude['ext'] : array();
if ( is_array( $ext_exclude ) && count( $ext_exclude ) && $type == 'file' ) {
// we will uses foreach and strcasecmp instead of regex cause it faster
foreach ( $ext_exclude as $ext ) {
if ( strcasecmp( pathinfo( $path, PATHINFO_EXTENSION ), $ext ) === 0 ) {
// match
return false;
}
}
}
// now filename
$filename_exclude = isset( $exclude['filename'] ) ? $exclude['filename'] : array();
if ( is_array( $filename_exclude ) && count( $filename_exclude ) && $type == 'file' ) {
foreach ( $filename_exclude as $filename ) {
if ( preg_match( '/' . $filename . '/', pathinfo( $path, PATHINFO_BASENAME ) ) ) {
return false;
}
}
}
// now abs path
$path_exclude = isset( $exclude['path'] ) ? $exclude['path'] : array();
if ( is_array( $path_exclude ) && count( $path_exclude ) && $type == 'file' ) {
foreach ( $path_exclude as $p ) {
if ( strcmp( $p, $path ) === 0 ) {
return false;
}
}
}
return true;
}
}