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