]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/attachment.pm
bugfixes
[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 (!ispage() 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=$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                 # Put the attachment in a subdir of the page it's attached
46                 # to, unless that page is an "index" page.
47                 my $page=$form->field('page');
48                 $page=~s/(^|\/)index//;
49                 $filename=$page."/".IkiWiki::basename($filename);
50                 
51                 # To untaint the filename, escape any hazardous characters,
52                 # and make sure it isn't pruned.
53                 $filename=IkiWiki::titlepage(IkiWiki::possibly_foolish_untaint($filename));
54                 if (IkiWiki::file_pruned($filename, $config{srcdir})) {
55                         error(gettext("bad attachment filename"));
56                 }
57                 
58                 # Check that the user is allowed to edit a page with the
59                 # name of the attachment.
60                 IkiWiki::check_canedit($filename, $q, $params{session}, 1);
61                 
62                 # Use a pagespec to test that the attachment is valid.
63                 if (exists $config{valid_attachments} &&
64                     length $config{valid_attachments}) {
65                         my $result=pagespec_match($filename, $config{valid_attachments},
66                                 file => $tempfile);
67                         if (! $result) {
68                                 error(gettext("attachment rejected")." ($result)");
69                         }
70                 }
71
72                 # Move the attachment into place.
73                 # Try to use a fast rename; fall back to copying.
74                 IkiWiki::prep_writefile($filename, $config{srcdir});
75                 unlink($config{srcdir}."/".$filename);
76                 if (! rename($tempfile, $config{srcdir}."/".$filename)) {
77                         my $fh=$q->upload('attachment');
78                         if (! defined $fh || ! ref $fh) {
79                                 error("failed to get filehandle");
80                         }
81                         binmode($fh);
82                         writefile($filename, $config{srcdir}, undef, 1, sub {
83                                 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
84                         });
85                 }
86
87                 # TODO add to vcs
88                 
89                 # TODO trigger a wiki build if there's no vcs
90         }
91 } # }}}
92
93 package IkiWiki::PageSpec;
94
95 sub parsesize { #{{{
96         my $size=shift;
97         no warnings;
98         my $base=$size+0; # force to number
99         use warnings;
100         my $multiple=1;
101         if ($size=~/kb?$/i) {
102                 $multiple=2**10;
103         }
104         elsif ($size=~/mb?$/i) {
105                 $multiple=2**20;
106         }
107         elsif ($size=~/gb?$/i) {
108                 $multiple=2**30;
109         }
110         elsif ($size=~/tb?$/i) {
111                 $multiple=2**40;
112         }
113         return $base * $multiple;
114 } #}}}
115
116 sub match_maxsize ($$;@) { #{{{
117         shift;
118         my $maxsize=eval{parsesize(shift)};
119         if ($@) {
120                 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
121         }
122
123         my %params=@_;
124         if (! exists $params{file}) {
125                 return IkiWiki::FailReason->new("no file specified");
126         }
127
128         if (-s $params{file} > $maxsize) {
129                 return IkiWiki::FailReason->new("file too large");
130         }
131         else {
132                 return IkiWiki::SuccessReason->new("file not too large");
133         }
134 } #}}}
135
136 sub match_minsize ($$;@) { #{{{
137         shift;
138         my $minsize=eval{parsesize(shift)};
139         if ($@) {
140                 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
141         }
142
143         my %params=@_;
144         if (! exists $params{file}) {
145                 return IkiWiki::FailReason->new("no file specified");
146         }
147
148         if (-s $params{file} < $minsize) {
149                 return IkiWiki::FailReason->new("file too small");
150         }
151         else {
152                 return IkiWiki::SuccessReason->new("file not too small");
153         }
154 } #}}}
155
156 sub match_ispage ($$;@) { #{{{
157         my $filename=shift;
158
159         if (defined IkiWiki::pagetype($filename)) {
160                 return IkiWiki::SuccessReason->new("file is a wiki page");
161         }
162         else {
163                 return IkiWiki::FailReason->new("file is not a wiki page");
164         }
165 } #}}}
166
167 1