#!/usr/bin/perl # Diplay errors. use CGI::Carp qw(fatalsToBrowser); # Environment variables. $Root = $ENV{'DOCUMENT_ROOT'}; $agent = $ENV{'HTTP_USER_AGENT'}; $server = $ENV{'SERVER_NAME'}; $server =~ s/www\.//; $cont_len = $ENV{'CONTENT_LENGTH'}; $query = $ENV{'QUERY_STRING'}; $cgi_url = $ENV{'SCRIPT_NAME'}; # Html shortcuts. $textbox = 'bgcolor=f5f5dc text=000080 autoactivate'; $btntag = 'text=000080 insetselection'; # Data paths and other variables. $title ="File Manager"; $data_path = $Root; $data_http = "http://$server"; $core_data_http = $data_http; $core_data_path = $data_path; $allowed_space = '512000'; $allowed_upload = '100000'; $dir_perms = '755'; $cgi_perms = '755'; $file_perms = '644'; # Parse input. read(STDIN, $buffer, $cont_len); if ($query) { $buffer = "$buffer\&$query"; } @pairs = split(/&/,$buffer); foreach (@pairs) { ($name,$value) = split(/=/); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; push(@dir_names, $value) if ($name eq 'dir_names'); push(@file_names, $value) if ($name eq 'file_names'); $FORM{$name} = $value; } # Check for allowed disk space. $du = `du $Root`; $du =~ s/\n/ /g; @terms = split(/\s+/,$du); $used_space = $terms[-2] / 2; $free_space = $allowed_space - $used_space; $allowed_upload = $free_space if ($free_space <= $allowed_upload); $delete_only = 'true' if ($free_space < 30); if ($delete_only eq 'true') { $disabled = "disabled"; } # Get data paths. if ($FORM{'dirname'}) { $dirname = $FORM{'dirname'}; $data_path = "$data_path/$dirname"; $data_http = "$data_http/$dirname"; $cgi = "$cgi_url?dirname=$dirname"; $working_dir = "$dirname/"; } else { $cgi = "$cgi_url?"; } # Begin select action. $action = $FORM{'action'}; if ($action) { if (!-e $data_path) { $report = "$dirname does not exist!"; } elsif ($action eq 'Delete') { &Confirm_Delete; } elsif ($action eq 'Confirm Delete') { &Delete; &list_files; } elsif ($action eq 'removefile') { &deletefile; &list_files; } elsif ($action eq 'confirm_removedir') { &confirm_removedir; } elsif ($action eq 'removedir') { &removedir; &list_files; } elsif ($action eq 'rename') { &rename; &list_files; } elsif ($action eq 'chmod') { &chmod; &list_files; } elsif ($action eq 'Chmod') { &confirm_chmod; } elsif ($action eq 'Move') { &confirm_move; } elsif ($action eq 'Confirm Move') { &move; &list_files; } elsif ($action eq 'viewzip') { &viewzip; } elsif ($action eq 'Cancel' or $action eq 'Exit Without Saving') { $report = "Action aborted! file and directory list."; &list_files; } elsif ( $delete_only eq 'true') { $report = 'This action aborted because you are nearly out of disk space.'; &list_files; } elsif ($action eq 'Beam File') { &beam_file; &list_files; } elsif ($action eq 'edit') { &edit; } elsif ($action eq 'Save Document') { &write; &list_files; } elsif ($action eq 'Preview Changes') { &preview; } elsif ($action eq 'Copy') { &confirm_copy; } elsif ($action eq 'Confirm Copy') { © &list_files; } elsif ($action eq 'Zip') { &confirm_zip; } elsif ($action eq 'Confirm Zip') { &zip; &list_files; } elsif ($action eq 'unzip') { &unzip; &list_files; } elsif ($action eq 'Gzip') { &confirm_gzip; } elsif ($action eq 'Confirm Gzip') { &gzip; &list_files; } elsif ($action eq 'gunzip') { &gunzip; &list_files; } elsif ($action eq 'upload') { &upload; &list_files; } elsif ($action eq 'makedir') { &makedir; &list_files; }} elsif (!$action) { &list_files; } else { if (!-e $data_path) { $report = "$dirname does not exist!"; } else { $report = "File and directory list."; &list_files; }} # Begin html header. sub header { if ($agent =~ /webtv/i) { $fntsz2 = 2; $fntsz3 = 3; } else { $fntsz2 = 3; $fntsz3 = 4; } print "Content-type: text/html\n\n"; print qq~ FM-$dirname
$title
New File New Dir Root Beamer
<font color=red size=6> Javascript Error! Reload Page!</font>
\n~; if ($agent =~ /webtv/i) { print ""; } else { $date = `/bin/date +"%A, %B %d, %Y"`; chomp($date); print "$date"; } print qq~ Free Space: $free_space kb

