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 if (! defined $tempfile) {
92 error("failed to determine temp filename");
95 $filename=IkiWiki::titlepage(
96 IkiWiki::possibly_foolish_untaint(
97 attachment_location($form->field('page')).
98 IkiWiki::basename($filename)));
99 if (IkiWiki::file_pruned($filename, $config{srcdir})) {
100 error(gettext("bad attachment filename"));
103 # Check that the user is allowed to edit a page with the
104 # name of the attachment.
105 IkiWiki::check_canedit($filename, $q, $session, 1);
107 # Use a special pagespec to test that the attachment is valid.
109 foreach my $admin (@{$config{adminuser}}) {
110 my $allowed_attachments=IkiWiki::userinfo_get($admin, "allowed_attachments");
111 if (defined $allowed_attachments &&
112 length $allowed_attachments) {
113 $allowed=pagespec_match($filename,
114 $allowed_attachments,
116 user => $session->param("name"),
117 ip => $ENV{REMOTE_ADDR},
123 error(gettext("attachment rejected")." ($allowed)");
126 # Needed for fast_file_copy and for rendering below.
127 require IkiWiki::Render;
129 # Move the attachment into place.
130 # Try to use a fast rename; fall back to copying.
131 IkiWiki::prep_writefile($filename, $config{srcdir});
132 unlink($config{srcdir}."/".$filename);
133 if (rename($tempfile, $config{srcdir}."/".$filename)) {
134 # The temp file has tight permissions; loosen up.
135 chmod(0666 & ~umask, $config{srcdir}."/".$filename);
138 my $fh=$q->upload('attachment');
139 if (! defined $fh || ! ref $fh) {
140 # needed by old CGI versions
141 $fh=$q->param('attachment');
142 if (! defined $fh || ! ref $fh) {
143 # even that doesn't always work,
144 # fall back to opening the tempfile
145 open($fh, "<", $tempfile) || error("failed to open $tempfile: $!");
149 writefile($filename, $config{srcdir}, undef, 1, sub {
150 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
154 # Check the attachment in and trigger a wiki refresh.
156 IkiWiki::rcs_add($filename);
157 IkiWiki::disable_commit_hook();
158 IkiWiki::rcs_commit($filename, gettext("attachment upload"),
159 IkiWiki::rcs_prepedit($filename),
160 $session->param("name"), $ENV{REMOTE_ADDR});
161 IkiWiki::enable_commit_hook();
162 IkiWiki::rcs_update();
165 IkiWiki::saveindex();
167 elsif ($form->submitted eq "Insert Links") {
169 foreach my $f ($q->param("attachment_select")) {
172 $form->field(name => 'editcontent',
173 value => $form->field('editcontent')."\n\n".$add,
174 force => 1) if length $add;
177 # Generate the attachment list only after having added any new
179 $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
182 sub attachment_location ($) {
185 # Put the attachment in a subdir of the page it's attached
186 # to, unless that page is an "index" page.
187 $page=~s/(^|\/)index//;
188 $page.="/" if length $page;
193 sub attachment_list ($) {
195 my $loc=attachment_location($page);
198 foreach my $f (values %pagesources) {
199 if (! defined IkiWiki::pagetype($f) &&
200 $f=~m/^\Q$loc\E[^\/]+$/ &&
201 -e "$config{srcdir}/$f") {
203 "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
204 link => htmllink($page, $page, $f, noimageinline => 1),
205 size => humansize((stat(_))[7]),
206 mtime => displaytime($IkiWiki::pagemtime{$f}),
207 mtime_raw => $IkiWiki::pagemtime{$f},
212 # Sort newer attachments to the top of the list, so a newly-added
213 # attachment appears just before the form used to add it.
214 return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;
217 my %units=( # size in bytes
242 zettabyte => 2 ** 70,
245 yottabyte => 2 ** 80,
246 # ikiwiki, if you find you need larger data quantities, either modify
247 # yourself to add them, or travel back in time to 2008 and kill me.
251 sub parsesize ($) { #{{{
255 my $base=$size+0; # force to number
257 foreach my $unit (sort keys %units) {
258 if ($size=~/[0-9\s]\Q$unit\E$/i) {
259 return $base * $units{$unit};
265 sub humansize ($) { #{{{
268 foreach my $unit (reverse sort { $units{$a} <=> $units{$b} || $b cmp $a } keys %units) {
269 if ($size / $units{$unit} > 0.25) {
270 return (int($size / $units{$unit} * 10)/10).$unit;
273 return $size; # near zero, or negative
276 package IkiWiki::PageSpec;
278 sub match_maxsize ($$;@) { #{{{
280 my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
282 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
286 if (! exists $params{file}) {
287 return IkiWiki::FailReason->new("no file specified");
290 if (-s $params{file} > $maxsize) {
291 return IkiWiki::FailReason->new("file too large (".(-s $params{file})." > $maxsize)");
294 return IkiWiki::SuccessReason->new("file not too large");
298 sub match_minsize ($$;@) { #{{{
300 my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
302 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
306 if (! exists $params{file}) {
307 return IkiWiki::FailReason->new("no file specified");
310 if (-s $params{file} < $minsize) {
311 return IkiWiki::FailReason->new("file too small");
314 return IkiWiki::SuccessReason->new("file not too small");
318 sub match_mimetype ($$;@) { #{{{
323 if (! exists $params{file}) {
324 return IkiWiki::FailReason->new("no file specified");
327 # Use ::magic to get the mime type, the idea is to only trust
328 # data obtained by examining the actual file contents.
329 eval q{use File::MimeInfo::Magic};
331 return IkiWiki::FailReason->new("failed to load File::MimeInfo::Magic ($@); cannot check MIME type");
333 my $mimetype=File::MimeInfo::Magic::magic($params{file});
334 if (! defined $mimetype) {
338 my $regexp=IkiWiki::glob2re($wanted);
339 if ($mimetype!~/^$regexp$/i) {
340 return IkiWiki::FailReason->new("file MIME type is $mimetype, not $wanted");
343 return IkiWiki::SuccessReason->new("file MIME type is $mimetype");
347 sub match_virusfree ($$;@) { #{{{
352 if (! exists $params{file}) {
353 return IkiWiki::FailReason->new("no file specified");
356 if (! exists $IkiWiki::config{virus_checker} ||
357 ! length $IkiWiki::config{virus_checker}) {
358 return IkiWiki::FailReason->new("no virus_checker configured");
361 # The file needs to be fed into the virus checker on stdin,
362 # because the file is not world-readable, and if clamdscan is
363 # used, clamd would fail to read it.
364 eval q{use IPC::Open2};
366 open (IN, "<", $params{file}) || return IkiWiki::FailReason->new("failed to read file");
369 $SIG{PIPE} = sub { $sigpipe=1 };
370 my $pid=open2(\*CHECKER_OUT, "<&IN", $IkiWiki::config{virus_checker});
371 my $reason=<CHECKER_OUT>;
373 1 while (<CHECKER_OUT>);
376 $SIG{PIPE}="DEFAULT";
377 if ($sigpipe || $?) {
378 return IkiWiki::FailReason->new("file seems to contain a virus ($reason)");
381 return IkiWiki::SuccessReason->new("file seems virusfree ($reason)");
385 sub match_ispage ($$;@) { #{{{
388 if (defined IkiWiki::pagetype($filename)) {
389 return IkiWiki::SuccessReason->new("file is a wiki page");
392 return IkiWiki::FailReason->new("file is not a wiki page");
396 sub match_user ($$;@) { #{{{
401 if (! exists $params{user}) {
402 return IkiWiki::FailReason->new("no user specified");
405 if (defined $params{user} && lc $params{user} eq lc $user) {
406 return IkiWiki::SuccessReason->new("user is $user");
409 return IkiWiki::FailReason->new("user is $params{user}, not $user");
413 sub match_ip ($$;@) { #{{{
418 if (! exists $params{ip}) {
419 return IkiWiki::FailReason->new("no IP specified");
422 if (defined $params{ip} && lc $params{ip} eq lc $ip) {
423 return IkiWiki::SuccessReason->new("IP is $ip");
426 return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");