diff options
-rw-r--r-- | perllib/Page.pm | 41 | ||||
-rwxr-xr-x | web/index.cgi | 53 | ||||
-rwxr-xr-x | web/questionnaire.cgi | 42 |
3 files changed, 82 insertions, 54 deletions
diff --git a/perllib/Page.pm b/perllib/Page.pm index 52ff87b38..c336d29ec 100644 --- a/perllib/Page.pm +++ b/perllib/Page.pm @@ -6,7 +6,7 @@ # Copyright (c) 2006 UK Citizens Online Democracy. All rights reserved. # Email: matthew@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: Page.pm,v 1.87 2008-04-10 11:06:14 matthew Exp $ +# $Id: Page.pm,v 1.88 2008-04-10 19:07:38 matthew Exp $ # package Page; @@ -16,6 +16,7 @@ use Carp; use mySociety::CGIFast qw(-no_xhtml); use Error qw(:try); use File::Slurp; +use Image::Magick; use LWP::Simple; use Digest::MD5 qw(md5_hex); use POSIX qw(strftime); @@ -645,4 +646,42 @@ sub recent_photos { return $out; } +sub check_photo { + my ($q, $fh) = @_; + my $ct = $q->uploadInfo($fh)->{'Content-Type'}; + my $cd = $q->uploadInfo($fh)->{'Content-Disposition'}; + # Must delete photo param, otherwise display functions get confused + $q->delete('photo'); + return 'Please upload a JPEG image only' unless + ($ct eq 'image/jpeg' || $ct eq 'image/pjpeg'); + return ''; +} + +sub process_photo { + my $fh = shift; + my $photo = Image::Magick->new; + my $err = $photo->Read(file => \*$fh); # Mustn't be stringified + close $fh; + throw Error::Simple("read failed: $err") if "$err"; + $err = $photo->Scale(geometry => "250x250>"); + throw Error::Simple("resize failed: $err") if "$err"; + my @blobs = $photo->ImageToBlob(); + undef $photo; + $photo = $blobs[0]; + return $photo; +} + +sub workaround_pg_bytea { + my ($st, $img_idx, @elements) = @_; + my $s = dbh()->prepare($st); + for (my $i=1; $i<=@elements; $i++) { + if ($i == $img_idx) { + $s->bind_param($i, $elements[$i-1], { pg_type => DBD::Pg::PG_BYTEA }); + } else { + $s->bind_param($i, $elements[$i-1]); + } + } + $s->execute(); +} + 1; diff --git a/web/index.cgi b/web/index.cgi index c6b5f840d..7b9557fe5 100755 --- a/web/index.cgi +++ b/web/index.cgi @@ -6,14 +6,13 @@ # Copyright (c) 2006 UK Citizens Online Democracy. All rights reserved. # Email: matthew@mysociety.org. WWW: http://www.mysociety.org # -# $Id: index.cgi,v 1.188 2008-04-08 11:33:43 matthew Exp $ +# $Id: index.cgi,v 1.189 2008-04-10 19:07:39 matthew Exp $ use strict; use Standard; use Error qw(:try); use File::Slurp; -use Image::Magick; use LWP::Simple; use RABX; use CGI::Carp; @@ -163,7 +162,7 @@ sub submit_update { my $fh = $q->upload('photo'); if ($fh) { - my $err = check_photo($q, $fh); + my $err = Page::check_photo($q, $fh); push @errors, $err if $err; } @@ -178,7 +177,7 @@ sub submit_update { my $image; if ($fh) { try { - $image = process_photo($fh); + $image = Page::process_photo($fh); } catch Error::Simple with { my $e = shift; push(@errors, "That image doesn't appear to have uploaded correctly ($e), please try again."); @@ -194,7 +193,7 @@ sub submit_update { return display_problem($q, @errors) if (@errors); my $id = dbh()->selectrow_array("select nextval('comment_id_seq');"); - workaround_pg_bytea("insert into comment + Page::workaround_pg_bytea("insert into comment (id, problem_id, name, email, website, text, state, mark_fixed, photo) values (?, ?, ?, ?, '', ?, 'unconfirmed', ?, ?)", 7, $id, $input{id}, $input{name}, $input{email}, $input{update}, @@ -210,19 +209,6 @@ sub submit_update { return $out; } -sub workaround_pg_bytea { - my ($st, $img_idx, @elements) = @_; - my $s = dbh()->prepare($st); - for (my $i=1; $i<=@elements; $i++) { - if ($i == $img_idx) { - $s->bind_param($i, $elements[$i-1], { pg_type => DBD::Pg::PG_BYTEA }); - } else { - $s->bind_param($i, $elements[$i-1]); - } - } - $s->execute(); -} - sub submit_problem { my $q = shift; my @vars = qw(council title detail name email phone pc easting northing skipped anonymous category flickr upload_fileid); @@ -236,7 +222,7 @@ sub submit_problem { my $fh = $q->upload('photo'); if ($fh) { - my $err = check_photo($q, $fh); + my $err = Page::check_photo($q, $fh); push @errors, $err if $err; } @@ -313,7 +299,7 @@ sub submit_problem { my $image; if ($fh) { try { - $image = process_photo($fh); + $image = Page::process_photo($fh); } catch Error::Simple with { my $e = shift; push(@errors, "That image doesn't appear to have uploaded correctly ($e), please try again."); @@ -350,7 +336,7 @@ sub submit_problem { } } else { $id = dbh()->selectrow_array("select nextval('problem_id_seq');"); - workaround_pg_bytea("insert into problem + Page::workaround_pg_bytea("insert into problem (id, postcode, easting, northing, title, detail, name, email, phone, photo, state, council, used_map, anonymous, category, areas) values @@ -878,28 +864,3 @@ sub map_pins { return ($pins, $current_map, $current, $fixed, $dist); } -sub check_photo { - my ($q, $fh) = @_; - my $ct = $q->uploadInfo($fh)->{'Content-Type'}; - my $cd = $q->uploadInfo($fh)->{'Content-Disposition'}; - # Must delete photo param, otherwise display functions get confused - $q->delete('photo'); - return 'Please upload a JPEG image only' unless - ($ct eq 'image/jpeg' || $ct eq 'image/pjpeg'); - return ''; -} - -sub process_photo { - my $fh = shift; - my $photo = Image::Magick->new; - my $err = $photo->Read(file => \*$fh); # Mustn't be stringified - close $fh; - throw Error::Simple("read failed: $err") if "$err"; - $err = $photo->Scale(geometry => "250x250>"); - throw Error::Simple("resize failed: $err") if "$err"; - my @blobs = $photo->ImageToBlob(); - undef $photo; - $photo = $blobs[0]; - return $photo; -} - diff --git a/web/questionnaire.cgi b/web/questionnaire.cgi index 907e99396..7fd9547ef 100755 --- a/web/questionnaire.cgi +++ b/web/questionnaire.cgi @@ -6,7 +6,7 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: matthew@mysociety.org. WWW: http://www.mysociety.org # -# $Id: questionnaire.cgi,v 1.24 2008-03-21 14:11:14 matthew Exp $ +# $Id: questionnaire.cgi,v 1.25 2008-04-10 19:07:39 matthew Exp $ use strict; use Standard; @@ -77,6 +77,22 @@ sub submit_questionnaire { if $input{been_fixed} eq 'No' && $problem->{state} eq 'fixed' && !$input{update}; return display_questionnaire($q, @errors) if @errors; + my $fh = $q->upload('photo'); + my $image; + if ($fh) { + my $err = Page::check_photo($q, $fh); + push @errors, $err if $err; + try { + $image = Page::process_photo($fh) unless $err; + } catch Error::Simple with { + my $e = shift; + push(@errors, "That image doesn't appear to have uploaded correctly ($e), please try again."); + }; + } + push @errors, 'Please provide some text as well as a photo' + if $image && !$input{update}; + return display_questionnaire($q, @errors) if @errors; + my $new_state = ''; $new_state = 'fixed' if $input{been_fixed} eq 'Yes' && $problem->{state} eq 'confirmed'; $new_state = 'confirmed' if $input{been_fixed} eq 'No' && $problem->{state} eq 'fixed'; @@ -105,19 +121,20 @@ sub submit_questionnaire { # Record an update if they've given one, or if there's a state change my $name = $problem->{anonymous} ? undef : $problem->{name}; my $update = $input{update} ? $input{update} : 'Questionnaire filled in by problem reporter'; - dbh()->do("insert into comment - (problem_id, name, email, website, text, state, mark_fixed, mark_open) - values (?, ?, ?, ?, ?, 'confirmed', ?, ?)", {}, - $problem->{id}, $name, $problem->{email}, '', $update, - $new_state eq 'fixed' ? 't' : 'f', $new_state eq 'confirmed' ? 't' : 'f' + Page::workaround_pg_bytea("insert into comment + (problem_id, name, email, website, text, state, mark_fixed, mark_open, photo) + values (?, ?, ?, '', ?, 'confirmed', ?, ?, ?)", 7, + $problem->{id}, $name, $problem->{email}, $update, + $new_state eq 'fixed' ? 't' : 'f', $new_state eq 'confirmed' ? 't' : 'f', + $image ) if $new_state || $input{update}; # If they've said they want another questionnaire, mark as such dbh()->do("update problem set send_questionnaire = 't' where id=?", {}, $problem->{id}) if ($input{been_fixed} eq 'No' || $input{been_fixed} eq 'Unknown') && $input{another} eq 'Yes'; - dbh()->commit(); + my $out; if ($input{been_fixed} eq 'Unknown') { $out = <<EOF; @@ -224,6 +241,17 @@ EOF your experience of getting the problem fixed?</p> <p><textarea name="update" style="max-width:90%" rows="7" cols="30">$input_h{update}</textarea></p> +<div id="fileupload_flashUI" style="display:none"> +<label for="form_photo">Photo:</label> +<input type="text" id="txtfilename" disabled="true" style="background-color: #ffffff;"> +<input type="button" value="Browse..." onclick="document.getElementById('txtfilename').value=''; swfu.cancelUpload(); swfu.selectFile();"> +<input type="hidden" name="upload_fileid" id="upload_fileid" value=""> +</div> +<div id="fileupload_normalUI"> +<label for="form_photo">Photo:</label> +<input type="file" name="photo" id="form_photo"> +</div> + <div id="another_qn"> <p>Would you like to receive another questionnaire in 4 weeks, reminding you to check the status?</p> <p align="center"> |