\n~; } # Begin list files. sub list_files { chdir($data_path); $du = `du`; @pairs = split(/\n/,$du); foreach (@pairs) { @terms = split(/\s+/); $size = $terms[0] / 2; @parts = split(/\.\//, $terms[1]); if ($size < 1000) { $SIZE{$parts[1]} = $size . "k"; } else { $SIZE{$parts[1]} = sprintf("%.2f", $size / 1000) . "m"; }} $ls = `ls -a`; @ls = split(/\s+/,$ls); &header; print qq~
Contents of: ~; if ($dirname) { print "$dirname"; } else { print "Root"; } print ""; if ($action) { print qq~
Action: $report~; } print "

"; foreach $temp_dir (sort @ls) { if (-d $temp_dir && $temp_dir ne '.') { $permissions = &get_stats($temp_dir, 'perms'); $permissions2 = &get_stats($temp_dir, 'perms2'); $LModTime = &get_stats($temp_dir, 'modtime'); $LastMod = &FormatTime($LModTime); if ($temp_dir eq '..') { if ($data_http ne $core_data_http) { @DirParts = split(/\//, $working_dir); pop(@DirParts); $last_part = pop(@DirParts); $prev_dir = ""; foreach $part (@DirParts) { $prev_dir .= "$part/"; } $prev_dir .= $last_part; print qq~

--> Parent Directory~; } } else { print qq~
D
$temp_dir $permissions2 $SIZE{$temp_dir} Rename   Delete~; }}} foreach $temp_file (sort @ls) { if (!-d $temp_file) { $permissions = &get_stats($temp_file, 'perms'); $permissions2 = &get_stats($temp_file, 'perms2'); $LModTime = &get_stats($temp_file, 'modtime'); $LastMod = &FormatTime($LModTime); $size = (-s $temp_file); if ($size > 1000000) { $size = sprintf("%.2f", $size / 1000000) . "m"; } elsif ($size > 1000) { $size = sprintf("%.1f", $size / 1000) . "k"; } else { $size .= "b"; } $Ext = &Check_Ext($temp_file); $no_ext = ''; if ($#ext_parts +1 < 2) { $no_ext = 'yes'; } if ($temp_file =~ /index/i) { $iconbg = 'eeeeee'; $icon = 'X'; $iconcolor = 'ff0000'; } elsif ($Ext =~ /html|htm|shtml|shtm/i) { $iconbg = 'eeeeee'; $icon = 'H'; $iconcolor = '000000'; } elsif ($Ext =~ /txt/i) { $iconbg = 'eeeeee'; $icon = 'T'; $iconcolor = '000000'; } elsif ($Ext =~ /gif|jpg|png|jpeg|bmp|swf/i) { $iconbg = 'cacaff'; $icon = 'I'; $iconcolor = '000000'; } elsif ($Ext =~ /rmf|s3m|xm|mid|mod|au|wav/i) { $iconbg = 'ffcaca'; $icon = 'S'; $iconcolor = '000000'; } elsif ($Ext =~ /zip|gz|tar/i) { $iconbg = 'ffffc0'; $icon = 'Z'; $iconcolor = '000000'; } elsif ($Ext =~ /readme/i) { $iconbg = 'eeeeee'; $icon = '!'; $iconcolor = 'ff0000'; } elsif ($Ext =~ /htaccess|htpasswd/i) { $iconbg = 'eeeeee'; $icon = 'A'; $iconcolor = 'ee0000'; } elsif ($Ext =~ /cgi|pl/i) { $iconbg = 'c0ffc0'; $icon = 'C'; $iconcolor = '000000'; } elsif ($Ext =~ /php|php3|php4/i) { $iconbg = 'c0ffff'; $icon = 'P'; $iconcolor = '000000'; } else { $iconbg = 'eeeeee'; $icon = '?'; $iconcolor = '0000e0'; } print qq~
$icon
$temp_file $permissions2 $size Rename ~; if (-T $temp_file) { print qq~ Edit W3c~; } elsif ($Ext =~ /zip/i) { print qq~ Unzip~; } elsif ($Ext =~ /gz/i) { print qq~ Unzip~; } elsif ($Ext =~ /^(gif|jpg|png|jpeg|tif|bmp|swf)$/i) { use Image::Size; ($x, $y) = imgsize("$data_path/$temp_file"); print qq~$x x $y~; } print qq~ Delete~; }} print qq~



 


\n~; } &footer; # Begin check file extension. sub Check_Ext { $ext_file = $_[0]; @ext_parts = split(/\./, $ext_file); return($ext_parts[-1]); } # Begin edit text file procedure. sub edit { $filename = $FORM{'filename'}; $edit_file = "$data_path/$filename"; $report = &checkname($filename); &header; print ""; if ($report) { $report = $report; &list_files; exit; } if (-e $edit_file) { @LINES = `cat $edit_file`; } else { @LINES = ''; $new = 'yes'; } if ($new) { $Ext = &Check_Ext($filename); if ($Ext =~ /cgi|pl/i) { $start_document = `cat default_cgi.txt`; print qq~
This Is A New CGI File.
The Start Of A Perl Script Is Below For Your Convenience.

