+
+ # Used by the XP publishing wizard -- largely the same as the code above
+ # but vastly simplified. Should we refactor?
+ if ($r->method eq "POST") {
+ my $apr = Apache2::Request->new($r);
+ my $client_size = $apr->param('size');
+ my $event = $apr->param('event');
+
+ my $file = $apr->upload('image');
+ my $filename = $file->filename();
+ if ($client_size != $file->size()) {
+ $r->content_type('text/plain; charset="utf-8"');
+ $r->status(403);
+ $r->print("Client-size resizing detected; refusing automatically");
+
+ $r->log->info("Client-size resized upload of $event/$filename detected");
+ return Apache2::Const::OK;
+ }
+
+ # Ugh, Windows XP seems to be sending this in... something that's not UTF-8, at least
+ my $takenby_given = Sesse::pr0n::Common::guess_charset($apr->param('takenby'));
+
+ if (defined($takenby_given) && $takenby_given !~ /^\s*$/ && $takenby_given !~ /[<>&]/ && length($takenby_given) <= 100) {
+ $takenby = $takenby_given;
+ }
+
+ my $ne_id = Sesse::pr0n::Common::guess_charset($apr->param('neweventid'));
+ my $ne_date = Sesse::pr0n::Common::guess_charset($apr->param('neweventdate'));
+ my $ne_desc = Sesse::pr0n::Common::guess_charset($apr->param('neweventdesc'));
+ if (defined($ne_id)) {
+ # Trying to add a new event, let's see if it already exists
+ my $q = $dbh->prepare('SELECT COUNT(*) AS cnt FROM events WHERE event=? AND vhost=?')
+ or dberror($r, "Couldn't prepare event count");
+ $q->execute($ne_id, $r->get_server_name)
+ or dberror($r, "Couldn't execute event count");
+ my $ref = $q->fetchrow_hashref;
+
+ if ($ref->{'cnt'} == 0) {
+ my @errors = Sesse::pr0n::Common::add_new_event($dbh, $ne_id, $ne_date, $ne_desc, $r->get_server_name);
+ if (scalar @errors > 0) {
+ die "Couldn't add new event $ne_id: " . join(', ', @errors);
+ }
+ }
+
+ $event = $ne_id;
+ }
+
+ # Remove evil characters
+ if ($filename =~ /[^a-zA-Z0-9._-]/) {
+ $filename =~ tr/a-zA-Z0-9.-/_/c;
+ }
+
+ # Get the new ID
+ my $ref = $dbh->selectrow_hashref("SELECT NEXTVAL('imageid_seq') AS id;");
+ my $newid = $ref->{'id'};
+ if (!defined($newid)) {
+ dberror($r, "Couldn't get new ID");
+ }
+
+ # Autorename if we need to
+ {
+ my $ref = $dbh->selectrow_hashref("SELECT COUNT(*) AS numfiles FROM images WHERE vhost=? AND event=? AND filename=?",
+ undef, $r->get_server_name, $event, $filename)
+ or dberror($r, "Couldn't check for existing files");
+ if ($ref->{'numfiles'} > 0) {
+ $r->log->info("Renaming $filename to $newid.jpeg");
+ $filename = "$newid.jpeg";
+ }
+ }
+
+ {
+ # Enable transactions and error raising temporarily
+ local $dbh->{AutoCommit} = 0;
+ local $dbh->{RaiseError} = 1;
+ my $fname;
+
+ # Try to insert this new file
+ eval {
+ $dbh->do('INSERT INTO images (id,vhost,event,uploadedby,takenby,filename) VALUES (?,?,?,?,?,?)',
+ undef, $newid, $r->get_server_name, $event, $user, $takenby, $filename);
+ $dbh->do('UPDATE events SET last_update=CURRENT_TIMESTAMP WHERE vhost=? AND event=?',
+ undef, $r->get_server_name, $event);