2 package IkiWiki::Plugin::attachment;
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);
15 sub getsetup () { #{{{
19 example => "clamdscan -",
20 description => "virus checker program (reads STDIN, returns nonzero if virus found)",
24 allowed_attachments => {
26 example => "mimetype(image/*) and maxsize(50kb)",
27 description => "enhanced PageSpec specifying what attachments are allowed",
28 description_html => htmllink("", "",
29 "ikiwiki/PageSpec/attachment",
31 linktext => "enhanced PageSpec",
32 )." specifying what attachments are allowed",
38 sub check_canattach ($$;$) { #{{{
40 my $dest=shift; # where it's going to be put, under the srcdir
41 my $file=shift; # the path to the attachment currently
43 # Don't allow an attachment to be uploaded with the same name as an
45 if (exists $pagesources{$dest} && $pagesources{$dest} ne $dest) {
46 error(sprintf(gettext("there is already a page named %s"), $dest));
49 # Use a special pagespec to test that the attachment is valid.
51 if (defined $config{allowed_attachments} &&
52 length $config{allowed_attachments}) {
53 $allowed=pagespec_match($dest,
54 $config{allowed_attachments},
56 user => $session->param("name"),
57 ip => $ENV{REMOTE_ADDR},
61 # XXX deprecated, should be removed eventually
63 foreach my $admin (@{$config{adminuser}}) {
64 my $allowed_attachments=IkiWiki::userinfo_get($admin, "allowed_attachments");
65 if (defined $allowed_attachments &&
66 length $allowed_attachments) {
67 $allowed=pagespec_match($dest,
70 user => $session->param("name"),
71 ip => $ENV{REMOTE_ADDR},
79 error(gettext("prohibited by allowed_attachments")." ($allowed)");
86 sub checkconfig () { #{{{
87 $config{cgi_disable_uploads}=0;
90 sub formbuilder_setup (@) { #{{{
92 my $form=$params{form};
95 if (defined $form->field("do") && $form->field("do") eq "edit") {
96 # Add attachment field, set type to multipart.
97 $form->enctype(&CGI::MULTIPART);
98 $form->field(name => 'attachment', type => 'file');
99 # These buttons are not put in the usual place, so
100 # are not added to the normal formbuilder button list.
101 $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
102 $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
104 # Add the javascript from the toggle plugin;
105 # the attachments interface uses it to toggle visibility.
106 require IkiWiki::Plugin::toggle;
107 $form->tmpl_param("javascript" => $IkiWiki::Plugin::toggle::javascript);
108 # Start with the attachments interface toggled invisible,
109 # but if it was used, keep it open.
110 if ($form->submitted ne "Upload Attachment" &&
111 (! defined $q->param("attachment_select") ||
112 ! length $q->param("attachment_select"))) {
113 $form->tmpl_param("attachments-class" => "toggleable");
116 $form->tmpl_param("attachments-class" => "toggleable-open");
119 elsif ($form->title eq "preferences") {
120 # XXX deprecated, should remove eventually
121 my $session=$params{session};
122 my $user_name=$session->param("name");
124 $form->field(name => "allowed_attachments", size => 50,
126 comment => "deprecated; please move to allowed_attachments in setup file",
128 if (! IkiWiki::is_admin($user_name)) {
129 $form->field(name => "allowed_attachments", type => "hidden");
131 if (! $form->submitted) {
132 my $value=IkiWiki::userinfo_get($user_name, "allowed_attachments");
134 $form->field(name => "allowed_attachments", force => 1,
135 value => IkiWiki::userinfo_get($user_name, "allowed_attachments"));
138 $form->field(name => "allowed_attachments", type => "hidden");
141 if ($form->submitted && $form->submitted eq 'Save Preferences') {
142 if (defined $form->field("allowed_attachments")) {
143 IkiWiki::userinfo_set($user_name, "allowed_attachments",
144 $form->field("allowed_attachments")) ||
145 error("failed to set allowed_attachments");
151 sub formbuilder (@) { #{{{
153 my $form=$params{form};
156 return if ! defined $form->field("do") || $form->field("do") ne "edit";
158 my $filename=$q->param('attachment');
159 if (defined $filename && length $filename &&
160 ($form->submitted eq "Upload Attachment" || $form->submitted eq "Save Page")) {
161 my $session=$params{session};
163 # This is an (apparently undocumented) way to get the name
164 # of the temp file that CGI writes the upload to.
165 my $tempfile=$q->tmpFileName($filename);
166 if (! defined $tempfile || ! length $tempfile) {
167 # perl 5.8 needs an alternative, awful method
168 if ($q =~ /HASH/ && exists $q->{'.tmpfiles'}) {
169 foreach my $key (keys(%{$q->{'.tmpfiles'}})) {
170 $tempfile=$q->tmpFileName(\$key);
171 last if defined $tempfile && length $tempfile;
174 if (! defined $tempfile || ! length $tempfile) {
175 error("CGI::tmpFileName failed to return the uploaded file name");
179 $filename=IkiWiki::linkpage(
180 IkiWiki::possibly_foolish_untaint(
181 attachment_location($form->field('page')).
182 IkiWiki::basename($filename)));
183 if (IkiWiki::file_pruned($filename, $config{srcdir})) {
184 error(gettext("bad attachment filename"));
187 # Check that the user is allowed to edit a page with the
188 # name of the attachment.
189 IkiWiki::check_canedit($filename, $q, $session, 1);
190 # And that the attachment itself is acceptable.
191 check_canattach($session, $filename, $tempfile);
193 # Needed for fast_file_copy and for rendering below.
194 require IkiWiki::Render;
196 # Move the attachment into place.
197 # Try to use a fast rename; fall back to copying.
198 IkiWiki::prep_writefile($filename, $config{srcdir});
199 unlink($config{srcdir}."/".$filename);
200 if (rename($tempfile, $config{srcdir}."/".$filename)) {
201 # The temp file has tight permissions; loosen up.
202 chmod(0666 & ~umask, $config{srcdir}."/".$filename);
205 my $fh=$q->upload('attachment');
206 if (! defined $fh || ! ref $fh) {
207 # needed by old CGI versions
208 $fh=$q->param('attachment');
209 if (! defined $fh || ! ref $fh) {
210 # even that doesn't always work,
211 # fall back to opening the tempfile
213 open($fh, "<", $tempfile) || error("failed to open \"$tempfile\": $!");
217 writefile($filename, $config{srcdir}, undef, 1, sub {
218 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
222 # Check the attachment in and trigger a wiki refresh.
224 IkiWiki::rcs_add($filename);
225 IkiWiki::disable_commit_hook();
226 IkiWiki::rcs_commit($filename, gettext("attachment upload"),
227 IkiWiki::rcs_prepedit($filename),
228 $session->param("name"), $ENV{REMOTE_ADDR});
229 IkiWiki::enable_commit_hook();
230 IkiWiki::rcs_update();
233 IkiWiki::saveindex();
235 elsif ($form->submitted eq "Insert Links") {
236 my $page=quotemeta($q->param("page"));
238 foreach my $f ($q->param("attachment_select")) {
242 $form->field(name => 'editcontent',
243 value => $form->field('editcontent')."\n\n".$add,
244 force => 1) if length $add;
247 # Generate the attachment list only after having added any new
249 $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
252 sub attachment_location ($) { #{{{
255 # Put the attachment in a subdir of the page it's attached
256 # to, unless that page is an "index" page.
257 $page=~s/(^|\/)index//;
258 $page.="/" if length $page;
263 sub attachment_list ($) { #{{{
265 my $loc=attachment_location($page);
268 foreach my $f (values %pagesources) {
269 if (! defined IkiWiki::pagetype($f) &&
270 $f=~m/^\Q$loc\E[^\/]+$/ &&
271 -e "$config{srcdir}/$f") {
273 "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
274 link => htmllink($page, $page, $f, noimageinline => 1),
275 size => humansize((stat(_))[7]),
276 mtime => displaytime($IkiWiki::pagemtime{$f}),
277 mtime_raw => $IkiWiki::pagemtime{$f},
282 # Sort newer attachments to the top of the list, so a newly-added
283 # attachment appears just before the form used to add it.
284 return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;
287 my %units=( #{{{ # size in bytes
312 zettabyte => 2 ** 70,
315 yottabyte => 2 ** 80,
316 # ikiwiki, if you find you need larger data quantities, either modify
317 # yourself to add them, or travel back in time to 2008 and kill me.
321 sub parsesize ($) { #{{{
325 my $base=$size+0; # force to number
327 foreach my $unit (sort keys %units) {
328 if ($size=~/[0-9\s]\Q$unit\E$/i) {
329 return $base * $units{$unit};
335 sub humansize ($) { #{{{
338 foreach my $unit (reverse sort { $units{$a} <=> $units{$b} || $b cmp $a } keys %units) {
339 if ($size / $units{$unit} > 0.25) {
340 return (int($size / $units{$unit} * 10)/10).$unit;
343 return $size; # near zero, or negative
346 package IkiWiki::PageSpec;
348 sub match_maxsize ($$;@) { #{{{
350 my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
352 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
356 if (! exists $params{file}) {
357 return IkiWiki::FailReason->new("no file specified");
360 if (-s $params{file} > $maxsize) {
361 return IkiWiki::FailReason->new("file too large (".(-s $params{file})." > $maxsize)");
364 return IkiWiki::SuccessReason->new("file not too large");
368 sub match_minsize ($$;@) { #{{{
370 my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
372 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
376 if (! exists $params{file}) {
377 return IkiWiki::FailReason->new("no file specified");
380 if (-s $params{file} < $minsize) {
381 return IkiWiki::FailReason->new("file too small");
384 return IkiWiki::SuccessReason->new("file not too small");
388 sub match_mimetype ($$;@) { #{{{
393 if (! exists $params{file}) {
394 return IkiWiki::FailReason->new("no file specified");
397 # Use ::magic to get the mime type, the idea is to only trust
398 # data obtained by examining the actual file contents.
399 eval q{use File::MimeInfo::Magic};
401 return IkiWiki::FailReason->new("failed to load File::MimeInfo::Magic ($@); cannot check MIME type");
403 my $mimetype=File::MimeInfo::Magic::magic($params{file});
404 if (! defined $mimetype) {
408 my $regexp=IkiWiki::glob2re($wanted);
409 if ($mimetype!~/^$regexp$/i) {
410 return IkiWiki::FailReason->new("file MIME type is $mimetype, not $wanted");
413 return IkiWiki::SuccessReason->new("file MIME type is $mimetype");
417 sub match_virusfree ($$;@) { #{{{
422 if (! exists $params{file}) {
423 return IkiWiki::FailReason->new("no file specified");
426 if (! exists $IkiWiki::config{virus_checker} ||
427 ! length $IkiWiki::config{virus_checker}) {
428 return IkiWiki::FailReason->new("no virus_checker configured");
431 # The file needs to be fed into the virus checker on stdin,
432 # because the file is not world-readable, and if clamdscan is
433 # used, clamd would fail to read it.
434 eval q{use IPC::Open2};
436 open (IN, "<", $params{file}) || return IkiWiki::FailReason->new("failed to read file");
439 $SIG{PIPE} = sub { $sigpipe=1 };
440 my $pid=open2(\*CHECKER_OUT, "<&IN", $IkiWiki::config{virus_checker});
441 my $reason=<CHECKER_OUT>;
443 1 while (<CHECKER_OUT>);
446 $SIG{PIPE}="DEFAULT";
447 if ($sigpipe || $?) {
448 if (! length $reason) {
449 $reason="virus checker $IkiWiki::config{virus_checker}; failed with no output";
451 return IkiWiki::FailReason->new("file seems to contain a virus ($reason)");
454 return IkiWiki::SuccessReason->new("file seems virusfree ($reason)");
458 sub match_ispage ($$;@) { #{{{
461 if (defined IkiWiki::pagetype($filename)) {
462 return IkiWiki::SuccessReason->new("file is a wiki page");
465 return IkiWiki::FailReason->new("file is not a wiki page");
469 sub match_user ($$;@) { #{{{
474 if (! exists $params{user}) {
475 return IkiWiki::FailReason->new("no user specified");
478 if (defined $params{user} && lc $params{user} eq lc $user) {
479 return IkiWiki::SuccessReason->new("user is $user");
481 elsif (! defined $params{user}) {
482 return IkiWiki::FailReason->new("not logged in");
485 return IkiWiki::FailReason->new("user is $params{user}, not $user");
489 sub match_ip ($$;@) { #{{{
494 if (! exists $params{ip}) {
495 return IkiWiki::FailReason->new("no IP specified");
498 if (defined $params{ip} && lc $params{ip} eq lc $ip) {
499 return IkiWiki::SuccessReason->new("IP is $ip");
502 return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");