in reply to Re: Posting Program
in thread Posting Program

here is the code for the program that reads the textareea and prints it into a file:
#!/usr/local/bin/perl ############################## #Post article of information # # by Matthew Manela # ############################## require 'subparseform.lib'; # sub parsing $basedir = "/data1/blah.net/blah/"; $baseurl = "http://blah.blah.net"; &Parse_Form; print "Content-type: text/html\n\n"; $tpass=$formdata{password}; $article=$formdata{text}; $destination=$formdata{destination}; #the source of the file to write to @info=split(/\n/,$article); # set $file to location of txt file $file="$basedir$destination\.txt"; # make sure destination is within parameters if($destination=~/^([a-zA-Z])+$/){ &main; }else{ &filename; } sub main{ if(-e $file){ open(WRITE, ">$file")|| &filename; foreach $line (@info){ print WRITE "$line\n"; } close(WRITE); }else{ &filename; } sub filename{ print"You can't write to that file!\n"; exit; }
and the code of the program that shows the info 45 lines at a time:
#!/usr/local/bin/perl # Get article form link and display # by Matthew Manela require 'subparseform.lib'; # sub parsing $basedir = "/data1/blah.net/blahs/"; $baseurl = "http://blah.blah.net"; &Parse_Form; print "Content-type: text/html\n\n"; $article=$formdata{'article'}; $page=$formdata{'page'}; $maxlength=43; $next=$page+1; $previous=$page-1; $start=($page-1)*$maxlength; $finish=$page*$maxlength; &main; sub main{ $file="$basedir$article\.txt"; open(TOP, "$basedir/top\.htm")|| &filename; @top=<TOP>; close(TOP); open(BOTTOM, "$basedir/bottom\.htm")|| &filename; @bottom=<BOTTOM>; close(BOTTOM); open(READ, "$file")|| &filename; my @info = grep { !/^\s*$/ } <READ>; chomp(@info); close(READ); $length=@info; foreach $part (@top) { if ($part=~/^<\/head>$/){ print"<base href=\"http://blah.blah.net\"></head>\n"; }else{ print"$part\n"; } } for($i=$start;$i<$finish;$i++){ if ($info[$i]=~ m/<!--\#include/) { }else{ print"$info[$i]\n"; } } if (($length>$finish)&&($page>1)){ print"<p align=left>&nbsp&nbsp<font color=yellow><--</font>&nbsp<a hre +f=\"http://blah.blah.net/cgi-bin/article.cgi?article=$article&page=$p +revious\">Go to the previous page</a><a href=\"http://blah.blah.net/c +gi-bin/article.cgi?article=$article&page=$next\">Continue to the next page</a>&nbsp<font color=yellow>--></font>&nbsp&nb +sp</p>\n"; }elsif(($length>$finish)&&($page==1)){ print"<p align=right><a href=\"http://blah.blah.net/cgi-bin/article.cg +i?article=$article&page=$next\">Continue to the next page</a>&nbsp<fo +nt color=yellow>--></font>&nbsp&nbsp</p>\n"; } foreach $thing (@bottom){ print"$thing\n"; } } sub filename{ print"<html><head><title>Article does not exist</title></head><body>\n +"; print"Sorry, That article does not exist.<br>\n"; print"</body></html>"; exit; }


Jemts

"If you're traveling in a time machine, and you're eating corn on the cob, I don't think it's going to affect things one way or the other. But here's the point I'm trying to make: Corn on the cob is good, isn't it."

