summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/attachment.pm
blob: 6168ea589073f9c9b4272d96ba4187ec458b8f3f (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. # Use a pagespec to test that the attachment is valid.
  41. if (exists $config{valid_attachments} &&
  42. length $config{valid_attachments}) {
  43. my $result=pagespec_match($filename, $config{valid_attachments},
  44. file => $tempfile);
  45. if (! $result) {
  46. error(gettext("attachment rejected")." ($result)");
  47. }
  48. }
  49. my $fh=$q->upload('attachment');
  50. if (! defined $fh || ! ref $fh) {
  51. error("failed to get filehandle");
  52. }
  53. binmode($fh);
  54. while (<$fh>) {
  55. print STDERR $_."\n";
  56. }
  57. }
  58. } # }}}
  59. package IkiWiki::PageSpec;
  60. sub parsesize { #{{{
  61. my $size=shift;
  62. no warnings;
  63. my $base=$size+0; # force to number
  64. use warnings;
  65. my $multiple=1;
  66. if ($size=~/kb?$/i) {
  67. $multiple=2**10;
  68. }
  69. elsif ($size=~/mb?$/i) {
  70. $multiple=2**20;
  71. }
  72. elsif ($size=~/gb?$/i) {
  73. $multiple=2**30;
  74. }
  75. elsif ($size=~/tb?$/i) {
  76. $multiple=2**40;
  77. }
  78. return $base * $multiple;
  79. } #}}}
  80. sub match_maxsize ($$;@) { #{{{
  81. shift;
  82. my $maxsize=eval{parsesize(shift)};
  83. if ($@) {
  84. return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
  85. }
  86. my %params=@_;
  87. if (! exists $params{file}) {
  88. return IkiWiki::FailReason->new("no file specified");
  89. }
  90. if (-s $params{file} > $maxsize) {
  91. return IkiWiki::FailReason->new("file too large");
  92. }
  93. else {
  94. return IkiWiki::SuccessReason->new("file not too large");
  95. }
  96. } #}}}
  97. sub match_minsize ($$;@) { #{{{
  98. shift;
  99. my $minsize=eval{parsesize(shift)};
  100. if ($@) {
  101. return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
  102. }
  103. my %params=@_;
  104. if (! exists $params{file}) {
  105. return IkiWiki::FailReason->new("no file specified");
  106. }
  107. if (-s $params{file} < $minsize) {
  108. return IkiWiki::FailReason->new("file too small");
  109. }
  110. else {
  111. return IkiWiki::SuccessReason->new("file not too small");
  112. }
  113. } #}}}
  114. sub match_ispage ($$;@) { #{{{
  115. my $filename=shift;
  116. if (IkiWiki::pagetype($filename)) {
  117. return IkiWiki::SuccessReason->new("file is a wiki page");
  118. }
  119. else {
  120. return IkiWiki::FailReason->new("file is not a wiki page");
  121. }
  122. } #}}}
  123. 1