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)",
26 sub check_canattach ($$;$) { #{{{
28 my $dest=shift; # where it's going to be put, under the srcdir
29 my $file=shift; # the path to the attachment currently
31 # Don't allow an attachment to be uploaded with the same name as an
33 if (exists $pagesources{$dest} && $pagesources{$dest} ne $dest) {
34 error(sprintf(gettext("there is already a page named %s"), $dest));
37 # Use a special pagespec to test that the attachment is valid.
39 foreach my $admin (@{$config{adminuser}}) {
40 my $allowed_attachments=IkiWiki::userinfo_get($admin, "allowed_attachments");
41 if (defined $allowed_attachments &&
42 length $allowed_attachments) {
43 $allowed=pagespec_match($dest,
46 user => $session->param("name"),
47 ip => $ENV{REMOTE_ADDR},
53 error(gettext("prohibited by allowed_attachments")." ($allowed)");
60 sub checkconfig () { #{{{
61 $config{cgi_disable_uploads}=0;
64 sub formbuilder_setup (@) { #{{{
66 my $form=$params{form};
69 if (defined $form->field("do") && $form->field("do") eq "edit") {
70 # Add attachment field, set type to multipart.
71 $form->enctype(&CGI::MULTIPART);
72 $form->field(name => 'attachment', type => 'file');
73 # These buttons are not put in the usual place, so
74 # are not added to the normal formbuilder button list.
75 $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
76 $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
78 # Add the javascript from the toggle plugin;
79 # the attachments interface uses it to toggle visibility.
80 require IkiWiki::Plugin::toggle;
81 $form->tmpl_param("javascript" => $IkiWiki::Plugin::toggle::javascript);
82 # Start with the attachments interface toggled invisible,
83 # but if it was used, keep it open.
84 if ($form->submitted ne "Upload Attachment" &&
85 (! defined $q->param("attachment_select") ||
86 ! length $q->param("attachment_select"))) {
87 $form->tmpl_param("attachments-class" => "toggleable");
90 $form->tmpl_param("attachments-class" => "toggleable-open");
93 elsif ($form->title eq "preferences") {
94 my $session=$params{session};
95 my $user_name=$session->param("name");
97 $form->field(name => "allowed_attachments", size => 50,
101 "ikiwiki/PageSpec/attachment",
103 linktext => "Enhanced PageSpec",
106 if (! IkiWiki::is_admin($user_name)) {
107 $form->field(name => "allowed_attachments", type => "hidden");
109 if (! $form->submitted) {
110 $form->field(name => "allowed_attachments", force => 1,
111 value => IkiWiki::userinfo_get($user_name, "allowed_attachments"));
113 if ($form->submitted && $form->submitted eq 'Save Preferences') {
114 if (defined $form->field("allowed_attachments")) {
115 IkiWiki::userinfo_set($user_name, "allowed_attachments",
116 $form->field("allowed_attachments")) ||
117 error("failed to set allowed_attachments");
123 sub formbuilder (@) { #{{{
125 my $form=$params{form};
128 return if ! defined $form->field("do") || $form->field("do") ne "edit";
130 my $filename=$q->param('attachment');
131 if (defined $filename && length $filename &&
132 ($form->submitted eq "Upload Attachment" || $form->submitted eq "Save Page")) {
133 my $session=$params{session};
135 # This is an (apparently undocumented) way to get the name
136 # of the temp file that CGI writes the upload to.
137 my $tempfile=$q->tmpFileName($filename);
138 if (! defined $tempfile || ! length $tempfile) {
139 # perl 5.8 needs an alternative, awful method
140 if ($q =~ /HASH/ && exists $q->{'.tmpfiles'}) {
141 foreach my $key (keys(%{$q->{'.tmpfiles'}})) {
142 $tempfile=$q->tmpFileName(\$key);
143 last if defined $tempfile && length $tempfile;
146 if (! defined $tempfile || ! length $tempfile) {
147 error("CGI::tmpFileName failed to return the uploaded file name");
151 $filename=IkiWiki::linkpage(
152 IkiWiki::possibly_foolish_untaint(
153 attachment_location($form->field('page')).
154 IkiWiki::basename($filename)));
155 if (IkiWiki::file_pruned($filename, $config{srcdir})) {
156 error(gettext("bad attachment filename"));
159 # Check that the user is allowed to edit a page with the
160 # name of the attachment.
161 IkiWiki::check_canedit($filename, $q, $session, 1);
162 # And that the attachment itself is acceptable.
163 check_canattach($session, $filename, $tempfile);
165 # Needed for fast_file_copy and for rendering below.
166 require IkiWiki::Render;
168 # Move the attachment into place.
169 # Try to use a fast rename; fall back to copying.
170 IkiWiki::prep_writefile($filename, $config{srcdir});
171 unlink($config{srcdir}."/".$filename);
172 if (rename($tempfile, $config{srcdir}."/".$filename)) {
173 # The temp file has tight permissions; loosen up.
174 chmod(0666 & ~umask, $config{srcdir}."/".$filename);
177 my $fh=$q->upload('attachment');
178 if (! defined $fh || ! ref $fh) {
179 # needed by old CGI versions
180 $fh=$q->param('attachment');
181 if (! defined $fh || ! ref $fh) {
182 # even that doesn't always work,
183 # fall back to opening the tempfile
185 open($fh, "<", $tempfile) || error("failed to open \"$tempfile\": $!");
189 writefile($filename, $config{srcdir}, undef, 1, sub {
190 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
194 # Check the attachment in and trigger a wiki refresh.
196 IkiWiki::rcs_add($filename);
197 IkiWiki::disable_commit_hook();
198 IkiWiki::rcs_commit($filename, gettext("attachment upload"),
199 IkiWiki::rcs_prepedit($filename),
200 $session->param("name"), $ENV{REMOTE_ADDR});
201 IkiWiki::enable_commit_hook();
202 IkiWiki::rcs_update();
205 IkiWiki::saveindex();
207 elsif ($form->submitted eq "Insert Links") {
208 my $page=quotemeta($q->param("page"));
210 foreach my $f ($q->param("attachment_select")) {
214 $form->field(name => 'editcontent',
215 value => $form->field('editcontent')."\n\n".$add,
216 force => 1) if length $add;
219 # Generate the attachment list only after having added any new
221 $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
224 sub attachment_location ($) { #{{{
227 # Put the attachment in a subdir of the page it's attached
228 # to, unless that page is an "index" page.
229 $page=~s/(^|\/)index//;
230 $page.="/" if length $page;
235 sub attachment_list ($) { #{{{
237 my $loc=attachment_location($page);
240 foreach my $f (values %pagesources) {
241 if (! defined IkiWiki::pagetype($f) &&
242 $f=~m/^\Q$loc\E[^\/]+$/ &&
243 -e "$config{srcdir}/$f") {
245 "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
246 link => htmllink($page, $page, $f, noimageinline => 1),
247 size => humansize((stat(_))[7]),
248 mtime => displaytime($IkiWiki::pagemtime{$f}),
249 mtime_raw => $IkiWiki::pagemtime{$f},
254 # Sort newer attachments to the top of the list, so a newly-added
255 # attachment appears just before the form used to add it.
256 return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;
259 my %units=( #{{{ # size in bytes
284 zettabyte => 2 ** 70,
287 yottabyte => 2 ** 80,
288 # ikiwiki, if you find you need larger data quantities, either modify
289 # yourself to add them, or travel back in time to 2008 and kill me.
293 sub parsesize ($) { #{{{
297 my $base=$size+0; # force to number
299 foreach my $unit (sort keys %units) {
300 if ($size=~/[0-9\s]\Q$unit\E$/i) {
301 return $base * $units{$unit};
307 sub humansize ($) { #{{{
310 foreach my $unit (reverse sort { $units{$a} <=> $units{$b} || $b cmp $a } keys %units) {
311 if ($size / $units{$unit} > 0.25) {
312 return (int($size / $units{$unit} * 10)/10).$unit;
315 return $size; # near zero, or negative
318 package IkiWiki::PageSpec;
320 sub match_maxsize ($$;@) { #{{{
322 my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
324 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
328 if (! exists $params{file}) {
329 return IkiWiki::FailReason->new("no file specified");
332 if (-s $params{file} > $maxsize) {
333 return IkiWiki::FailReason->new("file too large (".(-s $params{file})." > $maxsize)");
336 return IkiWiki::SuccessReason->new("file not too large");
340 sub match_minsize ($$;@) { #{{{
342 my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
344 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
348 if (! exists $params{file}) {
349 return IkiWiki::FailReason->new("no file specified");
352 if (-s $params{file} < $minsize) {
353 return IkiWiki::FailReason->new("file too small");
356 return IkiWiki::SuccessReason->new("file not too small");
360 sub match_mimetype ($$;@) { #{{{
365 if (! exists $params{file}) {
366 return IkiWiki::FailReason->new("no file specified");
369 # Use ::magic to get the mime type, the idea is to only trust
370 # data obtained by examining the actual file contents.
371 eval q{use File::MimeInfo::Magic};
373 return IkiWiki::FailReason->new("failed to load File::MimeInfo::Magic ($@); cannot check MIME type");
375 my $mimetype=File::MimeInfo::Magic::magic($params{file});
376 if (! defined $mimetype) {
380 my $regexp=IkiWiki::glob2re($wanted);
381 if ($mimetype!~/^$regexp$/i) {
382 return IkiWiki::FailReason->new("file MIME type is $mimetype, not $wanted");
385 return IkiWiki::SuccessReason->new("file MIME type is $mimetype");
389 sub match_virusfree ($$;@) { #{{{
394 if (! exists $params{file}) {
395 return IkiWiki::FailReason->new("no file specified");
398 if (! exists $IkiWiki::config{virus_checker} ||
399 ! length $IkiWiki::config{virus_checker}) {
400 return IkiWiki::FailReason->new("no virus_checker configured");
403 # The file needs to be fed into the virus checker on stdin,
404 # because the file is not world-readable, and if clamdscan is
405 # used, clamd would fail to read it.
406 eval q{use IPC::Open2};
408 open (IN, "<", $params{file}) || return IkiWiki::FailReason->new("failed to read file");
411 $SIG{PIPE} = sub { $sigpipe=1 };
412 my $pid=open2(\*CHECKER_OUT, "<&IN", $IkiWiki::config{virus_checker});
413 my $reason=<CHECKER_OUT>;
415 1 while (<CHECKER_OUT>);
418 $SIG{PIPE}="DEFAULT";
419 if ($sigpipe || $?) {
420 if (! length $reason) {
421 $reason="virus checker $IkiWiki::config{virus_checker}; failed with no output";
423 return IkiWiki::FailReason->new("file seems to contain a virus ($reason)");
426 return IkiWiki::SuccessReason->new("file seems virusfree ($reason)");
430 sub match_ispage ($$;@) { #{{{
433 if (defined IkiWiki::pagetype($filename)) {
434 return IkiWiki::SuccessReason->new("file is a wiki page");
437 return IkiWiki::FailReason->new("file is not a wiki page");
441 sub match_user ($$;@) { #{{{
446 if (! exists $params{user}) {
447 return IkiWiki::FailReason->new("no user specified");
450 if (defined $params{user} && lc $params{user} eq lc $user) {
451 return IkiWiki::SuccessReason->new("user is $user");
453 elsif (! defined $params{user}) {
454 return IkiWiki::FailReason->new("not logged in");
457 return IkiWiki::FailReason->new("user is $params{user}, not $user");
461 sub match_ip ($$;@) { #{{{
466 if (! exists $params{ip}) {
467 return IkiWiki::FailReason->new("no IP specified");
470 if (defined $params{ip} && lc $params{ip} eq lc $ip) {
471 return IkiWiki::SuccessReason->new("IP is $ip");
474 return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");