2 package IkiWiki::Plugin::attachment;
9 hook(type => "checkconfig", id => "attachment", call => \&checkconfig);
10 hook(type => "formbuilder_setup", id => "attachment", call => \&formbuilder_setup);
11 hook(type => "formbuilder", id => "attachment", call => \&formbuilder);
14 sub checkconfig () { #{{{
15 $config{cgi_disable_uploads}=0;
18 sub formbuilder_setup (@) { #{{{
20 my $form=$params{form};
23 if (defined $form->field("do") && $form->field("do") eq "edit") {
24 # Add attachment field, set type to multipart.
25 $form->enctype(&CGI::MULTIPART);
26 $form->field(name => 'attachment', type => 'file');
27 # These buttons are not put in the usual place, so
28 # are not added to the normal formbuilder button list.
29 $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
30 $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
32 # Add the javascript from the toggle plugin;
33 # the attachments interface uses it to toggle visibility.
34 require IkiWiki::Plugin::toggle;
35 $form->tmpl_param("javascript" => $IkiWiki::Plugin::toggle::javascript);
36 # Start with the attachments interface toggled invisible,
37 # but if it was used, keep it open.
38 if ($form->submitted ne "Upload Attachment" &&
39 (! defined $q->param("attachment_select") ||
40 ! length $q->param("attachment_select"))) {
41 $form->tmpl_param("attachments-class" => "toggleable");
44 $form->tmpl_param("attachments-class" => "toggleable-open");
47 elsif ($form->title eq "preferences") {
48 my $session=$params{session};
49 my $user_name=$session->param("name");
51 $form->field(name => "allowed_attachments", size => 50,
55 "ikiwiki/PageSpec/attachment",
57 linktext => "Enhanced PageSpec",
60 if (! IkiWiki::is_admin($user_name)) {
61 $form->field(name => "allowed_attachments", type => "hidden");
63 if (! $form->submitted) {
64 $form->field(name => "allowed_attachments", force => 1,
65 value => IkiWiki::userinfo_get($user_name, "allowed_attachments"));
67 if ($form->submitted && $form->submitted eq 'Save Preferences') {
68 if (defined $form->field("allowed_attachments")) {
69 IkiWiki::userinfo_set($user_name, "allowed_attachments",
70 $form->field("allowed_attachments")) ||
71 error("failed to set allowed_attachments");
77 sub formbuilder (@) { #{{{
79 my $form=$params{form};
82 return if ! defined $form->field("do") || $form->field("do") ne "edit";
84 my $filename=$q->param('attachment');
85 if (defined $filename && length $filename &&
86 ($form->submitted eq "Upload Attachment" || $form->submitted eq "Save Page")) {
87 my $session=$params{session};
89 # This is an (apparently undocumented) way to get the name
90 # of the temp file that CGI writes the upload to.
91 my $tempfile=$q->tmpFileName($filename);
92 if (! defined $tempfile || ! length $tempfile) {
93 # perl 5.8 needs an alternative, awful method
94 if ($q =~ /HASH/ && exists $q->{'.tmpfiles'}) {
95 foreach my $key (keys(%{$q->{'.tmpfiles'}})) {
96 $tempfile=$q->tmpFileName(\$key);
97 last if defined $tempfile && length $tempfile;
100 if (! defined $tempfile || ! length $tempfile) {
101 error("CGI::tmpFileName failed to return the uploaded file name");
105 $filename=IkiWiki::titlepage(
106 IkiWiki::possibly_foolish_untaint(
107 attachment_location($form->field('page')).
108 IkiWiki::basename($filename)));
109 if (IkiWiki::file_pruned($filename, $config{srcdir})) {
110 error(gettext("bad attachment filename"));
113 # Check that the user is allowed to edit a page with the
114 # name of the attachment.
115 IkiWiki::check_canedit($filename, $q, $session, 1);
117 # Use a special pagespec to test that the attachment is valid.
119 foreach my $admin (@{$config{adminuser}}) {
120 my $allowed_attachments=IkiWiki::userinfo_get($admin, "allowed_attachments");
121 if (defined $allowed_attachments &&
122 length $allowed_attachments) {
123 $allowed=pagespec_match($filename,
124 $allowed_attachments,
126 user => $session->param("name"),
127 ip => $ENV{REMOTE_ADDR},
133 error(gettext("attachment rejected")." ($allowed)");
136 # Needed for fast_file_copy and for rendering below.
137 require IkiWiki::Render;
139 # Move the attachment into place.
140 # Try to use a fast rename; fall back to copying.
141 IkiWiki::prep_writefile($filename, $config{srcdir});
142 unlink($config{srcdir}."/".$filename);
143 if (rename($tempfile, $config{srcdir}."/".$filename)) {
144 # The temp file has tight permissions; loosen up.
145 chmod(0666 & ~umask, $config{srcdir}."/".$filename);
148 my $fh=$q->upload('attachment');
149 if (! defined $fh || ! ref $fh) {
150 # needed by old CGI versions
151 $fh=$q->param('attachment');
152 if (! defined $fh || ! ref $fh) {
153 # even that doesn't always work,
154 # fall back to opening the tempfile
156 open($fh, "<", $tempfile) || error("failed to open \"$tempfile\": $!");
160 writefile($filename, $config{srcdir}, undef, 1, sub {
161 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
165 # Check the attachment in and trigger a wiki refresh.
167 IkiWiki::rcs_add($filename);
168 IkiWiki::disable_commit_hook();
169 IkiWiki::rcs_commit($filename, gettext("attachment upload"),
170 IkiWiki::rcs_prepedit($filename),
171 $session->param("name"), $ENV{REMOTE_ADDR});
172 IkiWiki::enable_commit_hook();
173 IkiWiki::rcs_update();
176 IkiWiki::saveindex();
178 elsif ($form->submitted eq "Insert Links") {
180 foreach my $f ($q->param("attachment_select")) {
183 $form->field(name => 'editcontent',
184 value => $form->field('editcontent')."\n\n".$add,
185 force => 1) if length $add;
188 # Generate the attachment list only after having added any new
190 $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
193 sub attachment_location ($) {
196 # Put the attachment in a subdir of the page it's attached
197 # to, unless that page is an "index" page.
198 $page=~s/(^|\/)index//;
199 $page.="/" if length $page;
204 sub attachment_list ($) {
206 my $loc=attachment_location($page);
209 foreach my $f (values %pagesources) {
210 if (! defined IkiWiki::pagetype($f) &&
211 $f=~m/^\Q$loc\E[^\/]+$/ &&
212 -e "$config{srcdir}/$f") {
214 "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
215 link => htmllink($page, $page, $f, noimageinline => 1),
216 size => humansize((stat(_))[7]),
217 mtime => displaytime($IkiWiki::pagemtime{$f}),
218 mtime_raw => $IkiWiki::pagemtime{$f},
223 # Sort newer attachments to the top of the list, so a newly-added
224 # attachment appears just before the form used to add it.
225 return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;
228 my %units=( # size in bytes
253 zettabyte => 2 ** 70,
256 yottabyte => 2 ** 80,
257 # ikiwiki, if you find you need larger data quantities, either modify
258 # yourself to add them, or travel back in time to 2008 and kill me.
262 sub parsesize ($) { #{{{
266 my $base=$size+0; # force to number
268 foreach my $unit (sort keys %units) {
269 if ($size=~/[0-9\s]\Q$unit\E$/i) {
270 return $base * $units{$unit};
276 sub humansize ($) { #{{{
279 foreach my $unit (reverse sort { $units{$a} <=> $units{$b} || $b cmp $a } keys %units) {
280 if ($size / $units{$unit} > 0.25) {
281 return (int($size / $units{$unit} * 10)/10).$unit;
284 return $size; # near zero, or negative
287 package IkiWiki::PageSpec;
289 sub match_maxsize ($$;@) { #{{{
291 my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
293 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
297 if (! exists $params{file}) {
298 return IkiWiki::FailReason->new("no file specified");
301 if (-s $params{file} > $maxsize) {
302 return IkiWiki::FailReason->new("file too large (".(-s $params{file})." > $maxsize)");
305 return IkiWiki::SuccessReason->new("file not too large");
309 sub match_minsize ($$;@) { #{{{
311 my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
313 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
317 if (! exists $params{file}) {
318 return IkiWiki::FailReason->new("no file specified");
321 if (-s $params{file} < $minsize) {
322 return IkiWiki::FailReason->new("file too small");
325 return IkiWiki::SuccessReason->new("file not too small");
329 sub match_mimetype ($$;@) { #{{{
334 if (! exists $params{file}) {
335 return IkiWiki::FailReason->new("no file specified");
338 # Use ::magic to get the mime type, the idea is to only trust
339 # data obtained by examining the actual file contents.
340 eval q{use File::MimeInfo::Magic};
342 return IkiWiki::FailReason->new("failed to load File::MimeInfo::Magic ($@); cannot check MIME type");
344 my $mimetype=File::MimeInfo::Magic::magic($params{file});
345 if (! defined $mimetype) {
349 my $regexp=IkiWiki::glob2re($wanted);
350 if ($mimetype!~/^$regexp$/i) {
351 return IkiWiki::FailReason->new("file MIME type is $mimetype, not $wanted");
354 return IkiWiki::SuccessReason->new("file MIME type is $mimetype");
358 sub match_virusfree ($$;@) { #{{{
363 if (! exists $params{file}) {
364 return IkiWiki::FailReason->new("no file specified");
367 if (! exists $IkiWiki::config{virus_checker} ||
368 ! length $IkiWiki::config{virus_checker}) {
369 return IkiWiki::FailReason->new("no virus_checker configured");
372 # The file needs to be fed into the virus checker on stdin,
373 # because the file is not world-readable, and if clamdscan is
374 # used, clamd would fail to read it.
375 eval q{use IPC::Open2};
377 open (IN, "<", $params{file}) || return IkiWiki::FailReason->new("failed to read file");
380 $SIG{PIPE} = sub { $sigpipe=1 };
381 my $pid=open2(\*CHECKER_OUT, "<&IN", $IkiWiki::config{virus_checker});
382 my $reason=<CHECKER_OUT>;
384 1 while (<CHECKER_OUT>);
387 $SIG{PIPE}="DEFAULT";
388 if ($sigpipe || $?) {
389 return IkiWiki::FailReason->new("file seems to contain a virus ($reason)");
392 return IkiWiki::SuccessReason->new("file seems virusfree ($reason)");
396 sub match_ispage ($$;@) { #{{{
399 if (defined IkiWiki::pagetype($filename)) {
400 return IkiWiki::SuccessReason->new("file is a wiki page");
403 return IkiWiki::FailReason->new("file is not a wiki page");
407 sub match_user ($$;@) { #{{{
412 if (! exists $params{user}) {
413 return IkiWiki::FailReason->new("no user specified");
416 if (defined $params{user} && lc $params{user} eq lc $user) {
417 return IkiWiki::SuccessReason->new("user is $user");
420 return IkiWiki::FailReason->new("user is $params{user}, not $user");
424 sub match_ip ($$;@) { #{{{
429 if (! exists $params{ip}) {
430 return IkiWiki::FailReason->new("no IP specified");
433 if (defined $params{ip} && lc $params{ip} eq lc $ip) {
434 return IkiWiki::SuccessReason->new("IP is $ip");
437 return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");