summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/attachment.pm
blob: 690b61cbc27255885c84dd48e62cce91759e9fc2 (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 => "checkconfig", id => "attachment", call => \&checkconfig);
  8. hook(type => "formbuilder_setup", id => "attachment", call => \&formbuilder_setup);
  9. hook(type => "formbuilder", id => "attachment", call => \&formbuilder);
  10. } # }}}
  11. sub checkconfig () { #{{{
  12. $config{cgi_disable_uploads}=0;
  13. } #}}}
  14. sub attachment_location ($) {
  15. my $page=shift;
  16. # Put the attachment in a subdir of the page it's attached
  17. # to, unless that page is an "index" page.
  18. $page=~s/(^|\/)index//;
  19. $page.="/" if length $page;
  20. return $page;
  21. }
  22. sub attachment_list ($) {
  23. my $page=shift;
  24. my $loc=attachment_location($page);
  25. my @ret;
  26. foreach my $f (values %pagesources) {
  27. if (! defined IkiWiki::pagetype($f) &&
  28. $f=~m/^\Q$loc\E[^\/]+$/ &&
  29. -e "$config{srcdir}/$f") {
  30. push @ret, {
  31. "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'">',
  32. link => htmllink($page, $page, $f, noimageinline => 1),
  33. size => (stat(_))[7],
  34. mtime => displaytime($IkiWiki::pagemtime{$f}),
  35. };
  36. }
  37. }
  38. return @ret;
  39. }
  40. sub formbuilder_setup (@) { #{{{
  41. my %params=@_;
  42. my $form=$params{form};
  43. if ($form->field("do") eq "edit") {
  44. $form->field(name => 'attachment', type => 'file');
  45. $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
  46. # These buttons are not put in the usual place, so
  47. # is not added to the normal formbuilder button list.
  48. $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
  49. $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
  50. }
  51. elsif ($form->title eq "preferences") {
  52. my $session=$params{session};
  53. my $user_name=$session->param("name");
  54. $form->field(name => "allowed_attachments", size => 50,
  55. fieldset => "admin",
  56. comment => "(".htmllink("", "", "ikiwiki/PageSpec", noimageinline => 1).")");
  57. if (! IkiWiki::is_admin($user_name)) {
  58. $form->field(name => "allowed_attachments", type => "hidden");
  59. }
  60. if (! $form->submitted) {
  61. $form->field(name => "allowed_attachments", force => 1,
  62. value => IkiWiki::userinfo_get($user_name, "allowed_attachments"));
  63. }
  64. if ($form->submitted && $form->submitted eq 'Save Preferences') {
  65. if (defined $form->field("allowed_attachments")) {
  66. IkiWiki::userinfo_set($user_name, "allowed_attachments",
  67. $form->field("allowed_attachments")) ||
  68. error("failed to set allowed_attachments");
  69. }
  70. }
  71. }
  72. } #}}}
  73. sub formbuilder (@) { #{{{
  74. my %params=@_;
  75. my $form=$params{form};
  76. my $q=$params{cgi};
  77. return if $form->field("do") ne "edit";
  78. if ($form->submitted eq "Upload" || $form->submitted eq "Save Page") {
  79. my $session=$params{session};
  80. my $filename=$q->param('attachment');
  81. if (! defined $filename || ! length $filename) {
  82. # no file, so do nothing
  83. return;
  84. }
  85. # This is an (apparently undocumented) way to get the name
  86. # of the temp file that CGI writes the upload to.
  87. my $tempfile=$q->tmpFileName($filename);
  88. $filename=IkiWiki::titlepage(
  89. IkiWiki::possibly_foolish_untaint(
  90. attachment_location($form->field('page')).
  91. IkiWiki::basename($filename)));
  92. if (IkiWiki::file_pruned($filename, $config{srcdir})) {
  93. error(gettext("bad attachment filename"));
  94. }
  95. # Check that the user is allowed to edit a page with the
  96. # name of the attachment.
  97. IkiWiki::check_canedit($filename, $q, $session, 1);
  98. # Use a special pagespec to test that the attachment is valid.
  99. my $allowed=1;
  100. foreach my $admin (@{$config{adminuser}}) {
  101. my $allowed_attachments=IkiWiki::userinfo_get($admin, "allowed_attachments");
  102. if (defined $allowed_attachments &&
  103. length $allowed_attachments) {
  104. $allowed=pagespec_match($filename,
  105. $allowed_attachments,
  106. file => $tempfile);
  107. last if $allowed;
  108. }
  109. }
  110. if (! $allowed) {
  111. error(gettext("attachment rejected")." ($allowed)");
  112. }
  113. # Needed for fast_file_copy and for rendering below.
  114. require IkiWiki::Render;
  115. # Move the attachment into place.
  116. # Try to use a fast rename; fall back to copying.
  117. IkiWiki::prep_writefile($filename, $config{srcdir});
  118. unlink($config{srcdir}."/".$filename);
  119. if (! rename($tempfile, $config{srcdir}."/".$filename)) {
  120. my $fh=$q->upload('attachment');
  121. if (! defined $fh || ! ref $fh) {
  122. error("failed to get filehandle");
  123. }
  124. binmode($fh);
  125. writefile($filename, $config{srcdir}, undef, 1, sub {
  126. IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
  127. });
  128. }
  129. # Check the attachment in and trigger a wiki refresh.
  130. if ($config{rcs}) {
  131. IkiWiki::rcs_add($filename);
  132. IkiWiki::disable_commit_hook();
  133. IkiWiki::rcs_commit($filename, gettext("attachment upload"),
  134. IkiWiki::rcs_prepedit($filename),
  135. $session->param("name"), $ENV{REMOTE_ADDR});
  136. IkiWiki::enable_commit_hook();
  137. IkiWiki::rcs_update();
  138. }
  139. IkiWiki::refresh();
  140. IkiWiki::saveindex();
  141. }
  142. elsif ($form->submitted eq "Insert Links") {
  143. my $add="";
  144. foreach my $f ($q->param("attachment_select")) {
  145. $add.="[[$f]]\n";
  146. }
  147. $form->field(name => 'editcontent',
  148. value => $form->field('editcontent')."\n\n".$add,
  149. force => 1);
  150. }
  151. } # }}}
  152. package IkiWiki::PageSpec;
  153. sub parsesize ($) { #{{{
  154. my $size=shift;
  155. no warnings;
  156. my $base=$size+0; # force to number
  157. use warnings;
  158. my $multiple=1;
  159. if ($size=~/kb?$/i) {
  160. $multiple=2**10;
  161. }
  162. elsif ($size=~/mb?$/i) {
  163. $multiple=2**20;
  164. }
  165. elsif ($size=~/gb?$/i) {
  166. $multiple=2**30;
  167. }
  168. elsif ($size=~/tb?$/i) {
  169. $multiple=2**40;
  170. }
  171. return $base * $multiple;
  172. } #}}}
  173. sub match_maxsize ($$;@) { #{{{
  174. shift;
  175. my $maxsize=eval{parsesize(shift)};
  176. if ($@) {
  177. return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
  178. }
  179. my %params=@_;
  180. if (! exists $params{file}) {
  181. return IkiWiki::FailReason->new("no file specified");
  182. }
  183. if (-s $params{file} > $maxsize) {
  184. return IkiWiki::FailReason->new("file too large");
  185. }
  186. else {
  187. return IkiWiki::SuccessReason->new("file not too large");
  188. }
  189. } #}}}
  190. sub match_minsize ($$;@) { #{{{
  191. shift;
  192. my $minsize=eval{parsesize(shift)};
  193. if ($@) {
  194. return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
  195. }
  196. my %params=@_;
  197. if (! exists $params{file}) {
  198. return IkiWiki::FailReason->new("no file specified");
  199. }
  200. if (-s $params{file} < $minsize) {
  201. return IkiWiki::FailReason->new("file too small");
  202. }
  203. else {
  204. return IkiWiki::SuccessReason->new("file not too small");
  205. }
  206. } #}}}
  207. sub match_ispage ($$;@) { #{{{
  208. my $filename=shift;
  209. if (defined IkiWiki::pagetype($filename)) {
  210. return IkiWiki::SuccessReason->new("file is a wiki page");
  211. }
  212. else {
  213. return IkiWiki::FailReason->new("file is not a wiki page");
  214. }
  215. } #}}}
  216. 1