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