~; } elsif ($Ext =~ /html|htm|shtml|shtm/i) { $start_document = `cat default_html.txt`; print qq~
This Is A New HTML File.
The Start Of An HTML Document Is Below For Your Convenience.

~; } else { print "
This Is A New File, Enter Your Text Below.

"; }} else { if (-T $edit_file) { print "
Modify $filename as needed below:

"; } else { $report = "$filename is not a text file, only text can be edited."; &list_files; } print ""; } print qq~




Alternate Filename:

Search For:


Replace With:

NOTE! Search & replace is case sensitive!
Example: word is not the same as WORD




~; &footer; } # Begin get file stats. sub get_stats { $filename = $_[0]; $type = $_[1]; if ($type eq "perms2") { $mode = (stat($filename))[2]; $perms = sprintf "%3o", $mode & 07777; $stat = $perms; } if ($type eq "modtime") { $stat = (stat($filename))[9]; } return $stat; } # Begin time format. sub FormatTime { $thetime = $_[0]; @dow = qw(Sun Mon Tue Wed Thu Fri Sat); @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); ($sec,$min,$hour,$day,$month,$year,$wday) = (localtime($thetime))[0,1,2,3,4,5,6]; if ($day == 1 || $day == 21 || $day == 31) { $end = 'st'; } elsif ($day == 2 || $day == 22) { $end = 'nd'; } elsif ($day == 3 || $day == 23) { $end = 'rd'; } else { $end = 'th'; } if ($year < 2000) { $year = $year + 1900; } $min = sprintf("%02d", $min); $sec = sprintf("%02d", $sec); $FTime = "$dow[$wday], $months[$month] $day$end, $year $hour:$min:$sec"; } # Begin list directories. sub DirList { $ls = `ls -R $Root`; @pairs = split(/\n/,$ls); @Dirs = (); foreach (@pairs) { if (m/$Root/) { s#$Root/?(.)#$1#; chop; push(@Dirs, $_); }} foreach $Dir (sort @Dirs) { $selected = ''; if ($Dir eq $dirname) { $selected = "selected"; } print qq~

Copy these $sel_files$sel_both$sel_dirs from: $stay_dir...

to the directory in the text box below.

Enter new names in the text boxes or
leave them blank to keep the same name.
~; foreach $cp_dir (@dir_names) { print qq~
$cp_dir ~; } foreach $cp_file (@file_names) { print qq~
$cp_file ~; } print qq~

  Stay in $stay_dir after copy?

