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