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