| Category: | CGI |
| Author/Contact Info | George_Sherston |
| Description: | I wrote this script so that I can edit my scripts directly on my remote web space, rather than going through all the business of uploading, changing the shebang line from what I need to run locally to what I need to run in the web space etc etc. It also backs up the script each time I edit it. I'm posting this as much in the hope of getting some comments on overall programming style as to offer something that others might find useful (though it wd be great if somebody did find it useful). I'd be particularly grateful for comments on the security aspects of this script, as I have only a very limited grasp of the issues, and am not at all sure it's secure enough to go on my web space. |
#!/usr/bin/perl -w
use strict;
use Cwd;
use CGI qw/:standard/;
use DBI;
use File::Find;
my $ext = 'pl';
my @dirs = (getcwd);
my $q = new CGI;
&CheckPass;
sub
CheckPass
{
# check the password and user name,
# send log in page if they are wrong or absent
# go to &Main of they are ok
if ($q->param('UserName') and $q->param('PassWord')) {
my $dbh = DBI->connect("DBI:mysql:database=mydb");
my $ref = $dbh->selectcol_arrayref("SELECT * FROM " . $q->para
+m('UserName') . " WHERE PassWord = " . $q->param('PassWord'));
$dbh->disconnect;
if ($ref->[0]) {
&Main;
}
else {
&LogInPage('Log In Failed',$q->param('Action') ? $q->param
+('Action') : 'FileTree',$q->param('File'));
}
}
else {
&LogInPage('Hello There',$q->param('Action') ? $q->param('Acti
+on') : 'FileTree',$q->param('File'));
}
}
sub
Main
{
# look at the 'Action' parameter, and depending
# on what it is, send the file tree page, send the script editor,
# send the script editor with lines wrapped / unwrapped,
# or save the file
if ($q->param('Action') eq "FileTree") {
my $cookie = $q->cookie(
-name=>'editor',
-value=>$q->param('UserName'),
-expires=>'+10y',
);
print $q->header(
-cookie=>$cookie # set the cookie in case th
+is is the first visit
);
print $q->start_html;
find(\&GetFileTree,@dirs);
print $q->end_html;
}
elsif ($q->param('Action') eq "GetScript") {
(my $Table = $q->param('File')) =~ s/\//_/g;
$Table =~ s/\.pl//;
my $dbh = DBI->connect("DBI:mysql:database=hudex");
my $sth = $dbh->do("DESCRIBE $Table");
unless ($sth) { # create the back up db tabl
+e unless it already exists
$dbh->do("CREATE TABLE $Table (ID MEDIUMINT UNSIGNED PRIMA
+RY KEY NOT NULL AUTO_INCREMENT, Script LONGTEXT, Date BIGINT UNSIGNED
+)");
}
open FILE, $q->param('File') or die "can't open file $!";
read FILE, my $buffer, -s(FILE);
$buffer =~ s/'/\\'/g;
$dbh->do("INSERT INTO $Table VALUES (NULL, '$buffer', " . time
+ . ")"); # back up the script
&EditPage($q->param('File'));
}
elsif ($q->param('Action') eq "Wrap") {
my $Wrap = $q->param('WrapStatus') eq 'OFF' ? 'PHYSICAL' : 'OF
+F';
&EditPage($q->param('File'),$Wrap,'ReSend');
}
elsif ($q->param('Action') eq "Save") {
my @Script = split /\n/, $q->param('Script');
my $File = $q->param('File');
open FILE, ">$File" or die "can't open $File $!";
my %subhash = ( # respect to japhy's excellent r
+egex book for this move
'<' => '<',
'>' => '>',
);
for (@Script) {
chomp;
$_ = substr($_, 5, (length $_) - 5) if /\A\d/;
s/(>|<)/$subhash{$1}/g; # resto
+re html tags for saving
print FILE $_,"\n";
}
close FILE;
&EditPage($q->param('File'));
}
else {
&LogInPage('Hello There',$q->param('Action') ? $q->param('Acti
+on') : 'FileTree',$q->param('File'));
}
}
sub
EditPage
{
# send the script to a browser window in the form of a <textarea> fiel
+d
# optional arguments: $Wrap toggles line wrapping on and off;
# $Send tells us whether to send back the contents of $q->param('File'
+)
# unchanged or retrieve them from $File
my ($File, $Wrap, $Send) = @_;
$Wrap = $Wrap ? $Wrap : 'OFF';
(my $Title = $File) =~ s/(.{1})/$1 /g;
$Title = 'E D I T :: ' . $Title;
print $q->header;
print $q->start_html(
-title=>$Title,
);
unless ($Send eq 'ReSend') {
open FILE, "$File" or die "couldn't open $File, $!";
print "WARNING: FILE MAY BE TOO LARGE TO BACK UP!<BR>" if -s(F
+ILE) > 2e32; # warn if file is larger than BIGTEXT (which is most
+unlikely)
}
print $q->start_form(
-action=>'editor.pl',
-method=>'POST',
-name=>'mainform',
);
print $q->submit(
-name=>'Action',
-value=>'Wrap',
);
print $q->submit(
-name=>'Action',
-value=>'Save',
);
$q->param(
-name=>'File',
-value=>$File
);
print $q->hidden('File');
print $q->hidden('UserName');
print $q->hidden('PassWord');
$q->param(
-name=>'WrapStatus',
-value=>$Wrap
);
print $q->hidden('WrapStatus');
print "<textarea name=\"Script\" wrap=\"$Wrap\" style=\"overflow:a
+uto;width:100%;height:95%\">";
my %subhash = ( # respect to japhy's excellent regex
+ book for this move
'<' => '<',
'>' => '>',
);
if ($Send eq 'ReSend') {
(my $Script = $q->param('Script') )=~ s/(<|>)/$subhash{$1}/g;;
print $Script;
}
else {
my $counter = 1;
for (<FILE>) {
s/(<|>)/$subhash{$1}/g; # make sure there
+ aren't any html tags inside the textarea
print $counter, (" " x (4 - int(log($counter)/log(10)))),
+$_ ;
$counter++;
$counter = $counter < 10000 ? $counter : 1;
}
}
print '</textarea>';
print $q->end_form();
print $q->end_table();
print $q->end_html;
}
sub
LogInPage
{
# send the log in page. Arguments:
# $Header sends a friendly or informative message to the user;
# $Action tells the script what to do after the log in is successful;
# $File tells the script what file to do it with
my $UserName = $q->param('UserName') ? $q->param('UserName') : $q-
+>cookie('editor');
my ($Header,$Action,$File) = @_;
print $q->header;
print $q->start_html('E D I T O R :: L O G :: I N ');
print '<table width="100%" height="100%"><tr valign=middle><td ali
+gn=center>';
print $q->start_form(
-action=>'editor.pl',
-method=>'POST',
);
print "<h3>$Header:</h3>";
print 'User Name ';
print $q->textfield(
-name=>'UserName',
-value=>$UserName,
);
print '<BR>Password ';
print $q->textfield('PassWord');
print '<BR>';
print $q->submit(
-value=>'Log In',
);
$q->param(
-name=>'Action',
-value=>$Action,
);
print $q->hidden('Action');
$q->param(
-name=>'File',
-value=>$File,
);
print $q->hidden('File');
print $q->end_form();
print '</td></tr></table>';
print $q->end_table();
print $q->end_html;
}
sub
GetFileTree
{
print "<div style=\"margin-left:$Posn\"><a href=\"editor.pl?Action
+=GetScript&File=$File::Find::name\" target=\"$File::Find::name\">$Fil
+e::Find::name</a><\div>" if /\.$ext\Z/;
}
|
|
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Easy Script Editor
by pixel (Scribe) on Oct 05, 2001 at 19:17 UTC | |
by George_Sherston (Vicar) on Oct 05, 2001 at 21:33 UTC | |
by pixel (Scribe) on Oct 06, 2001 at 02:18 UTC | |
|
Re: Easy Script Editor
by ajt (Prior) on Oct 05, 2001 at 18:16 UTC | |
|
Re: Easy Script Editor
by jj808 (Hermit) on Oct 05, 2001 at 19:01 UTC | |
|
Re: Easy Script Editor
by mandog (Curate) on Oct 06, 2001 at 09:20 UTC | |
|
Update: Re: Ongoing chmod lack of success
by George_Sherston (Vicar) on Oct 06, 2001 at 21:20 UTC |