summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/attachment.pm
blob: ee105a1709f3fe71e0e44a47782e9596791dbbf9 (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 => $session->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(
  166. file => $filename,
  167. message => gettext("attachment upload"),
  168. token => IkiWiki::rcs_prepedit($filename),
  169. session => $session,
  170. );
  171. IkiWiki::enable_commit_hook();
  172. IkiWiki::rcs_update();
  173. }
  174. IkiWiki::refresh();
  175. IkiWiki::saveindex();
  176. }
  177. elsif ($form->submitted eq "Insert Links") {
  178. my $page=quotemeta(Encode::decode_utf8($q->param("page")));
  179. my $add="";
  180. foreach my $f ($q->param("attachment_select")) {
  181. $f=Encode::decode_utf8($f);
  182. $f=~s/^$page\///;
  183. if (IkiWiki::isinlinableimage($f) &&
  184. UNIVERSAL::can("IkiWiki::Plugin::img", "import")) {
  185. $add.='[[!img '.$f.' align="right" size="" alt=""]]';
  186. }
  187. else {
  188. $add.="[[$f]]";
  189. }
  190. $add.="\n";
  191. }
  192. $form->field(name => 'editcontent',
  193. value => $form->field('editcontent')."\n\n".$add,
  194. force => 1) if length $add;
  195. }
  196. # Generate the attachment list only after having added any new
  197. # attachments.
  198. $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
  199. }
  200. sub attachment_location ($) {
  201. my $page=shift;
  202. # Put the attachment in a subdir of the page it's attached
  203. # to, unless that page is an "index" page.
  204. $page=~s/(^|\/)index//;
  205. $page.="/" if length $page;
  206. return $page;
  207. }
  208. sub attachment_list ($) {
  209. my $page=shift;
  210. my $loc=attachment_location($page);
  211. my @ret;
  212. foreach my $f (values %pagesources) {
  213. if (! defined pagetype($f) &&
  214. $f=~m/^\Q$loc\E[^\/]+$/) {
  215. push @ret, {
  216. "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
  217. link => htmllink($page, $page, $f, noimageinline => 1),
  218. size => IkiWiki::Plugin::filecheck::humansize((stat(_))[7]),
  219. mtime => displaytime($IkiWiki::pagemtime{$f}),
  220. mtime_raw => $IkiWiki::pagemtime{$f},
  221. };
  222. }
  223. }
  224. # Sort newer attachments to the top of the list, so a newly-added
  225. # attachment appears just before the form used to add it.
  226. return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;
  227. }
  228. 1