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