script translation
Posted: Tue Aug 26, 2003 11:30 am
Can anyone translate this perl script to php for me?
Code: Select all
########################################################
# URL of this script
$script = "http://theboxnet.servehttp.com/browse/tabs.cgi";
########################################################
# Base absolute path and base URL, $basedir SHOULD point to $baseurl
$basedir = "c:\server\www";
$baseurl = "theboxnet.servehttp.com";
########################################################
# Header and Footer files
$header = "c:\server\www\browse\header.txt";
$footer = "c:\server\www\browse\footer.txt";
########################################################
# URL to images directory, relative or absolute
$imagedir = "images";
########################################################
# File types with their own images, the images should be named їtype].gif
@filetypes = ("avi", "bmp", "gif", "html", "jpg", "mov", "mpeg", "swf", "txt", "zip");
########################################################
# To enable internal text viewer, set to 1, or 0 otherwise
$textviewer= 1;
########################################################
# File types which uses the internal text viewer
@texttypes = ("txt", "msg", "ini");
# END OF CONFIG
########################################################
########################################################
# By the way, to Perl programmers:
# I'm not quite familiar with directory handling
# I used a quite silly method to determine which is a dir
# and which is a file (but it works, of course)
# If you have a better method, please email me
($action, $vir_path) = split(/&/, $ENV{'QUERY_STRING'});
$vir_path =~ s/%(їa-fA-F0-9]їa-fA-F0-9])/pack("C", hex($1))/eg;
if ($ENV{'QUERY_STRING'} ne "") {
if ($vir_path =~ /^\.\./ || $vir_path =~ /\/\.\.\// || $vir_path =~ /\.\.$/) {
print "Content-type: text/html\n\n";
print &header;
print "<font size=3 face=arial><b>You are not allowed to access a higher level directory with ..</b></font>\n";
print &footer;
exit;
}
else {
$path = "$basedir/$vir_path";
}
}
else {
$path = $basedir;
$action = "browse";
}
$thisdir = ($vir_path eq "") ? "" : "$vir_path/";
($action eq "browse") ? &browse : &get;
###############
sub browse {
print "Content-type: text/html\n\n";
opendir(BASE, $path);
@names = readdir(BASE);
closedir(BASE);
foreach $name (@names) {
$isdir = opendir(DIR, "$basedir/$thisdir$name");
if ($isdir == 1) {
push(@dirs, $name) unless ($name eq '.' || $name eq '..');
}
else {
push(@files, $name);
}
closedir(DIR);
(undef, undef, undef, undef, undef, undef, undef,
$size, undef, $modified, undef, undef, undef) = stat("$basedir/$thisdir$name");
($min, $hr, $day, $mon, $yr) = (localtime($modified))ї1,2,3,4,5];
$min = "0$min" unless $min >= 10;
$hr = "0$hr" unless $hr >= 10;
$day = "0$day" unless $day >= 10;
$mon = "0$mon" unless $mon >= 10;
$filesize{$name} = $size;
$last_mod{$name} = "$mon/$day/$yr $hr:$min";
}
print &header;
$thisdir =~ s/ /\%20/g;
print "<table border=0 cellspacing=1 width=80%>\n";
print "<tr>\n";
print "<td align=center width=50%><font size=2 face=arial><b>Name</b></font>\n";
print "<td align=center width=30%><font size=2 face=arial><b>Last Modified</b></font>\n";
print "<td align=center width=20%><font size=2 face=arial><b>Size (Bytes)</b></font>\n";
print "</tr>\n";
print "<tr>\n<td colspan=3>\n";
print "<font size=2 face=arial><b>\n";
if ($vir_path ne "") {
$temp = $vir_path =~ tr#/#/#;
if ($temp > 0) {
$pardir = "?browse&".substr($vir_path, 0, rindex($vir_path, "/"));
}
else {
$pardir = "";
}
print "<img src="$imagedir/up.gif" align="absbottom"><a href="$script$pardir">Parent Directory</a>\n";
}
print "</b></font>\n";
print "</td>\n</tr>\n";
foreach $dir (sort @dirs) {
$dir_url = $dir;
$dir_url =~ s/ /\%20/g;
print "<tr>\n<td>\n";
print "<font size=2 face=arial><b>\n";
print "<img src="$imagedir/dir.gif" align="absbottom"><a href="$script?browse&$thisdir$dir_url">$dir</a></b></font>\n";
print "</td>\n";
print "<td align=right><font size=2 face=arial><b>$last_mod{$dir}</b></font></td>\n";
print "<td align=right><font size=2 face=arial><b>$filesize{$dir}</b></font></td>\n";
print "</tr>\n";
}
foreach $file (sort @files) {
$ext = substr($file, rindex($file, "\.")+1);
$type = "file";
foreach $filetype (@filetypes) {
if (lc($ext) eq $filetype) {$type = $filetype;}
}
$file_url = $file;
$file_url =~ s/ /\%20/g;
print "<tr>\n<td>\n";
print "<font size=2 face=arial><b>\n";
print "<img src="$imagedir/$type.gif" align="absbottom"><a href="$script?get&$thisdir$file_url">$file</a></b></font>\n";
print "</td>\n";
print "<td align=right><font size=2 face=arial><b>$last_mod{$file}</b></font></td>\n";
print "<td align=right><font size=2 face=arial><b>$filesize{$file}</b></font></td>\n";
print "</tr>\n";
}
print "</table>\n";
print &footer;
}
sub get {
if ($textviewer) {
$ext = substr($vir_path, rindex($vir_path, "\.")+1);
foreach $type (@texttypes) {
if ($type eq $ext) {
$flag = 1;
}
}
if ($flag) {
print "Content-type: text/html\n\n";
print &header;
open(FILE, "$basedir/$vir_path");
print "<font face=Courier size=2>\n";
$line = join("<br>", <FILE>);
$line =~ s/ / /g;
print $line;
print "</font>\n";
close(FILE);
print &footer;
exit;
}
}
print "Location: $baseurl/$vir_path\n\n";
}
##############################
# Return the Header contents
sub header {
open(HEAD, $header) or die("Cannot open $header");
@lines = <HEAD>;
close(HEAD);
return @lines;
}
##############################
# Return the Footer contents
sub footer {
open(FOOT, $footer) or die("Cannot open $footer");
@lines = <FOOT>;
close(FOOT);
return @lines;
}