summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/attachment.pm
blob: a8e50f833b4121d918cb3c5296a5d442a71f3a57 (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 => humansize((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. my %units=( # size in bytes
  153. B => 1,
  154. byte => 1,
  155. KB => 2 ** 10,
  156. kilobyte => 2 ** 10,
  157. K => 2 ** 10,
  158. KB => 2 ** 10,
  159. kilobyte => 2 ** 10,
  160. M => 2 ** 20,
  161. MB => 2 ** 20,
  162. megabyte => 2 ** 20,
  163. G => 2 ** 30,
  164. GB => 2 ** 30,
  165. gigabyte => 2 ** 30,
  166. T => 2 ** 40,
  167. TB => 2 ** 40,
  168. terabyte => 2 ** 40,
  169. P => 2 ** 50,
  170. PB => 2 ** 50,
  171. petabyte => 2 ** 50,
  172. E => 2 ** 60,
  173. EB => 2 ** 60,
  174. exabyte => 2 ** 60,
  175. Z => 2 ** 70,
  176. ZB => 2 ** 70,
  177. zettabyte => 2 ** 70,
  178. Y => 2 ** 80,
  179. YB => 2 ** 80,
  180. yottabyte => 2 ** 80,
  181. # ikiwiki, if you find you need larger data quantities, either modify
  182. # yourself to add them, or travel back in time to 2008 and kill me.
  183. # -- Joey
  184. );
  185. sub parsesize ($) { #{{{
  186. my $size=shift;
  187. no warnings;
  188. my $base=$size+0; # force to number
  189. use warnings;
  190. foreach my $unit (sort keys %units) {
  191. if ($size=~/\Q$unit\E/i) {
  192. return $base * $units{$unit};
  193. }
  194. }
  195. return $base;
  196. } #}}}
  197. sub humansize ($) { #{{{
  198. my $size=shift;
  199. foreach my $unit (reverse sort { $units{$a} <=> $units{$b} || $b cmp $a } keys %units) {
  200. if ($size / $units{$unit} > 0.25) {
  201. return (int($size / $units{$unit} * 100)/100)."$unit";
  202. }
  203. }
  204. return $size; # near zero, or negative
  205. } #}}}
  206. package IkiWiki::PageSpec;
  207. sub match_maxsize ($$;@) { #{{{
  208. shift;
  209. my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
  210. if ($@) {
  211. return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
  212. }
  213. my %params=@_;
  214. if (! exists $params{file}) {
  215. return IkiWiki::FailReason->new("no file specified");
  216. }
  217. if (-s $params{file} > $maxsize) {
  218. return IkiWiki::FailReason->new("file too large");
  219. }
  220. else {
  221. return IkiWiki::SuccessReason->new("file not too large");
  222. }
  223. } #}}}
  224. sub match_minsize ($$;@) { #{{{
  225. shift;
  226. my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
  227. if ($@) {
  228. return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
  229. }
  230. my %params=@_;
  231. if (! exists $params{file}) {
  232. return IkiWiki::FailReason->new("no file specified");
  233. }
  234. if (-s $params{file} < $minsize) {
  235. return IkiWiki::FailReason->new("file too small");
  236. }
  237. else {
  238. return IkiWiki::SuccessReason->new("file not too small");
  239. }
  240. } #}}}
  241. sub match_ispage ($$;@) { #{{{
  242. my $filename=shift;
  243. if (defined IkiWiki::pagetype($filename)) {
  244. return IkiWiki::SuccessReason->new("file is a wiki page");
  245. }
  246. else {
  247. return IkiWiki::FailReason->new("file is not a wiki page");
  248. }
  249. } #}}}
  250. 1