Simple WWW Editor CGI

Thorsten Ludewig ([email protected])
Fri, 9 Dec 1994 16:07:31 +0100


Hi

i have written a Simple WWW/HTML Editor CGI in Perl.
It should be placed in a non public cgi-bin directory because all
documents with access rights for the ServerUser can be edited.

Bye Thorsten.
______________________________________________________________________
__________
/___ ___/|
|__/ /|__|/ Thorsten Ludewig
/ / / /|
/__/ / / / E-Mail: [email protected]
|__|/ /_/____ WWW-Homepage: http://www.fh-wolfenbuettel.de/~th/
/_________/| IRC-Nickname: Daimos
|_________|/

----------------------------- WWWedit --------------------------------

#!/usr/local/bin/perl

# Simple WWW Editor v0.5 December 1994
# by Thorsten Ludewig ([email protected])
# URL http://www.fh-wolfenbuettel.de/~th/
#

# server
$def_url = "http://www.fh-wolfenbuettel.de";

# document root
$doc_root = "/app/www/Docs";

# copy command
$copy_cmd = "/bin/cp";

# backup extension
$bak_ext = ".bak";

#
# ReadParse is merged from Steven E. Brenner's cgi-lib.pl
#
# ReadParse by Copyright 1993 Steven E. Brenner
# Reads in GET or POST data, converts it to unescaped text, and puts
# one key=value in each member of the list "@in"
# Also creates key/value pairs in %in, using '\0' to separate multiple
# selections
sub ReadParse {
local ($i, $loc, $key, $val);

# Read in text
if ($ENV{'REQUEST_METHOD'} eq "GET") {
$in = $ENV{'QUERY_STRING'};
} elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
for ($i = 0; $i < $ENV{'CONTENT_LENGTH'}; $i++) {
$in .= getc;
}
}

@in = split(/&/,$in);

foreach $i (0 .. $#in) {
# Convert plus's to spaces
$in[$i] =~ s/\+/ /g;

# Convert %XX from hex numbers to alphanumeric
$in[$i] =~ s/%(..)/pack("c",hex($1))/ge;

# Split into key and value.
$loc = index($in[$i],"=");
$key = substr($in[$i],0,$loc);
$val = substr($in[$i],$loc+1);
$in{$key} .= '\0' if (defined($in{$key})); # \0 is the multiple separator
$in{$key} .= $val;
}

$url = $in{"url"};
$action = $in{"action"};

return 1; # just for fun
}

#
# Print HTML Header
#
sub OutputHeader {
print "Content-type: text/html\n\n";
print "<title>Simple WWW Editor</title>\n";
print "<h1>Simple WWW Editor</h1>\n";
}

#
# Print my URL as footer
#
sub OutputFooter {
print "<hr><address>";
print "<a href=\"http://www.fh-wolfenbuettel.de/~th/\">Thorsten Ludewig</a>, ";
print " 9. Dezember 1994</address></pre>\n";
}

sub Error {
local ( $msg ) = @_;
print "<h1>ERROR: $msg</h1>\n";
&OutputFooter;
exit;
}

#
# Print the notice for a check
#
sub Output {
open( DATA, "$doc_root$url" ) || &Error( "Could'nt open $doc_root$url!" );

print "<form action=$WWWedit>\n";
print "URL: <i>$def_url</i><br>";
print "<input name=url size=80 ";
print "value=\"$url\"" if ( url );
print "><br>\n";
print "<input type=radio name=action value=read checked>Read ";
print "<input type=radio name=action value=write checked>Write ";
print "<input type=radio name=action value=test checked>Test\n";
print "<textarea name=text size=80,25>";
if ( $action eq "read" ) {
while( <DATA> ) {
s/&/&amp;/g;
s/</&lt;/g;
s/>/&gt;/g;
print $_;
}
close( DATA );
}

print "</textarea>\n";
print "<p>\n";
print "<input type=\"submit\" value=\" OK \"> ";
print "<input type=\"reset\" value=\" Reset \">\n";
</form>

}

sub OutputTest {
print "Content-type: text/html\n\n";
print $in{"text"};
}

sub WriteDocument {
if ( -f "$doc_root$url" ) {
system( "$copy_cmd $doc_root$url $doc_root$url$bak_ext" );
}

open( DATA, ">$doc_root$url" ) || &Error( "Could'nt open $doc_root$url!" );
print DATA $in{"text"};
close( DATA );
print "<h2>Document \"$doc_root$url\" successfuly written.</h2>";
}

#
# Main
#
$| = 1;
&ReadParse;

if ( $action eq "test" ) {
&OutputTest;
}
elsif ( $action eq "write" ) {
&OutputHeader;
&WriteDocument;
&OutputFooter;
}
else {
&OutputHeader;
&Output;
&OutputFooter;
}