#!/usr/local/bin/perl
# The top line points to the perl location on your server.
use strict;
use CGI;
$CGI::HEADERS_ONCE=1;
use CGI::Carp qw(fatalsToBrowser);
# You must have CGI.PM module on your server (most servers do.)
use Net::FTP;
# You must have Net::FTP, Net::Cmd and Net::Config modules
# installed on your server. If you don't have enough priviledges
# to install them in system path, make a directory called Net in
# the directory where your ftp2.cgi script will be and put FTP.pm,
# Cmd.pm and Config.pm there. These Net libraries can be got from
# http://www.gbarr.demon.co.uk/
##################################################################
# WWW FTP CLIENT
# by Maxim Bakin
# Copyright 2000-2001
# If you use this script please make a link to Maximus Site.
# http://www.maximus.f2s.com
#
# I will not be liable for indirect, special, or consequential
# damages (or any loss of revenue, profits, or data) arising in
# connection with this script.
# Please feel free to modify and distribute this program as well as
# send me any critic, comments and improvements.
# Maxim Bakin
# max@maximus.f2s.com
#
# VERSION 1.1
# Last modified 03-July-2001
# Changes from version 1.0:
# 1) Added ability to create and edit remote text files.
# 2) Fixed some minor bug with directory listings on Windows NT box.
# 3) Configurable name and place of temporary directory
#
# I expect to rewrite this script from scratch in the near future.
# Changes will be in the structure of the script as well as I think
# I'll try to make FTP operations built-in instead of relying on
# modules that unfortunately do not come with standard Perl distribution,
# and installing them on the hosting company's server is a kind of a
# headache.
###################################################################
# HERE ARE CONFIGURATION VARIABLES
my $upload_files=6; #The number of upload fields
my $temp_dir="ajha9svft"; #Path to temporary directory with uploads/downloads
#I prefer using absolute path but it can be left
#with a relative path just as now.
#It's recommended to make it somewhere outside your
#cgi-bin directory, so please change this default.
my $www_temp_dir="ajha9svft"; #Path from the web to temporary directory
#say you have ftp2.cgi at http://server2047.virtualave.net/83v-9s/ftp2.cgi
#and temporary dir at http://server2047.virtualave.net/83v-9s/ajha9svft
#so you can write "../ajha9svft" or full "http://www.yoursite.com/ajha9svft"
#Certainly it's connected with $temp_dir variable
####################################################################
my $query=new CGI;
my $ftp;
my $site=$query->param("site");
my $user=$query->param("user");
my $pass=$query->param("pass");
my $dir=$query->param("dir")? $query->param("dir") : "";
my $action=$query->param("action");
print $query->header();
print "
EOT
}
sub action(){
my $action=shift;
if ($action eq "view"){
mkdir("$temp_dir", 0777) unless (-e "$temp_dir");
unlink <$temp_dir/*>;
my $name=$query->param("name");
my $new_name;
login_remote($site, $user, $pass, $dir);
if ($name=~/\.pl|\.cgi|\.php.|\.shtml|\.css$/){
($new_name=$name)=~s/(.*)(\..*)$/$1/;
$new_name.=".txt";
}
else {
$new_name=$name;
}
$ftp->get("$name", "$temp_dir/$new_name") or do {error_here("file_not_got", $name); return;};
print "";
}
if ($action eq "edit"){
my $sure=$query->param("sure");
if ($sure eq "yes"){
my $name=$query->param("name");
my $file_content=$query->param("file_content");
open(FILE, ">$temp_dir/$name") or die "Cannot open file: $!";
print FILE $file_content;
close(FILE) or die "Cannot close file: $!";
login_remote($site, $user, $pass, $dir);
if ($ftp->put("$temp_dir/$name")){
print "$name has been modified. \n";
}
else{
&error_here("bad_edit");
}
unlink ("$temp_dir/$name");
}
else {
my $name=$query->param("name");
return unless $name;
mkdir("$temp_dir", 0777) unless (-e "$temp_dir");
unlink <$temp_dir/*>;
login_remote($site, $user, $pass, $dir);
$ftp->get("$name", "$temp_dir/$name") or do {error_here("file_not_got", $name); return;};
open(FILE, "$temp_dir/$name") or die "Cannot open file: $!";
print <<"EOT";
Edit
EOT
exit;
}
}
if ($action eq "chmod"){
my @names=$query->param("name");
if (@names){
if ($query->param("new_mode_$names[0]")){
login_remote($site, $user, $pass, $dir);
foreach my $name(@names){
my $mode=$query->param("new_mode_$name");
#here we check whether the user entered a valid mode
unless ($mode=~/[0-7][0-7][0-7]/){
error_here("bad_mode", $name, $mode);
next;
}
my $response=($ftp->site("CHMOD $mode $name"));
#site method returns most significant digit of the response code,
#so if it is not 2 the command wasn"t ok
unless ($response == 2){
error_here("file_not_chmodded", $name, $mode);
next;
}
print "$name has been chmoded to $mode. ";
}
}
else {
print <<"EOT";
Please make sure you are not making a mistake.
Only checked files will be chmodded.