7 use open qw{:utf8 :std};
12 sub printheader ($) { #{{{
15 if ($config{sslcookie}) {
16 print $session->header(-charset => 'utf-8',
17 -cookie => $session->cookie(-secure => 1));
19 print $session->header(-charset => 'utf-8');
24 sub redirect ($$) { #{{{
27 if (! $config{w3mmode}) {
28 print $q->redirect($url);
31 print "Content-type: text/plain\n";
32 print "W3m-control: GOTO $url\n\n";
36 sub page_locked ($$;$) { #{{{
41 my $user=$session->param("name");
42 return if defined $user && is_admin($user);
44 foreach my $admin (@{$config{adminuser}}) {
45 my $locked_pages=userinfo_get($admin, "locked_pages");
46 if (pagespec_match($page, userinfo_get($admin, "locked_pages"))) {
47 return 1 if $nonfatal;
48 error(htmllink("", "", $page, 1)." is locked by ".
49 userlink($admin)." and cannot be edited.");
56 sub decode_form_utf8 ($) { #{{{
58 foreach my $f ($form->field) {
59 next if Encode::is_utf8(scalar $form->field($f));
60 $form->field(name => $f,
61 value => decode_utf8($form->field($f)),
67 sub cgi_recentchanges ($) { #{{{
70 # Optimisation: building recentchanges means calculating lots of
71 # links. Memoizing htmllink speeds it up a lot (can't be memoized
72 # during page builds as the return values may change, but they
78 eval q{use Time::Duration};
81 my $changelog=[rcs_recentchanges(100)];
82 foreach my $change (@$changelog) {
83 $change->{when} = concise(ago($change->{when}));
85 $change->{user} = userlink($change->{user});
87 my $is_excess = exists $change->{pages}[10]; # limit pages to first 10
88 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
91 $_->{link} = htmllink("", "", $_->{page}, 1);
95 push @{$change->{pages}}, { link => '...' } if $is_excess;
98 my $template=template("recentchanges.tmpl");
100 title => "RecentChanges",
101 indexlink => indexlink(),
102 wikiname => $config{wikiname},
103 changelog => $changelog,
104 baseurl => baseurl(),
106 run_hooks(pagetemplate => sub {
107 shift->(page => "", destpage => "", template => $template);
109 print $q->header(-charset => 'utf-8'), $template->output;
112 sub cgi_signin ($$) { #{{{
116 eval q{use CGI::FormBuilder};
118 my $form = CGI::FormBuilder->new(
126 action => $config{cgiurl},
128 template => (-e "$config{templatedir}/signin.tmpl" ?
129 {template_params("signin.tmpl")} : ""),
130 stylesheet => baseurl()."style.css",
132 my $buttons=["Login"];
134 $form->field(name => "do", type => "hidden");
136 if ($q->param("do") ne "signin" && !$form->submitted) {
137 $form->text("You need to log in first.");
140 run_hooks(formbuilder_setup => sub {
141 shift->(form => $form, cgi => $q, session => $session);
144 decode_form_utf8($form);
146 if (exists $hooks{formbuilder}) {
147 run_hooks(formbuilder => sub {
148 shift->(form => $form, cgi => $q, session => $session,
149 buttons => $buttons);
153 if ($form->submitted) {
156 printheader($session);
157 print misctemplate($form->title, $form->render(submit => $buttons));
161 sub cgi_postsignin ($$) { #{{{
165 # Continue with whatever was being done before the signin process.
166 if (defined $q->param("do") && $q->param("do") ne "signin" &&
167 defined $session->param("postsignin")) {
168 my $postsignin=CGI->new($session->param("postsignin"));
169 $session->clear("postsignin");
170 cgi($postsignin, $session);
171 cgi_savesession($session);
175 redirect($q, $config{url});
179 sub cgi_prefs ($$) { #{{{
183 eval q{use CGI::FormBuilder};
185 my $form = CGI::FormBuilder->new(
186 title => "preferences",
196 action => $config{cgiurl},
197 template => (-e "$config{templatedir}/prefs.tmpl" ?
198 {template_params("prefs.tmpl")} : ""),
199 stylesheet => baseurl()."style.css",
201 my $buttons=["Save Preferences", "Logout", "Cancel"];
203 run_hooks(formbuilder_setup => sub {
204 shift->(form => $form, cgi => $q, session => $session);
207 $form->field(name => "do", type => "hidden");
208 $form->field(name => "email", size => 50);
209 $form->field(name => "subscriptions", size => 50,
210 comment => "(".htmllink("", "", "PageSpec", 1).")");
211 $form->field(name => "locked_pages", size => 50,
212 comment => "(".htmllink("", "", "PageSpec", 1).")");
213 $form->field(name => "banned_users", size => 50);
215 my $user_name=$session->param("name");
216 if (! is_admin($user_name)) {
217 $form->field(name => "locked_pages", type => "hidden");
218 $form->field(name => "banned_users", type => "hidden");
221 if (! $form->submitted) {
222 $form->field(name => "email", force => 1,
223 value => userinfo_get($user_name, "email"));
224 $form->field(name => "subscriptions", force => 1,
225 value => userinfo_get($user_name, "subscriptions"));
226 $form->field(name => "locked_pages", force => 1,
227 value => userinfo_get($user_name, "locked_pages"));
228 if (is_admin($user_name)) {
229 $form->field(name => "banned_users", force => 1,
230 value => join(" ", get_banned_users()));
234 decode_form_utf8($form);
236 if ($form->submitted eq 'Logout') {
238 redirect($q, $config{url});
241 elsif ($form->submitted eq 'Cancel') {
242 redirect($q, $config{url});
245 elsif ($form->submitted eq "Save Preferences" && $form->validate) {
246 foreach my $field (qw(email subscriptions locked_pages)) {
247 if (defined $form->field($field) && length $form->field($field)) {
248 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
251 if (is_admin($user_name)) {
252 set_banned_users(grep { ! is_admin($_) }
253 split(' ', $form->field("banned_users")));
255 $form->text("Preferences saved.");
258 if (exists $hooks{formbuilder}) {
259 run_hooks(formbuilder => sub {
260 shift->(form => $form, cgi => $q, session => $session,
261 buttons => $buttons);
265 printheader($session);
266 print misctemplate($form->title, $form->render(submit => $buttons));
270 sub cgi_editpage ($$) { #{{{
274 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
275 my @buttons=("Save Page", "Preview", "Cancel");
277 eval q{use CGI::FormBuilder};
279 my $form = CGI::FormBuilder->new(
285 editcontent => '/.+/',
287 required => [qw{editcontent}],
290 action => $config{cgiurl},
292 template => (-e "$config{templatedir}/editpage.tmpl" ?
293 {template_params("editpage.tmpl")} : ""),
296 run_hooks(formbuilder_setup => sub {
297 shift->(form => $form, cgi => $q, session => $session);
300 decode_form_utf8($form);
302 # This untaint is safe because titlepage removes any problematic
304 my ($page)=$form->field('page');
305 $page=titlepage(possibly_foolish_untaint($page));
306 if (! defined $page || ! length $page ||
307 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
308 error("bad page name");
312 if (defined $form->field('from')) {
313 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
318 if (exists $pagesources{$page}) {
319 $file=$pagesources{$page};
320 $type=pagetype($file);
323 $type=$form->param('type');
324 if (defined $type && length $type && $hooks{htmlize}{$type}) {
325 $type=possibly_foolish_untaint($type);
327 elsif (defined $from) {
328 # favor the type of linking page
329 $type=pagetype($pagesources{$from});
331 $type=$config{default_pageext} unless defined $type;
332 $file=$page.".".$type;
336 if (! -e "$config{srcdir}/$file") {
340 $form->field(name => "do", type => 'hidden');
341 $form->field(name => "from", type => 'hidden');
342 $form->field(name => "rcsinfo", type => 'hidden');
343 $form->field(name => "subpage", type => 'hidden');
344 $form->field(name => "page", value => $page, force => 1);
345 $form->field(name => "type", value => $type, force => 1);
346 $form->field(name => "comments", type => "text", size => 80);
347 $form->field(name => "editcontent", type => "textarea", rows => 20,
349 $form->tmpl_param("can_commit", $config{rcs});
350 $form->tmpl_param("indexlink", indexlink());
351 $form->tmpl_param("helponformattinglink",
352 htmllink("", "", "HelpOnFormatting", 1));
353 $form->tmpl_param("baseurl", baseurl());
354 if (! $form->submitted) {
355 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
359 if ($form->submitted eq "Cancel") {
360 if ($newfile && defined $from) {
361 redirect($q, "$config{url}/".htmlpage($from));
364 redirect($q, $config{url});
367 redirect($q, "$config{url}/".htmlpage($page));
371 elsif ($form->submitted eq "Preview") {
372 my $content=$form->field('editcontent');
373 my $comments=$form->field('comments');
374 $form->field(name => "editcontent",
375 value => $content, force => 1);
376 $form->field(name => "comments",
377 value => $comments, force => 1);
378 $config{rss}=$config{atom}=0; # avoid preview writing a feed!
379 $form->tmpl_param("page_preview",
380 htmlize($page, $type,
382 preprocess($page, $page,
383 filter($page, $content)))));
386 $form->tmpl_param("page_preview", "");
388 $form->tmpl_param("page_conflict", "");
390 if (! $form->submitted || $form->submitted eq "Preview" ||
392 if ($form->field("do") eq "create") {
395 if (! defined $from || ! length $from ||
396 $from ne $form->field('from') ||
397 $from=~/$config{wiki_file_prune_regexp}/ ||
399 $form->submitted eq "Preview") {
400 @page_locs=$best_loc=$page;
406 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
407 $page eq 'discussion') {
408 $best_loc="$from/$page";
411 $best_loc=$dir.$page;
414 push @page_locs, $dir.$page;
415 push @page_locs, "$from/$page";
416 while (length $dir) {
418 push @page_locs, $dir.$page;
423 ! exists $pagecase{lc $_} &&
424 ! page_locked($_, $session, 1)
428 # hmm, someone else made the page in the
430 redirect($q, "$config{url}/".htmlpage($page));
435 if (exists $hooks{htmlize}) {
436 @page_types=keys %{$hooks{htmlize}};
439 $form->tmpl_param("page_select", 1);
440 $form->field(name => "page", type => 'select',
441 options => \@page_locs, value => $best_loc);
442 $form->field(name => "type", type => 'select',
443 options => \@page_types);
444 $form->title("creating ".pagetitle($page));
446 elsif ($form->field("do") eq "edit") {
447 page_locked($page, $session);
448 if (! defined $form->field('editcontent') ||
449 ! length $form->field('editcontent')) {
451 if (exists $pagesources{$page}) {
452 $content=readfile(srcfile($pagesources{$page}));
453 $content=~s/\n/\r\n/g;
455 $form->field(name => "editcontent", value => $content,
458 $form->tmpl_param("page_select", 0);
459 $form->field(name => "page", type => 'hidden');
460 $form->field(name => "type", type => 'hidden');
461 $form->title("editing ".pagetitle($page));
464 print $form->render(submit => \@buttons);
468 page_locked($page, $session);
470 my $content=$form->field('editcontent');
472 $content=~s/\r\n/\n/g;
474 writefile($file, $config{srcdir}, $content);
478 if (defined $form->field('comments') &&
479 length $form->field('comments')) {
480 $message=$form->field('comments');
486 # prevent deadlock with post-commit hook
488 # presumably the commit will trigger an update
490 my $conflict=rcs_commit($file, $message,
491 $form->field("rcsinfo"),
492 $session->param("name"), $ENV{REMOTE_ADDR});
494 if (defined $conflict) {
495 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
497 $form->tmpl_param("page_conflict", 1);
498 $form->field("editcontent", value => $conflict, force => 1);
499 $form->field(name => "comments", value => $form->field('comments'), force => 1);
500 $form->field("do", "edit)");
501 $form->tmpl_param("page_select", 0);
502 $form->field(name => "page", type => 'hidden');
503 $form->field(name => "type", type => 'hidden');
504 $form->title("editing $page");
505 print $form->render(submit => \@buttons);
510 require IkiWiki::Render;
515 # The trailing question mark tries to avoid broken
516 # caches and get the most recent version of the page.
517 redirect($q, "$config{url}/".htmlpage($page)."?updated");
521 sub cgi_getsession ($) { #{{{
524 eval q{use CGI::Session};
525 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
527 my $oldmask=umask(077);
528 my $session = CGI::Session->new("driver:DB_File", $q,
529 { FileName => "$config{wikistatedir}/sessions.db" });
535 sub cgi_savesession ($) { #{{{
538 # Force session flush with safe umask.
539 my $oldmask=umask(077);
554 run_hooks(cgi => sub { shift->($q) });
557 my $do=$q->param('do');
558 if (! defined $do || ! length $do) {
559 my $error = $q->cgi_error;
561 error("Request not processed: $error");
564 error("\"do\" parameter missing");
568 # Things that do not need a session.
569 if ($do eq 'recentchanges') {
570 cgi_recentchanges($q);
573 elsif ($do eq 'hyperestraier') {
577 # Need to lock the wiki before getting a session.
581 $session=cgi_getsession($q);
584 # Auth hooks can sign a user in.
585 if ($do ne 'signin' && ! defined $session->param("name")) {
586 run_hooks(auth => sub {
587 shift->($q, $session)
589 if (defined $session->param("name")) {
590 # Make sure whatever user was authed is in the
592 if (! userinfo_get($session->param("name"), "regdate")) {
593 userinfo_setall($session->param("name"), {
602 # Everything below this point needs the user to be signed in.
603 if (((! $config{anonok} || $do eq 'prefs') &&
604 (! defined $session->param("name") ||
605 ! userinfo_get($session->param("name"), "regdate")))
606 || $do eq 'signin') {
607 if ($do ne 'signin' && ! defined $session->param("postsignin")) {
608 $session->param(postsignin => $ENV{QUERY_STRING});
610 cgi_signin($q, $session);
611 cgi_savesession($session);
614 elsif (defined $session->param("postsignin")) {
615 cgi_postsignin($q, $session);
618 if (defined $session->param("name") && userinfo_get($session->param("name"), "banned")) {
619 print $q->header(-status => "403 Forbidden");
621 print "You are banned.";
622 cgi_savesession($session);
626 if ($do eq 'create' || $do eq 'edit') {
627 cgi_editpage($q, $session);
629 elsif ($do eq 'prefs') {
630 cgi_prefs($q, $session);
632 elsif ($do eq 'blog') {
633 my $page=titlepage(decode_utf8($q->param('title')));
634 # if the page already exists, munge it to be unique
635 my $from=$q->param('from');
637 while (exists $pagecase{lc "$from/$page$add"}) {
638 $add=1 unless length $add;
641 $q->param('page', $page.$add);
642 # now run same as create
643 $q->param('do', 'create');
644 cgi_editpage($q, $session);
647 error("unknown do parameter");
651 sub userlink ($) { #{{{
654 eval q{use CGI 'escapeHTML'};
656 if ($user =~ m!^https?://! &&
657 eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
658 # Munge user-urls, as used by eg, OpenID.
659 my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
660 my $display=$oid->display;
661 # Convert "user.somehost.com" to "user [somehost.com]".
662 if ($display !~ /\[/) {
663 $display=~s/^(.*?)\.([^.]+\.[a-z]+)$/$1 [$2]/;
665 $display=~s!^https?://!!; # make sure this is removed
666 return "<a href=\"$user\">".escapeHTML($display)."</a>";
669 return htmllink("", "", escapeHTML(
670 length $config{userdir} ? $config{userdir}."/".$user : $user