summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/attachment.pm
blob: 087c315a9af355569921c9150c05c0ba89ec8df1 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::attachment;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 3.00;
  6. sub import {
  7. add_underlay("javascript");
  8. hook(type => "getsetup", id => "attachment", call => \&getsetup);
  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);
  12. IkiWiki::loadplugin("filecheck");
  13. }
  14. sub getsetup () {
  15. return
  16. plugin => {
  17. safe => 1,
  18. rebuild => 0,
  19. },
  20. allowed_attachments => {
  21. type => "pagespec",
  22. example => "virusfree() and mimetype(image/*) and maxsize(50kb)",
  23. description => "enhanced PageSpec specifying what attachments are allowed",
  24. link => "ikiwiki/PageSpec/attachment",
  25. safe => 1,
  26. rebuild => 0,
  27. },
  28. virus_checker => {
  29. type => "string",
  30. example => "clamdscan -",
  31. description => "virus checker program (reads STDIN, returns nonzero if virus found)",
  32. safe => 0, # executed
  33. rebuild => 0,
  34. },
  35. }
  36. sub check_canattach ($$;$) {
  37. my $session=shift;
  38. my $dest=shift; # where it's going to be put, under the srcdir
  39. my $file=shift; # the path to the attachment currently
  40. # Don't allow an attachment to be uploaded with the same name as an
  41. # existing page.
  42. if (exists $IkiWiki::pagesources{$dest} &&
  43. $IkiWiki::pagesources{$dest} ne $dest) {
  44. error(sprintf(gettext("there is already a page named %s"), $dest));
  45. }
  46. # Use a special pagespec to test that the attachment is valid.
  47. my $allowed=1;
  48. if (defined $config{allowed_attachments} &&
  49. length $config{allowed_attachments}) {
  50. $allowed=pagespec_match($dest,
  51. $config{allowed_attachments},
  52. file => $file,
  53. user => $session->param("name"),
  54. ip => $ENV{REMOTE_ADDR},
  55. );
  56. }
  57. if (! $allowed) {
  58. error(gettext("prohibited by allowed_attachments")." ($allowed)");
  59. }
  60. else {
  61. return 1;
  62. }
  63. }
  64. sub checkconfig () {
  65. $config{cgi_disable_uploads}=0;
  66. }
  67. sub formbuilder_setup (@) {
  68. my %params=@_;
  69. my $form=$params{form};
  70. my $q=$params{cgi};
  71. if (defined $form->field("do") && ($form->field("do") eq "edit" ||
  72. $form->field("do") eq "create")) {
  73. # Add attachment field, set type to multipart.
  74. $form->enctype(&CGI::MULTIPART);
  75. $form->field(name => 'attachment', type => 'file');
  76. # These buttons are not put in the usual place, so
  77. # are not added to the normal formbuilder button list.
  78. $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
  79. $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
  80. # Add the toggle javascript; the attachments interface uses
  81. # it to toggle visibility.
  82. require IkiWiki::Plugin::toggle;
  83. $form->tmpl_param("javascript" => IkiWiki::Plugin::toggle::include_javascript($params{page}, 1));
  84. # Start with the attachments interface toggled invisible,
  85. # but if it was used, keep it open.
  86. if ($form->submitted ne "Upload Attachment" &&
  87. (! defined $q->param("attachment_select") ||
  88. ! length $q->param("attachment_select"))) {
  89. $form->tmpl_param("attachments-class" => "toggleable");
  90. }
  91. else {
  92. $form->tmpl_param("attachments-class" => "toggleable-open");
  93. }
  94. }
  95. }
  96. sub formbuilder (@) {
  97. my %params=@_;
  98. my $form=$params{form};
  99. my $q=$params{cgi};
  100. return if ! defined $form->field("do") || ($form->field("do") ne "edit" && $form->field("do") ne "create") ;
  101. my $filename=$q->param('attachment');
  102. if (defined $filename && length $filename &&
  103. ($form->submitted eq "Upload Attachment" || $form->submitted eq "Save Page")) {
  104. my $session=$params{session};
  105. # This is an (apparently undocumented) way to get the name
  106. # of the temp file that CGI writes the upload to.
  107. my $tempfile=$q->tmpFileName($filename);
  108. if (! defined $tempfile || ! length $tempfile) {
  109. # perl 5.8 needs an alternative, awful method
  110. if ($q =~ /HASH/ && exists $q->{'.tmpfiles'}) {
  111. foreach my $key (keys(%{$q->{'.tmpfiles'}})) {
  112. $tempfile=$q->tmpFileName(\$key);
  113. last if defined $tempfile && length $tempfile;
  114. }
  115. }
  116. if (! defined $tempfile || ! length $tempfile) {
  117. error("CGI::tmpFileName failed to return the uploaded file name");
  118. }
  119. }
  120. $filename=linkpage(IkiWiki::possibly_foolish_untaint(
  121. attachment_location($form->field('page')).
  122. IkiWiki::basename($filename)));
  123. if (IkiWiki::file_pruned($filename, $config{srcdir})) {
  124. error(gettext("bad attachment filename"));
  125. }
  126. # Check that the user is allowed to edit a page with the
  127. # name of the attachment.
  128. IkiWiki::check_canedit($filename, $q, $session, 1);
  129. # And that the attachment itself is acceptable.
  130. check_canattach($session, $filename, $tempfile);
  131. # Needed for fast_file_copy and for rendering below.
  132. require IkiWiki::Render;
  133. # Move the attachment into place.
  134. # Try to use a fast rename; fall back to copying.
  135. IkiWiki::prep_writefile($filename, $config{srcdir});
  136. unlink($config{srcdir}."/".$filename);
  137. if (rename($tempfile, $config{srcdir}."/".$filename)) {
  138. # The temp file has tight permissions; loosen up.
  139. chmod(0666 & ~umask, $config{srcdir}."/".$filename);
  140. }
  141. else {
  142. my $fh=$q->upload('attachment');
  143. if (! defined $fh || ! ref $fh) {
  144. # needed by old CGI versions
  145. $fh=$q->param('attachment');
  146. if (! defined $fh || ! ref $fh) {
  147. # even that doesn't always work,
  148. # fall back to opening the tempfile
  149. $fh=undef;
  150. open($fh, "<", $tempfile) || error("failed to open \"$tempfile\": $!");
  151. }
  152. }
  153. binmode($fh);
  154. writefile($filename, $config{srcdir}, undef, 1, sub {
  155. IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
  156. });
  157. }
  158. # Check the attachment in and trigger a wiki refresh.
  159. if ($config{rcs}) {
  160. IkiWiki::rcs_add($filename);
  161. IkiWiki::disable_commit_hook();
  162. IkiWiki::rcs_commit($filename, gettext("attachment upload"),
  163. IkiWiki::rcs_prepedit($filename),
  164. $session->param("name"), $ENV{REMOTE_ADDR});
  165. IkiWiki::enable_commit_hook();
  166. IkiWiki::rcs_update();
  167. }
  168. IkiWiki::refresh();
  169. IkiWiki::saveindex();
  170. }
  171. elsif ($form->submitted eq "Insert Links") {
  172. my $page=quotemeta($q->param("page"));
  173. my $add="";
  174. foreach my $f ($q->param("attachment_select")) {
  175. $f=~s/^$page\///;
  176. $add.="[[$f]]\n";
  177. }
  178. $form->field(name => 'editcontent',
  179. value => $form->field('editcontent')."\n\n".$add,
  180. force => 1) if length $add;
  181. }
  182. # Generate the attachment list only after having added any new
  183. # attachments.
  184. $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
  185. }
  186. sub attachment_location ($) {
  187. my $page=shift;
  188. # Put the attachment in a subdir of the page it's attached
  189. # to, unless that page is an "index" page.
  190. $page=~s/(^|\/)index//;
  191. $page.="/" if length $page;
  192. return $page;
  193. }
  194. sub attachment_list ($) {
  195. my $page=shift;
  196. my $loc=attachment_location($page);
  197. my @ret;
  198. foreach my $f (values %pagesources) {
  199. if (! defined pagetype($f) &&
  200. $f=~m/^\Q$loc\E[^\/]+$/ &&
  201. -e "$config{srcdir}/$f") {
  202. push @ret, {
  203. "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
  204. link => htmllink($page, $page, $f, noimageinline => 1),
  205. size => IkiWiki::Plugin::filecheck::humansize((stat(_))[7]),
  206. mtime => displaytime($IkiWiki::pagemtime{$f}),
  207. };
  208. }
  209. }
  210. # Sort newer attachments to the top of the list, so a newly-added
  211. # attachment appears just before the form used to add it.
  212. return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;
  213. }
  214. 1