]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/attachment.pm
only darcs add files not yet in version control
[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             $form->field("do") eq "create")) {
99                 # Add attachment field, set type to multipart.
100                 $form->enctype(&CGI::MULTIPART);
101                 $form->field(name => 'attachment', type => 'file');
102                 # These buttons are not put in the usual place, so
103                 # are not added to the normal formbuilder button list.
104                 $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
105                 $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
106
107                 # Add the javascript from the toggle plugin;
108                 # the attachments interface uses it to toggle visibility.
109                 require IkiWiki::Plugin::toggle;
110                 $form->tmpl_param("javascript" => $IkiWiki::Plugin::toggle::javascript);
111                 # Start with the attachments interface toggled invisible,
112                 # but if it was used, keep it open.
113                 if ($form->submitted ne "Upload Attachment" &&
114                     (! defined $q->param("attachment_select") ||
115                     ! length $q->param("attachment_select"))) {
116                         $form->tmpl_param("attachments-class" => "toggleable");
117                 }
118                 else {
119                         $form->tmpl_param("attachments-class" => "toggleable-open");
120                 }
121         }
122         elsif ($form->title eq "preferences") {
123                 # XXX deprecated, should remove eventually
124                 my $session=$params{session};
125                 my $user_name=$session->param("name");
126
127                 $form->field(name => "allowed_attachments", size => 50,
128                         fieldset => "admin",
129                         comment => "deprecated; please move to allowed_attachments in setup file",
130                 );
131                 if (! IkiWiki::is_admin($user_name)) {
132                         $form->field(name => "allowed_attachments", type => "hidden");
133                 }
134                 if (! $form->submitted) {
135                         my $value=IkiWiki::userinfo_get($user_name, "allowed_attachments");
136                         if (length $value) {
137                                 $form->field(name => "allowed_attachments", force => 1,
138                                         value => IkiWiki::userinfo_get($user_name, "allowed_attachments"));
139                         }
140                         else {
141                                 $form->field(name => "allowed_attachments", type => "hidden");
142                         }
143                 }
144                 if ($form->submitted && $form->submitted eq 'Save Preferences') {
145                         if (defined $form->field("allowed_attachments")) {
146                                 IkiWiki::userinfo_set($user_name, "allowed_attachments",
147                                 $form->field("allowed_attachments")) ||
148                                         error("failed to set allowed_attachments");
149                                 if (! length $form->field("allowed_attachments")) {
150                                         $form->field(name => "allowed_attachments", type => "hidden");
151                                 }
152                         }
153                 }
154         }
155 } #}}}
156
157 sub formbuilder (@) { #{{{
158         my %params=@_;
159         my $form=$params{form};
160         my $q=$params{cgi};
161
162         return if ! defined $form->field("do") || ($form->field("do") ne "edit" && $form->field("do") ne "create") ;
163
164         my $filename=$q->param('attachment');
165         if (defined $filename && length $filename &&
166             ($form->submitted eq "Upload Attachment" || $form->submitted eq "Save Page")) {
167                 my $session=$params{session};
168                 
169                 # This is an (apparently undocumented) way to get the name
170                 # of the temp file that CGI writes the upload to.
171                 my $tempfile=$q->tmpFileName($filename);
172                 if (! defined $tempfile || ! length $tempfile) {
173                         # perl 5.8 needs an alternative, awful method
174                         if ($q =~ /HASH/ && exists $q->{'.tmpfiles'}) {
175                                 foreach my $key (keys(%{$q->{'.tmpfiles'}})) {
176                                         $tempfile=$q->tmpFileName(\$key);
177                                         last if defined $tempfile && length $tempfile;
178                                 }
179                         }
180                         if (! defined $tempfile || ! length $tempfile) {
181                                 error("CGI::tmpFileName failed to return the uploaded file name");
182                         }
183                 }
184
185                 $filename=linkpage(IkiWiki::possibly_foolish_untaint(
186                                 attachment_location($form->field('page')).
187                                 IkiWiki::basename($filename)));
188                 if (IkiWiki::file_pruned($filename, $config{srcdir})) {
189                         error(gettext("bad attachment filename"));
190                 }
191                 
192                 # Check that the user is allowed to edit a page with the
193                 # name of the attachment.
194                 IkiWiki::check_canedit($filename, $q, $session, 1);
195                 # And that the attachment itself is acceptable.
196                 check_canattach($session, $filename, $tempfile);
197
198                 # Needed for fast_file_copy and for rendering below.
199                 require IkiWiki::Render;
200
201                 # Move the attachment into place.
202                 # Try to use a fast rename; fall back to copying.
203                 IkiWiki::prep_writefile($filename, $config{srcdir});
204                 unlink($config{srcdir}."/".$filename);
205                 if (rename($tempfile, $config{srcdir}."/".$filename)) {
206                         # The temp file has tight permissions; loosen up.
207                         chmod(0666 & ~umask, $config{srcdir}."/".$filename);
208                 }
209                 else {
210                         my $fh=$q->upload('attachment');
211                         if (! defined $fh || ! ref $fh) {
212                                 # needed by old CGI versions
213                                 $fh=$q->param('attachment');
214                                 if (! defined $fh || ! ref $fh) {
215                                         # even that doesn't always work,
216                                         # fall back to opening the tempfile
217                                         $fh=undef;
218                                         open($fh, "<", $tempfile) || error("failed to open \"$tempfile\": $!");
219                                 }
220                         }
221                         binmode($fh);
222                         writefile($filename, $config{srcdir}, undef, 1, sub {
223                                 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
224                         });
225                 }
226
227                 # Check the attachment in and trigger a wiki refresh.
228                 if ($config{rcs}) {
229                         IkiWiki::rcs_add($filename);
230                         IkiWiki::disable_commit_hook();
231                         IkiWiki::rcs_commit($filename, gettext("attachment upload"),
232                                 IkiWiki::rcs_prepedit($filename),
233                                 $session->param("name"), $ENV{REMOTE_ADDR});
234                         IkiWiki::enable_commit_hook();
235                         IkiWiki::rcs_update();
236                 }
237                 IkiWiki::refresh();
238                 IkiWiki::saveindex();
239         }
240         elsif ($form->submitted eq "Insert Links") {
241                 my $page=quotemeta($q->param("page"));
242                 my $add="";
243                 foreach my $f ($q->param("attachment_select")) {
244                         $f=~s/^$page\///;
245                         $add.="[[$f]]\n";
246                 }
247                 $form->field(name => 'editcontent',
248                         value => $form->field('editcontent')."\n\n".$add,
249                         force => 1) if length $add;
250         }
251         
252         # Generate the attachment list only after having added any new
253         # attachments.
254         $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
255 } # }}}
256
257 sub attachment_location ($) { #{{{
258         my $page=shift;
259         
260         # Put the attachment in a subdir of the page it's attached
261         # to, unless that page is an "index" page.
262         $page=~s/(^|\/)index//;
263         $page.="/" if length $page;
264         
265         return $page;
266 } #}}}
267
268 sub attachment_list ($) { #{{{
269         my $page=shift;
270         my $loc=attachment_location($page);
271
272         my @ret;
273         foreach my $f (values %pagesources) {
274                 if (! defined pagetype($f) &&
275                     $f=~m/^\Q$loc\E[^\/]+$/ &&
276                     -e "$config{srcdir}/$f") {
277                         push @ret, {
278                                 "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
279                                 link => htmllink($page, $page, $f, noimageinline => 1),
280                                 size => IkiWiki::Plugin::filecheck::humansize((stat(_))[7]),
281                                 mtime => displaytime($IkiWiki::pagemtime{$f}),
282                                 mtime_raw => $IkiWiki::pagemtime{$f},
283                         };
284                 }
285         }
286
287         # Sort newer attachments to the top of the list, so a newly-added
288         # attachment appears just before the form used to add it.
289         return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;
290 } #}}}
291
292 1