Editing parts of an HTML with CGI
From JmPm
If you look at the source code of http://www.henyregev.com/index_heb.html you will see tags like: <start_shakuf> bla bla <end_shakuf>
This tags will be ignored by the browser, but will be used by edit.pl (link at the buttom of the page).
Here is the code for it:
#!/usr/bin/perl use CGI; use CGI::Carp qw(fatalsToBrowser); my $q=new CGI; print $q->header(-charset => "windows-1255"); print '<HTML><HEAD><meta http-equiv="Content-Type" content="text/html; charset=windows-1255"/></HEAD><BODY>',"\n"; my $HTTP_REFERER = $ENV{'HTTP_REFERER'}; if ($HTTP_REFERER =~ /html?/ ) { read_file(); } else { save_file(); } print '</BODY></HTML>'; exit; ######################################################## sub read_file { my $HTTP_HOST = $ENV{'HTTP_HOST'}; $HTTP_REFERER =~ s/$HTTP_HOST//; $HTTP_REFERER =~ s|^http://|..|; print "Proccessing file:<b>$HTTP_REFERER</b>"; my $file_edited = $HTTP_REFERER; open( my $fh, $file_edited ) or die "cannot open file $file_edited $!\n"; my $text = do { local( $/ ) ; <$fh> } ; # slurp $text =~ s/\n/new_row/g; print '<FORM method="POST">'; while ($text=~m/<start_(.*?)>(.*?)<end_(.*?)>/g ) { my $code = $1; my $data = $2; my $end_code = $3; if ($code ne $end_code) { print "_____ start code ne end code _____" if $code ne $end_code; next; } $data =~ s/new_row/\n/g; my $rows=2; $rows+=1 while $data=~/\n/g; # count the number of lines print <<EndOfHTML; $code: <br> <textarea dir="rtl" id="$code" name="$code" rows="$rows" cols="100" maxlength="10000">$data</textarea> <br> EndOfHTML } print <<EndOfHTML; <input type="hidden" name="file_edited" value="$file_edited"> Password: <Input type=password name="password"> <input type="submit" value="Save changes"> </FORM> EndOfHTML $file_edited=~s/\/htdocs//; print "<br><a href=$file_edited>To cancel changes press here</a>"; } ######################################################## sub save_file { if ($q->param('password') ne '__the_pw__') { print "WRONG PASSWORD"; exit; } print '<B>Saving:</B>',"\n"; my $file_edited = $q->param('file_edited'); print "$file_edited\n"; print "<br>\n"; open( my $fh, $file_edited ) or die "cannot open file $file_edited $!\n"; my $text = do { local( $/ ) ; <$fh> } ; # slurp my $backup_text = $text; $text =~ s/\n/new_row/g; my @param_names = $q->param; foreach (@param_names) { my $param_name = $_; next if $param_name eq "file_edited" or $param_name eq "password"; my $param_value = $q->param($param_name); #print "$param_name - $param_value <br>\n"; $text=~s/<start_$param_name>.*?<end_$param_name>/<start_$param_name>$param_value<end_$param_name>/; } # backing up file before change: rename($file_edited,"$file_edited.old"); open(FH_OLD, ">$file_edited.old") or die "Cant open $file_edited.old message:$!\n"; print FH_OLD $backup_text; print "Old file saved as $backup_name <br>\n"; # saved the changed file: open(FH_OUT, ">$file_edited") or die "Cant open $file_edited message:$!\n"; $text =~ s/new_row/\n/g; print FH_OUT $text; $file_edited=~s/\/htdocs//; print "<br><a href=$file_edited>prees here to see changed file</a>"; }