#!/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->param('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('Action') : '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 this 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 table unless it already exists $dbh->do("CREATE TABLE $Table (ID MEDIUMINT UNSIGNED PRIMARY 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' : 'OFF'; &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 regex book for this move '<' => '<', '>' => '>', ); for (@Script) { chomp; $_ = substr($_, 5, (length $_) - 5) if /\A\d/; s/(>|<)/$subhash{$1}/g; # restore html tags for saving print FILE $_,"\n"; } close FILE; &EditPage($q->param('File')); } else { &LogInPage('Hello There',$q->param('Action') ? $q->param('Action') : 'FileTree',$q->param('File')); } } sub EditPage { # send the script to a browser window in the form of a '; 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 '
'; print $q->start_form( -action=>'editor.pl', -method=>'POST', ); print "

$Header:

"; print 'User Name '; print $q->textfield( -name=>'UserName', -value=>$UserName, ); print '
Password '; print $q->textfield('PassWord'); print '
'; 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 '
'; print $q->end_table(); print $q->end_html; } sub GetFileTree { print "
$File::Find::name<\div>" if /\.$ext\Z/; }