copy the attachment into srcdir
[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 (!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=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                 # XXX Put the attachment in a subdir corresponding to the
53                 # page being edited.
54                 # The editpage code has already checked that
55                 # $form->field('page') is valid.
56                 $filename="XXX/$filename";
57                 
58                 # Use a pagespec to test that the attachment is valid.
59                 if (exists $config{valid_attachments} &&
60                     length $config{valid_attachments}) {
61                         my $result=pagespec_match($filename, $config{valid_attachments},
62                                 file => $tempfile);
63                         if (! $result) {
64                                 error(gettext("attachment rejected")." ($result)");
65                         }
66                 }
67
68                 # Move the attachment into place.
69                 # Try to use a fast rename; fall back to copying.
70                 prep_writefile($filename, $config{srcdir});
71                 unlink($config{srcdir}."/".$filename);
72                 if (! rename($tempfile, $config{srcdir}."/".$filename)) {
73                         my $fh=$q->upload('attachment');
74                         if (! defined $fh || ! ref $fh) {
75                                 error("failed to get filehandle");
76                         }
77                         binmode($fh);
78                         writefile($filename, $config{srcdir}, undef, 1, sub {
79                                 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
80                         });
81                 }
82
83                 # TODO add to vcs
84                 
85                 # TODO trigger a wiki build if there's no vcs
86         }
87 } # }}}
88
89 package IkiWiki::PageSpec;
90
91 sub parsesize { #{{{
92         my $size=shift;
93         no warnings;
94         my $base=$size+0; # force to number
95         use warnings;
96         my $multiple=1;
97         if ($size=~/kb?$/i) {
98                 $multiple=2**10;
99         }
100         elsif ($size=~/mb?$/i) {
101                 $multiple=2**20;
102         }
103         elsif ($size=~/gb?$/i) {
104                 $multiple=2**30;
105         }
106         elsif ($size=~/tb?$/i) {
107                 $multiple=2**40;
108         }
109         return $base * $multiple;
110 } #}}}
111
112 sub match_maxsize ($$;@) { #{{{
113         shift;
114         my $maxsize=eval{parsesize(shift)};
115         if ($@) {
116                 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
117         }
118
119         my %params=@_;
120         if (! exists $params{file}) {
121                 return IkiWiki::FailReason->new("no file specified");
122         }
123
124         if (-s $params{file} > $maxsize) {
125                 return IkiWiki::FailReason->new("file too large");
126         }
127         else {
128                 return IkiWiki::SuccessReason->new("file not too large");
129         }
130 } #}}}
131
132 sub match_minsize ($$;@) { #{{{
133         shift;
134         my $minsize=eval{parsesize(shift)};
135         if ($@) {
136                 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
137         }
138
139         my %params=@_;
140         if (! exists $params{file}) {
141                 return IkiWiki::FailReason->new("no file specified");
142         }
143
144         if (-s $params{file} < $minsize) {
145                 return IkiWiki::FailReason->new("file too small");
146         }
147         else {
148                 return IkiWiki::SuccessReason->new("file not too small");
149         }
150 } #}}}
151
152 sub match_ispage ($$;@) { #{{{
153         my $filename=shift;
154
155         if (IkiWiki::pagetype($filename)) {
156                 return IkiWiki::SuccessReason->new("file is a wiki page");
157         }
158         else {
159                 return IkiWiki::FailReason->new("file is not a wiki page");
160         }
161 } #}}}
162
163 1