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