C:\$Recycle.Bin\S-1-5-21-3967099092-2644009230-141905953-500\$RD9NVG6\ftp\includes\browse.inc.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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
<?php

//   -------------------------------------------------------------------------------
//  |                  net2ftp: a web based FTP client                              |
//  |              Copyright (c) 2003-2006 by David Gartner                         |
//  |                                                                               |
//  | This program is free software; you can redistribute it and/or                 |
//  | modify it under the terms of the GNU General Public License                   |
//  | as published by the Free Software Foundation; either version 2                |
//  | of the License, or (at your option) any later version.                        |
//  |                                                                               |
//   -------------------------------------------------------------------------------




// **************************************************************************************
// **************************************************************************************
// **                                                                                  **
// **                                                                                  **

function ftp_getlist($conn_id$directory) {

// --------------
// This function connects to the FTP server and returns an array with a list of directories and files.
// One row per directory or file, with rows from index 1 to n
//
// Step 1: send ftp_rawlist request to the FTP server; this returns a string
// Step 2: parse that string and get a first array ($templist)
// Step 3: move the rows to another array, to index 1 to n ($list)
//
// This function is used in all functions which process directories recursively.
// --------------

// -------------------------------------------------------------------------
// Global variables
// -------------------------------------------------------------------------
    
global $net2ftp_globals;

// -------------------------------------------------------------------------
// Step 1: Chdir to the directory and get the current directory
// -------------------------------------------------------------------------

// ----------------------------------------------
// Step 1a - Directory is "/"
// Chdir to the directory because otherwise the ftp_rawlist does not work on some FTP servers
// ----------------------------------------------
    
if ($directory == "/") { 
        
$result1a = @ftp_chdir($conn_id$directory);    
    }

// ----------------------------------------------
// Step 1b - Directory is ""
// If the directory is "" then the user can be directed to his home directory
// We don't know which directory it is, so we request it from the FTP server
// ----------------------------------------------
    
elseif ($directory == "") { 
        
$result1b = @ftp_chdir($conn_id$directory);
        
$directory = @ftp_pwd($conn_id);
    }

// ----------------------------------------------
// Step 1c - Directory is not "/" or ""
// ----------------------------------------------
    
else {

// 1c1 - Replace \' by \\' to be able to delete directories with names containing \'
        
$directory1 str_replace("\'""\\\'"$directory); 

// 1c2 - Chdir to the directory
// This is to check if the directory exists, but also because otherwise
// the ftp_rawlist does not work on some FTP servers.
        
$result1c = @ftp_chdir($conn_id$directory1);

// 1c3 - If the first ftp_chdir returns false, try a second time without the leading /
// Some Windows FTP servers do not work when you use a leading /
        
if ($result1c == false) {
            
$directory2 stripDirectory($directory1);
            
$result2 = @ftp_chdir($conn_id$directory2);

// 1c3 - If the second ftp_chdir also does not work:
//           For the Browse screen ==> go to the user's root directory
//           For all other screens ==> return error
            
if ($result2 == false) {
                if (
$net2ftp_globals["state"] == "browse") {
                    
$rootdirectory getRootdirectory();

                    
// User's root directory is different from the current directory, so switch to it
                    
if ($directory != $rootdirectory) {
                        
$warnings __("The directory <b>%1\$s</b> does not exist or could not be selected, so the directory <b>%2\$s</b> is shown instead."$directory$rootdirectory);
                        
$directory $rootdirectory;
                        
$result3 = @ftp_chdir($conn_id$directory);
                    }

                    
// The current directory *is* the user's root directory!
                    // We cannot display any other directory (like /), so print an error message.
                    
else {
                        
$errormessage __("Your root directory <b>%1\$s</b> does not exist or could not be selected."$directory);
                        
setErrorVars(false$errormessagedebug_backtrace(), __FILE____LINE__);
                    }
                }
                else {
                    
$errormessage __("The directory <b>%1\$s</b> could not be selected - you may not have sufficient rights to view this directory, or it may not exist."$directory);
                    
setErrorVars(false$errormessagedebug_backtrace(), __FILE____LINE__);
                }
            } 
// end if result2

        
// end if result1
    
    
// end if / or "" or else

// Store the warnings and new directory in $list["stats"]
    
if (isset($warnings) == true) { $list["stats"]["warnings"] = $warnings; }
    else                          { 
$list["stats"]["warnings"] = ""; }
    
$list["stats"]["newdirectory"] = $directory;

// -------------------------------------------------------------------------
// Step 2 - Get list of directories and files
// The -a option is used to show the hidden files as well on some FTP servers
// Some servers do not return anything when using -a, so in that case try again without the -a option
// -------------------------------------------------------------------------
    
$rawlist ftp_rawlist($conn_id"-a");
    if (
sizeof($rawlist) <= 1) { $rawlist ftp_rawlist($conn_id""); }


// -------------------------------------------------------------------------
// Step 3 - Parse the raw list
// -------------------------------------------------------------------------

// ----------------------------------------------
// Initialize variables
// ----------------------------------------------
    
$list["directories"]  = array();
    
$list["files"]        = array();
    
$list["symlinks"]     = array();
    
$list["unrecognized"] = array();
    
$directory_index    1;
    
$file_index         1;
    
$symlink_index      1;
    
$unrecognized_index 1;
    
$list["stats"]["directories"]["total_number"]   = 0;
    
$list["stats"]["directories"]["total_size"]     = 0;
    
$list["stats"]["directories"]["total_skipped"]  = 0;
    
$list["stats"]["files"]["total_number"]         = 0;
    
$list["stats"]["files"]["total_size"]           = 0;
    
$list["stats"]["files"]["total_skipped"]        = 0;
    
$list["stats"]["symlinks"]["total_number"]      = 0;
    
$list["stats"]["symlinks"]["total_size"]        = 0;
    
$list["stats"]["symlinks"]["total_skipped"]     = 0;
    
$list["stats"]["unrecognized"]["total_number"]  = 0;
    
$list["stats"]["unrecognized"]["total_size"]    = 0;
    
$list["stats"]["unrecognized"]["total_skipped"] = 0;

// ----------------------------------------------
// Loop over the raw list lines
// ----------------------------------------------
    
for($i=0$i<sizeof($rawlist); $i++) {

// ----------------------------------------------
// Scan each line
// ----------------------------------------------
        
$listline ftp_scanline($directory$rawlist[$i]);

// If $listline is empty (e.g. if it contained ".."), continue to the next line
        
if ($listline == "") { continue; }

// Encode the name for HTML and Javascript
        
if (isset($listline["dirfilename"])) { 
            
$listline["dirfilename_html"] = htmlEncode2($listline["dirfilename"]);
            
$listline["dirfilename_url"]  = urlEncode2($listline["dirfilename"]);
            
$listline["dirfilename_js"]   = javascriptEncode($listline["dirfilename"]);
        }

// ----------------------------------------------
// Depending on if the line contained a directory/file/symlink/unrecognized 
// row, store the result in different variables
// ----------------------------------------------

        
if ($listline["dirorfile"] == "d") { 
            
$list["directories"][$directory_index] = $listline;
            
$directory_index++;
            if (isset(
$listline["size"]) && is_numeric($listline["size"])) { 
                
$list["stats"]["directories"]["total_size"] = $list["stats"]["directories"]["total_size"] + $listline["size"]; 
            }
            else { 
                
$list["stats"]["directories"]["total_skipped"] = $list["stats"]["directories"]["total_skipped"] + 1
            }
        } 
// end if
        
elseif ($listline["dirorfile"] == "-") { 
            
$list["files"][$file_index] = $listline;
            
$file_index++;
            if (isset(
$listline["size"]) && is_numeric($listline["size"])) { 
// Check if the filesize is bigger than the maximum authorized filesize
                
if ($listline["size"] > $net2ftp_settings["max_filesize"]) { $listline["too_big"] = true;    }
                else { 
$listline["too_big"] = false; }
                
$list["stats"]["files"]["total_size"] = $list["stats"]["files"]["total_size"] + $listline["size"]; 
            }
            else { 
                
$list["stats"]["files"]["total_skipped"] = $list["stats"]["files"]["total_skipped"] + 1
            }
        } 
// end elseif
        
elseif ($listline["dirorfile"] == "l") { 
            
$list["symlinks"][$symlink_index] = $listline;
            
$symlink_index++;
        } 
// end elseif
        
elseif ($listline["dirorfile"] == "u") { 
            
$list["unrecognized"][$unrecognized_index] = $listline;
            
$unrecognized_index++;
        } 
// end elseif

    
// end for

// Store the statistics
    
$list["stats"]["directories"]["total_size_formated"] = formatFilesize($list["stats"]["directories"]["total_size"]);
    
$list["stats"]["files"]["total_size_formated"]       = formatFilesize($list["stats"]["files"]["total_size"]);
    
$list["stats"]["directories"]["total_number"]  = $directory_index 1;
    
$list["stats"]["files"]["total_number"]        = $file_index 1;
    
$list["stats"]["symlinks"]["total_number"]     = $symlink_index 1;
    
$list["stats"]["unrecognized"]["total_number"] = $unrecognized_index 1;

// Put everything together in $list["all"]
    
$list["all"] = $list["directories"] + $list["files"] + $list["symlinks"] + $list["unrecognized"];

// -------------------------------------------------------------------------
// Step 4 - Return the result
// -------------------------------------------------------------------------
    
return $list;




// -------------------------------------------------------------------------
// Some documentation:
// 1 - Some FTP servers return a total on the first line
// 2 - Some FTP servers return . and .. in their list of directories
// ftp_scanline does not return those entries.
// -------------------------------------------------------------------------


// 1 - After doing some tests on different public FTP servers, it appears that
// they reply differently to the ftp_rawlist request:
//      - some FTP servers, like ftp.belnet.be, start with a line summarizing how
//        many subdirectories and files there are in the current directory. The
//        real list of subdirectories and files starts on the second line.
//               [0] => total 15
//               [1] => drwxr-xr-x 11 BELNET Archive 512 Feb 6 2000 BELNET
//               [2] => drwxr-xr-x 2 BELNET Archive 512 Oct 29 2001 FVD-SFI
//      - some other FTP servers, like ftp.redhat.com/pub, start directly with the
//        list of subdirectories and files.
//               [0] => drwxr-xr-x 9 ftp ftp 4096 Jan 11 06:34 contrib
//               [1] => drwxr-xr-x 13 ftp ftp 4096 Jan 29 21:59 redhat
//               [2] => drwxrwsr-x 6 ftp ftp 4096 Jun 05 2002 up2date


// 2 - Some FTP servers return "." and ".." as directories. These fake entries
// have to be eliminated!
// They would cause infinite loops in the copy/move/delete functions.
//               [0] => drwxr-xr-x 5 80 www 512 Apr 10 09:39 .
//               [1] => drwxr-xr-x 16 80 www 512 Apr 9 08:51 ..
//               [2] => -rw-r--r-- 1 80 www 5647 Apr 9 08:12 _CHANGES_v0.5
//               [3] => -rw-r--r-- 1 80 www 1239 Apr 9 08:12 _CREDITS_v0.5


// End function ftp_getlist

// **                                                                                  **
// **                                                                                  **
// **************************************************************************************
// **************************************************************************************








// **************************************************************************************
// **************************************************************************************
// **                                                                                  **
// **                                                                                  **
function ftp_scanline($directory$rawlistline) {

// --------------
// This function scans an ftp_rawlist line string and returns its parts (directory/file, name, size,...) using preg_match()
//
//  !!! Documentation about preg_match and FTP server's outputs are now at the end of the function !!!
// --------------

// -------------------------------------------------------------------------
// Global variables
// -------------------------------------------------------------------------
    
global $net2ftp_settings;


// -------------------------------------------------------------------------
// Scanning:
//   1. first scan with strict rules
//   2. if that does not match, scan with less strict rules
//   3. if that does not match, scan with rules for specific FTP servers (AS400)
//   4. and if that does not match, return the raw line
// -------------------------------------------------------------------------

// ----------------------------------------------
// 1. Strict rules
// ----------------------------------------------
    
if (preg_match("/([-dl])([rwxsStT-]{9})[ ]+([0-9]+)[ ]+([^ ]+)[ ]+(.+)[ ]+([0-9]+)[ ]+([a-zA-Z]+[ ]+[0-9]+)[ ]+([0-9:]+)[ ]+(.*)/"$rawlistline$regs) == true) {
//                  permissions             number      owner      group   size        month         day        year/hour    filename
        
$listline["scanrule"]         = "rule-1";
        
$listline["dirorfile"]        = "$regs[1]";        // Directory ==> d, File ==> -
        
$listline["dirfilename"]      = "$regs[9]";        // Filename
        
$listline["dirfilename_html"] = htmlEncode2($listline["dirfilename"]);
        
$listline["dirfilename_url"]  = urlEncode2($listline["dirfilename"]);
        
$listline["dirfilename_js"]   = javascriptEncode($listline["dirfilename"]);
        
$listline["size"]             = "$regs[6]";        // Size
        
$listline["owner"]            = "$regs[4]";        // Owner
        
$listline["group"]            = "$regs[5]";        // Group
        
$listline["permissions"]      = "$regs[2]";        // Permissions
        
$listline["mtime"]            = "$regs[7] $regs[8]";    // Mtime -- format depends on what FTP server returns (year, month, day, hour, minutes... see above)

        
if ($listline["dirorfile"] == "d") {
            
$listline["newdir"]      = glueDirectories($directory$listline["dirfilename"]); 
            
$listline["newdir_html"] = htmlEncode2($listline["newdir"]);
            
$listline["newdir_url"]  = urlEncode2($listline["newdir"]);
            
$listline["newdir_js"]   = javascriptEncode($listline["newdir"]);
        }

    }

// ----------------------------------------------
// 2. Less strict rules
// ----------------------------------------------
    
elseif (preg_match("/([-dl])([rwxsStT-]{9})[ ]+(.*)[ ]+([a-zA-Z0-9 ]+)[ ]+([0-9:]+)[ ]+(.*)/"$rawlistline$regs) == true) {
//                      permissions             number/owner/group/size
//                                                      month-day          year/hour    filename
        
$listline["scanrule"]         = "rule-2";
        
$listline["dirorfile"]        = "$regs[1]";        // Directory ==> d, File ==> -
        
$listline["dirfilename"]      = "$regs[6]";        // Filename
        
$listline["dirfilename_html"] = htmlEncode2($listline["dirfilename"]);
        
$listline["dirfilename_url"]  = urlEncode2($listline["dirfilename"]);
        
$listline["dirfilename_js"]   = javascriptEncode($listline["dirfilename"]);
        
$listline["size"]             = "$regs[3]";        // Number/Owner/Group/Size
        
$listline["permissions"]      = "$regs[2]";        // Permissions
        
$listline["mtime"]            = "$regs[4] $regs[5]";    // Mtime -- format depends on what FTP server returns (year, month, day, hour, minutes... see above)

        
if ($listline["dirorfile"] == "d") {
            
$listline["newdir"]      = glueDirectories($directory$listline["dirfilename"]); 
            
$listline["newdir_html"] = htmlEncode2($listline["newdir"]);
            
$listline["newdir_url"]  = urlEncode2($listline["newdir"]);
            
$listline["newdir_js"]   = javascriptEncode($listline["newdir"]);
        }

    }

// ----------------------------------------------
// 3. Specific FTP server rules
// ----------------------------------------------

// ---------------
// 3.1 Windows
// ---------------
    
elseif (preg_match("/([0-9\\/-]+)[ ]+([0-9:AMP]+)[ ]+([0-9]*|<DIR>)[ ]+(.*)/"$rawlistline$regs) == true) {
//                      date          time            size              filename

        
$listline["scanrule"]         = "rule-3.1";
        if (
$regs[3] == "<DIR>") { $listline["size"] = ""; }
        else                     { 
$listline["size"] = "$regs[3]"; } // Size
        
$listline["dirfilename"] = "$regs[4]";        // Filename
        
$listline["dirfilename_html"] = htmlEncode2($listline["dirfilename"]);
        
$listline["dirfilename_url"]  = urlEncode2($listline["dirfilename"]);
        
$listline["dirfilename_js"]   = javascriptEncode($listline["dirfilename"]);
        
$listline["owner"]            = "";            // Owner
        
$listline["group"]            = "";            // Group
        
$listline["permissions"]      = "";            // Permissions
        
$listline["mtime"]            = "$regs[1] $regs[2]";    // Mtime -- format depends on what FTP server returns (year, month, day, hour, minutes... see above)

        
if ($listline["size"] != "") { $listline["dirorfile"] = "-"; }
        else                         { 
$listline["dirorfile"] = "d"; }

        if (
$listline["dirorfile"] == "d") {
            
$listline["newdir"]      = glueDirectories($directory$listline["dirfilename"]); 
            
$listline["newdir_html"] = htmlEncode2($listline["newdir"]);
            
$listline["newdir_url"]  = urlEncode2($listline["newdir"]);
            
$listline["newdir_js"]   = javascriptEncode($listline["newdir"]);
        }

    }

// ---------------
// 3.2 Netware
// Thanks to Danny!
// ---------------
    
elseif (preg_match("/([-]|[d])[ ]+(.{10})[ ]+([^ ]+)[ ]+([0-9]*)[ ]+([a-zA-Z]*[ ]+[0-9]*)[ ]+([0-9:]*)[ ]+(.*)/"$rawlistline$regs) == true) {
//                      dir/file perms          owner      size        month        day         hour         filename
        
$listline["scanrule"]         = "rule-3.2";
        
$listline["dirorfile"]        = "$regs[1]";        // Directory ==> d, File ==> -
        
$listline["dirfilename"]      = "$regs[7]";        // Filename
        
$listline["dirfilename_html"] = htmlEncode2($listline["dirfilename"]);
        
$listline["dirfilename_url"]  = urlEncode2($listline["dirfilename"]);
        
$listline["dirfilename_js"]   = javascriptEncode($listline["dirfilename"]);
        
$listline["size"]             = "$regs[4]";        // Size
        
$listline["owner"]            = "$regs[3]";        // Owner
        
$listline["group"]            = "";            // Group
        
$listline["permissions"]      = "$regs[2]";        // Permissions
        
$listline["mtime"]            = "$regs[5] $regs6";    // Mtime -- format depends on what FTP server returns (year, month, day, hour, minutes... see above)

        
if ($listline["dirorfile"] == "d") {
            
$listline["newdir"]      = glueDirectories($directory$listline["dirfilename"]); 
            
$listline["newdir_html"] = htmlEncode2($listline["newdir"]);
            
$listline["newdir_js"]   = javascriptEncode($listline["newdir"]);
        }

    }

// ---------------
// 3.3 AS400
// ---------------
    
elseif (preg_match("/([a-zA-Z0-9_-]+)[ ]+([0-9]+)[ ]+([0-9\\/-]+)[ ]+([0-9:]+)[ ]+([a-zA-Z0-9_ -\*]+)[ \\/]+([^\\/]+)/"$rawlistline$regs) == true) {
//                      owner               size        date          time         type                    filename

        
if ($regs[5] != "*STMF") { $directory_or_file "d"; }
        elseif (
$regs[5] == "*STMF") { $directory_or_file "-"; }

        
$listline["scanrule"]         = "rule-3.3";
        
$listline["dirorfile"]        = "$directory_or_file";// Directory ==> d, File ==> -
        
$listline["dirfilename"]      = "$regs[6]";        // Filename
        
$listline["dirfilename_html"] = htmlEncode2($listline["dirfilename"]);
        
$listline["dirfilename_url"]  = urlEncode2($listline["dirfilename"]);
        
$listline["dirfilename_js"]   = javascriptEncode($listline["dirfilename"]);
        
$listline["size"]             = "$regs[2]";        // Size
        
$listline["owner"]            = "$regs[1]";        // Owner
        
$listline["group"]            = "";            // Group
        
$listline["permissions"]      = "";            // Permissions
        
$listline["mtime"]            = "$regs[3] $regs[4]";    // Mtime -- format depends on what FTP server returns (year, month, day, hour, minutes... see above)

        
if ($listline["dirorfile"] == "d") {
            
$listline["newdir"]      = glueDirectories($directory$listline["dirfilename"]); 
            
$listline["newdir_html"] = htmlEncode2($listline["newdir"]);
            
$listline["newdir_url"]  = urlEncode2($listline["newdir"]);
            
$listline["newdir_js"]   = javascriptEncode($listline["newdir"]);
        }

    }

// ---------------
// 3.4 Titan
// Owner, group are modified compared to rule 1
// TO DO: integrate this rule in rule 1 itself
// ---------------
    
elseif (preg_match("/([-dl])([rwxsStT-]{9})[ ]+([0-9]+)[ ]+([a-zA-Z0-9]+)[ ]+([a-zA-Z0-9]+)[ ]+([0-9]+)[ ]+([a-zA-Z]+[ ]+[0-9]+)[ ]+([0-9:]+)[ ](.*)/"$rawlistline$regs) == true) {
//                      dir/file permissions    number      owner             group             size         month       date        time       file
        
$listline["scanrule"]         = "rule-3.4";
        
$listline["dirorfile"]        = "$regs[1]";        // Directory ==> d, File ==> -
        
$listline["dirfilename"]      = "$regs[9]";        // Filename
        
$listline["dirfilename_html"] = htmlEncode2($listline["dirfilename"]);
        
$listline["dirfilename_url"]  = urlEncode2($listline["dirfilename"]);
        
$listline["dirfilename_js"]   = javascriptEncode($listline["dirfilename"]);
        
$listline["size"]             = "$regs[6]";        // Size
        
$listline["owner"]            = "$regs[4]";        // Owner
        
$listline["group"]            = "$regs[5]";        // Group
        
$listline["permissions"]      = "$regs[2]";        // Permissions
        
$listline["mtime"]            = "$regs[7] $regs[8]";    // Mtime -- format depends on what FTP server returns (year, month, day, hour, minutes... see above)

        
if ($listline["dirorfile"] == "d") {
            
$listline["newdir"]      = glueDirectories($directory$listline["dirfilename"]); 
            
$listline["newdir_html"] = htmlEncode2($listline["newdir"]);
            
$listline["newdir_url"]  = urlEncode2($listline["newdir"]);
            
$listline["newdir_js"]   = javascriptEncode($listline["newdir"]);
        }

    }

// ----------------------------------------------
// 4. If nothing matchs, return the raw line
// ----------------------------------------------
    
else {
        
$listline["scanrule"]         = "rule-4";
        
$listline["dirorfile"]        = "u";
        
$listline["dirfilename"]      = $rawlistline;
        
$listline["dirfilename_html"] = htmlEncode2($listline["dirfilename"]);
        
$listline["dirfilename_url"]  = urlEncode2($listline["dirfilename"]);
        
$listline["dirfilename_js"]   = javascriptEncode($listline["dirfilename"]);
    }

// -------------------------------------------------------------------------
// Remove the . and .. entries
// Remove the total line that some servers return
// -------------------------------------------------------------------------
    
if ($listline["dirfilename"] == "." || $listline["dirfilename"] == "..") { return ""; }
    elseif (
substr($rawlistline,0,5) == "total") { return ""; }

// -------------------------------------------------------------------------
// And finally... return the nice list!
// -------------------------------------------------------------------------
    
return $listline;




// -------------------------------------------------------------------------
// Documentation
// -------------------------------------------------------------------------

/*

mholdgate@wakefield.co.uk
11-Jan-2002 11:51

^                Start of String
$                End of string

n*               Zero or more of 'n'
n+               One or more of 'n'
n?               A possible 'n'

n{2}             Exactly two of 'n'
n{2,}            At least 2 or more of 'n'
n{2,4}           From 2 to 4 of 'n'

()               Parenthesis to group expressions
(n|a)            Either 'n' or 'a'

.                Any single character

[1-6]            A number between 1 and 6
[c-h]            A lower case character between c and h
[D-M]            An upper case character between D and M
[^a-z]           Absence of lower case a to z
[_a-zA-Z]        An underscore or any letter of the alphabet

^.{2}[a-z]{1,2}_?[0-9]*([1-6]|[a-f])[^1-9]{2}a+$

A string beginning with any two characters
Followed by either 1 or 2 lower case alphabet letters
Followed by an optional underscore
Followed by zero or more digits
Followed by either a number between 1 and 6 or a character between a and f (Lowercase)
Followed by a two characters which are not digits between 1 and 9
Followed by one or more n characters at the end of a string

// $regs can contain a maximum of 10 elements !! (regs[0] to regs[9])
// To specify what you really want back from ereg, use (). Only what is within () will be returned. See below.

*/

// ----------------------------------------------
// Sample FTP server's output
// ----------------------------------------------

// ---------------
// 1. "Standard" FTP servers output
// ---------------
// ftp.redhat.com
//drwxr-xr-x    6 0        0            4096 Aug 21  2001 pub (one or more spaces between entries)
//
// ftp.suse.com
//drwxr-xr-x   2 root     root         4096 Jan  9  2001 bin
//-rw-r--r--    1 suse     susewww       664 May 23 16:24 README.txt
//
// ftp.belnet.be
//-rw-r--r--   1 BELNET   Mirror        162 Aug  6  2000 HEADER.html
//drwxr-xr-x  53 BELNET   Archive      2048 Nov 13 12:03 mirror
//
// ftp.microsoft.com
//-r-xr-xr-x   1 owner    group               0 Nov 27  2000 dirmap.htm
//
// ftp.sourceforge.net
//-rw-r--r--   1 root     staff    29136068 Apr 21 22:07 ls-lR.gz
//
// ftp.nec.com
//dr-xr-xr-x  12 other        512 Apr  3  2002 pub
//
// ftp.intel.com
//drwxr-sr-x   11 root     ftp          4096 Sep 23 16:36 pub

// ---------------
// 3.1 Windows
// ---------------
//06-10-04  07:56PM                 8175 garantie.html
//04-09-04  04:27PM       <DIR>          images
//05-25-04  09:18AM                 9505 index.html

// ---------------
// 3.2 Netware
// ---------------
// total 0
// - [RWCEAFMS] USER 12 Mar 08 10:48 check.txt
// d [RWCEAFMS] USER 512 Mar 18 17:55 latest


// ---------------
// 3.3 AS400
// ---------------
// RGOVINDAN 932 03/29/01 14:59:53 *STMF /cert.txt
// QSYS 77824 12/17/01 15:33:14 *DIR /QOpenSys/
// QDOC 24576 12/31/69 20:00:00 *FLR /QDLS/
// QSYS 12832768 04/14/03 16:47:25 *LIB /QSYS.LIB/
// QDFTOWN 2147483647 12/31/69 20:00:00 *DDIR /QOPT/
// QSYS 2144 04/12/03 12:49:00 *DDIR /QFileSvr.400/
// QDFTOWN 1136 04/12/03 12:49:01 *DDIR /QNTC/

// ---------------
// 3.4 Titan FTP server
// ---------------
// total 6
// drwxrwx--- 1 owner group 512 Apr 19 11:44 .
// drwxrwx--- 1 owner group 512 Apr 19 11:44 ..
// -rw-rw---- 1 owner group 13171 Apr 15 13:50 default.asp
// drwxrwx--- 1 owner group 512 Apr 19 11:44 forum
// drwxrwx--- 1 owner group 512 Apr 15 13:32 images
// -rw-rw---- 1 owner group 764 Apr 15 11:07 styles.css

// End function ftp_scanline

// **                                                                                  **
// **                                                                                  **
// **************************************************************************************
// **************************************************************************************





// **************************************************************************************
// **************************************************************************************
// **                                                                                  **
// **                                                                                  **

function ftp2http($directory$list_files$htmltags) {

// --------------
// This function calculates the HTTP URL based on the FTP URL
//
// Given the FTP server (ftp.name.com),
//       the directory and file (/directory/file.php)
// It has to return
//       http://www.name.com/directory/file.php
//
// $htmltags indicates whether the url should be returned enclosed in HTML tags or not
//
// For efficiency reasons, this function processes a list of files
// --------------

// -------------------------------------------------------------------------
// Global variables
// -------------------------------------------------------------------------
    
global $net2ftp_globals;

// -------------------------------------------------------------------------
// If no list is supplied, return ""
// -------------------------------------------------------------------------
    
if (sizeof($list_files) == 0) { return ""; }

// -------------------------------------------------------------------------
// Prepare the variables
// -------------------------------------------------------------------------

// Directory
    
if ($directory == "/") { $directory ""; }

    
// Convert single quotes from ' to &#039;
    
if ($htmltags == "no") { $directory javascriptEncode($directory); }
    else                   { 
$directory urlEncode2($directory); }

// Filenames
    
if ($htmltags == "no") { $encoding "dirfilename_js"; }
    else                   { 
$encoding "dirfilename_url"; }    

// Username
    
if ($htmltags == "no") { $username javascriptEncode($net2ftp_globals["username"]); }
    else                   { 
$username htmlEncode2($net2ftp_globals["username"]); }

// -------------------------------------------------------------------------
// "ftp.t35.com" -----> "http://username"  (username = username.t35.com)
// "ftp.t35.net" -----> "http://username"  (username = username.t35.net)
// -------------------------------------------------------------------------
    
if (strpos($net2ftp_globals["ftpserver"], "ftp.t35") !== false) {
        for (
$i=1$i<=sizeof($list_files); $i++) {
            
$URL "http://" $username $directory "/" $list_files[$i][$encoding];
            if (
$htmltags == "no") { $list_links[$i] = $URL; }
            else                   { 
$list_links[$i] = "<a href=\"" $URL "\" target=\"_blank\" title=\"" __("Execute %1\$s in a new window"$list_files[$i][$encoding]) . "\">" $list_files[$i][$encoding] . "</a>"; }
        } 
// end for
    
}

// -------------------------------------------------------------------------
// "ftp-www.earthlink.net/webdocs/directory" -----> "http://home.earthlink.net/~username/directory"
// -------------------------------------------------------------------------
    
elseif (strpos($net2ftp_globals["ftpserver"], "ftp-www.earthlink.net") !== false) {
        if (
strlen($directory) < 8) {
            for (
$i=1$i<=sizeof($list_files); $i++) {
                if (
$htmltags == "no") { $list_links[$i] = "javascript:alert('" __("This file is not accessible from the web") . "');"; }
                else                   { 
$list_links[$i] = "<a title=\"" __("This file is not accessible from the web") . "\" onClick=\"alert('" __("This file is not accessible from the web") . "');\">" $list_files[$i][$encoding] . "</a>"; }
            } 
// end for
        
}
        else {
            
// Transform directory from /webdocs/dir to /dir  --> remove the first 4 characters
            
$directory substr($directory8);

            for (
$i=1$i<=sizeof($list_files); $i++) {
                
$URL "http://home.earthlink.net/~" $username $directory "/" $list_files[$i][$encoding];
                if (
$htmltags == "no") { $list_links[$i] = $URL; }
                else                   { 
$list_links[$i] = "<a href=\"" $URL "\" target=\"_blank\" title=\"" __("Execute %1\$s in a new window"$list_files[$i][$encoding]) . "\">" $list_files[$i][$encoding] . "</a>"; }
            } 
// end for
        
// end if else strlen
    
}

// -------------------------------------------------------------------------
// "ftpperso.free.fr" -----> "http://username.free.fr"
// -------------------------------------------------------------------------
    
elseif (strpos($net2ftp_globals["ftpserver"], "ftpperso.free.fr") !== false) {
        for (
$i=1$i<=sizeof($list_files); $i++) {
            
$URL "http://" $username ".free.fr" $directory "/" $list_files[$i][$encoding];
            if (
$htmltags == "no") { $list_links[$i] = $URL; }
            else                   { 
$list_links[$i] = "<a href=\"" $URL "\" target=\"_blank\" title=\"" __("Execute %1\$s in a new window"$list_files[$i][$encoding]) . "\">" $list_files[$i][$encoding] . "</a>"; }
        } 
// end for
    
}

// -------------------------------------------------------------------------
// "ftp.membres.lycos.fr" -----> "http://membres.lycos.fr/username"
// -------------------------------------------------------------------------
    
elseif (strpos($net2ftp_globals["ftpserver"],"ftp.membres.lycos.fr") !== false) {
        for (
$i=1$i<=sizeof($list_files); $i++) {
            
$URL "http://membres.lycos.fr/" $username $directory "/" $list_files[$i][$encoding];
            if (
$htmltags == "no") { $list_links[$i] = $URL; }
            else                   { 
$list_links[$i] = "<a href=\"" $URL "\" target=\"_blank\" title=\"" __("Execute %1\$s in a new window"$list_files[$i][$encoding]) . "\">" $list_files[$i][$encoding] . "</a>"; }
        } 
// end for
    
}

// -------------------------------------------------------------------------
// "home.planetinternet.be" -----> "http://home.planetinternet.be/~username"
// -------------------------------------------------------------------------
    
elseif (strpos($net2ftp_globals["ftpserver"], "home.planetinternet.be") !== false) {
        for (
$i=1$i<=sizeof($list_files); $i++) {
            
$URL "http://home.planetinternet.be/~" $username $directory "/" $list_files[$i][$encoding];
            if (
$htmltags == "no") { $list_links[$i] = $URL; }
            else                   { 
$list_links[$i] = "<a href=\"" $URL "\" target=\"_blank\" title=\"" __("Execute %1\$s in a new window"$list_files[$i][$encoding]) . "\">" $list_files[$i][$encoding] . "</a>"; }
        } 
// end for
    
}

// -------------------------------------------------------------------------
// "home.planet.nl" -----> "http://home.planet.nl/~username"
// -------------------------------------------------------------------------
    
elseif (strpos($net2ftp_globals["ftpserver"], "home.planet.nl") !== false) {
        for (
$i=1$i<=sizeof($list_files); $i++) {
            
$URL "http://home.planet.nl/~" $username $directory "/" $list_files[$i][$encoding];
            if (
$htmltags == "no") { $list_links[$i] = $URL; }
            else                   { 
$list_links[$i] = "<a href=\"" $URL "\" target=\"_blank\" title=\"" __("Execute %1\$s in a new window"$list_files[$i][$encoding]) . "\">" $list_files[$i][$encoding] . "</a>"; }
        } 
// end for
    
}

// -------------------------------------------------------------------------
// "users.skynet.be" -----> "http://users.skynet.be/username"
// -------------------------------------------------------------------------
    
elseif (strpos($net2ftp_globals["ftpserver"], "users.skynet.be") !== false) {
        for (
$i=1$i<=sizeof($list_files); $i++) {
            
$URL "http://users.skynet.be/" $username $directory "/" $list_files[$i][$encoding];
            if (
$htmltags == "no") { $list_links[$i] = $URL; }
            else                   { 
$list_links[$i] = "<a href=\"" $URL "\" target=\"_blank\" title=\"" __("Execute %1\$s in a new window"$list_files[$i][$encoding]) . "\">" $list_files[$i][$encoding] . "</a>"; }
        } 
// end for
    
}

// -------------------------------------------------------------------------
// "ftp.tripod.com" -----> "http://username.tripod.com"
// -------------------------------------------------------------------------
    
elseif (strpos($net2ftp_globals["ftpserver"], "ftp.tripod.com") !== false) {
        for (
$i=1$i<=sizeof($list_files); $i++) {
            
$URL "http://" $username ".tripod.com" $directory "/" $list_files[$i][$encoding];
            if (
$htmltags == "no") { $list_links[$i] = $URL; }
            else                   { 
$list_links[$i] = "<a href=\"" $URL "\" target=\"_blank\" title=\"" __("Execute %1\$s in a new window"$list_files[$i][$encoding]) . "\">" $list_files[$i][$encoding] . "</a>"; }
        } 
// end for
    
}

// -------------------------------------------------------------------------
// "ftp.wanadoo.es" -----> "http://perso.wanadoo.es/username"
// -------------------------------------------------------------------------
    
elseif (strpos($net2ftp_globals["ftpserver"], "ftp.wanadoo.es") !== false) {
        for (
$i=1$i<=sizeof($list_files); $i++) {
            
$URL "http://perso.wanadoo.es/" $username $directory "/" $list_files[$i][$encoding];
            if (
$htmltags == "no") { $list_links[$i] = $URL; }
            else                   { 
$list_links[$i] = "<a href=\"" $URL "\" target=\"_blank\" title=\"" __("Execute %1\$s in a new window"$list_files[$i][$encoding]) . "\">" $list_files[$i][$encoding] . "</a>"; }
        } 
// end for
    
}

// -------------------------------------------------------------------------
// "perso-ftp.wanadoo.fr" -----> "http://perso.wanadoo.fr/username"
// -------------------------------------------------------------------------
    
elseif (strpos($net2ftp_globals["ftpserver"], "perso-ftp.wanadoo.fr") !== false) {
        for (
$i=1$i<=sizeof($list_files); $i++) {
            
$URL "http://perso.wanadoo.fr/" $username $directory "/" $list_files[$i][$encoding];
            if (
$htmltags == "no") { $list_links[$i] = $URL; }
            else                   { 
$list_links[$i] = "<a href=\"" $URL "\" target=\"_blank\" title=\"" __("Execute %1\$s in a new window"$list_files[$i][$encoding]) . "\">" $list_files[$i][$encoding] . "</a>"; }
        } 
// end for
    
}

// -------------------------------------------------------------------------
// "home.wanadoo.nl" -----> "http://home.wanadoo.nl/username"
// -------------------------------------------------------------------------
    
elseif (strpos($net2ftp_globals["ftpserver"], "home.wanadoo.nl") !== false) {
        for (
$i=1$i<=sizeof($list_files); $i++) {
            
$URL "http://home.wanadoo.nl/" $username $directory "/" $list_files[$i][$encoding];
            if (
$htmltags == "no") { $list_links[$i] = $URL; }
            else                   { 
$list_links[$i] = "<a href=\"" $URL "\" target=\"_blank\" title=\"" __("Execute %1\$s in a new window"$list_files[$i][$encoding]) . "\">" $list_files[$i][$encoding] . "</a>"; }
        } 
// end for
    
}

// -------------------------------------------------------------------------
// wanadoo uk
// "uploads.webspace.freeserve.net" -----> "http://www.username.freeserve.co.uk"
// -------------------------------------------------------------------------
    
elseif (strpos($net2ftp_globals["ftpserver"], "uploads.webspace.freeserve.net") !== false) {
        for (
$i=1$i<=sizeof($list_files); $i++) {
            
$URL "http://www." $username ".freeserve.co.uk" $directory "/" $list_files[$i][$encoding];
            if (
$htmltags == "no") { $list_links[$i] = $URL; }
            else                   { 
$list_links[$i] = "<a href=\"" $URL "\" target=\"_blank\" title=\"" __("Execute %1\$s in a new window"$list_files[$i][$encoding]) . "\">" $list_files[$i][$encoding] . "</a>"; }
        } 
// end for
    
}

// -------------------------------------------------------------------------
// "ftp.xs4all.nl/WWW/directory" -----> "http://www.xs4all.nl/~username/directory"
// -------------------------------------------------------------------------
    
elseif (strpos($net2ftp_globals["ftpserver"], "ftp.xs4all.nl") !== false) {
        if (
strlen($directory) < 4) {
            for (
$i=1$i<=sizeof($list_files); $i++) {
                if (
$htmltags == "no") { $list_links[$i] = "javascript:alert('" __("This file is not accessible from the web") . "');"; }
                else                   { 
$list_links[$i] = "<a title=\"" __("This file is not accessible from the web") . "\" onClick=\"alert('" __("This file is not accessible from the web") . "');\">" $list_files[$i][$encoding] . "</a>"; }
            } 
// end for
        
}
        else {
            
// Transform directory from /WWW/dir to /dir  --> remove the first 4 characters
            
$directory substr($directory4);

            for (
$i=1$i<=sizeof($list_files); $i++) {
                
$URL "http://www.xs4all.nl/~" $username $directory "/" $list_files[$i][$encoding];
                if (
$htmltags == "no") { $list_links[$i] = $URL; }
                else                   { 
$list_links[$i] = "<a href=\"" $URL "\" target=\"_blank\" title=\"" __("Execute %1\$s in a new window"$list_files[$i][$encoding]) . "\">" $list_files[$i][$encoding] . "</a>"; }
            } 
// end for
        
}
    }

// -------------------------------------------------------------------------
// "ftp.server.com/directory/file" -----> "http://www.server.com/directory/file"
// -------------------------------------------------------------------------
    
elseif (preg_match("/ftp.(.+)(.{2,4})/"$net2ftp_globals["ftpserver"], $regs)) {

// Check if the FTP directory contains "htdocs", "httpdocs" or "public_html"
// If it does, the HTTP directory root starts from there on
// Example: /srv/www/htdocs/directory1 ==> /directory1

        
$specialdirectories[1] = "htdocs";
        
$specialdirectories[2] = "httpdocs";
        
$specialdirectories[3] = "public_html";

        for (
$i=1$i<=sizeof($specialdirectories); $i++) {
            
$pos strpos($directory$specialdirectories[$i]);
            if (
$pos !== false) { 
                
$directory substr($directory$pos strlen($specialdirectories[$i])); 
                break;
            }
        }

// Calculate all the URLs on the Browse screen

        
for ($i=1$i<=sizeof($list_files); $i++) {
            
$URL "http://www." $regs[1] . $regs[2] . $directory "/" $list_files[$i][$encoding];
            if (
$htmltags == "no") { $list_links[$i] = $URL; }
            else                   { 
$list_links[$i] = "<a href=\"" $URL "\" target=\"_blank\" title=\"" __("Execute %1\$s in a new window"$list_files[$i][$encoding]) . "\">" $list_files[$i][$encoding] . "</a>"; }
        } 
// end for

    
}

// -------------------------------------------------------------------------
// "http://192.168.0.1/directory/file" can be determined using "192.168.0.1/directory/file":
// -------------------------------------------------------------------------
    
else {

// Check if the FTP directory contains "htdocs", "httpdocs" or "public_html"
// If it does, the HTTP directory root starts from there on
// Example: /srv/www/htdocs/directory1 ==> /directory1

        
$specialdirectories[1] = "htdocs";
        
$specialdirectories[2] = "httpdocs";
        
$specialdirectories[3] = "public_html";

        for (
$i=1$i<=sizeof($specialdirectories); $i++) {
            
$pos strpos($directory$specialdirectories[$i]);
            if (
$pos !== false) { 
                
$directory substr($directory$pos strlen($specialdirectories[$i])); 
                break;
            }
        }

// Calculate all the URLs on the Browse screen
        
for ($i=1$i<=sizeof($list_files); $i++) {
            
$URL "http://" $net2ftp_globals["ftpserver"]. $directory "/" $list_files[$i][$encoding];
            if (
$htmltags == "no") { $list_links[$i] = $URL; }
            else                   { 
$list_links[$i] = "<a href=\"" $URL "\" target=\"_blank\" title=\"" __("Execute %1\$s in a new window"$list_files[$i][$encoding]) . "\">" $list_files[$i][$encoding] . "</a>"; }
        } 
// end for
    
}

    return 
$list_links;

// end function ftp2http

// **                                                                                  **
// **                                                                                  **
// **************************************************************************************
// **************************************************************************************

?>