Thanks a load everyone,
I did perl -cw scriptname that worked very nicely and I've got rig of most
of the typos. But I still think I've done some actual coding wrong, as I got
the forms to post an article and re-make one. But I still get Internal
Server error when submitting them.
One thing I just want too check is. Is it possible too do this:
sub article {
#Makes article.html file named after $artid
open HTMLFILE, "> $art_dir/$artid.html" or die "Could not write to
$artid.html";
select HTMLFILE;
my $artid = shift;
printfile("header"); #Print articles html header
printarticle($artid); #Print article with chosen style
(printarticle)
printfile("footer"); #Print articles html footer
select STDOUT;
}
and then all the print statements in the subs called are written to
HTMLFILE? Or does it have too be done another way?....Maby someone could
have another look @ the code and maby find any coding misakes. I whould be
most pleased :) I'm only been coding for a week now :S
Damien :)
He'res the revised post.cgi
-------------------------------------- start
post.cgi --------------------------------------
#!/usr/bin/perl
use CGI;
require "config.pl";
my $cgi=new CGI;
$op = $cgi->param('op');
################
# DataGeek-CMS #
################
# - Admin include script for making article.html pages form article.data
files -
if ($op eq "makearticle") {
if ($cgi->param('artid')) {
article($artid);
} else {
print $cgi->header();
print $cgi->start_html("DataGeekCMS");
print "<FORM ACTION=\"$site_url/post.cgi\" METHOD=\"GET\">";
print "Enter the id or the article you want too re-generate: <INPUT
TYPE=\"TEXT\" NAME=\"artid\"><br>";
print "<INPUT TYPE=\"HIDDEN\" NAME=\"op\" VALUE=\"makearticle\"><br>";
print "<INPUT TYPE=\"SUBMIT\" NAME=\"re-generate\">";
print "</FORM>";
print $cgi->end_html();
}
} #Make html article page
elsif ($op eq "newarticle") {
if ($cgi->param('title')) {
open BKUPDIR, $art_dir or die "Couldn't open articles directory $art_dir:
$!\n";
while ($_ = readdir(BKUPDIR)) {
my $artid_tmp = $_;
}
my @articlename = split /./, $artid_tmp;
$artid = $articlename[0];
$artid++;
my @articlestuff = ("bah", $cgi->param('poster'),
$cgi->param('contributor'), $cgi->param('topic'), $cgi->param('link'),
$cgi->param('title'), $cgi->param('articletext'), $cgi->param('moretext'));
my $articledata = join "::", @articlestuff;
open ARTICLEDATA, "> $bkup_dir/$artid.data" or die "Could not open file
$bkup_dir/$artid.data: $!\n";
print ARTICLEDATA $articledata;
article($artid);
} else {
print $cgi->header();
print $cgi->start_html("DataGeekCMS");
print "<FORM ACTION=\"$site_url/post.cgi\" METHOD=\"POST\">";
print "Title: <INPUT TYPE=\"TEXT\" NAME=\"title\"><br><br>";
print "Poster: <INPUT TYPE=\"TEXT\" NAME=\"poster\"><br><br>";
print "Contributor: <INPUT TYPE=\"TEXT\" NAME=\"contributor\"><br><br>";
print "Topic: <select name=\"topic\">";
print "<option value=\"\">Select Topic</option>";
open TOPICS, "config/topics.data" or die "Could not open file
config/topics.data: $!\n";
while (<INCLUDE>) {
my @topic = split /::/, $_;
print "<option value=\"$topic[0]\">$topic[1]</option>";
}
print "</select><br><br>";
print "Article text:<br><textarea wrap=\"virtual\" cols=\"70\" rows=\"12\"
name=\"hometext\"></textarea><br><br>";
print "More text:<br><textarea wrap=\"virtual\" cols=\"70\" rows=\"12\"
name=\"moretext\"></textarea><br><br>";
print "Related Link: <INPUT TYPE=\"TEXT\" NAME=\"link\"><br><br>";
print "<INPUT TYPE=\"HIDDEN\" NAME=\"op\" VALUE=\"newarticle\"><br>";
print "<INPUT TYPE=\"SUBMIT\" NAME=\"Post article\">";
print "</FORM>";
print $cgi->end_html();
}
}
#############
# Functions #
#############
sub getart {
#Gets articles .data file and returns results in hash %article
my $artid = shift;
$/ = undef;
open ARTICLE, "$bkup_dir/$artid.data" or die "Could not open article data
file $bkup_dir/$artid.data: $!\n";
my $filedata = <ARTICLE>;
print $filedata, "\n";
my @data = split /::/, $filedata;
my %article=(
date => $data[0],
poster => $data[1],
sentby => $data[2],
topic => $data[3],
link => $data[4],
title => $data[5],
maintext => $data[6],
moretext => $data[7]
);
$/ = "\n";
return (%article);
}
sub printarticle {
#Layout for article html pages
my $artid = shift;
my %article = getart($artid);
print "<table border=0 cellpadding=0 cellspacing=0 bgcolor=ffffff
width=\"100%\"><tr><td>
<table border=0 cellpadding=1 cellspacing=1 bgcolor=000000
width=\"100%\"><tr><td>
<table border=0 cellpadding=3 cellspacing=0 bgcolor=cfcfbb
width=\"100%\"><tr><td align=left>
<font size=3 color=\"#363636\"><b>$article{title}</b></font>
<font size=2> $article{date}</font><br><font size=2>";
if ($article{sentby}) {
print "Contributed by: $article{sentby}";
} else {
print "Posted my: $article{poster}";
}
print "</font></td></tr></table></td></tr></table><br>";
print "<a href=\"$site_url/search.cgi?type=topic&str=", $article{topic},
"\"><img src=\"images/topics/", $article{topic}, ".gif\" border=\"0\"
align=right hspace=10 vspace=10></a>";
print "$article{maintext}<br>\n";
print "$article{moretext}\n";
print "</td></tr></table><br>";
}
sub printfile {
#Prints either header or footer
$file = shift;
$fileuc = uc($file);
open $fileuc, "config/$file.data" or die "Could not open file $file.data:
$!\n";
while (<$fileuc>) {
print $_, "\n";
}
}
sub article {
#Makes article.html file named after $artid
open HTMLFILE, "> $art_dir/$artid.html" or die "Could not write to
$artid.html";
select HTMLFILE;
my $artid = shift;
printfile("header"); #Print articles html header
printarticle($artid); #Print article with chosen style (printarticle)
printfile("footer"); #Print articles html footer
select STDOUT;
}
-------------------------------------- end
post.cgi --------------------------------------
----- Original Message -----
From: "Timothy Kimball" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, May 21, 2001 7:53 PM
Subject: Re: Help with CGI script
>
> Damien wrote:
> : ...
> : So here are the files, and I home someone can help :)
> : ...
>
> It would be a lot more useful if you could get the error message in
> your server's error log. Find out where this is and tattoo that
> location on the inside of your eyelids. The error log is a CGI
> programmer's best friend.
>
> Otherwise, try:
>
> * "perl -cw scriptname" to check for syntax errors
>
> * Are all the files you're writing to in writeable directories?
>
> * I notice that the first line of "sub article" doesn't have a closing
> quote & a semicolon.
>
> -- tdk
>
> : ------------------------------------------
post.cgi ------------------------------------------
> :
> : #!/usr/bin/perl
> : use CGI;
> :
> : require "config.pl";
> : my $cgi=new CGI;
> : $op = $cgi->param('op');
> :
> : ################
> : # DataGeek-CMS #
> : ################
> :
> :
> : # - Admin include script for making article.html pages form article.data
files -
> :
> : if ($op eq "makearicle") {
> : if ($cgi->param('artid')) {
> : article($artid);
> : } else {
> : print $cgi->header();
> : print $cgi->start_html("DataGeekCMS");
> : print "<FORM ACTION=\"$site_url/post.cgi\" METHOD=\"GET\">";
> : print "Enter the id or the article you want too re-generate: <INPUT
TYPE=\"TEXT\" NAME=\"artid\"><br>";
> : print "<INPUT TYPE=\"HIDDEN\" NAME=\"op\" VALUE=\"makearticle\"><br>";
> : print "<INPUT TYPE=\"SUBMIT\" NAME=\"re-generate\">";
> : print "</FORM>";
> : print $cgi->end_html();
> : }
> : } #Make html article page
> :
> : elsif ($op eq "newarticle") {
> : if ($cgi->param('title')) {
> : open BKUPDIR, $art_dir or die "Couldn't open articles directory
$art_dir: $!\n";
> : while ($_ = readdir(BKUPDIR)) {
> : my $artid_tmp = $_;
> : }
> : my @articlename = split /./, $artid_tmp;
> : $artid = $articlename[0];
> : $artid++;
> :
> : my @articlestuff = ("bah", $cgi->param('poster'),
$cgi->param('contributor'), $cgi->param('topic'), $cgi->param('link'),
$cgi->param('title'), $cgi->param('articletext'), $cgi->param('moretext'));
> :
> : my $articledata = join "::", @articlestuff;
> : open ARTICLEDATA, "> $bkup_dir/$artid.data" or die "Could not open
file $bkup_dir/$artid.data: $!\n";
> : print ARTICLEDATA $articledata;
> :
> : article($artid);
> : } else {
> : print $cgi->header();
> : print $cgi->start_html("DataGeekCMS");
> : print "<FORM ACTION=\"$site_url/post.cgi\" METHOD=\"POST\">";
> : print "Title: <INPUT TYPE=\"TEXT\" NAME=\"title\"><br><br>";
> : print "Poster: <INPUT TYPE=\"TEXT\" NAME=\"poster\"><br><br>";
> : print "Contributor: <INPUT TYPE=\"TEXT\"
NAME=\"contributor\"><br><br>";
> : print "Topic: <select name=\"topic\">";
> : print "<option value=\"\">Select Topic</option>";
> : open TOPICS, "config/topics.data" or die "Could not open file
config/topics.data: $!\n";
> : while (<INCLUDE>) {
> : my @topic = split /::/, $_;
> : print "<option value=\"$topic[0]\">$topic[1]</option>";
> : }
> : print "</select><br><br>";
> : print "Article text:<br><textarea wrap=\"virtual\" cols=\"70\"
rows=\"12\" name=\"hometext\"></textarea><br><br>";
> : print "More text:<br><textarea wrap=\"virtual\" cols=\"70\"
rows=\"12\" name=\"moretext\"></textarea><br><br>";
> : print "Related Link: <INPUT TYPE=\"TEXT\" NAME=\"link\"><br><br>";
> : print "<INPUT TYPE=\"HIDDEN\" NAME=\"op\" VALUE=\"newarticle\"><br>";
> : print "<INPUT TYPE=\"SUBMIT\" NAME=\"Post article\">";
> : print "</FORM>";
> : print $cgi->end_html();
> : }
> : }
> :
> :
> : #############
> : # Functions #
> : #############
> :
> : sub getart {
> : #Gets articles .data file and returns results in hash %article
> : my $artid = shift;
> : $/ = undef;
> : open ARTICLE, "$bkup_dir/$artid.data" or die "Could not open article
data file $bkup_dir/$artid.data: $!\n";
> : my $filedata = <ARTICLE>;
> : print $filedata, "\n";
> : my @data = split /::/, $filedata;
> : my %article=(
> : date => $data[0],
> : poster => $data[1],
> : sentby => $data[2],
> : topic => $data[3],
> : link => $data[4],
> : title => $data[5],
> : maintext => $data[6],
> : moretext => $data[7]
> : );
> : $/ = "\n";
> : return (%article);
> : }
> :
> : sub printarticle {
> : #Layout for article html pages
> : my $artid = shift;
> : my %article = getart($artid);
> : print "<table border=0 cellpadding=0 cellspacing=0 bgcolor=ffffff
width=\"100%\"><tr><td>
> : <table border=0 cellpadding=1 cellspacing=1 bgcolor=000000
width=\"100%\"><tr><td>
> : <table border=0 cellpadding=3 cellspacing=0 bgcolor=cfcfbb
width=\"100%\"><tr><td align=left>
> : <font size=3 color=\"#363636\"><b>$article{title}</b></font>
> : <font size=2> $article{date}</font><br><font size=2>";
> : if ($article{sentby}) {
> : print "Contributed by: $article{sentby}";
> : } else {
> : print "Posted my: $article{poster}";
> : }
> : print "</font></td></tr></table></td></tr></table><br>";
> : print "<a href=\"$site_url/search.cgi?type=topic&str=",
$article{topic}, "\"><img src=\"images/topics/", $article{topic}, ".gif\"
border=\"0\" align=right hspace=10 vspace=10></a>";
> : print "$article{maintext}<br>\n";
> : print "$article{moretext}\n";
> : print "</td></tr></table><br>";
> : }
> :
> : sub printfile {
> : #Prints either header or footer
> : $file = shift;
> : $fileuc = uc($file);
> : open $fileuc, "config/$file.data" or die "Could not open file
$file.data: $!\n";
> : while (<$fileuc>) {
> : print $_, "\n";
> : }
> : }
> :
> : sub article {
> : #Makes article.html file named after $artid
> : open HTMLFILE, "> $art_dir/$artid.html" or die "Could not write to
$artid.html
> : select HTMLFILE;
> : my $artid = shift;
> : printfile("header"); #Print articles html header
> : printarticle($artid); #Print article with chosen style (printarticle)
> : printfile("footer"); #Print articles html footer
> : select STDOUT;
> : }
> :
> : ------------------------------------------ end
post.cgi ------------------------------------------
> :
> : ------------------------------------------
config.pl ------------------------------------------
> :
> : #!/usr/bin/perl
> :
> : ################
> : # DataGeek-CMS #
> : ################
> :
> : #Global varibles
> :
> : $site_name = "DataGeek.org";
> : $site_url = "http://www.datageek.org/perlcms";
> :
> : $index_art_num = 20; #number of articles to display on the index page
> : $art_dir = "news"; #where all of your sites html articles will go
> : $bkup_dir = "backup"; #where all of your sites data files will go
> :
> : ------------------------------------------ end
config.pl ------------------------------------------
>