#!/usr/bin/perl -w # now the standard modules use CGI; use DBI; use CGI::Carp qw(fatalsToBrowser); use Text::Template; use Apache::Session::MySQL; use GD::SecurityImage; use Digest::MD5 qw(md5_hex); use strict; my $cgi = new CGI; my %p = $cgi->Vars(); my $page = ($p{'page'})? $p{'page'} . ".html" : "home.html"; if($page eq 'index.html'){ $page = 'home.html';} if($page eq 'contact-us.html' || $page eq 'employment-application.html'){ &get_page_with_cookie(); }else{ &get_page(); } sub get_page{ if (!(-e "pages/$page")){ $page = 'not-found.html'; } my $mini_temp = { 'template' => "pages/$page", 'interiorlinks' => &get_subnav(), }; my $main = { 'template' => 'templates/main-template.html', 'background_image' => &get_background(), 'display_bug' => &displayBug(), 'content' => &get_template($mini_temp), }; print $cgi->header(); print &get_template($main); } sub get_page_with_cookie{ #get the session to check the captcha key my $session_id = $cgi->cookie('cfs_id'); my %session = &get_session($session_id); #clear old session id if exists clear it so we can create a new one if($session_id){ my $which = $session{'random'}; unlink("images/captcha/captcha$which.jpg"); &clear_session(); } #set session with the captcha key my $new_session_id = &set_session(); my $session_cookie = $cgi->cookie(-name=>'cfs_id',-value=>$new_session_id,-path=>"/",-domain=>"customfoodsolutions.com",-expires=>'+1d'); %session = &get_session($new_session_id); my $identifier = $session{'random'}; #create security image my $image = GD::SecurityImage->new(width => 100, height => 30, lines => 8, gd_font => 'giant'); $image->random($session{'captcha_key'}); $image->create(normal => 'rect', , '#000000', '#c8c8c8'); my($image_data, $mime_type, $random_number) = $image->out; my ($bytesread,$buffer,$blob); open (OUTFILE,">images/captcha/captcha$identifier.jpg") or die "$!"; print OUTFILE $image_data; close (OUTFILE); if (!(-e "pages/$page")){ $page = 'not-found.html'; } my $mini_temp = { 'template' => "pages/$page", 'interiorlinks' => &get_subnav(), 'captcha' => "", }; my $main = { 'template' => 'templates/main-template.html', 'background_image' => &get_background(), 'display_bug' => &displayBug(), 'content' => &get_template($mini_temp), }; print $cgi->header( -cookie => [$session_cookie] ); print &get_template($main); } ################################################## ##### get the sub nav for the interior pages ##### ################################################## sub get_subnav{ my $interiorlinks; my %linkhash = (); #this nav is used for all of the pages that are NOT for a particular product my %interiornav = ('ready-to-use-products.html' => 'Ready-to-Use Products','custom-products.html' => 'Custom Products','facility-tour.html' => 'Facility Tour'); #this nav is used for all of the pages that describe ONE product like Soups my %productnav = ('soups.html' => 'Soups', 'salsas.html' => 'Salsas', 'sauces.html' => 'Sauces', 'glazes.html' => 'Glazes', 'sides.html' => 'Sides', 'fillings.html' => 'Fillings', 'sous-vide.html' => 'Sous Vide','desserts.html' => 'Desserts', 'dips.html' => 'Dips & Table Sauces', 'dressings.html' => 'Dressings & Condiments'); #if the page matches a key in the productnav hash #then it will return a value and we will know to use that one #if it doesn't return anything we use the interiornav by default #one exception to this though, the OUR PRODUCTS page does not use the productnav it uses the interiornav. my $which = $productnav{"$page"}; if($which && $page ne 'our-products.html'){ %linkhash = %productnav; }else{ %linkhash = %interiornav } #while ( my ($key, $value) = each(%linkhash) ) { #if the nav page does NOT equal the current page, display it #don't display the navigation for the page we are currently on #if($key ne $page){ #$interiorlinks .= qq[
  • $value
  • ]; #} #} for my $key ( keys %linkhash ) { my $value = $linkhash{$key}; if($key ne $page){ $interiorlinks .= qq[
  • $value
  • ]; } } return $interiorlinks; } ################################################## ##### get background image for main template ##### ################################################## sub get_background{ my $background= qq[cfssmallbg.jpg]; if($page eq 'home.html'){ $background = qq[cfsbg.jpg]; } return $background; } sub displayBug{ my @productnav = ('soups.html', 'salsas.html', 'sauces.html', 'glazes.html', 'sides.html', 'fillings.html', 'sous-vide.html','desserts.html', 'dips.html', 'dressings.html'); my $display = "none"; my $i = 0; foreach(@productnav){ if($page eq $productnav[$i]){ $display = "block";} $i++; } return $display; } ######################### ##### standard subs ##### ######################### sub get_template { my $hashref = shift; my $template = Text::Template->new( SOURCE => $hashref->{'template'}, DELIMITERS => ['',''], ) or die "Couldn't construct template: $Text::Template::ERROR"; my $result = $template->fill_in( HASH => $hashref ); return $result; } sub dbconnect { my $dbh = DBI->connect("DBI:mysql:customfoodsolutions:localhost","customfoodsolutions","tWFkFRGm9b2g7Eu", {RaiseError => 1}) or die "Can't connect to database: $DBI::errstr\n"; return $dbh; } ######################## ##### session subs ##### ######################## sub set_session { my $dbh = &dbconnect(); my %session; tie %session, 'Apache::Session::MySQL', undef, { Handle => $dbh, LockHandle => $dbh }; $session{'captcha_key'} = &get_random(); $session{'random'} = int(rand(10)); my $session_id = $session{'_session_id'}; untie(%session); $dbh->disconnect(); return $session_id; } #create a random string for the security image sub get_random{ my $random_str = int(rand(9999999999)); $random_str = md5_hex($random_str); $random_str = substr($random_str,0,6); return $random_str; } sub get_session { my ($session_id) = @_; my $dbh = &dbconnect(); my %session; eval { tie %session, 'Apache::Session::MySQL', $session_id, { Handle => $dbh, LockHandle => $dbh }; }; if ($@) { %session = (); $dbh->disconnect(); return %session; } else { my %saved_session = %session; untie(%session); $dbh->disconnect(); return %saved_session; } } sub clear_session { my($session_id) = @_; my $dbh = &dbconnect(); my $sth = $dbh->prepare('delete from sessions where id = ?'); $sth->execute($session_id); $sth->finish(); $dbh->disconnect(); }