]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/attachment.pm
progress: switch to new pagespec_match_list
[git.ikiwiki.info.git] / IkiWiki / Plugin / attachment.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::attachment;
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
8 sub import {
9         add_underlay("javascript");
10         hook(type => "getsetup", id => "attachment", call => \&getsetup);
11         hook(type => "checkconfig", id => "attachment", call => \&checkconfig);
12         hook(type => "formbuilder_setup", id => "attachment", call => \&formbuilder_setup);
13         hook(type => "formbuilder", id => "attachment", call => \&formbuilder);
14         IkiWiki::loadplugin("filecheck");
15 }
17 sub getsetup () {
18         return
19                 plugin => {
20                         safe => 1,
21                         rebuild => 0,
22                 },
23                 allowed_attachments => {
24                         type => "pagespec",
25                         example => "virusfree() and mimetype(image/*) and maxsize(50kb)",
26                         description => "enhanced PageSpec specifying what attachments are allowed",
27                         link => "ikiwiki/PageSpec/attachment",
28                         safe => 1,
29                         rebuild => 0,
30                 },
31                 virus_checker => {
32                         type => "string",
33                         example => "clamdscan -",
34                         description => "virus checker program (reads STDIN, returns nonzero if virus found)",
35                         safe => 0, # executed
36                         rebuild => 0,
37                 },
38 }
40 sub check_canattach ($$;$) {
41         my $session=shift;
42         my $dest=shift; # where it's going to be put, under the srcdir
43         my $file=shift; # the path to the attachment currently
45         # Don't allow an attachment to be uploaded with the same name as an
46         # existing page.
47         if (exists $IkiWiki::pagesources{$dest} &&
48             $IkiWiki::pagesources{$dest} ne $dest) {
49                 error(sprintf(gettext("there is already a page named %s"), $dest));
50         }
52         # Use a special pagespec to test that the attachment is valid.
53         my $allowed=1;
54         if (defined $config{allowed_attachments} &&
55             length $config{allowed_attachments}) {
56                 $allowed=pagespec_match($dest,
57                         $config{allowed_attachments},
58                         file => $file,
59                         user => $session->param("name"),
60                         ip => $ENV{REMOTE_ADDR},
61                 );
62         }
64         if (! $allowed) {
65                 error(gettext("prohibited by allowed_attachments")." ($allowed)");
66         }
67         else {
68                 return 1;
69         }
70 }
72 sub checkconfig () {
73         $config{cgi_disable_uploads}=0;
74 }
76 sub formbuilder_setup (@) {
77         my %params=@_;
78         my $form=$params{form};
79         my $q=$params{cgi};
81         if (defined $form->field("do") && ($form->field("do") eq "edit" ||
82             $form->field("do") eq "create")) {
83                 # Add attachment field, set type to multipart.
84                 $form->enctype(&CGI::MULTIPART);
85                 $form->field(name => 'attachment', type => 'file');
86                 # These buttons are not put in the usual place, so
87                 # are not added to the normal formbuilder button list.
88                 $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
89                 $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
91                 # Add the toggle javascript; the attachments interface uses
92                 # it to toggle visibility.
93                 require IkiWiki::Plugin::toggle;
94                 $form->tmpl_param("javascript" => IkiWiki::Plugin::toggle::include_javascript($params{page}, 1));
95                 # Start with the attachments interface toggled invisible,
96                 # but if it was used, keep it open.
97                 if ($form->submitted ne "Upload Attachment" &&
98                     (! defined $q->param("attachment_select") ||
99                     ! length $q->param("attachment_select"))) {
100                         $form->tmpl_param("attachments-class" => "toggleable");
101                 }
102                 else {
103                         $form->tmpl_param("attachments-class" => "toggleable-open");
104                 }
105         }
108 sub formbuilder (@) {
109         my %params=@_;
110         my $form=$params{form};
111         my $q=$params{cgi};
113         return if ! defined $form->field("do") || ($form->field("do") ne "edit" && $form->field("do") ne "create") ;
115         my $filename=$q->param('attachment');
116         if (defined $filename && length $filename &&
117             ($form->submitted eq "Upload Attachment" || $form->submitted eq "Save Page")) {
118                 my $session=$params{session};
119                 
120                 # This is an (apparently undocumented) way to get the name
121                 # of the temp file that CGI writes the upload to.
122                 my $tempfile=$q->tmpFileName($filename);
123                 if (! defined $tempfile || ! length $tempfile) {
124                         # perl 5.8 needs an alternative, awful method
125                         if ($q =~ /HASH/ && exists $q->{'.tmpfiles'}) {
126                                 foreach my $key (keys(%{$q->{'.tmpfiles'}})) {
127                                         $tempfile=$q->tmpFileName(\$key);
128                                         last if defined $tempfile && length $tempfile;
129                                 }
130                         }
131                         if (! defined $tempfile || ! length $tempfile) {
132                                 error("CGI::tmpFileName failed to return the uploaded file name");
133                         }
134                 }
136                 $filename=linkpage(IkiWiki::possibly_foolish_untaint(
137                                 attachment_location($form->field('page')).
138                                 IkiWiki::basename($filename)));
139                 if (IkiWiki::file_pruned($filename, $config{srcdir})) {
140                         error(gettext("bad attachment filename"));
141                 }
142                 
143                 # Check that the user is allowed to edit a page with the
144                 # name of the attachment.
145                 IkiWiki::check_canedit($filename, $q, $session, 1);
146                 # And that the attachment itself is acceptable.
147                 check_canattach($session, $filename, $tempfile);
149                 # Needed for fast_file_copy and for rendering below.
150                 require IkiWiki::Render;
152                 # Move the attachment into place.
153                 # Try to use a fast rename; fall back to copying.
154                 IkiWiki::prep_writefile($filename, $config{srcdir});
155                 unlink($config{srcdir}."/".$filename);
156                 if (rename($tempfile, $config{srcdir}."/".$filename)) {
157                         # The temp file has tight permissions; loosen up.
158                         chmod(0666 & ~umask, $config{srcdir}."/".$filename);
159                 }
160                 else {
161                         my $fh=$q->upload('attachment');
162                         if (! defined $fh || ! ref $fh) {
163                                 # needed by old CGI versions
164                                 $fh=$q->param('attachment');
165                                 if (! defined $fh || ! ref $fh) {
166                                         # even that doesn't always work,
167                                         # fall back to opening the tempfile
168                                         $fh=undef;
169                                         open($fh, "<", $tempfile) || error("failed to open \"$tempfile\": $!");
170                                 }
171                         }
172                         binmode($fh);
173                         writefile($filename, $config{srcdir}, undef, 1, sub {
174                                 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
175                         });
176                 }
178                 # Check the attachment in and trigger a wiki refresh.
179                 if ($config{rcs}) {
180                         IkiWiki::rcs_add($filename);
181                         IkiWiki::disable_commit_hook();
182                         IkiWiki::rcs_commit($filename, gettext("attachment upload"),
183                                 IkiWiki::rcs_prepedit($filename),
184                                 $session->param("name"), $ENV{REMOTE_ADDR});
185                         IkiWiki::enable_commit_hook();
186                         IkiWiki::rcs_update();
187                 }
188                 IkiWiki::refresh();
189                 IkiWiki::saveindex();
190         }
191         elsif ($form->submitted eq "Insert Links") {
192                 my $page=quotemeta($q->param("page"));
193                 my $add="";
194                 foreach my $f ($q->param("attachment_select")) {
195                         $f=~s/^$page\///;
196                         $add.="[[$f]]\n";
197                 }
198                 $form->field(name => 'editcontent',
199                         value => $form->field('editcontent')."\n\n".$add,
200                         force => 1) if length $add;
201         }
202         
203         # Generate the attachment list only after having added any new
204         # attachments.
205         $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
208 sub attachment_location ($) {
209         my $page=shift;
210         
211         # Put the attachment in a subdir of the page it's attached
212         # to, unless that page is an "index" page.
213         $page=~s/(^|\/)index//;
214         $page.="/" if length $page;
215         
216         return $page;
219 sub attachment_list ($) {
220         my $page=shift;
221         my $loc=attachment_location($page);
223         my @ret;
224         foreach my $f (values %pagesources) {
225                 if (! defined pagetype($f) &&
226                     $f=~m/^\Q$loc\E[^\/]+$/ &&
227                     -e "$config{srcdir}/$f") {
228                         push @ret, {
229                                 "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
230                                 link => htmllink($page, $page, $f, noimageinline => 1),
231                                 size => IkiWiki::Plugin::filecheck::humansize((stat(_))[7]),
232                                 mtime => displaytime($IkiWiki::pagemtime{$f}),
233                         };
234                 }
235         }
237         # Sort newer attachments to the top of the list, so a newly-added
238         # attachment appears just before the form used to add it.
239         return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;