summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/attachment.pm
blob: ae06922d4b0528a171ae29c1c5559e682c75789e (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 formbuilder_setup (@) { #{{{
  15. my %params=@_;
  16. my $form=$params{form};
  17. if ($form->field("do") eq "edit") {
  18. $form->field(name => 'attachment', type => 'file');
  19. # These buttons are not put in the usual place, so
  20. # are not added to the normal formbuilder button list.
  21. $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
  22. $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
  23. }
  24. elsif ($form->title eq "preferences") {
  25. my $session=$params{session};
  26. my $user_name=$session->param("name");
  27. $form->field(name => "allowed_attachments", size => 50,
  28. fieldset => "admin",
  29. comment => "(".htmllink("", "", "ikiwiki/PageSpec", noimageinline => 1).")");
  30. if (! IkiWiki::is_admin($user_name)) {
  31. $form->field(name => "allowed_attachments", type => "hidden");
  32. }
  33. if (! $form->submitted) {
  34. $form->field(name => "allowed_attachments", force => 1,
  35. value => IkiWiki::userinfo_get($user_name, "allowed_attachments"));
  36. }
  37. if ($form->submitted && $form->submitted eq 'Save Preferences') {
  38. if (defined $form->field("allowed_attachments")) {
  39. IkiWiki::userinfo_set($user_name, "allowed_attachments",
  40. $form->field("allowed_attachments")) ||
  41. error("failed to set allowed_attachments");
  42. }
  43. }
  44. }
  45. } #}}}
  46. sub formbuilder (@) { #{{{
  47. my %params=@_;
  48. my $form=$params{form};
  49. my $q=$params{cgi};
  50. return if $form->field("do") ne "edit";
  51. my $filename=$q->param('attachment');
  52. if (defined $filename && length $filename &&
  53. ($form->submitted eq "Upload Attachment" || $form->submitted eq "Save Page")) {
  54. my $session=$params{session};
  55. # This is an (apparently undocumented) way to get the name
  56. # of the temp file that CGI writes the upload to.
  57. my $tempfile=$q->tmpFileName($filename);
  58. $filename=IkiWiki::titlepage(
  59. IkiWiki::possibly_foolish_untaint(
  60. attachment_location($form->field('page')).
  61. IkiWiki::basename($filename)));
  62. if (IkiWiki::file_pruned($filename, $config{srcdir})) {
  63. error(gettext("bad attachment filename"));
  64. }
  65. # Check that the user is allowed to edit a page with the
  66. # name of the attachment.
  67. IkiWiki::check_canedit($filename, $q, $session, 1);
  68. # Use a special pagespec to test that the attachment is valid.
  69. my $allowed=1;
  70. foreach my $admin (@{$config{adminuser}}) {
  71. my $allowed_attachments=IkiWiki::userinfo_get($admin, "allowed_attachments");
  72. if (defined $allowed_attachments &&
  73. length $allowed_attachments) {
  74. $allowed=pagespec_match($filename,
  75. $allowed_attachments,
  76. file => $tempfile);
  77. last if $allowed;
  78. }
  79. }
  80. if (! $allowed) {
  81. error(gettext("attachment rejected")." ($allowed)");
  82. }
  83. # Needed for fast_file_copy and for rendering below.
  84. require IkiWiki::Render;
  85. # Move the attachment into place.
  86. # Try to use a fast rename; fall back to copying.
  87. IkiWiki::prep_writefile($filename, $config{srcdir});
  88. unlink($config{srcdir}."/".$filename);
  89. if (rename($tempfile, $config{srcdir}."/".$filename)) {
  90. # The temp file has tight permissions; loosen up.
  91. chmod(0666 & ~umask, $config{srcdir}."/".$filename);
  92. }
  93. else {
  94. my $fh=$q->upload('attachment');
  95. if (! defined $fh || ! ref $fh) {
  96. error("failed to get filehandle");
  97. }
  98. binmode($fh);
  99. writefile($filename, $config{srcdir}, undef, 1, sub {
  100. IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
  101. });
  102. }
  103. # Check the attachment in and trigger a wiki refresh.
  104. if ($config{rcs}) {
  105. IkiWiki::rcs_add($filename);
  106. IkiWiki::disable_commit_hook();
  107. IkiWiki::rcs_commit($filename, gettext("attachment upload"),
  108. IkiWiki::rcs_prepedit($filename),
  109. $session->param("name"), $ENV{REMOTE_ADDR});
  110. IkiWiki::enable_commit_hook();
  111. IkiWiki::rcs_update();
  112. }
  113. IkiWiki::refresh();
  114. IkiWiki::saveindex();
  115. }
  116. elsif ($form->submitted eq "Insert Links") {
  117. my $add="";
  118. foreach my $f ($q->param("attachment_select")) {
  119. $add.="[[$f]]\n";
  120. }
  121. $form->field(name => 'editcontent',
  122. value => $form->field('editcontent')."\n\n".$add,
  123. force => 1);
  124. }
  125. # Generate the attachment list only after having added any new
  126. # attachments.
  127. $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
  128. } # }}}
  129. sub attachment_location ($) {
  130. my $page=shift;
  131. # Put the attachment in a subdir of the page it's attached
  132. # to, unless that page is an "index" page.
  133. $page=~s/(^|\/)index//;
  134. $page.="/" if length $page;
  135. return $page;
  136. }
  137. sub attachment_list ($) {
  138. my $page=shift;
  139. my $loc=attachment_location($page);
  140. my @ret;
  141. foreach my $f (values %pagesources) {
  142. if (! defined IkiWiki::pagetype($f) &&
  143. $f=~m/^\Q$loc\E[^\/]+$/ &&
  144. -e "$config{srcdir}/$f") {
  145. push @ret, {
  146. "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'">',
  147. link => htmllink($page, $page, $f, noimageinline => 1),
  148. size => humansize((stat(_))[7]),
  149. mtime => displaytime($IkiWiki::pagemtime{$f}),
  150. mtime_raw => $IkiWiki::pagemtime{$f},
  151. };
  152. }
  153. }
  154. # Sort newer attachments to the top of the list, so a newly-added
  155. # attachment appears just before the form used to add it.
  156. return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;
  157. }
  158. my %units=( # size in bytes
  159. B => 1,
  160. byte => 1,
  161. KB => 2 ** 10,
  162. kilobyte => 2 ** 10,
  163. K => 2 ** 10,
  164. KB => 2 ** 10,
  165. kilobyte => 2 ** 10,
  166. M => 2 ** 20,
  167. MB => 2 ** 20,
  168. megabyte => 2 ** 20,
  169. G => 2 ** 30,
  170. GB => 2 ** 30,
  171. gigabyte => 2 ** 30,
  172. T => 2 ** 40,
  173. TB => 2 ** 40,
  174. terabyte => 2 ** 40,
  175. P => 2 ** 50,
  176. PB => 2 ** 50,
  177. petabyte => 2 ** 50,
  178. E => 2 ** 60,
  179. EB => 2 ** 60,
  180. exabyte => 2 ** 60,
  181. Z => 2 ** 70,
  182. ZB => 2 ** 70,
  183. zettabyte => 2 ** 70,
  184. Y => 2 ** 80,
  185. YB => 2 ** 80,
  186. yottabyte => 2 ** 80,
  187. # ikiwiki, if you find you need larger data quantities, either modify
  188. # yourself to add them, or travel back in time to 2008 and kill me.
  189. # -- Joey
  190. );
  191. sub parsesize ($) { #{{{
  192. my $size=shift;
  193. no warnings;
  194. my $base=$size+0; # force to number
  195. use warnings;
  196. foreach my $unit (sort keys %units) {
  197. if ($size=~/[0-9\s]\Q$unit\E$/i) {
  198. return $base * $units{$unit};
  199. }
  200. }
  201. return $base;
  202. } #}}}
  203. sub humansize ($) { #{{{
  204. my $size=shift;
  205. foreach my $unit (reverse sort { $units{$a} <=> $units{$b} || $b cmp $a } keys %units) {
  206. if ($size / $units{$unit} > 0.25) {
  207. return (int($size / $units{$unit} * 10)/10).$unit;
  208. }
  209. }
  210. return $size; # near zero, or negative
  211. } #}}}
  212. package IkiWiki::PageSpec;
  213. sub match_maxsize ($$;@) { #{{{
  214. shift;
  215. my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
  216. if ($@) {
  217. return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
  218. }
  219. my %params=@_;
  220. if (! exists $params{file}) {
  221. return IkiWiki::FailReason->new("no file specified");
  222. }
  223. if (-s $params{file} > $maxsize) {
  224. return IkiWiki::FailReason->new("file too large (".(-s $params{file})." > $maxsize)");
  225. }
  226. else {
  227. return IkiWiki::SuccessReason->new("file not too large");
  228. }
  229. } #}}}
  230. sub match_minsize ($$;@) { #{{{
  231. shift;
  232. my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
  233. if ($@) {
  234. return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
  235. }
  236. my %params=@_;
  237. if (! exists $params{file}) {
  238. return IkiWiki::FailReason->new("no file specified");
  239. }
  240. if (-s $params{file} < $minsize) {
  241. return IkiWiki::FailReason->new("file too small");
  242. }
  243. else {
  244. return IkiWiki::SuccessReason->new("file not too small");
  245. }
  246. } #}}}
  247. sub match_ispage ($$;@) { #{{{
  248. my $filename=shift;
  249. if (defined IkiWiki::pagetype($filename)) {
  250. return IkiWiki::SuccessReason->new("file is a wiki page");
  251. }
  252. else {
  253. return IkiWiki::FailReason->new("file is not a wiki page");
  254. }
  255. } #}}}
  256. 1