hi,

I'm just tryin gtoo wite a CGi script that will make html pages of articles, via an 
admin interface. I know there are millions of things like that around. I'm just doing 
it for programming practice.

It's quite a long script.....The main probem is I get an Internal Server Error when I 
run the script. The function of it is too take either post.cgi?op=newarticle then 
display a form. When the person fills it in and submits it, it makes a html file in 
the specified directory. Collecting the header and footer of the html file from 
header.data and footer.data. It also stores a backup of the file in /backup. 

I souldn't be to hard too work out (I hope) but I'm really after an explantion for why 
I can't even get it to run...

My only guess is it could be someone too do with:
    my @articlename =  split /./, $artid_tmp;
on line 36...but I dunno, is it correct?

So here are the files, and I home someone can help :)

Damien.
------------------------------------------ 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 
------------------------------------------

Reply via email to