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 attachment_location ($) {
21 # Put the attachment in a subdir of the page it's attached
22 # to, unless that page is an "index" page.
23 $page=~s/(^|\/)index//;
24 $page.="/" if length $page;
29 sub attachment_list ($) {
30 my $loc=attachment_location(shift);
33 foreach my $f (values %pagesources) {
34 print STDERR ">>$f\n" if ! defined IkiWiki::pagetype($f);
35 if (! defined IkiWiki::pagetype($f) &&
36 $f=~m/^\Q$loc\E[^\/]+$/ &&
37 -e "$config{srcdir}/$f") {
39 "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'">',
42 mtime => displaytime($IkiWiki::pagemtime{$f}),
50 sub formbuilder_setup (@) { #{{{
52 my $form=$params{form};
54 if ($form->field("do") eq "edit") {
55 $form->field(name => 'attachment', type => 'file');
56 $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
58 # These buttons are not put in the usual place, so
59 # is not added to the normal formbuilder button list.
60 $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
61 $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
63 elsif ($form->title eq "preferences") {
64 my $session=$params{session};
65 my $user_name=$session->param("name");
67 $form->field(name => "allowed_attachments", size => 50,
69 comment => "(".htmllink("", "", "ikiwiki/PageSpec", noimageinline => 1).")");
70 if (! IkiWiki::is_admin($user_name)) {
71 $form->field(name => "allowed_attachments", type => "hidden");
73 if (! $form->submitted) {
74 $form->field(name => "allowed_attachments", force => 1,
75 value => IkiWiki::userinfo_get($user_name, "allowed_attachments"));
77 if ($form->submitted && $form->submitted eq 'Save Preferences') {
78 if (defined $form->field("allowed_attachments")) {
79 IkiWiki::userinfo_set($user_name, "allowed_attachments",
80 $form->field("allowed_attachments")) ||
81 error("failed to set allowed_attachments");
87 sub formbuilder (@) { #{{{
89 my $form=$params{form};
91 return if $form->field("do") ne "edit";
93 if ($form->submitted eq "Upload" || $form->submitted eq "Save Page") {
95 my $session=$params{session};
97 my $filename=$q->param('attachment');
98 if (! defined $filename || ! length $filename) {
99 # no file, so do nothing
103 # This is an (apparently undocumented) way to get the name
104 # of the temp file that CGI writes the upload to.
105 my $tempfile=$q->tmpFileName($filename);
107 $filename=IkiWiki::titlepage(
108 IkiWiki::possibly_foolish_untaint(
109 attachment_location($form->field('page')).
110 IkiWiki::basename($filename)));
111 if (IkiWiki::file_pruned($filename, $config{srcdir})) {
112 error(gettext("bad attachment filename"));
115 # Check that the user is allowed to edit a page with the
116 # name of the attachment.
117 IkiWiki::check_canedit($filename, $q, $session, 1);
119 # Use a special pagespec to test that the attachment is valid.
121 foreach my $admin (@{$config{adminuser}}) {
122 my $allowed_attachments=IkiWiki::userinfo_get($admin, "allowed_attachments");
123 if (defined $allowed_attachments &&
124 length $allowed_attachments) {
125 $allowed=pagespec_match($filename,
126 $allowed_attachments,
132 error(gettext("attachment rejected")." ($allowed)");
135 # Needed for fast_file_copy and for rendering below.
136 require IkiWiki::Render;
138 # Move the attachment into place.
139 # Try to use a fast rename; fall back to copying.
140 IkiWiki::prep_writefile($filename, $config{srcdir});
141 unlink($config{srcdir}."/".$filename);
142 if (! rename($tempfile, $config{srcdir}."/".$filename)) {
143 my $fh=$q->upload('attachment');
144 if (! defined $fh || ! ref $fh) {
145 error("failed to get filehandle");
148 writefile($filename, $config{srcdir}, undef, 1, sub {
149 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
153 # Check the attachment in and trigger a wiki refresh.
155 IkiWiki::rcs_add($filename);
156 IkiWiki::disable_commit_hook();
157 IkiWiki::rcs_commit($filename, gettext("attachment upload"),
158 IkiWiki::rcs_prepedit($filename),
159 $session->param("name"), $ENV{REMOTE_ADDR});
160 IkiWiki::enable_commit_hook();
161 IkiWiki::rcs_update();
164 IkiWiki::saveindex();
168 package IkiWiki::PageSpec;
170 sub parsesize ($) { #{{{
173 my $base=$size+0; # force to number
176 if ($size=~/kb?$/i) {
179 elsif ($size=~/mb?$/i) {
182 elsif ($size=~/gb?$/i) {
185 elsif ($size=~/tb?$/i) {
188 return $base * $multiple;
191 sub match_maxsize ($$;@) { #{{{
193 my $maxsize=eval{parsesize(shift)};
195 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
199 if (! exists $params{file}) {
200 return IkiWiki::FailReason->new("no file specified");
203 if (-s $params{file} > $maxsize) {
204 return IkiWiki::FailReason->new("file too large");
207 return IkiWiki::SuccessReason->new("file not too large");
211 sub match_minsize ($$;@) { #{{{
213 my $minsize=eval{parsesize(shift)};
215 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
219 if (! exists $params{file}) {
220 return IkiWiki::FailReason->new("no file specified");
223 if (-s $params{file} < $minsize) {
224 return IkiWiki::FailReason->new("file too small");
227 return IkiWiki::SuccessReason->new("file not too small");
231 sub match_ispage ($$;@) { #{{{
234 if (defined IkiWiki::pagetype($filename)) {
235 return IkiWiki::SuccessReason->new("file is a wiki page");
238 return IkiWiki::FailReason->new("file is not a wiki page");