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