2 package IkiWiki::Plugin::attachment;
8 $CGI::DISABLE_UPLOADS=0;
10 # TODO move to admin prefs
11 $config{valid_attachments}="(*.mp3 and maxsize(15mb)) or (!ispage() and maxsize(50kb))";
14 hook(type => "formbuilder_setup", id => "attachment", call => \&formbuilder_setup);
15 hook(type => "formbuilder", id => "attachment", call => \&formbuilder);
18 sub formbuilder_setup { #{{{
20 my $form=$params{form};
22 return if $form->field("do") ne "edit";
24 $form->field(name => 'attachment', type => 'file');
27 sub formbuilder (@) { #{{{
29 my $form=$params{form};
31 return if $form->field("do") ne "edit";
33 if ($form->submitted eq "Upload") {
35 my $filename=IkiWiki::basename($q->param('attachment'));
36 if (! defined $filename || ! length $filename) {
37 # no file, so do nothing
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);
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"));
52 # XXX Put the attachment in a subdir corresponding to the
54 # The editpage code has already checked that
55 # $form->field('page') is valid.
56 $filename="XXX/$filename";
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},
64 error(gettext("attachment rejected")." ($result)");
68 # Also check that the user is allowed to edit it by other
70 IkiWiki::check_canedit($filename, $q, $params{session}, 1);
72 # Move the attachment into place.
73 # Try to use a fast rename; fall back to copying.
74 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");
82 writefile($filename, $config{srcdir}, undef, 1, sub {
83 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
89 # TODO trigger a wiki build if there's no vcs
93 package IkiWiki::PageSpec;
98 my $base=$size+0; # force to number
101 if ($size=~/kb?$/i) {
104 elsif ($size=~/mb?$/i) {
107 elsif ($size=~/gb?$/i) {
110 elsif ($size=~/tb?$/i) {
113 return $base * $multiple;
116 sub match_maxsize ($$;@) { #{{{
118 my $maxsize=eval{parsesize(shift)};
120 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
124 if (! exists $params{file}) {
125 return IkiWiki::FailReason->new("no file specified");
128 if (-s $params{file} > $maxsize) {
129 return IkiWiki::FailReason->new("file too large");
132 return IkiWiki::SuccessReason->new("file not too large");
136 sub match_minsize ($$;@) { #{{{
138 my $minsize=eval{parsesize(shift)};
140 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
144 if (! exists $params{file}) {
145 return IkiWiki::FailReason->new("no file specified");
148 if (-s $params{file} < $minsize) {
149 return IkiWiki::FailReason->new("file too small");
152 return IkiWiki::SuccessReason->new("file not too small");
156 sub match_ispage ($$;@) { #{{{
159 if (defined IkiWiki::pagetype($filename)) {
160 return IkiWiki::SuccessReason->new("file is a wiki page");
163 return IkiWiki::FailReason->new("file is not a wiki page");