#!/usr/local/bin/perl # This is a sample script how to edit a file in a browser # #This script operates in several modes depending which CGI parameters are # specified # # show_mode: no parameters are specified at all. This is the end point # of the other modes. #change mode: do_change parameter must be given. A parameter Text= must # specify a changed content to write in the file being edited # # Acknowledgement # The idea for this script and inspiration are due to James C. Harper $FILE_TO_EDIT = "/tmp/sample.txt"; print "content-type: text/html\nPragma: no-cache\n\n"; sub cgi_die{ print shift; exit 0; } # Read the file $FILE_TO_EDIT and display its contents in a TEXTAREA sub show_mode { print < Editing a file

Editing a file $FILE_TO_EDIT


 

EOF } # Writing the new content for the file sub change_mode { my $file_name; ($file_name = $FORM{File}) or cgi_die "File parameter missing"; exists $FORM{Text} or cgi_die "Text parameter is missing"; open(FILENEW,">$file_name") or cgi_die "Failed to open $file_name $!"; print FILENEW $FORM{Text}; close FILENEW; } # Root module &parse_form; if (exists $FORM{"do_change"}){ change_mode; } show_mode; # Parse the POST message (or the QUERY_STRING) and put all the FORM's # parameters into a @FORM hash, as name= associations sub parse_form { # Get the input if( $ENV{"REQUEST_METHOD"} eq "POST" ) { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } else { $buffer = $ENV{"QUERY_STRING"}; } # Split the name-value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; if (exists $FORM{$name}){ $FORM{$name} .= " ". $value; } else { $FORM{$name}=$value; } } }