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