~; &footer;} else { $report = 'You must choose 1 or more files or directories to use copy.'; &list_files; } } # Begin copy. sub copy { $last_dir = $FORM{'last_dir'}; $new_dir = $FORM{'new_dir'}; $copied = &checkname($new_dir); if ($last_dir eq $dirname) { $show_last_dir = ""; } elsif (!$last_dir && $dirname eq $new_dir) { $show_last_dir = "Root/"; } elsif ($dirname eq $new_dir) { $show_last_dir = "$last_dir/"; } if (!$new_dir) { $show_new_dir = "Root/"; } if ($last_dir) { $cp_dir = "$last_dir/"; } if ($new_dir) { $new_dir_path = "$core_data_path/$new_dir"; } else { $new_dir_path = $core_data_path; } if (!-e $new_dir_path) { `mkdir $new_dir_path`; `chmod $dir_perms $new_dir`; if (-e $new_dir_path) { $copied .= "Directory: $new_dir has been created.
"; } else { $copied .= "Directory: $new_dir was not created.
"; }} foreach $cp_file (@file_names) { $new_file = $FORM{$cp_file}; if ($new_dir && $new_file) { $new_full_name = "$new_dir/$new_file"; } elsif ($new_dir && !$new_file) { $new_full_name = "$new_dir/$cp_file"; } elsif (!$new_dir && $new_file) { $new_full_name = $new_file; } else { $new_full_name = $cp_file; } $copied .= &checkname($new_full_name); if ($dirname eq $last_dir) { $cp_file_name = $cp_file; if ($dirname eq $new_dir) { if ($new_file) { $new_file_name = $new_file; } else { $new_file_name = $cp_file; }} else { $new_file_name = $show_new_dir . $new_full_name; }} else { $cp_file_name = $show_last_dir . $cp_file; if ($dirname eq $new_dir) { if ($new_file) { $new_file_name = $new_file; } else { $new_file_name = $cp_file; }} else { $new_file_name = $new_full_name; }} $cp_file_path = "$core_data_path/" . $cp_dir . $cp_file; $new_file_path = "$core_data_path/$new_full_name"; if (-e $new_file_path) { $copied .= "$new_file_name already exists.
"; } else { `cp $cp_file_path $new_file_path`; if (-e $new_file_path) { $copied .= "$cp_file_name has been copied to $new_file_name.
"; } else { $copied .= "$cp_file_name was not copied.
"; }}} $report = $copied; } # Begin confirm move. sub confirm_move { $sel_files = ''; $sel_dirs = ''; if (@file_names) { $sel_files = "files"; } if (@dir_names) { $sel_dirs = "directories"; } if (@file_names && @dir_names) { $sel_both = " and "; } if ($dirname eq '') { $stay_dir = "Root Directory"; } else { $stay_dir = $dirname; } if ($sel_files || $sel_dirs) { &header; print qq~

Move these $sel_files$sel_both$sel_dirs from: $stay_dir...

to the directory in the text box below.

~; foreach $mv_dir (@dir_names) { print qq~$mv_dir
~; } foreach $mv_file (@file_names) { print qq~$mv_file
~; } print qq~

Stay in $stay_dir after move?

