[6965] in www-talk@info.cern.ch

home help back first fref pref prev next nref lref last post

Simple WWW Editor CGI

daemon@ATHENA.MIT.EDU (Thorsten Ludewig)
Fri Dec 9 10:41:27 1994

Date: Fri, 9 Dec 1994 16:11:45 +0100
Errors-To: listmaster@www0.cern.ch
Reply-To: Thorsten.Ludewig@rz.fh-wolfenbuettel.de
From: Thorsten Ludewig <Thorsten.Ludewig@rz.fh-wolfenbuettel.de>
To: Multiple recipients of list <www-talk@www0.cern.ch>

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: th@rz.fh-wolfenbuettel.de
  |__|/  /_/____     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 (th@rz.fh-wolfenbuettel.de)
# 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;
}


home help back first fref pref prev next nref lref last post