]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/Plugin/attachment.pm
explicitly set form type to multipart
[git.ikiwiki.info.git] / IkiWiki / Plugin / attachment.pm
index cb762e453e9f92c368fed3784c1ad5820455e6f6..84bf643905f75dccf9122dc4a91bbfc201cc7079 100644 (file)
@@ -21,6 +21,8 @@ sub formbuilder_setup (@) { #{{{
        my $q=$params{cgi};
 
        if (defined $form->field("do") && $form->field("do") eq "edit") {
+               # Add attachment field, set type to multipart.
+               $form->enctype(&CGI::MULTIPART);
                $form->field(name => 'attachment', type => 'file');
                # These buttons are not put in the usual place, so
                # are not added to the normal formbuilder button list.
@@ -34,7 +36,8 @@ sub formbuilder_setup (@) { #{{{
                # Start with the attachments interface toggled invisible,
                # but if it was used, keep it open.
                if ($form->submitted ne "Upload Attachment" &&
-                   ! length $q->param("attachment_select")) {
+                   (! defined $q->param("attachment_select") ||
+                   ! length $q->param("attachment_select"))) {
                        $form->tmpl_param("attachments-class" => "toggleable");
                }
                else {
@@ -87,6 +90,10 @@ sub formbuilder (@) { #{{{
                # of the temp file that CGI writes the upload to.
                my $tempfile=$q->tmpFileName($filename);
                
+               if (! defined $tempfile || ! length $tempfile) {
+                       error("failed to determine tempfile name");
+               }
+
                $filename=IkiWiki::titlepage(
                        IkiWiki::possibly_foolish_untaint(
                                attachment_location($form->field('page')).
@@ -132,7 +139,14 @@ sub formbuilder (@) { #{{{
                else {
                        my $fh=$q->upload('attachment');
                        if (! defined $fh || ! ref $fh) {
-                               error("failed to get filehandle");
+                               # needed by old CGI versions
+                               $fh=$q->param('attachment');
+                               if (! defined $fh || ! ref $fh) {
+                                       # even that doesn't always work,
+                                       # fall back to opening the tempfile
+                                       $fh=undef;
+                                       open($fh, "<", $tempfile) || error("failed to open $tempfile: $!");
+                               }
                        }
                        binmode($fh);
                        writefile($filename, $config{srcdir}, undef, 1, sub {