#!/usr/bin/perl use strict; use CGI qw(:standard start_div end_div start_center end_center); use CGI::Pretty; use Mail::Sender; my ($errors, $basepath); main(); exit (0); sub main { our ($q, %form); $q = CGI->new(); %form = $q->Vars; $basepath = '/home/www-data/co.ordinate.org/callahans/gallery'; print_page($q, %form); } sub print_page { my ($q, %form) = @_; my ($file, $fh, $type, $info, $error); print $q->header('text/html'); print $q->start_html({title => 'Submission Results', style => {src => 'gallery.css'}, class => 'pale'}); print $q->start_div({class => 'content70'}); print $q->center(img({src => 'chrome/results.png', align => 'CENTER', height => '60', width => '234', alt => 'Form Results'}), hr); unless ($form{name}) { error($q, "Your name is required for your nametag."); } if ($form{email1} && $form{email2}) { unless ($form{email1} eq $form{email2}) { error ($q, "Email addresses do not match.  Please re-enter."); } } elsif ($form{email1} || $form{email2}) { error ($q, "Please confirm your email address."); } else { error ($q, "Your email address is required for questions or notification."); } if ($file = $form{photo}) { $fh = $q->upload('photo') || error ($q,"Cannot upload $file!"); unless($errors) { my ($buffer, $name); ($name = $form{name}) =~ tr/A-Z/a-z/; $name =~ tr/a-z/a-z/cd; $file =~ s/^.*(\.\w+)$/$name$1/; open (OUT, ">$basepath/temp/$file") || die "Cannot open $basepath/temp/$file for writing"; while (read($fh, $buffer, 1024)) { print OUT $buffer; } close (OUT); if (($buffer = `file $basepath/temp/$file`) =~ m#^$basepath/temp/$file:\s+(.*)#) { $type = $1; if ($type =~ /^(PNG|JPEG|GIF) image data/) { write_data($name, \%form); print $q->center(img({src => "temp/$file", height => 200, width => 200}), p("Your photo was successfully uploaded and your data saved."), p("You will be notified by email when it has been mounted (if necessary) and loaded.")); print $q->p("NOTE:  The preview above is fixed at 200x200 pixels for technical reasons (there's a CGI.pm bug that I haven't yet figured out how to work around).  This probably results in your photo being squashed in one direction or the other.  This will NOT affect your framed photo."); send_email($name, $file, \%form); print $q->center(p("An upload notification has been sent to the Maintainer.")); } else { error ($q,"File type '$type' is not permitted."); print $q->center(p("Please select a PNG, JPEG or GIF image file and try again.")); unlink("$basepath/temp/$file"); } } else { error ($q,"Could not detect file type!"); print $q->center(p("Please try again.")); unlink("$basepath/temp/$file"); } } } else { error ($q, "Did you forget to attach your photo?"); } print $q->end_div; print $q->end_html; } sub error { my ($q, $msg) = @_; print $q->center(p({class=>'alert'}, $msg)); $errors++; } sub write_data { my ($name, $form) = @_; if ($form->{frame} =~ /Carved/) { $form->{frame} = 'carved'; } elsif (!($form->{frame} =~ /^Callahan/)) { $form->{frame} =~ tr/A-Z /a-z-/; } unless ($form->{orientation} eq 'Best Fit') { $form->{orientation} =~ tr/A-Z/a-z/; } open (OUT, ">$basepath/temp/$name.data"); printf OUT ("name: %s\nframe: %s\norientation: %s\nemail: %s\nobscure: %s\nnets: %s\nurl: %s\nnotes: %s\n", $form->{name}, $form->{frame}, $form->{orientation}, $form->{email1}, $form->{obscure}, $form->{nets} || '-none-', $form->{url} || '-none-', $form->{notes} || ''); close (OUT); } sub send_email { my ($name, $file, $form) = @_; my $msg = new Mail::Sender {smtp => 'mail.novylen.net', from => 'anonymous@novylen.net', fake_from => 'gallery@co.ordinate.org'}; $msg->MailFile({to => 'alaric@caerllewys.net', subject => sprintf('Photo uploaded from %s', $form->{name}), msg => sprintf('%s uploaded a photo for the Gallery.', $form->{name}), file => "$basepath/temp/$file, $basepath/temp/$name.data"}); $msg->Close; }