~; &footer; } else { $report = 'You must choose 1 or more files or directories to use move.'; &list_files; } } # Begin move. sub move { $last_dir = $FORM{'last_dir'}; $new_dir = $FORM{'new_dir'}; $moved = &checkname($new_dir); if ($last_dir eq $dirname) { $show_last_dir = ""; } elsif (!$last_dir && $dirname eq $new_dir) { $show_last_dir = "Root/"; } elsif ($dirname eq $new_dir) { $show_last_dir = "$last_dir/"; } if (!$new_dir) { $show_new_dir = "Root/"; } if ($last_dir) { $mv_last_dir = "$last_dir/"; } if ($new_dir) { $new_dir_path = "$core_data_path/$new_dir"; } else { $new_dir_path = $core_data_path; } if (!-e $new_dir_path) { `mkdir $new_dir_path`; `chmod $dir_perms $new_dir`; if (-e $new_dir_path) {$copied .= "Directory: $new_dir has been created.
";} else { $moved .= "Directory: $new_dir was not created.
"; }} foreach $mv_file (@file_names) {# create new file path from choices if ($new_dir) { $new_full_name = "$new_dir/$mv_file"; } else { $new_full_name = $mv_file; } $moved .= &checkname($new_full_name); if ($dirname eq $last_dir) { # stayed in the same directory $mv_file_name = $mv_file; if ($dirname eq $new_dir) { $new_file_name = $mv_file; } else { $new_file_name = $show_new_dir . $new_full_name; }} else { # changed to a different directory $mv_file_name = $show_last_dir . $mv_file; if ($dirname eq $new_dir) { $new_file_name = $mv_file; } else { $new_file_name = $new_full_name; }} $mv_file_path = "$core_data_path/" . $mv_last_dir . $mv_file; $new_file_path = "$core_data_path/$new_full_name"; if (-e $new_file_path) { $moved .= "$new_file_name already exists.
"; } else { `mv $mv_file_path $new_file_path`; if (-e $new_file_path) { $moved .= "$mv_file_name has been moved to $new_file_name.
"; } else { $moved .= "$mv_file_name has not been moved.
"; }}} $report = $moved; } # Begin confirm delete. sub Confirm_Delete { $sel_files = ''; $sel_dirs = ''; if (@file_names) { $sel_files = "files"; } if (@dir_names) { $sel_dirs = "directories"; } if (@file_names && @dir_names) { $sel_both = " and "; } &header; print qq~

Are you sure you want to delete these $sel_files$sel_both$sel_dirs?

~; foreach $del_dir (@dir_names) { print qq~ $del_dir
~; } foreach $del_file (@file_names) { print qq~ $del_file
~; } print qq~

~; &footer; } # Begin delete. sub Delete { foreach $del_dir (@dir_names) { $del_dir_path = "$data_path/$del_dir"; if (-e $del_dir_path) { `rmdir $del_dir_path`; if (-e $del_dir_path) { $removed .= "$del_dir was not removed.
"; } else { $removed .= "$del_dir was removed.
"; }} else { $removed .= "$del_dir was not found.
"; }} foreach $del_file (@file_names) { $del_file_path = "$data_path/$del_file"; if (-e $del_file_path) { `rm $del_file_path`; if (-e $del_file_path) { $removed .= "$del_file was not removed.
"; } else { $removed .= "$del_file was removed.
"; }} else { $removed .= "$del_file was not found.
"; }} $report = $removed; } # Begin confirm remove directory. sub confirm_removedir { $remove_dir = $FORM{'directory'}; $dir_path = "$data_path/$remove_dir"; $working_path = $working_dir . $remove_dir; if (-e $dir_path) { chdir($dir_path); $du = `du`; @pairs = split(/\n/, $du); $testls = `ls -a`; @testls = split(/\s+/, $testls); foreach $pair (@pairs) { @terms = split(/\s+/,$pair); @parts = split(/\.\//,$terms[1]); if ($parts[1] ne "") { push (@rmDirs, $parts[1]); }} if ($#rmDirs +1 > 0 || $#testls -1 > 0) { &header; print qq~

Are you sure you want to delete $working_path and everything in it?

$working_path
~; $dirls = `ls -a $dir_path`; @dirls = split(/\s+/, $dirls); foreach $file (sort @dirls) { if (!-d "$dir_path/$file") { print qq~$file
~; }} print "

"; foreach $Dir (sort @rmDirs) { print qq~$working_path/$Dir
~; $rmls = `ls -a $Dir`; @rmls = split(/\s+/, $rmls); foreach $file (sort @rmls) { if (!-d "$dir_path/$Dir/$file") { print qq~$file
~; }} print "

"; } print qq~

