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