#!/usr/bin/perl #print "Content-type:text/html\n\n\n"; # # My Theme # # # mytheme.cgi -- Dynamically generates site theme # More information: http://www.downes.ca/edurss02.htm # # by Stephen Downes # Version 0.2 - January 2, 2006 # # # Copyright 2006 Stephen Downes, National Research Council Canada # Prerelease - please do not distribute - I'll GPL it when it's ready # use DBI; use CGI; use CGI::Carp qw(fatalsToBrowser); $query = new CGI; my $vars = $query->Vars; if ($vars->{theme} eq "select") { &theme_list; } print "Content-type:text/html\n"; print "Working...
";
# Open database
my $dbh = &db_open('DBI:mysql:edurss:downes.ca','downes','bartandpolly') or print "Database connect error: $!";
# Check for preselected theme
my $theme = $query->cookie('theme');
# Check for selected theme
if ($vars->{theme}) { $theme = $vars->{theme}; }
unless ($theme) { # generate new theme randomly
# Count Number of Themes
$stmt = "SELECT COUNT(*) AS items FROM theme";
my $sth = $dbh -> prepare($stmt);
$sth -> execute(@bind);
my $ref = $sth -> fetchrow_hashref();
my $count = $ref->{items};
# Generate Random CSS Index Number
srand( time() ^ ($$ + ($$ << 15)) );
$theme = int(rand $count) + 1;
$main_name = "http://www.downes.ca/themes/main_". $theme . ".jpg";
$banner_name = "http://www.downes.ca/themes/banner_". $theme . ".jpg";
}
# Save theme as cookie
my $cookie1 = $query->cookie(-name=>'theme',-value=>$theme,-path=>'/',-domain=>'www.downes.ca',-secure=>0);
print $query->header(-cookie=>[$cookie1]);
print "\n";
# Get Data from Database
my $stmt = qq{ SELECT * FROM theme WHERE theme_id = ?};
my $sth = $dbh -> prepare($stmt);
$sth -> execute($theme);
my $ref = $sth -> fetchrow_hashref();
# Increment Hits
unless ($ref->{theme_hits}) { $ref->{theme_hits} = 0; }
if ($ref->{theme_hits} eq "NULL") { $ref->{theme_hits} = 0; }
my $hits = $ref->{theme_hits} + 1;
$dbh->do(qq{UPDATE theme SET theme_hits = ? WHERE theme_id = ?},undef,$hits,$theme);
if ($dbh) { $dbh->disconnect; }
# Define CSS
my $css_str = qq||;
$css_str =~ s/"/\\"/g;
$css_str =~ s/\n//g;
# Main Image
my $img_str = qq|function main_img() {document.write(\\"\\");}|;
# Main Image Caption
$ref->{theme_main_cap} =~ s/"/"/g;
my $cap_str = qq|function main_cap() {document.write(\\"
$ref->{theme_main_cap} All photos are under the usual Creative Commons license; feel free to reprint them.
This theme is called $ref->{theme_title}. If you would like to view a different theme, click [Theme]. [View Themes].
\\");}|; # Portrait Image my $port_img = qq|function port_img() {document.write(\\"$ref->{theme_port_cap}
\\");}|; print qq| document.write("$css_str"); document.write(""); |; exit; sub db_open { my ($dsn,$user,$password) = @_; my $dbh = DBI->connect($dsn, $user, $password) or die "Database connect error: $! \n"; # if ($dbh) { $dbh->trace(2,"dberror.txt"); } return $dbh; } sub theme_list { print "Content-type: text/html\n\n"; print qq{View a theme by clicking on the theme name.
| ID | Theme Name | Hits |
| $ref->{theme_id} | $ref->{theme_title} | $ref->{theme_hits} |