#!/usr/bin/perl -wT # $Id: titles.cgi,v 1.2 2003/09/12 21:33:14 adamw Exp $ use Mysql; use CGI; sub get_books( $ ); sub html_safe( $ ); sub parse_form( ); if (CGI::param('t') =~ /(\w+)$/) { $type = $1; } else { $type = 'titles'; } $tmpl_file = "$type.tmpl"; $html_file = "../dynamic/$type.html"; if ((! -e $html_file) || (-M $html_file gt 1) || CGI::param()) { if (!$dbh) { $dbh = DBI->connect( "DBI:mysql:books", "web", "gronk" ); } umask( 0002 ); open( IN, "< $tmpl_file"); open( OUT, "> $html_file") || die "shit, couldn't open $html_file for writing"; while () { if (/^#TITLE$/) { get_books( 'title' ); } elsif (/^#AUTHOR$/) { get_books( 'author_last, author_first, title' ); } elsif (/^#LASTUP$/) { get_last_updated( ); } else { print OUT $_; } } close( OUT ); close( IN ); $dbh->disconnect; } print "Content-type:text/html\n\n"; open( IN, "< $html_file"); while () { print $_; } close( IN ); sub get_books( $ ) { my ($author, $row, $sth, $sth2, $type); $order = shift( @_ ); $sth = $dbh->prepare( "select * from book where type != 'normal' order by $order" ); $sth->execute; while ($row = $sth->fetchrow_hashref( )) { $author = $row->{'author_first'} . " " . $row->{'author_last'}; $title = "{'n'}\">$row->{'title'}"; if (substr( $order, 0, 5 ) eq 'title') { print OUT "$title, $author."; } else { print OUT "$author, $title."; } #print OUT " {'n'}\" class=R>Add Review\n"; print OUT "
\n"; } $sth->finish; } sub get_last_updated( ) { @row = $dbh->selectrow_array( 'select max(time) from review' ); print OUT "$row[0]\n"; } sub html_safe( $ ) { my $string = shift; if (!$string) { return ''; } $string =~ s//>/g; $string =~ s/\r\n|\n\r|[\n\r]/
/g; return $string; }