Replies are listed 'Best First'.
Re: Posting Program
by cLive ;-) (Prior) on May 26, 2002 at 11:26 UTC
    Ouch. There are many things here you need to look at, but to get you going:
    • it's &nbsp; with a semi-colon
    • your ssi check only removes includes - what about the other ssi calls? use generic instead
    • use CGI and strict
    • know when to use an array and when to use a scalar
    • sub &main is not needed

    Here is a quick rewrite (untested) of the above code. Please ask if there's anything you don't understand.

    #!/usr/local/bin/perl -w use strict; use CGI; my $basedir = "/data1/blah.net/blah/"; my $baseurl = "http://blah.blah.net"; my $q=CGI::new(); my $tpass = $q->param('password'); my $article = $q->param('text'); my $destination = $q->param('destination'); # set $file to location of txt file my $file="$basedir$destination\.txt"; # strip out all possible SSI calls here, not below (ie, remove all com +ments) $article =~ s/<!--.*?-->//gis; # make sure destination is within parameters (allow underscores as wel +l if ($destination !~ /^\w+$/){ &filename; } # ?? only overwrite existing file? If so use -T instead # if(-e $file) if(-T $file) { open(WRITE, ">$file") || &filename; print WRITE "$line\n"; close(WRITE); } else { &filename; } sub filename{ print $q->header, $q->start_html, $q->h4("Error: You can't write to that file!"), $q->end_html; exit(0); } ###################################################################### +# #!/usr/local/bin/perl-w use strict; use CGI; my $q = CGI::new(); my $basedir = "/data1/blah.net/blahs/"; my $baseurl = "http://blah.blah.net"; my $article = $q->param('article'); my $page = $q->param('page'); my $maxlength = 43; my $next = $page+1; my $previous = $page-1; my $start = ($page-1)*$maxlength; my $finish = $page*$maxlength; my $file="$basedir$article\.txt"; open(TOP, "$basedir/top\.htm")|| &filename; my $top= join '',(<TOP>); close(TOP); open(BOTTOM, "$basedir/bottom\.htm")|| &filename; my $bottom = join '', (<BOTTOM>); close(BOTTOM); open(READ, "$file")|| &filename; my @info = (<READ>); close(READ); $length=@info; $top =~ s|</head>|<base href="http://blah.blah.net"></head>|is; print $q->header, $top; print $q->header, $q->start_html, $info[$start..$finish]; # note, this -> $info[$start..$finish]; may not work - it's late and s +eemed like a good idea :) if ( ($length>$finish) && ($page>1) ) { print $q->p("&nbsp;&nbsp;", $q->font({-color=>'yellow'}, '<--&nbsp;' ), $q->a({-href => "http://blah.blah.net/cgi-bin/article. +cgi?article=$article&page=$previous"}, 'Go to the previous page' ), $q->a({-href => "http://blah.blah.net/cgi-bin/article. +cgi?article=$article&page=$next"}, 'Continue to the next page' ), $q->font({-color=>'yellow'}, '&nbsp;-->' ) ); } elsif ( ($length>$finish) && ($page==1) ) { print $q->p({-align => 'right'}, $q->a({-href => "http://blah.blah.net/cgi-bin/article. +cgi?article=$article&page=$next"}, 'Continue to the next page' ), $q->font({-color=>'yellow'}, '&nbsp-->' ) ); } print $bottom, $q->end_html; exit(0); sub filename{ print $q->header, $q->start_html( -title => 'Article does not exist'), $q->p('Sorry, That article does not exist), $q->end_html; exit(0); }

    This is by no means how I would write this, but I hope you will take the time to look up the CGI docs and use it in future. It's a lot better than trusting your own sub!

    This should keep you going for now...

    As to your characters per line issue, you don't state whether paragraph breaks are needed. Here's a hint anyway for the first script:

    $article =~ s/\s+/ /gs; $article =~ s|([^\n]\b{0,100})|$1<br/>|gs;

    hth

    cLive ;-)

    --
    seek(JOB,$$LA,0);

      After looking over my code and yours I am able to clarify exactly what I need. Basically when the program reads from the text area I need every line to have a return character after it. How would I easily accomplish this?

      Jemts

      "If you're traveling in a time machine, and you're eating corn on the cob, I don't think it's going to affect things one way or the other. But here's the point I'm trying to make: Corn on the cob is good, isn't it."