]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/attachment.pm
don't special case preview
[ikiwiki.git] / IkiWiki / Plugin / attachment.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::attachment;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 sub import { #{{{
9         hook(type => "getsetup", id => "attachment", call => \&getsetup);
10         hook(type => "checkconfig", id => "attachment", call => \&checkconfig);
11         hook(type => "formbuilder_setup", id => "attachment", call => \&formbuilder_setup);
12         hook(type => "formbuilder", id => "attachment", call => \&formbuilder);
13         IkiWiki::loadplugin("filecheck");
14 } # }}}
15
16 sub getsetup () { #{{{
17         return
18                 plugin => {
19                         safe => 1,
20                         rebuild => 0,
21                 },
22                 allowed_attachments => {
23                         type => "pagespec",
24                         example => "virusfree() and mimetype(image/*) and maxsize(50kb)",
25                         description => "enhanced PageSpec specifying what attachments are allowed",
26                         link => "ikiwiki/PageSpec/attachment",
27                         safe => 1,
28                         rebuild => 0,
29                 },
30                 virus_checker => {
31                         type => "string",
32                         example => "clamdscan -",
33                         description => "virus checker program (reads STDIN, returns nonzero if virus found)",
34                         safe => 0, # executed
35                         rebuild => 0,
36                 },
37 } #}}}
38
39 sub check_canattach ($$;$) { #{{{
40         my $session=shift;
41         my $dest=shift; # where it's going to be put, under the srcdir
42         my $file=shift; # the path to the attachment currently
43
44         # Don't allow an attachment to be uploaded with the same name as an
45         # existing page.
46         if (exists $IkiWiki::pagesources{$dest} &&
47             $IkiWiki::pagesources{$dest} ne $dest) {
48                 error(sprintf(gettext("there is already a page named %s"), $dest));
49         }
50
51         # Use a special pagespec to test that the attachment is valid.
52         my $allowed=1;
53         if (defined $config{allowed_attachments} &&
54             length $config{allowed_attachments}) {
55                 $allowed=pagespec_match($dest,
56                         $config{allowed_attachments},
57                         file => $file,
58                         user => $session->param("name"),
59                         ip => $ENV{REMOTE_ADDR},
60                 );
61         }
62
63         # XXX deprecated, should be removed eventually
64         if ($allowed) {
65                 foreach my $admin (@{$config{adminuser}}) {
66                         my $allowed_attachments=IkiWiki::userinfo_get($admin, "allowed_attachments");
67                         if (defined $allowed_attachments &&
68                             length $allowed_attachments) {
69                                 $allowed=pagespec_match($dest,
70                                         $allowed_attachments,
71                                         file => $file,
72                                         user => $session->param("name"),
73                                         ip => $ENV{REMOTE_ADDR},
74                                 );
75                                 last if $allowed;
76                         }
77                 }
78         }
79
80         if (! $allowed) {
81                 error(gettext("prohibited by allowed_attachments")." ($allowed)");
82         }
83         else {
84                 return 1;
85         }
86 } #}}}
87
88 sub checkconfig () { #{{{
89         $config{cgi_disable_uploads}=0;
90 } #}}}
91
92 sub formbuilder_setup (@) { #{{{
93         my %params=@_;
94         my $form=$params{form};
95         my $q=$params{cgi};
96
97         if (defined $form->field("do") && $form->field("do") eq "edit") {
98                 # Add attachment field, set type to multipart.
99                 $form->enctype(&CGI::MULTIPART);
100                 $form->field(name => 'attachment', type => 'file');
101                 # These buttons are not put in the usual place, so
102                 # are not added to the normal formbuilder button list.
103                 $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
104                 $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
105
106                 # Add the javascript from the toggle plugin;
107                 # the attachments interface uses it to toggle visibility.
108                 require IkiWiki::Plugin::toggle;
109                 $form->tmpl_param("javascript" => $IkiWiki::Plugin::toggle::javascript);
110                 # Start with the attachments interface toggled invisible,
111                 # but if it was used, keep it open.
112                 if ($form->submitted ne "Upload Attachment" &&
113                     (! defined $q->param("attachment_select") ||
114                     ! length $q->param("attachment_select"))) {
115                         $form->tmpl_param("attachments-class" => "toggleable");
116                 }
117                 else {
118                         $form->tmpl_param("attachments-class" => "toggleable-open");
119                 }
120         }
121         elsif ($form->title eq "preferences") {
122                 # XXX deprecated, should remove eventually
123                 my $session=$params{session};
124                 my $user_name=$session->param("name");
125
126                 $form->field(name => "allowed_attachments", size => 50,
127                         fieldset => "admin",
128                         comment => "deprecated; please move to allowed_attachments in setup file",
129                 );
130                 if (! IkiWiki::is_admin($user_name)) {
131                         $form->field(name => "allowed_attachments", type => "hidden");
132                 }
133                 if (! $form->submitted) {
134                         my $value=IkiWiki::userinfo_get($user_name, "allowed_attachments");
135                         if (length $value) {
136                                 $form->field(name => "allowed_attachments", force => 1,
137                                         value => IkiWiki::userinfo_get($user_name, "allowed_attachments"));
138                         }
139                         else {
140                                 $form->field(name => "allowed_attachments", type => "hidden");
141                         }
142                 }
143                 if ($form->submitted && $form->submitted eq 'Save Preferences') {
144                         if (defined $form->field("allowed_attachments")) {
145                                 IkiWiki::userinfo_set($user_name, "allowed_attachments",
146                                 $form->field("allowed_attachments")) ||
147                                         error("failed to set allowed_attachments");
148                                 if (! length $form->field("allowed_attachments")) {
149                                         $form->field(name => "allowed_attachments", type => "hidden");
150                                 }
151                         }
152                 }
153         }
154 } #}}}
155
156 sub formbuilder (@) { #{{{
157         my %params=@_;
158         my $form=$params{form};
159         my $q=$params{cgi};
160
161         return if ! defined $form->field("do") || $form->field("do") ne "edit";
162
163         my $filename=$q->param('attachment');
164         if (defined $filename && length $filename &&
165             ($form->submitted eq "Upload Attachment" || $form->submitted eq "Save Page")) {
166                 my $session=$params{session};
167                 
168                 # This is an (apparently undocumented) way to get the name
169                 # of the temp file that CGI writes the upload to.
170                 my $tempfile=$q->tmpFileName($filename);
171                 if (! defined $tempfile || ! length $tempfile) {
172                         # perl 5.8 needs an alternative, awful method
173                         if ($q =~ /HASH/ && exists $q->{'.tmpfiles'}) {
174                                 foreach my $key (keys(%{$q->{'.tmpfiles'}})) {
175                                         $tempfile=$q->tmpFileName(\$key);
176                                         last if defined $tempfile && length $tempfile;
177                                 }
178                         }
179                         if (! defined $tempfile || ! length $tempfile) {
180                                 error("CGI::tmpFileName failed to return the uploaded file name");
181                         }
182                 }
183
184                 $filename=linkpage(IkiWiki::possibly_foolish_untaint(
185                                 attachment_location($form->field('page')).
186                                 IkiWiki::basename($filename)));
187                 if (IkiWiki::file_pruned($filename, $config{srcdir})) {
188                         error(gettext("bad attachment filename"));
189                 }
190                 
191                 # Check that the user is allowed to edit a page with the
192                 # name of the attachment.
193                 IkiWiki::check_canedit($filename, $q, $session, 1);
194                 # And that the attachment itself is acceptable.
195                 check_canattach($session, $filename, $tempfile);
196
197                 # Needed for fast_file_copy and for rendering below.
198                 require IkiWiki::Render;
199
200                 # Move the attachment into place.
201                 # Try to use a fast rename; fall back to copying.
202                 IkiWiki::prep_writefile($filename, $config{srcdir});
203                 unlink($config{srcdir}."/".$filename);
204                 if (rename($tempfile, $config{srcdir}."/".$filename)) {
205                         # The temp file has tight permissions; loosen up.
206                         chmod(0666 & ~umask, $config{srcdir}."/".$filename);
207                 }
208                 else {
209                         my $fh=$q->upload('attachment');
210                         if (! defined $fh || ! ref $fh) {
211                                 # needed by old CGI versions
212                                 $fh=$q->param('attachment');
213                                 if (! defined $fh || ! ref $fh) {
214                                         # even that doesn't always work,
215                                         # fall back to opening the tempfile
216                                         $fh=undef;
217                                         open($fh, "<", $tempfile) || error("failed to open \"$tempfile\": $!");
218                                 }
219                         }
220                         binmode($fh);
221                         writefile($filename, $config{srcdir}, undef, 1, sub {
222                                 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
223                         });
224                 }
225
226                 # Check the attachment in and trigger a wiki refresh.
227                 if ($config{rcs}) {
228                         IkiWiki::rcs_add($filename);
229                         IkiWiki::disable_commit_hook();
230                         IkiWiki::rcs_commit($filename, gettext("attachment upload"),
231                                 IkiWiki::rcs_prepedit($filename),
232                                 $session->param("name"), $ENV{REMOTE_ADDR});
233                         IkiWiki::enable_commit_hook();
234                         IkiWiki::rcs_update();
235                 }
236                 IkiWiki::refresh();
237                 IkiWiki::saveindex();
238         }
239         elsif ($form->submitted eq "Insert Links") {
240                 my $page=quotemeta($q->param("page"));
241                 my $add="";
242                 foreach my $f ($q->param("attachment_select")) {
243                         $f=~s/^$page\///;
244                         $add.="[[$f]]\n";
245                 }
246                 $form->field(name => 'editcontent',
247                         value => $form->field('editcontent')."\n\n".$add,
248                         force => 1) if length $add;
249         }
250         
251         # Generate the attachment list only after having added any new
252         # attachments.
253         $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
254 } # }}}
255
256 sub attachment_location ($) { #{{{
257         my $page=shift;
258         
259         # Put the attachment in a subdir of the page it's attached
260         # to, unless that page is an "index" page.
261         $page=~s/(^|\/)index//;
262         $page.="/" if length $page;
263         
264         return $page;
265 } #}}}
266
267 sub attachment_list ($) { #{{{
268         my $page=shift;
269         my $loc=attachment_location($page);
270
271         my @ret;
272         foreach my $f (values %pagesources) {
273                 if (! defined pagetype($f) &&
274                     $f=~m/^\Q$loc\E[^\/]+$/ &&
275                     -e "$config{srcdir}/$f") {
276                         push @ret, {
277                                 "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
278                                 link => htmllink($page, $page, $f, noimageinline => 1),
279                                 size => IkiWiki::Plugin::filecheck::humansize((stat(_))[7]),
280                                 mtime => displaytime($IkiWiki::pagemtime{$f}),
281                                 mtime_raw => $IkiWiki::pagemtime{$f},
282                         };
283                 }
284         }
285
286         # Sort newer attachments to the top of the list, so a newly-added
287         # attachment appears just before the form used to add it.
288         return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;
289 } #}}}
290
291 package IkiWiki::PageSpec;
292
293 sub match_user ($$;@) { #{{{
294         shift;
295         my $user=shift;
296         my %params=@_;
297         
298         if (! exists $params{user}) {
299                 return IkiWiki::FailReason->new("no user specified");
300         }
301
302         if (defined $params{user} && lc $params{user} eq lc $user) {
303                 return IkiWiki::SuccessReason->new("user is $user");
304         }
305         elsif (! defined $params{user}) {
306                 return IkiWiki::FailReason->new("not logged in");
307         }
308         else {
309                 return IkiWiki::FailReason->new("user is $params{user}, not $user");
310         }
311 } #}}}
312
313 sub match_admin ($$;@) { #{{{
314         shift;
315         shift;
316         my %params=@_;
317         
318         if (! exists $params{user}) {
319                 return IkiWiki::FailReason->new("no user specified");
320         }
321
322         if (defined $params{user} && IkiWiki::is_admin($params{user})) {
323                 return IkiWiki::SuccessReason->new("user is an admin");
324         }
325         elsif (! defined $params{user}) {
326                 return IkiWiki::FailReason->new("not logged in");
327         }
328         else {
329                 return IkiWiki::FailReason->new("user is not an admin");
330         }
331 } #}}}
332
333 sub match_ip ($$;@) { #{{{
334         shift;
335         my $ip=shift;
336         my %params=@_;
337         
338         if (! exists $params{ip}) {
339                 return IkiWiki::FailReason->new("no IP specified");
340         }
341
342         if (defined $params{ip} && lc $params{ip} eq lc $ip) {
343                 return IkiWiki::SuccessReason->new("IP is $ip");
344         }
345         else {
346                 return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");
347         }
348 } #}}}
349
350 1