-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathftp-crawler.class.php
More file actions
415 lines (399 loc) · 14.9 KB
/
ftp-crawler.class.php
File metadata and controls
415 lines (399 loc) · 14.9 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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<?php
// Original PHP code by Dan at github.com/Ultrabenosaurus
// Please acknowledge use of this code by including this header.
class FTPCrawler{
private $server; // (string) host address of FTP server
private $user; // (string) username for logging into FTP server
private $password; // (string) password for logging into FTP server
private $start_dir; // (string) directory to treat as root [default: www]
private $port; // (integer) port to connect to [default: 21]
private $passive; // (boolean) whether or not a passive connection is required
private $list; // (array) the list of files found on the server
private $dirs; // (array) queue of all directories yet to be crawled
private $ignore_types; // (array) whitelist of files to listed
private $ignore_dirs; // (array|null) blacklist of directories [currently applies to any depth]
private $ftp_conn; // (resource) FTP connection pointer
// sets initial values of variables
public function __construct($_server, $_user, $_password, $_start = '/www'){
// essential variables, bare minimum for script to work
$this->server = $_server;
$this->user = $_user;
$this->password = $_password;
$this->start_dir = $_start;
// optional connection modifiers
$this->port = 21;
$this->passive = false;
// crawling settings
$this->list = array();
$this->dirs = array($_start);
$this->ignore_types = array('exe', 'air', 'rpm', 'bin', 'sh', 'dmg', 'htaccess', 'cml');
$this->ignore_dirs = array();
$ftp_conn = null;
}
// allows user to change variables if values are valid
public function settings($options = 'get'){
// ensure settings are given
if(is_array($options) && count($options) > 0){
// loop through all settings given
foreach ($options as $setting => $value) {
// check for existence of desired variable
if(property_exists('FTPCrawler', $setting)){
// switch through all variables for validation
switch ($setting) {
// check if desired value is a string
case 'server':
case 'user':
case 'password':
case 'start_dir':
if (gettype($value) === 'string') {
// set variable, return true
$this->{$setting} = $value;
$return[$setting] = true;
} else {
// return errors
$return[$setting] = array('value' => $value, 'error' => 'Given value was type '.gettype($value).', <em>string</em> required');
}
break;
// check if value is an integer
case 'port':
if(gettype($value) === 'integer' || intval($value) !== 0){
// set variable, return true
$this->{$setting} = $value;
$return[$setting] = true;
} else {
// return errors
$return[$setting] = array('value' => $value, 'error' => 'Given value was type '.gettype($value).', <em>integer</em> required');
}
break;
// check if value is boolean
case 'passive':
if(gettype($value) === 'boolean'){
// set variable, return true
$this->{$setting} = $value;
$return[$setting] = true;
} else {
// return errors
$return[$setting] = array('value' => $value, 'error' => 'Given value was type '.gettype($value).', <em>boolean</em> required');
}
break;
// check if value is populated array
case 'ignore_types':
if(gettype($value) === 'array' && count($value) > 0){
// loop through array
foreach ($value as $type) {
// check if value is a string
if(gettype($type) === 'string'){
// set variable, return true
$this->{$setting}[] = $type;
$return[$setting][$type] = true;
} else {
// return errors if not string
$return[$setting][$type] = array('type' => $type, 'error' => 'Given value was type '.gettype($value).', <em>string</em> required');
}
}
// remove duplicates from array
$this->{$setting} = array_unique($this->{$setting});
} else {
// return errors if not array
$return[$setting] = array('value' => $value, 'error' => 'Given $value was type '.gettype($value).', <em>array</em> required');
}
break;
// check if value is populated array
case 'ignore_dirs':
if(gettype($value) === 'array' && count($value) > 0){
// loop through array
foreach ($value as $type) {
// check if value is a string
if(gettype($type) === 'string'){
// set variable, return true
$this->{$setting}[] = $type;
$return[$setting][$type] = true;
} else {
// return errors if not string
$return[$setting][$type] = array('type' => $type, 'error' => 'Given $value was type '.gettype($value).', <em>string</em> required');
}
}
// remove duplicates from array
$this->{$setting} = array_unique($this->{$setting});
// check if value is null
} elseif(is_null($value)){
// set variable, return true
$this->{$setting} = $value;
$return[$setting] = true;
} else {
// return errors if not array or null
$return[$setting] = array('setting' => $setting, 'value' => $value, 'error' => 'Given $value was type '.gettype($value).', <em>array</em> or <em>null</em> required');
}
break;
default:
$return[$setting] = array('setting'=>$setting, 'value'=>$value, 'error'=>'Given setting does not exist or cannot be altered');
break;
}
} else {
$return[$setting] = array('setting'=>$setting, 'value'=>$value, 'error'=>'Given setting does not exist or cannot be altered');
}
}
} else {
// if $options is not an array, switch it to decide what to do
switch ($options) {
case 'get':
// if $options is 'get', print all variables but $dirs (usually empty) and $list (could be massive)
// initiate a ReflectionClass object, gather properties
$reflect = new ReflectionClass($this);
$properties = $reflect->getProperties();
// loop through properties, add to array if not $list or $dirs
foreach ($properties as $prop) {
$name = $prop->getName();
if($name !== 'list' && $name !== 'dirs' && $name !== 'ftp_conn'){
$return[$name] = $this->{$name};
}
}
break;
}
}
// return results
return $return;
}
// closes connection, destroys variables
public function __destruct(){
// close connection
$disconn = $this->disconnect();
// unset all variables
foreach (get_object_vars($this) as $key => $value) {
unset($this->{$key});
}
}
// attempts connection and login to FTP server
private function connect(){
// try to connect to FTP server
$this->ftp_conn = ftp_connect($this->server, $this->port);
// if cannot login, return errors
if(!ftp_login($this->ftp_conn, $this->user, $this->password)){
$return = array('details' => array('server' => $this->server, 'port' => $this->port, 'username' => $this->user, 'password' => $this->password, 'passive' => $this->passive), 'connected' => is_resource($this->ftp_conn), 'logged_in' => false, 'other'=>'Could not login.');
} else {
// check if passive mode is required
if($this->passive){
// if cannot enable passive mode return errors
if(!ftp_pasv($this->ftp_conn, true)){
$return = array('details' => array('server' => $this->server, 'port' => $this->port, 'username' => $this->user, 'password' => $this->password, 'passive' => $this->passive), 'connected' => is_resource($this->ftp_conn), 'logged_in' => true, 'other' => 'Could not turn <em>passive mode</em> on.');
// otherwise try to move to starting directory
} else {
// otherwise return true
$return = true;
}
// otherwise try to move to starting directory
} else {
// otherwise return true
$return = true;
}
}
// return results
return $return;
}
private function crawl(){
// check that a connection has been established
if(is_null($this->ftp_conn)){
// if no connection, attempt to make one
$results = $this->connect();
// if connection fails, display errors and prevent crawl() from running
if(is_array($results)){
echo "<pre>" . print_r($results, true) . "</pre>";
return false;
}
}
// make a copy of $dirs to keep track of changes, loop through it
$temp_dirs = $this->dirs;
foreach ($temp_dirs as $dir) {
// change dir, list everything there
$temp_list = ftp_nlist($this->ftp_conn, $dir);
// loop through directory listing, check if file or directory
foreach ($temp_list as $path) {
if($this->ftpIsDir($path)){
// if directory, check if it should be ignored
if(!empty($this->ignore_dirs)){
$temp_path = explode('/', $path);
$add = true;
// loop through directory blacklist
foreach ($this->ignore_dirs as $key => $value) {
// if directory should be ignored, don't add it
if(array_search($value, $temp_path) === true){
$add = false;
}
}
// add directory to $dirs if not in blacklist
if($add){
$this->dirs[] = $path;
}
// if no blacklisted directories, add all to $dirs
} else {
$this->dirs[] = $path;
}
} else {
// if a file, check the filetype against the whitelist
$filetype = explode('.', $path);
$filetype = $filetype[count($filetype)-1];
if(array_search($filetype, $this->ignore_types) === false){
// if on whitelist, remove the leading /www and add to $list
$this->list[] = $this->pathFix($path);
}
}
}
// remove current directory from $dirs, replace copy with the new list
array_shift($this->dirs);
$temp_dirs = $this->dirs;
}
// until $dirs is empty, keep crawling
if(count($this->dirs) > 0){
$this->crawl();
}
// although unlikely, make sure there are no duplicates
$this->arrayShuffle();
}
// sorts arrays
private function arrayShuffle(){
// remove duplicate values
$this->list = array_unique($this->list);
// sort alphabetically
sort($this->list);
// reset numeric keys
$this->list = array_values($this->list);
}
// removes the leading directory (eg: /www) so the reported paths can be used as links easily
private function pathFix($dir){
// split the given path on each slash
$dirname = explode('/', $dir);
// get rid of the first two items
array_shift($dirname);
array_shift($dirname);
// put it back together
$dirname = '/'.implode('/', $dirname);
return $dirname;
}
// attempt disconnect from server
public function disconnect(){
// if a connection is established, try to close it
if(isset($this->ftp_conn) && is_resource($this->ftp_conn)){
// if cannot close connection return errors
if(!ftp_close($this->ftp_conn)){
$return = array('details' => array('server' => $this->server, 'port' => $this->port, 'username' => $this->user, 'password' => $this->password, 'passive' => $this->passive), 'connected' => is_resource($this->ftp_conn), 'logged_in' => true, 'other' => 'Could not terminate FTP connection.');
// otherwise return true
} else {
$return = true;
}
// if no open connection return errors
} else {
$return = array('details' => array('server' => $this->server, 'port' => $this->port, 'username' => $this->user, 'password' => $this->password, 'passive' => $this->passive), 'connected' => is_resource($this->ftp_conn), 'logged_in' => false, 'other' => 'No FTP connection available to be terminated.');
}
// return results
return $return;
}
// used to toggle passive mode without using the settings() method
public function passiveToggle(){
// invert current setting for $passive, set error text
if($this->passive){
$this->passive = false;
$toggle = 'off';
} else {
$this->passive = true;
$toggle = 'on';
}
// if cannot change passive status return errors
if(!ftp_pasv($this->ftp_conn, $this->passive)){
$return = array('details' => array('server' => $this->server, 'port' => $this->port, 'username' => $this->user, 'password' => $this->password, 'passive' => $this->passive), 'connected' => is_resource($this->ftp_conn), 'logged_in' => true, 'other' => 'Could not turn <em>passive mode</em> '.$toggle.'.');
// otherwise return true
} else {
$return = true;
}
// return results
return $return;
}
// check if the desired path is a directory or file
private function ftpIsDir($dir){
// try to move directory to see if if works
if(@ftp_chdir($this->ftp_conn, $dir)){
// if it works, go back one and return true
ftp_chdir($this->ftp_conn, '..');
return true;
// otherwise return false
} else {
return false;
}
}
// formats data for output to user
public function output($type = 'php'){
// gather data by crawling the server
$this->crawl();
switch ($type) {
case 'php':
// for php format, collect data into a multi-dimensional array
$return['details']['server'] = $this->server;
$return['details']['port'] = $this->port;
$return['details']['username'] = $this->user;
$return['details']['password'] = $this->password;
if($this->passive){
$return['details']['passive'] = 'true';
}
$return['details']['start_dir'] = $this->start_dir;
$return['list_total'] = count($this->list);
$return['list'] = $this->list;
break;
case 'xml':
// new DOMDocument
$xml_doc = new DOMDocument();
$xml_doc->formatOutput = true;
$root = $xml_doc->createElement('ftp_crawl');
// settings used for FTP server
$settings_elem = $xml_doc->createElement('settings');
$settings = $this->settings('get');
foreach ($settings as $key => $value) {
if(is_array($value)){
$sett = $xml_doc->createElement($key);
foreach ($value as $val) {
if(is_null($val) || empty($val)){
$temp = $xml_doc->createElement('value', 'null');
} else {
$temp = $xml_doc->createElement('value', $val);
}
$sett->appendChild($temp);
}
} else {
$sett = $xml_doc->createElement($key, $value);
}
$settings_elem->appendChild($sett);
}
// results of crawl
$results_elem = $xml_doc->createElement('results');
$total = $xml_doc->createElement('total', count($this->list));
$list = $xml_doc->createElement('list');
foreach ($this->list as $key => $value) {
$item = $xml_doc->createElement('list_item');
$path = $xml_doc->createElement('path', $value);
$url = $xml_doc->createElement('url', 'http://'.$this->server.$value);
$item->appendChild($path);
$item->appendChild($url);
$list->appendChild($item);
}
// finish up
$root->appendChild($settings_elem);
$root->appendChild($list);
$xml_doc->appendChild($root);
$xml_doc->normalizeDocument();
// name, save and return
$name = explode('.', $this->server);
if(count($name) > 2){
$name = $name[1];
} else {
$name = $name[0];
}
$name = $name.'_ftpcrawl.xml';
$xml_file = fopen($name, 'w');
fwrite($xml_file, $xml_doc->saveXML());
fclose($xml_file);
$return = $name;
break;
}
// return formatted data
return $return;
}
}
?>