summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/attachment.pm
blob: 88b26e0415eff3a85f75875ba6fbc669f4ea6342 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::attachment;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. use CGI;
  7. $CGI::DISABLE_UPLOADS=0;
  8. # TODO move to admin prefs
  9. $config{valid_attachments}="(*.mp3 and maxsize(15mb)) or (!ispage() and maxsize(50kb))";
  10. sub import { #{{{
  11. hook(type => "formbuilder_setup", id => "attachment", call => \&formbuilder_setup);
  12. hook(type => "formbuilder", id => "attachment", call => \&formbuilder);
  13. } # }}}
  14. sub formbuilder_setup { #{{{
  15. my %params=@_;
  16. my $form=$params{form};
  17. return if $form->field("do") ne "edit";
  18. $form->field(name => 'attachment', type => 'file');
  19. } #}}}
  20. sub formbuilder (@) { #{{{
  21. my %params=@_;
  22. my $form=$params{form};
  23. return if $form->field("do") ne "edit";
  24. if ($form->submitted eq "Upload") {
  25. my $q=$params{cgi};
  26. my $filename=IkiWiki::basename($q->param('attachment'));
  27. if (! defined $filename || ! length $filename) {
  28. # no file, so do nothing
  29. return;
  30. }
  31. # This is an (apparently undocumented) way to get the name
  32. # of the temp file that CGI writes the upload to.
  33. my $tempfile=$q->tmpFileName($filename);
  34. # To untaint the filename, escape any hazardous characters,
  35. # and make sure it isn't pruned.
  36. $filename=IkiWiki::possibly_foolish_untaint(IkiWiki::titlepage($filename));
  37. if (IkiWiki::file_pruned($filename, $config{srcdir})) {
  38. error(gettext("bad attachment filename"));
  39. }
  40. # XXX Put the attachment in a subdir corresponding to the
  41. # page being edited.
  42. # The editpage code has already checked that
  43. # $form->field('page') is valid.
  44. $filename="XXX/$filename";
  45. # Use a pagespec to test that the attachment is valid.
  46. if (exists $config{valid_attachments} &&
  47. length $config{valid_attachments}) {
  48. my $result=pagespec_match($filename, $config{valid_attachments},
  49. file => $tempfile);
  50. if (! $result) {
  51. error(gettext("attachment rejected")." ($result)");
  52. }
  53. }
  54. # Move the attachment into place.
  55. # Try to use a fast rename; fall back to copying.
  56. prep_writefile($filename, $config{srcdir});
  57. unlink($config{srcdir}."/".$filename);
  58. if (! rename($tempfile, $config{srcdir}."/".$filename)) {
  59. my $fh=$q->upload('attachment');
  60. if (! defined $fh || ! ref $fh) {
  61. error("failed to get filehandle");
  62. }
  63. binmode($fh);
  64. writefile($filename, $config{srcdir}, undef, 1, sub {
  65. IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
  66. });
  67. }
  68. # TODO add to vcs
  69. # TODO trigger a wiki build if there's no vcs
  70. }
  71. } # }}}
  72. package IkiWiki::PageSpec;
  73. sub parsesize { #{{{
  74. my $size=shift;
  75. no warnings;
  76. my $base=$size+0; # force to number
  77. use warnings;
  78. my $multiple=1;
  79. if ($size=~/kb?$/i) {
  80. $multiple=2**10;
  81. }
  82. elsif ($size=~/mb?$/i) {
  83. $multiple=2**20;
  84. }
  85. elsif ($size=~/gb?$/i) {
  86. $multiple=2**30;
  87. }
  88. elsif ($size=~/tb?$/i) {
  89. $multiple=2**40;
  90. }
  91. return $base * $multiple;
  92. } #}}}
  93. sub match_maxsize ($$;@) { #{{{
  94. shift;
  95. my $maxsize=eval{parsesize(shift)};
  96. if ($@) {
  97. return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
  98. }
  99. my %params=@_;
  100. if (! exists $params{file}) {
  101. return IkiWiki::FailReason->new("no file specified");
  102. }
  103. if (-s $params{file} > $maxsize) {
  104. return IkiWiki::FailReason->new("file too large");
  105. }
  106. else {
  107. return IkiWiki::SuccessReason->new("file not too large");
  108. }
  109. } #}}}
  110. sub match_minsize ($$;@) { #{{{
  111. shift;
  112. my $minsize=eval{parsesize(shift)};
  113. if ($@) {
  114. return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
  115. }
  116. my %params=@_;
  117. if (! exists $params{file}) {
  118. return IkiWiki::FailReason->new("no file specified");
  119. }
  120. if (-s $params{file} < $minsize) {
  121. return IkiWiki::FailReason->new("file too small");
  122. }
  123. else {
  124. return IkiWiki::SuccessReason->new("file not too small");
  125. }
  126. } #}}}
  127. sub match_ispage ($$;@) { #{{{
  128. my $filename=shift;
  129. if (IkiWiki::pagetype($filename)) {
  130. return IkiWiki::SuccessReason->new("file is a wiki page");
  131. }
  132. else {
  133. return IkiWiki::FailReason->new("file is not a wiki page");
  134. }
  135. } #}}}
  136. 1