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 $form->field(name => 'attachment', type => 'file');
25 # These buttons are not put in the usual place, so
26 # are not added to the normal formbuilder button list.
27 $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
28 $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
30 # Add the javascript from the toggle plugin;
31 # the attachments interface uses it to toggle visibility.
32 require IkiWiki::Plugin::toggle;
33 $form->tmpl_param("javascript" => $IkiWiki::Plugin::toggle::javascript);
34 # Start with the attachments interface toggled invisible,
35 # but if it was used, keep it open.
36 if ($form->submitted ne "Upload Attachment" &&
37 (! defined $q->param("attachment_select") ||
38 ! length $q->param("attachment_select"))) {
39 $form->tmpl_param("attachments-class" => "toggleable");
42 $form->tmpl_param("attachments-class" => "toggleable-open");
45 elsif ($form->title eq "preferences") {
46 my $session=$params{session};
47 my $user_name=$session->param("name");
49 $form->field(name => "allowed_attachments", size => 50,
53 "ikiwiki/PageSpec/attachment",
55 linktext => "Enhanced PageSpec",
58 if (! IkiWiki::is_admin($user_name)) {
59 $form->field(name => "allowed_attachments", type => "hidden");
61 if (! $form->submitted) {
62 $form->field(name => "allowed_attachments", force => 1,
63 value => IkiWiki::userinfo_get($user_name, "allowed_attachments"));
65 if ($form->submitted && $form->submitted eq 'Save Preferences') {
66 if (defined $form->field("allowed_attachments")) {
67 IkiWiki::userinfo_set($user_name, "allowed_attachments",
68 $form->field("allowed_attachments")) ||
69 error("failed to set allowed_attachments");
75 sub formbuilder (@) { #{{{
77 my $form=$params{form};
80 return if ! defined $form->field("do") || $form->field("do") ne "edit";
82 my $filename=$q->param('attachment');
83 if (defined $filename && length $filename &&
84 ($form->submitted eq "Upload Attachment" || $form->submitted eq "Save Page")) {
85 my $session=$params{session};
87 # This is an (apparently undocumented) way to get the name
88 # of the temp file that CGI writes the upload to.
89 my $tempfile=$q->tmpFileName($filename);
91 $filename=IkiWiki::titlepage(
92 IkiWiki::possibly_foolish_untaint(
93 attachment_location($form->field('page')).
94 IkiWiki::basename($filename)));
95 if (IkiWiki::file_pruned($filename, $config{srcdir})) {
96 error(gettext("bad attachment filename"));
99 # Check that the user is allowed to edit a page with the
100 # name of the attachment.
101 IkiWiki::check_canedit($filename, $q, $session, 1);
103 # Use a special pagespec to test that the attachment is valid.
105 foreach my $admin (@{$config{adminuser}}) {
106 my $allowed_attachments=IkiWiki::userinfo_get($admin, "allowed_attachments");
107 if (defined $allowed_attachments &&
108 length $allowed_attachments) {
109 $allowed=pagespec_match($filename,
110 $allowed_attachments,
112 user => $session->param("name"),
113 ip => $ENV{REMOTE_ADDR},
119 error(gettext("attachment rejected")." ($allowed)");
122 # Needed for fast_file_copy and for rendering below.
123 require IkiWiki::Render;
125 # Move the attachment into place.
126 # Try to use a fast rename; fall back to copying.
127 IkiWiki::prep_writefile($filename, $config{srcdir});
128 unlink($config{srcdir}."/".$filename);
129 if (rename($tempfile, $config{srcdir}."/".$filename)) {
130 # The temp file has tight permissions; loosen up.
131 chmod(0666 & ~umask, $config{srcdir}."/".$filename);
134 my $fh=$q->upload('attachment');
135 if (! defined $fh || ! ref $fh) {
136 error("failed to get filehandle");
139 writefile($filename, $config{srcdir}, undef, 1, sub {
140 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
144 # Check the attachment in and trigger a wiki refresh.
146 IkiWiki::rcs_add($filename);
147 IkiWiki::disable_commit_hook();
148 IkiWiki::rcs_commit($filename, gettext("attachment upload"),
149 IkiWiki::rcs_prepedit($filename),
150 $session->param("name"), $ENV{REMOTE_ADDR});
151 IkiWiki::enable_commit_hook();
152 IkiWiki::rcs_update();
155 IkiWiki::saveindex();
157 elsif ($form->submitted eq "Insert Links") {
159 foreach my $f ($q->param("attachment_select")) {
162 $form->field(name => 'editcontent',
163 value => $form->field('editcontent')."\n\n".$add,
164 force => 1) if length $add;
167 # Generate the attachment list only after having added any new
169 $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
172 sub attachment_location ($) {
175 # Put the attachment in a subdir of the page it's attached
176 # to, unless that page is an "index" page.
177 $page=~s/(^|\/)index//;
178 $page.="/" if length $page;
183 sub attachment_list ($) {
185 my $loc=attachment_location($page);
188 foreach my $f (values %pagesources) {
189 if (! defined IkiWiki::pagetype($f) &&
190 $f=~m/^\Q$loc\E[^\/]+$/ &&
191 -e "$config{srcdir}/$f") {
193 "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
194 link => htmllink($page, $page, $f, noimageinline => 1),
195 size => humansize((stat(_))[7]),
196 mtime => displaytime($IkiWiki::pagemtime{$f}),
197 mtime_raw => $IkiWiki::pagemtime{$f},
202 # Sort newer attachments to the top of the list, so a newly-added
203 # attachment appears just before the form used to add it.
204 return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;
207 my %units=( # size in bytes
232 zettabyte => 2 ** 70,
235 yottabyte => 2 ** 80,
236 # ikiwiki, if you find you need larger data quantities, either modify
237 # yourself to add them, or travel back in time to 2008 and kill me.
241 sub parsesize ($) { #{{{
245 my $base=$size+0; # force to number
247 foreach my $unit (sort keys %units) {
248 if ($size=~/[0-9\s]\Q$unit\E$/i) {
249 return $base * $units{$unit};
255 sub humansize ($) { #{{{
258 foreach my $unit (reverse sort { $units{$a} <=> $units{$b} || $b cmp $a } keys %units) {
259 if ($size / $units{$unit} > 0.25) {
260 return (int($size / $units{$unit} * 10)/10).$unit;
263 return $size; # near zero, or negative
266 package IkiWiki::PageSpec;
268 sub match_maxsize ($$;@) { #{{{
270 my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
272 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
276 if (! exists $params{file}) {
277 return IkiWiki::FailReason->new("no file specified");
280 if (-s $params{file} > $maxsize) {
281 return IkiWiki::FailReason->new("file too large (".(-s $params{file})." > $maxsize)");
284 return IkiWiki::SuccessReason->new("file not too large");
288 sub match_minsize ($$;@) { #{{{
290 my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
292 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
296 if (! exists $params{file}) {
297 return IkiWiki::FailReason->new("no file specified");
300 if (-s $params{file} < $minsize) {
301 return IkiWiki::FailReason->new("file too small");
304 return IkiWiki::SuccessReason->new("file not too small");
308 sub match_mimetype ($$;@) { #{{{
313 if (! exists $params{file}) {
314 return IkiWiki::FailReason->new("no file specified");
317 # Use ::magic to get the mime type, the idea is to only trust
318 # data obtained by examining the actual file contents.
319 eval q{use File::MimeInfo::Magic};
321 return IkiWiki::FailReason->new("failed to load File::MimeInfo::Magic ($@); cannot check MIME type");
323 my $mimetype=File::MimeInfo::Magic::magic($params{file});
324 if (! defined $mimetype) {
328 my $regexp=IkiWiki::glob2re($wanted);
329 if ($mimetype!~/^$regexp$/i) {
330 return IkiWiki::FailReason->new("file MIME type is $mimetype, not $wanted");
333 return IkiWiki::SuccessReason->new("file MIME type is $mimetype");
337 sub match_virusfree ($$;@) { #{{{
342 if (! exists $params{file}) {
343 return IkiWiki::FailReason->new("no file specified");
346 if (! exists $IkiWiki::config{virus_checker} ||
347 ! length $IkiWiki::config{virus_checker}) {
348 return IkiWiki::FailReason->new("no virus_checker configured");
351 # The file needs to be fed into the virus checker on stdin,
352 # because the file is not world-readable, and if clamdscan is
353 # used, clamd would fail to read it.
354 eval q{use IPC::Open2};
356 open (IN, "<", $params{file}) || return IkiWiki::FailReason->new("failed to read file");
359 $SIG{PIPE} = sub { $sigpipe=1 };
360 my $pid=open2(\*CHECKER_OUT, "<&IN", $IkiWiki::config{virus_checker});
361 my $reason=<CHECKER_OUT>;
363 1 while (<CHECKER_OUT>);
366 $SIG{PIPE}="DEFAULT";
367 if ($sigpipe || $?) {
368 return IkiWiki::FailReason->new("file seems to contain a virus ($reason)");
371 return IkiWiki::SuccessReason->new("file seems virusfree ($reason)");
375 sub match_ispage ($$;@) { #{{{
378 if (defined IkiWiki::pagetype($filename)) {
379 return IkiWiki::SuccessReason->new("file is a wiki page");
382 return IkiWiki::FailReason->new("file is not a wiki page");
386 sub match_user ($$;@) { #{{{
391 if (! exists $params{user}) {
392 return IkiWiki::FailReason->new("no user specified");
395 if (defined $params{user} && lc $params{user} eq lc $user) {
396 return IkiWiki::SuccessReason->new("user is $user");
399 return IkiWiki::FailReason->new("user is $params{user}, not $user");
403 sub match_ip ($$;@) { #{{{
408 if (! exists $params{ip}) {
409 return IkiWiki::FailReason->new("no IP specified");
412 if (defined $params{ip} && lc $params{ip} eq lc $ip) {
413 return IkiWiki::SuccessReason->new("IP is $ip");
416 return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");