~; &footer; } else { `rmdir $dir_path`; if (-e $dir_path) { $report = "$remove_dir was not removed."; &list_files; } else { $report = "$remove_dir was removed. It was empty."; &list_files; }}} else { $report = "$remove_dir was not found."; &list_files; } } # Begin remove directory. sub removedir { $delete_dir = $FORM{'delete_dir'}; $delete_path = "$data_path/$delete_dir"; if (-e $delete_path) { `rm -r $delete_path`; if (-e $delete_path) { $report = "$delete_dir was not removed."; } else { $report = "$delete_dir and everything in it was removed."; }} else { $report = "$delete_dir was not found."; } } # Begin preview. sub preview { print "Content-type: text/html\n\n"; print $FORM{'file'}; } # Begin write text file procedure: sub write { $filename = $FORM{'filename'}; $report = &checkname($filename); $Ext = &Check_Ext($filename); $new_path = "$data_path/$filename"; $new_http = "$data_http/$filename"; if (!-e $new_path) { $new = 'yes'; } unless ($report) { $FORM{'file'} =~ s/\cM\n/\n/g; open FILE,">$new_path"; print FILE "$FORM{'file'}"; close FILE; if ($new) { if ($Ext =~ /cgi|pl/i) { $perms = $cgi_perms; } else { $perms = $file_perms; } `chmod $perms $new_path`; }} if ($new) { if (-e $new_path) { $report .= "$filename has been created.";} else { $report .= "$filename has not been created."; }} else { $report .= "$filename has been edited."; } $report = $report; } # Begin quick delete routine. sub deletefile { $file = $FORM{'file'}; $file_path = "$data_path/$file"; unlink($file_path); $report = "$file has been removed."; } # Begin viewzip routine. sub viewzip { $file = $FORM{'file'}; $file_path = "$data_path/$file"; &header; print qq~

Unzip these files?

~; @data = `cd $data_path; unzip -l $file_path`; foreach $i (@data) { print qq~$i
~; } print qq~

~; &footer; } # Begin unzip routine. sub unzip { $file = $FORM{'file'}; $file_path = "$data_path/$file"; @data = `cd $data_path; unzip $file_path`; shift(@data); foreach $i (@data) { $zipdata .= "$i
"; } $report = "$zipdata$file has been unzipped."; } # Begin zip confirm. sub confirm_zip { $sel_files = ''; $sel_dirs = ''; if (@file_names) { $sel_files = "files"; } if (@dir_names) { $sel_dirs = "directories"; } if (@file_names && @dir_names) { $sel_both = " and "; } if ($sel_files || $sel_dirs) { &header; print qq~

Zip these $sel_files$sel_both$sel_dirs?

~; foreach $zip_dir (@dir_names) { print qq~$zip_dir
~; } foreach $zip_file (@file_names) { print qq~$zip_file
~; } print qq~

Filename:

~; &footer; } else { $report = 'You must choose one or more files or directories to use zip.'; &list_files; } } # Being zip. sub zip { $file = $FORM{'file'}; $file_path = "$data_path/$file"; @data = `cd $data_path; zip -r $file @file_names @dir_names`; foreach $i (@data) { $zipdata .= "$i
"; } if (-e $file_path) { $report = "$zipdata$file has been created."; } else { $report = "$zipdata$file has not been created."; } } # Begin gzip confirm. sub confirm_gzip { $sel_files = ''; if (@file_names) { $sel_files = "files"; } if ($sel_files) { &header; print qq~

Gzip these $sel_files?

~; foreach $zip_file (@file_names) { print qq~$zip_file
~; } print qq~
~; &footer; } else { $report = 'You must choose one or more files to use Gzip.'; &list_files; } } # Begin gzip. sub gzip { $file = $FORM{'file'}; @data = `cd $data_path; gzip -9 $file @file_names`; foreach $i (@file_names) { $zipdata .= "$i has been Gzipped
"; } $report = $zipdata; } # Begin gunzip routine. sub gunzip { $file = $FORM{'file'}; $file_path = "$data_path/$file"; @data = `cd $data_path; gunzip $file_path`; shift(@data); foreach $i (@data) { $zipdata .= "$i
"; } $report = "$file has been Gunzipped."; } # Begin beam routine. sub beam_file { $new_file = $FORM{'new_file'}; $new_name = $FORM{'new_name'}; use LWP::Simple; $data =~ s/`|;|\s//g; $data = get($new_file); if (!$data) { $report = 'Nothing was beamed in.'; } else { `mkdir $data_path; chmod $dir_perms $data_path` if (!-e $data_path); open FILE,">$data_path/$new_name"; print FILE $data; close FILE; if ($new_name =~ /(pl)|(cgi)$/i) { $perms = $cgi_perms; } else { $perms = $file_perms; } `chmod $perms $data_path/$new_name`; $report = "$new_file
was beamed and renamed: $new_name"; } } # Begin html footer. sub footer { print "
"; }