-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdirectory.php
372 lines (342 loc) · 10.6 KB
/
directory.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/* Copyright 2005-2011 Tonio Loewald */
require_once( 'utilities.php' );
class FMdirectory {
private $d = NULL;
// the path we used
public $path = '';
public $exists = false;
public $continuation = false;
public $first = 0;
function __construct( $path ) {
if( preg_match( '/\.continue\./', $path )){
list( $path, $this->first ) = preg_split( '/\.continue\./', $path );
if( substr( $this->first, -1 ) == '/' ){
$this->first = substr( $this->first, 0, -1 );
}
$this->continuation = true;
}
if( file_exists($path) && filetype($path) == 'dir' ){
$this->exists = true;
$this->path = $path;
$this->d = dir( $path );
} else {
if( !file_exists( $path ) ){
FatalError( "directory constructor failed: “$path” does not exist." );
} else {
FatalError( "directory constructor failed: “$path” is not a directory." );
}
}
}
function __destruct() {
if( $this->d ){
$this->d->close();
}
}
function filter( $options, $entry ){
$show_invisibles = Option( $options, 'show_invisibles', false );
if( !$show_invisibles && substr($entry, 0, 1) == '.' ){
return false;
}
$match = Option( $options, 'match', '' );
if( $match != '' && $entry != $match ){
return false;
}
$fileTails = OptionArray( $options, 'fileTails', false );
if( $fileTails && !stringEndsWith( $entry, $fileTails ) ){
return false;
}
$fileTypes = OptionArray( $options, 'fileTypes', false );
if( $fileTypes && !stringEndsWith( $entry, $fileTypes, '.' ) ){
return false;
}
$find = Option( $options, 'find', '' );
if( $find != '' && strpos( $entry, $find ) === false ){
return false;
}
$regex = Option( $options, 'regex', '' );
if( $regex != '' && preg_match( $regex, $entry ) == 0 ){
return false;
}
return true;
}
function listing( $options = NULL ){
$listing = Option( $options, 'listing', NULL );
$recursive = Option( $options, 'recursive', false );
$limit_recursion = Option( $options, 'limit_recursion', false ); // don't drill into a folder if the folder itself is a hit
$first_match = Option( $options, 'first_match', false ); // stop searching once you find a match
$show_folders = Option( $options, 'show_folders', false );
$show_files = Option( $options, 'show_files', true );
$folder_suffix = Option( $options, 'folder_suffix', '' );
// allow scaling for arbitrarily large directories...
// if limit is positive, this is the maximum size list we will pass; if there are more files, we will
// return a continuation token (a virtual subdirectory which effectively points at the next set of files)
// the tokens look like /.continue.n/ where n is the index of the first file that will be returned
$limit = Option( $options, 'limit', 0 );
if( Option( $options, 'simple', false ) ){
$detail = 'simple';
}
if( Option( $options, 'simple_no_path', false ) ){
$detail = 'simple_no_path';
}
$detail = Option( $options, 'detail', false );
if( gettype($listing) != "array" ){
$listing = array();
}
profile_start('directory->listing');
$done = false;
$skip = $this->first;
if( $this->d ){
$path = $this->d->path;
$this->d->rewind();
while( ($entry = $this->d->read()) && !$done ){
if( $entry == '.' || $entry == '..' ){
} else {
$entry_path = catpath( $path, $entry );
$type = filetype($entry_path);
$match = $this->filter( $options, $entry );
if ( $match ){
if (
($show_folders && $type == 'dir') || ($show_files && $type == 'file')
){
if( $skip > 0 ){
// don't add entries until we get to the "first"
$skip--;
} else {
if( $type == 'dir' ){
$entry .= $folder_suffix;
$entry_path .= $folder_suffix;
}
switch( $detail ){
case 'simple':
$listing[] = $entry_path;
break;
case 'simple_no_path':
$listing[] = $entry;
break;
case 'detailed':
$listing[] = array(
'name' => $entry,
'path' => $entry_path,
'type' => $type,
'size' => filesize( $entry_path ),
'mtime' => filemtime( $entry_path )
);
break;
default:
$listing[$entry_path] = $entry;
}
if( $first_match ){
continue;
}
}
}
}
// Recurse if required
if( $type == 'dir' && $recursive && (!$limit_recursion || !$match) ){
$sub_dir = new FMdirectory( $entry_path );
$options['listing'] = $listing;
$listing = $sub_dir->listing( $options );
}
}
if( $limit > 0 && count( $listing ) >= $limit ){
$token = '.continue.' . ($this->first + $limit) . $folder_suffix;
switch( $detail ){
case 'simple':
$listing[] = catpath( $this->path, $token );
break;
case 'simple_no_path':
$listing[] = $token;
break;
case 'detailed':
$listing[] = array(
'name' => $token,
'path' => catpath($this->path, $token),
'type' => 'dir',
'size' => 1,
'mtime' => time()
);
break;
default:
$listing[ catpath($this->path, $token) ] = $token;
}
$done = true;
}
}
}
profile_end('directory->listing');
return $listing;
}
}
/*
Note that FMwebDirectory is in fact VERY different from FMdirectory and really only uses
the filter method.
*/
class FMwebDirectory extends FMdirectory {
private $url_parts;
private $host;
private $url;
function __construct( $url ){
if( preg_match( '/\.continue\./', $url )){
list( $url, $this->first ) = preg_split( '/\.continue\./', $url );
if( substr( $this->first, -1 ) == '/' ){
$this->first = substr( $this->first, 0, -1 );
}
$this->continuation = true;
}
$this->url = $url;
$this->url_parts = parse_url( $url );
if( !isset( $this->url_parts['path'] ) ){
$this->url_parts['path'] = '';
}
$this->path = $this->url_parts['path'];
if( isset( $this->url_parts['scheme'] ) ){
$this->exists = true; // actually a bit optimistic...
$this->host = $this->url_parts['scheme'] . '://' . $this->url_parts['host'] . '/';
}
}
function __destruct() {
// don't need to do anything
}
function make_absolute( $url ){
if( substr( $url , 0, 1 ) == '/' ){
return catpath( $this->host, $url );
} else if ( substr( $url, 0, 7 ) == 'http://' ){
return $url;
} else {
return catpath($this->url, $url);
}
}
function listing( $options = NULL ) {
$listing = Option( $options, 'listing', NULL );
$recursive = Option( $options, 'recursive', false );
$show_folders = Option( $options, 'show_folders', false );
$show_files = Option( $options, 'show_files', true );
$limit = Option( $options, 'limit', 0 );
if( Option( $options, 'simple', false ) ){
$detail = 'simple';
}
if( Option( $options, 'simple_no_path', false ) ){
$detail = 'simple_no_path';
}
$detail = Option( $options, 'detail', false );
if( gettype($listing) != "array" ){
$listing = array();
}
$html = file_get_contents($this->url);
// echo $html;
// preg_match_all( '/\<a [^\>]*href\=\"([^\"]+)\"[^\>]*\>([^\<]*)/', $html, $matches );
preg_match_all( '/\<a [^\>]*href\=\"([^\"]+)\"[^\>]*\>([^\<]*)[^\n^0-9]*([0-9]{1,2}\-[0-9a-zA-Z]{1,4}\-[0-9]{4}[^0-9]+[0-9]{1,2}:[0-9]{2})[^\n^0-9]*([0-9\.\,]*[kKmMgG]{0,1})/', $html, $matches );
$urls = $matches[1];
$names = $matches[2];
$times = $matches[3];
$sizes = $matches[4];
$done = false;
$skip = $this->first;
for( $i = 0; $i < count($urls) && !$done; $i++ ){
$url = $urls[$i];
$name = $names[$i];
$good = true;
if( $name == 'Parent Directory' ){
$good = false;
}
if( substr( $url, 0, 1 ) == '?' && strlen( $url ) == 8 ){
$good = false;
}
if( substr( $url, 0, 11 ) == 'javascript:' ){
$good = false;
}
$absolute_url = $this->make_absolute( $url );
if( $absolute_url == $this->url ){
$good = false;
}
if( $good ){
$type = substr( $absolute_url, -1 ) == '/' ? 'dir' : 'file' ;
if( $this->filter( $options, $url ) ){
if ( ( $show_folders && $type == 'dir' ) || ( $show_files && $type == 'file' ) ){
if( $skip > 0 ){
$skip--;
} else {
switch( $detail ){
case 'simple_no_path':
$listing[] = $url;
break;
case 'simple':
$listing[] = $absolute_url;
break;
case 'detailed':
$size = $sizes[$i];
switch( substr( $size, -1 ) ){
case 'k':
case 'K':
$size *= 1024;
break;
case 'm':
case 'M':
$size *= 1048576;
break;
case 'g':
case 'G':
$size *= 1073741824;
break;
}
$size = round( $size );
$modified = strtotime( $times[$i] );
$listing[] = array(
'name' => $url,
'path' => $absolute_url,
'type' => $type,
'size' => $size,
'mtime' => $modified
);
break;
default:
if( $type == 'file' ){
$parts = explode( '/', $url );
if( count( $parts ) > 1 ){
$url = array_pop( $parts );
}
} else {
$parts = parse_url( $url );
$url = $parts[ 'path' ];
}
$listing[$absolute_url] = $url;
}
if( $limit > 0 && count( $listing ) >= $limit ){
$token = '.continue.' . ($this->first + $limit) . '/';
switch( $detail ){
case 'simple':
$listing[] = catpath( $this->path, $token );
break;
case 'simple_no_path':
$listing[] = $token;
break;
case 'detailed':
$listing[] = array(
'name' => $token,
'path' => catpath($this->url, $token),
'type' => 'dir',
'size' => 1,
'mtime' => time()
);
break;
default:
$listing[ $this->make_absolute(catpath($this->path, $token)) ] = $token;
}
$done = true;
}
}
}
}
// Recurse if required
if ( $type == 'dir' && $recursive ){
$sub_dir = new FMwebDirectory( $absolute_url );
$options['listing'] = $listing;
$listing = $sub_dir->listing( $options );
}
}
}
return $listing;
}
}
?>