basic attachment plugin, unfinished
[ikiwiki] / 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                 # Check that the attachment matches the configured
53                 # pagespec.
54                 my $result=pagespec_match($filename, $config{valid_attachments},
55                         tempfile => $tempfile);
56                 if (! $result) {
57                         error(gettext("attachment rejected")." ($result)");
58                 }
59                 
60                 my $fh=$q->upload('attachment');
61                 if (! defined $fh || ! ref $fh) {
62                         error("failed to get filehandle");
63                 }
64                 binmode($fh);
65                 while (<$fh>) {
66                         print STDERR $_."\n";
67                 }
68         }
69 } # }}}
70
71 package IkiWiki::PageSpec;
72
73 sub parsesize { #{{{
74         my $size=shift;
75         no warnings;
76         my $base=$size+0; # force to number
77         use warnings;
78         my $exponent=1;
79         if ($size=~/kb?$/i) {
80                 $exponent=10;
81         }
82         elsif ($size=~/mb?$/i) {
83                 $exponent=20;
84         }
85         elsif ($size=~/gb?$/i) {
86                 $exponent=30;
87         }
88         elsif ($size=~/tb?$/i) {
89                 $exponent=40;
90         }
91         return $base * 2**$exponent;
92 } #}}}
93
94 sub match_maxsize ($$;@) { #{{{
95         shift;
96         my $maxsize=eval{parsesize(shift)};
97         if ($@) {
98                 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
99         }
100
101         my %params=@_;
102         if (! exists $params{tempfile}) {
103                 return IkiWiki::FailReason->new("no tempfile specified");
104         }
105
106         if (-s $params{tempfile} > $maxsize) {
107                 return IkiWiki::FailReason->new("attachment too large");
108         }
109         else {
110                 return IkiWiki::SuccessReason->new("attachment size ok");
111         }
112 } #}}}
113
114 sub match_minsize ($$;@) { #{{{
115         shift;
116         my $minsize=eval{parsesize(shift)};
117         if ($@) {
118                 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
119         }
120
121         my %params=@_;
122         if (! exists $params{tempfile}) {
123                 return IkiWiki::FailReason->new("no tempfile specified");
124         }
125
126         if (-s $params{tempfile} < $minsize) {
127                 return IkiWiki::FailReason->new("attachment too small");
128         }
129         else {
130                 return IkiWiki::SuccessReason->new("attachment size ok");
131         }
132 } #}}}
133
134 1