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