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