better approach for cgi upload disabling
[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
8 # TODO move to admin prefs
9 $config{valid_attachments}="(*.mp3 and maxsize(15mb)) or (!ispage() and maxsize(50kb))";
10
11 sub import { #{{{
12         hook(type => "checkconfig", id => "attachment", call => \&checkconfig);
13         hook(type => "formbuilder_setup", id => "attachment", call => \&formbuilder_setup);
14         hook(type => "formbuilder", id => "attachment", call => \&formbuilder);
15 } # }}}
16
17 sub checkconfig () { #{{{
18         $config{cgi_disable_uploads}=0;
19 } #}}}
20
21 sub formbuilder_setup (@) { #{{{
22         my %params=@_;
23         my $form=$params{form};
24
25         return if $form->field("do") ne "edit";
26
27         $form->field(name => 'attachment', type => 'file');
28 } #}}}
29
30 sub formbuilder (@) { #{{{
31         my %params=@_;
32         my $form=$params{form};
33
34         return if $form->field("do") ne "edit";
35
36         if ($form->submitted eq "Upload") {
37                 my $q=$params{cgi};
38                 my $filename=$q->param('attachment');
39                 if (! defined $filename || ! length $filename) {
40                         # no file, so do nothing
41                         return;
42                 }
43                 
44                 # This is an (apparently undocumented) way to get the name
45                 # of the temp file that CGI writes the upload to.
46                 my $tempfile=$q->tmpFileName($filename);
47                 
48                 # Put the attachment in a subdir of the page it's attached
49                 # to, unless that page is an "index" page.
50                 my $page=$form->field('page');
51                 $page=~s/(^|\/)index//;
52                 $filename=$page."/".IkiWiki::basename($filename);
53                 
54                 # To untaint the filename, escape any hazardous characters,
55                 # and make sure it isn't pruned.
56                 $filename=IkiWiki::titlepage(IkiWiki::possibly_foolish_untaint($filename));
57                 if (IkiWiki::file_pruned($filename, $config{srcdir})) {
58                         error(gettext("bad attachment filename"));
59                 }
60                 
61                 # Check that the user is allowed to edit a page with the
62                 # name of the attachment.
63                 IkiWiki::check_canedit($filename, $q, $params{session}, 1);
64                 
65                 # Use a pagespec to test that the attachment is valid.
66                 if (exists $config{valid_attachments} &&
67                     length $config{valid_attachments}) {
68                         my $result=pagespec_match($filename, $config{valid_attachments},
69                                 file => $tempfile);
70                         if (! $result) {
71                                 error(gettext("attachment rejected")." ($result)");
72                         }
73                 }
74
75                 # Move the attachment into place.
76                 # Try to use a fast rename; fall back to copying.
77                 IkiWiki::prep_writefile($filename, $config{srcdir});
78                 unlink($config{srcdir}."/".$filename);
79                 if (! rename($tempfile, $config{srcdir}."/".$filename)) {
80                         my $fh=$q->upload('attachment');
81                         if (! defined $fh || ! ref $fh) {
82                                 error("failed to get filehandle");
83                         }
84                         binmode($fh);
85                         writefile($filename, $config{srcdir}, undef, 1, sub {
86                                 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
87                         });
88                 }
89
90                 # TODO add to vcs
91                 
92                 # TODO trigger a wiki build if there's no vcs
93         }
94 } # }}}
95
96 package IkiWiki::PageSpec;
97
98 sub parsesize { #{{{
99         my $size=shift;
100         no warnings;
101         my $base=$size+0; # force to number
102         use warnings;
103         my $multiple=1;
104         if ($size=~/kb?$/i) {
105                 $multiple=2**10;
106         }
107         elsif ($size=~/mb?$/i) {
108                 $multiple=2**20;
109         }
110         elsif ($size=~/gb?$/i) {
111                 $multiple=2**30;
112         }
113         elsif ($size=~/tb?$/i) {
114                 $multiple=2**40;
115         }
116         return $base * $multiple;
117 } #}}}
118
119 sub match_maxsize ($$;@) { #{{{
120         shift;
121         my $maxsize=eval{parsesize(shift)};
122         if ($@) {
123                 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
124         }
125
126         my %params=@_;
127         if (! exists $params{file}) {
128                 return IkiWiki::FailReason->new("no file specified");
129         }
130
131         if (-s $params{file} > $maxsize) {
132                 return IkiWiki::FailReason->new("file too large");
133         }
134         else {
135                 return IkiWiki::SuccessReason->new("file not too large");
136         }
137 } #}}}
138
139 sub match_minsize ($$;@) { #{{{
140         shift;
141         my $minsize=eval{parsesize(shift)};
142         if ($@) {
143                 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
144         }
145
146         my %params=@_;
147         if (! exists $params{file}) {
148                 return IkiWiki::FailReason->new("no file specified");
149         }
150
151         if (-s $params{file} < $minsize) {
152                 return IkiWiki::FailReason->new("file too small");
153         }
154         else {
155                 return IkiWiki::SuccessReason->new("file not too small");
156         }
157 } #}}}
158
159 sub match_ispage ($$;@) { #{{{
160         my $filename=shift;
161
162         if (defined IkiWiki::pagetype($filename)) {
163                 return IkiWiki::SuccessReason->new("file is a wiki page");
164         }
165         else {
166                 return IkiWiki::FailReason->new("file is not a wiki page");
167         }
168 } #}}}
169
170 1