2 package IkiWiki::Plugin::attachment;
9 hook(type => "checkconfig", id => "attachment", call => \&checkconfig);
10 hook(type => "formbuilder_setup", id => "attachment", call => \&formbuilder_setup);
11 hook(type => "formbuilder", id => "attachment", call => \&formbuilder);
14 sub checkconfig () { #{{{
15 $config{cgi_disable_uploads}=0;
18 sub formbuilder_setup (@) { #{{{
20 my $form=$params{form};
22 if ($form->field("do") eq "edit") {
23 $form->field(name => 'attachment', type => 'file');
25 elsif ($form->title eq "preferences") {
26 my $session=$params{session};
27 my $user_name=$session->param("name");
29 $form->field(name => "allowed_attachments", size => 50,
31 comment => "(".htmllink("", "", "ikiwiki/PageSpec", noimageinline => 1).")");
32 if (! IkiWiki::is_admin($user_name)) {
33 $form->field(name => "allowed_attachments", type => "hidden");
35 if (! $form->submitted) {
36 $form->field(name => "allowed_attachments", force => 1,
37 value => IkiWiki::userinfo_get($user_name, "allowed_attachments"));
39 if ($form->submitted && $form->submitted eq 'Save Preferences') {
40 if (defined $form->field("allowed_attachments")) {
41 IkiWiki::userinfo_set($user_name, "allowed_attachments",
42 $form->field("allowed_attachments")) ||
43 error("failed to set allowed_attachments");
49 sub formbuilder (@) { #{{{
51 my $form=$params{form};
53 return if $form->field("do") ne "edit";
55 if ($form->submitted eq "Upload") {
57 my $session=$params{session};
59 my $filename=$q->param('attachment');
60 if (! defined $filename || ! length $filename) {
61 # no file, so do nothing
65 # This is an (apparently undocumented) way to get the name
66 # of the temp file that CGI writes the upload to.
67 my $tempfile=$q->tmpFileName($filename);
69 # Put the attachment in a subdir of the page it's attached
70 # to, unless that page is an "index" page.
71 my $page=$form->field('page');
72 $page=~s/(^|\/)index//;
73 $filename=(length $page ? $page."/" : "").IkiWiki::basename($filename);
75 # To untaint the filename, escape any hazardous characters,
76 # and make sure it isn't pruned.
77 $filename=IkiWiki::titlepage(IkiWiki::possibly_foolish_untaint($filename));
78 if (IkiWiki::file_pruned($filename, $config{srcdir})) {
79 error(gettext("bad attachment filename"));
82 # Check that the user is allowed to edit a page with the
83 # name of the attachment.
84 IkiWiki::check_canedit($filename, $q, $session, 1);
86 # Use a special pagespec to test that the attachment is valid.
88 foreach my $admin (@{$config{adminuser}}) {
89 my $allowed_attachments=IkiWiki::userinfo_get($admin, "allowed_attachments");
90 if (defined $allowed_attachments &&
91 length $allowed_attachments) {
92 $allowed=pagespec_match($filename,
99 error(gettext("attachment rejected")." ($allowed)");
102 # Needed for fast_file_copy and for rendering below.
103 require IkiWiki::Render;
105 # Move the attachment into place.
106 # Try to use a fast rename; fall back to copying.
107 IkiWiki::prep_writefile($filename, $config{srcdir});
108 unlink($config{srcdir}."/".$filename);
109 if (! rename($tempfile, $config{srcdir}."/".$filename)) {
110 my $fh=$q->upload('attachment');
111 if (! defined $fh || ! ref $fh) {
112 error("failed to get filehandle");
115 print STDERR "copying $filename\n";
116 writefile($filename, $config{srcdir}, undef, 1, sub {
117 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
121 # Check the attachment in and trigger a wiki refresh.
123 IkiWiki::rcs_add($filename);
124 IkiWiki::disable_commit_hook();
125 IkiWiki::rcs_commit($filename, gettext("attachment upload"),
126 IkiWiki::rcs_prepedit($filename),
127 $session->param("name"), $ENV{REMOTE_ADDR});
128 IkiWiki::enable_commit_hook();
129 IkiWiki::rcs_update();
131 print STDERR "refreshing\n";
133 IkiWiki::saveindex();
137 package IkiWiki::PageSpec;
139 sub parsesize ($) { #{{{
142 my $base=$size+0; # force to number
145 if ($size=~/kb?$/i) {
148 elsif ($size=~/mb?$/i) {
151 elsif ($size=~/gb?$/i) {
154 elsif ($size=~/tb?$/i) {
157 return $base * $multiple;
160 sub match_maxsize ($$;@) { #{{{
162 my $maxsize=eval{parsesize(shift)};
164 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
168 if (! exists $params{file}) {
169 return IkiWiki::FailReason->new("no file specified");
172 if (-s $params{file} > $maxsize) {
173 return IkiWiki::FailReason->new("file too large");
176 return IkiWiki::SuccessReason->new("file not too large");
180 sub match_minsize ($$;@) { #{{{
182 my $minsize=eval{parsesize(shift)};
184 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
188 if (! exists $params{file}) {
189 return IkiWiki::FailReason->new("no file specified");
192 if (-s $params{file} < $minsize) {
193 return IkiWiki::FailReason->new("file too small");
196 return IkiWiki::SuccessReason->new("file not too small");
200 sub match_ispage ($$;@) { #{{{
203 if (defined IkiWiki::pagetype($filename)) {
204 return IkiWiki::SuccessReason->new("file is a wiki page");
207 return IkiWiki::FailReason->new("file is not a wiki page");