summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/attachment.pm
blob: 3bbe27b1a42cbfbdf2f63568b19c856e00385a03 (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 => "(".
  44. htmllink("", "",
  45. "ikiwiki/PageSpec/attachment",
  46. noimageinline => 1,
  47. linktext => "Enhanced PageSpec",
  48. ).")"
  49. );
  50. if (! IkiWiki::is_admin($user_name)) {
  51. $form->field(name => "allowed_attachments", type => "hidden");
  52. }
  53. if (! $form->submitted) {
  54. $form->field(name => "allowed_attachments", force => 1,
  55. value => IkiWiki::userinfo_get($user_name, "allowed_attachments"));
  56. }
  57. if ($form->submitted && $form->submitted eq 'Save Preferences') {
  58. if (defined $form->field("allowed_attachments")) {
  59. IkiWiki::userinfo_set($user_name, "allowed_attachments",
  60. $form->field("allowed_attachments")) ||
  61. error("failed to set allowed_attachments");
  62. }
  63. }
  64. }
  65. } #}}}
  66. sub formbuilder (@) { #{{{
  67. my %params=@_;
  68. my $form=$params{form};
  69. my $q=$params{cgi};
  70. return if $form->field("do") ne "edit";
  71. my $filename=$q->param('attachment');
  72. if (defined $filename && length $filename &&
  73. ($form->submitted eq "Upload Attachment" || $form->submitted eq "Save Page")) {
  74. my $session=$params{session};
  75. # This is an (apparently undocumented) way to get the name
  76. # of the temp file that CGI writes the upload to.
  77. my $tempfile=$q->tmpFileName($filename);
  78. $filename=IkiWiki::titlepage(
  79. IkiWiki::possibly_foolish_untaint(
  80. attachment_location($form->field('page')).
  81. IkiWiki::basename($filename)));
  82. if (IkiWiki::file_pruned($filename, $config{srcdir})) {
  83. error(gettext("bad attachment filename"));
  84. }
  85. # Check that the user is allowed to edit a page with the
  86. # name of the attachment.
  87. IkiWiki::check_canedit($filename, $q, $session, 1);
  88. # Use a special pagespec to test that the attachment is valid.
  89. my $allowed=1;
  90. foreach my $admin (@{$config{adminuser}}) {
  91. my $allowed_attachments=IkiWiki::userinfo_get($admin, "allowed_attachments");
  92. if (defined $allowed_attachments &&
  93. length $allowed_attachments) {
  94. $allowed=pagespec_match($filename,
  95. $allowed_attachments,
  96. file => $tempfile,
  97. user => $session->param("name"),
  98. ip => $ENV{REMOTE_ADDR},
  99. );
  100. last if $allowed;
  101. }
  102. }
  103. if (! $allowed) {
  104. error(gettext("attachment rejected")." ($allowed)");
  105. }
  106. # Needed for fast_file_copy and for rendering below.
  107. require IkiWiki::Render;
  108. # Move the attachment into place.
  109. # Try to use a fast rename; fall back to copying.
  110. IkiWiki::prep_writefile($filename, $config{srcdir});
  111. unlink($config{srcdir}."/".$filename);
  112. if (rename($tempfile, $config{srcdir}."/".$filename)) {
  113. # The temp file has tight permissions; loosen up.
  114. chmod(0666 & ~umask, $config{srcdir}."/".$filename);
  115. }
  116. else {
  117. my $fh=$q->upload('attachment');
  118. if (! defined $fh || ! ref $fh) {
  119. error("failed to get filehandle");
  120. }
  121. binmode($fh);
  122. writefile($filename, $config{srcdir}, undef, 1, sub {
  123. IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
  124. });
  125. }
  126. # Check the attachment in and trigger a wiki refresh.
  127. if ($config{rcs}) {
  128. IkiWiki::rcs_add($filename);
  129. IkiWiki::disable_commit_hook();
  130. IkiWiki::rcs_commit($filename, gettext("attachment upload"),
  131. IkiWiki::rcs_prepedit($filename),
  132. $session->param("name"), $ENV{REMOTE_ADDR});
  133. IkiWiki::enable_commit_hook();
  134. IkiWiki::rcs_update();
  135. }
  136. IkiWiki::refresh();
  137. IkiWiki::saveindex();
  138. }
  139. elsif ($form->submitted eq "Insert Links") {
  140. my $add="";
  141. foreach my $f ($q->param("attachment_select")) {
  142. $add.="[[$f]]\n";
  143. }
  144. $form->field(name => 'editcontent',
  145. value => $form->field('editcontent')."\n\n".$add,
  146. force => 1) if length $add;
  147. }
  148. # Generate the attachment list only after having added any new
  149. # attachments.
  150. $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
  151. } # }}}
  152. sub attachment_location ($) {
  153. my $page=shift;
  154. # Put the attachment in a subdir of the page it's attached
  155. # to, unless that page is an "index" page.
  156. $page=~s/(^|\/)index//;
  157. $page.="/" if length $page;
  158. return $page;
  159. }
  160. sub attachment_list ($) {
  161. my $page=shift;
  162. my $loc=attachment_location($page);
  163. my @ret;
  164. foreach my $f (values %pagesources) {
  165. if (! defined IkiWiki::pagetype($f) &&
  166. $f=~m/^\Q$loc\E[^\/]+$/ &&
  167. -e "$config{srcdir}/$f") {
  168. push @ret, {
  169. "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
  170. link => htmllink($page, $page, $f, noimageinline => 1),
  171. size => humansize((stat(_))[7]),
  172. mtime => displaytime($IkiWiki::pagemtime{$f}),
  173. mtime_raw => $IkiWiki::pagemtime{$f},
  174. };
  175. }
  176. }
  177. # Sort newer attachments to the top of the list, so a newly-added
  178. # attachment appears just before the form used to add it.
  179. return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;
  180. }
  181. my %units=( # size in bytes
  182. B => 1,
  183. byte => 1,
  184. KB => 2 ** 10,
  185. kilobyte => 2 ** 10,
  186. K => 2 ** 10,
  187. KB => 2 ** 10,
  188. kilobyte => 2 ** 10,
  189. M => 2 ** 20,
  190. MB => 2 ** 20,
  191. megabyte => 2 ** 20,
  192. G => 2 ** 30,
  193. GB => 2 ** 30,
  194. gigabyte => 2 ** 30,
  195. T => 2 ** 40,
  196. TB => 2 ** 40,
  197. terabyte => 2 ** 40,
  198. P => 2 ** 50,
  199. PB => 2 ** 50,
  200. petabyte => 2 ** 50,
  201. E => 2 ** 60,
  202. EB => 2 ** 60,
  203. exabyte => 2 ** 60,
  204. Z => 2 ** 70,
  205. ZB => 2 ** 70,
  206. zettabyte => 2 ** 70,
  207. Y => 2 ** 80,
  208. YB => 2 ** 80,
  209. yottabyte => 2 ** 80,
  210. # ikiwiki, if you find you need larger data quantities, either modify
  211. # yourself to add them, or travel back in time to 2008 and kill me.
  212. # -- Joey
  213. );
  214. sub parsesize ($) { #{{{
  215. my $size=shift;
  216. no warnings;
  217. my $base=$size+0; # force to number
  218. use warnings;
  219. foreach my $unit (sort keys %units) {
  220. if ($size=~/[0-9\s]\Q$unit\E$/i) {
  221. return $base * $units{$unit};
  222. }
  223. }
  224. return $base;
  225. } #}}}
  226. sub humansize ($) { #{{{
  227. my $size=shift;
  228. foreach my $unit (reverse sort { $units{$a} <=> $units{$b} || $b cmp $a } keys %units) {
  229. if ($size / $units{$unit} > 0.25) {
  230. return (int($size / $units{$unit} * 10)/10).$unit;
  231. }
  232. }
  233. return $size; # near zero, or negative
  234. } #}}}
  235. package IkiWiki::PageSpec;
  236. sub match_maxsize ($$;@) { #{{{
  237. shift;
  238. my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
  239. if ($@) {
  240. return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
  241. }
  242. my %params=@_;
  243. if (! exists $params{file}) {
  244. return IkiWiki::FailReason->new("no file specified");
  245. }
  246. if (-s $params{file} > $maxsize) {
  247. return IkiWiki::FailReason->new("file too large (".(-s $params{file})." > $maxsize)");
  248. }
  249. else {
  250. return IkiWiki::SuccessReason->new("file not too large");
  251. }
  252. } #}}}
  253. sub match_minsize ($$;@) { #{{{
  254. shift;
  255. my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
  256. if ($@) {
  257. return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
  258. }
  259. my %params=@_;
  260. if (! exists $params{file}) {
  261. return IkiWiki::FailReason->new("no file specified");
  262. }
  263. if (-s $params{file} < $minsize) {
  264. return IkiWiki::FailReason->new("file too small");
  265. }
  266. else {
  267. return IkiWiki::SuccessReason->new("file not too small");
  268. }
  269. } #}}}
  270. sub match_mimetype ($$;@) { #{{{
  271. shift;
  272. my $wanted=shift;
  273. my %params=@_;
  274. if (! exists $params{file}) {
  275. return IkiWiki::FailReason->new("no file specified");
  276. }
  277. # Use ::magic to get the mime type, the idea is to only trust
  278. # data obtained by examining the actual file contents.
  279. eval q{use File::MimeInfo::Magic};
  280. if ($@) {
  281. return IkiWiki::FailReason->new("failed to load File::MimeInfo::Magic ($@); cannot check MIME type");
  282. }
  283. my $mimetype=File::MimeInfo::Magic::magic($params{file});
  284. if (! defined $mimetype) {
  285. $mimetype="unknown";
  286. }
  287. my $regexp=IkiWiki::glob2re($wanted);
  288. if ($mimetype!~/^$regexp$/i) {
  289. return IkiWiki::FailReason->new("file MIME type is $mimetype, not $wanted");
  290. }
  291. else {
  292. return IkiWiki::SuccessReason->new("file MIME type is $mimetype");
  293. }
  294. } #}}}
  295. sub match_ispage ($$;@) { #{{{
  296. my $filename=shift;
  297. if (defined IkiWiki::pagetype($filename)) {
  298. return IkiWiki::SuccessReason->new("file is a wiki page");
  299. }
  300. else {
  301. return IkiWiki::FailReason->new("file is not a wiki page");
  302. }
  303. } #}}}
  304. sub match_user ($$;@) { #{{{
  305. shift;
  306. my $user=shift;
  307. my %params=@_;
  308. if (! exists $params{user}) {
  309. return IkiWiki::FailReason->new("no user specified");
  310. }
  311. if (defined $params{user} && lc $params{user} eq lc $user) {
  312. return IkiWiki::SuccessReason->new("user is $user");
  313. }
  314. else {
  315. return IkiWiki::FailReason->new("user is $params{user}, not $user");
  316. }
  317. } #}}}
  318. sub match_ip ($$;@) { #{{{
  319. shift;
  320. my $ip=shift;
  321. my %params=@_;
  322. if (! exists $params{ip}) {
  323. return IkiWiki::FailReason->new("no IP specified");
  324. }
  325. if (defined $params{ip} && lc $params{ip} eq lc $ip) {
  326. return IkiWiki::SuccessReason->new("IP is $ip");
  327. }
  328. else {
  329. return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");
  330. }
  331. } #}}}
  332. 1