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 showform ($$$$) { #{{{
30 if (exists $hooks{formbuilder}) {
31 run_hooks(formbuilder => sub {
32 shift->(form => $form, cgi => $cgi, session => $session,
37 printheader($session);
38 print misctemplate($form->title, $form->render(submit => $buttons));
41 sub redirect ($$) { #{{{
44 if (! $config{w3mmode}) {
45 print $q->redirect($url);
48 print "Content-type: text/plain\n";
49 print "W3m-control: GOTO $url\n\n";
53 sub check_canedit ($$$;$) { #{{{
60 run_hooks(canedit => sub {
61 return if defined $canedit;
62 my $ret=shift->($page, $q, $session);
63 if (defined $ret && $ret eq "") {
66 elsif (defined $ret) {
68 error($ret) unless $nonfatal;
74 sub decode_form_utf8 ($) { #{{{
76 foreach my $f ($form->field) {
77 next if Encode::is_utf8(scalar $form->field($f));
78 $form->field(name => $f,
79 value => decode_utf8($form->field($f)),
85 sub cgi_recentchanges ($) { #{{{
88 # Optimisation: building recentchanges means calculating lots of
89 # links. Memoizing htmllink speeds it up a lot (can't be memoized
90 # during page builds as the return values may change, but they
96 eval q{use Time::Duration};
99 my $changelog=[rcs_recentchanges(100)];
100 foreach my $change (@$changelog) {
101 $change->{when} = concise(ago($change->{when}));
103 $change->{user} = userlink($change->{user});
105 my $is_excess = exists $change->{pages}[10]; # limit pages to first 10
106 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
109 $_->{link} = htmllink("", "", $_->{page},
111 linktext => pagetitle($_->{page}));
113 } @{$change->{pages}}
115 push @{$change->{pages}}, { link => '...' } if $is_excess;
118 my $template=template("recentchanges.tmpl");
120 title => "RecentChanges",
121 indexlink => indexlink(),
122 wikiname => $config{wikiname},
123 changelog => $changelog,
124 baseurl => baseurl(),
126 run_hooks(pagetemplate => sub {
127 shift->(page => "", destpage => "", template => $template);
129 print $q->header(-charset => 'utf-8'), $template->output;
132 # Check if the user is signed in. If not, redirect to the signin form and
133 # save their place to return to later.
134 sub needsignin ($$) { #{{{
138 if (! defined $session->param("name") ||
139 ! userinfo_get($session->param("name"), "regdate")) {
140 if (! defined $session->param("postsignin")) {
141 $session->param(postsignin => $ENV{QUERY_STRING});
143 cgi_signin($q, $session);
144 cgi_savesession($session);
149 sub cgi_signin ($$) { #{{{
153 eval q{use CGI::FormBuilder};
155 my $form = CGI::FormBuilder->new(
163 action => $config{cgiurl},
165 template => {type => 'div'},
166 stylesheet => baseurl()."style.css",
168 my $buttons=["Login"];
170 if ($q->param("do") ne "signin" && !$form->submitted) {
171 $form->text(gettext("You need to log in first."));
173 $form->field(name => "do", type => "hidden", value => "signin",
176 decode_form_utf8($form);
178 run_hooks(formbuilder_setup => sub {
179 shift->(form => $form, cgi => $q, session => $session,
180 buttons => $buttons);
183 if ($form->submitted) {
187 showform($form, $buttons, $session, $q);
190 sub cgi_postsignin ($$) { #{{{
194 # Continue with whatever was being done before the signin process.
195 if (defined $session->param("postsignin")) {
196 my $postsignin=CGI->new($session->param("postsignin"));
197 $session->clear("postsignin");
198 cgi($postsignin, $session);
199 cgi_savesession($session);
203 # This can occur, for example, if a user went to the signin
204 # url via a bookmark.
205 redirect($q, $config{url});
209 sub cgi_prefs ($$) { #{{{
213 needsignin($q, $session);
215 eval q{use CGI::FormBuilder};
217 my $form = CGI::FormBuilder->new(
218 title => "preferences",
219 name => "preferences",
229 action => $config{cgiurl},
230 template => {type => 'div'},
231 stylesheet => baseurl()."style.css",
233 [login => gettext("Login")],
234 [preferences => gettext("Preferences")],
235 [admin => gettext("Admin")]
238 my $buttons=["Save Preferences", "Logout", "Cancel"];
240 decode_form_utf8($form);
242 run_hooks(formbuilder_setup => sub {
243 shift->(form => $form, cgi => $q, session => $session,
244 buttons => $buttons);
247 $form->field(name => "do", type => "hidden");
248 $form->field(name => "email", size => 50, fieldset => "preferences");
249 $form->field(name => "subscriptions", size => 50,
250 fieldset => "preferences",
251 comment => "(".htmllink("", "", "ikiwiki/PageSpec", noimageinline => 1).")");
252 $form->field(name => "banned_users", size => 50,
253 fieldset => "admin");
255 my $user_name=$session->param("name");
256 if (! is_admin($user_name)) {
257 $form->field(name => "banned_users", type => "hidden");
260 if (! $form->submitted) {
261 $form->field(name => "email", force => 1,
262 value => userinfo_get($user_name, "email"));
263 $form->field(name => "subscriptions", force => 1,
264 value => userinfo_get($user_name, "subscriptions"));
265 if (is_admin($user_name)) {
266 $form->field(name => "banned_users", force => 1,
267 value => join(" ", get_banned_users()));
271 if ($form->submitted eq 'Logout') {
273 redirect($q, $config{url});
276 elsif ($form->submitted eq 'Cancel') {
277 redirect($q, $config{url});
280 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
281 foreach my $field (qw(email subscriptions)) {
282 if (defined $form->field($field) && length $form->field($field)) {
283 userinfo_set($user_name, $field, $form->field($field)) ||
284 error("failed to set $field");
287 if (is_admin($user_name)) {
288 set_banned_users(grep { ! is_admin($_) }
290 $form->field("banned_users"))) ||
291 error("failed saving changes");
293 $form->text(gettext("Preferences saved."));
296 showform($form, $buttons, $session, $q);
299 sub cgi_editpage ($$) { #{{{
303 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
304 my @buttons=("Save Page", "Preview", "Cancel");
306 eval q{use CGI::FormBuilder};
308 my $form = CGI::FormBuilder->new(
313 required => [qw{editcontent}],
316 action => $config{cgiurl},
319 template => scalar template_params("editpage.tmpl"),
320 wikiname => $config{wikiname},
323 decode_form_utf8($form);
325 run_hooks(formbuilder_setup => sub {
326 shift->(form => $form, cgi => $q, session => $session,
327 buttons => \@buttons);
330 # This untaint is safe because titlepage removes any problematic
332 my ($page)=$form->field('page');
333 $page=titlepage(possibly_foolish_untaint($page));
334 if (! defined $page || ! length $page ||
335 file_pruned($page, $config{srcdir}) || $page=~/^\//) {
336 error("bad page name");
340 if (defined $form->field('from')) {
341 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
346 if (exists $pagesources{$page} && $form->field("do") ne "create") {
347 $file=$pagesources{$page};
348 $type=pagetype($file);
349 if (! defined $type) {
350 error(sprintf(gettext("%s is not an editable page"), $page));
352 if (! $form->submitted) {
353 $form->field(name => "rcsinfo",
354 value => rcs_prepedit($file), force => 1);
356 $form->field(name => "editcontent", validate => '/.*/');
359 $type=$form->param('type');
360 if (defined $type && length $type && $hooks{htmlize}{$type}) {
361 $type=possibly_foolish_untaint($type);
363 elsif (defined $from) {
364 # favor the type of linking page
365 $type=pagetype($pagesources{$from});
367 $type=$config{default_pageext} unless defined $type;
368 $file=$page.".".$type;
369 if (! $form->submitted) {
370 $form->field(name => "rcsinfo", value => "", force => 1);
372 $form->field(name => "editcontent", validate => '/.+/');
375 $form->field(name => "do", type => 'hidden');
376 $form->field(name => "from", type => 'hidden');
377 $form->field(name => "rcsinfo", type => 'hidden');
378 $form->field(name => "subpage", type => 'hidden');
379 $form->field(name => "page", value => pagetitle($page, 1), force => 1);
380 $form->field(name => "type", value => $type, force => 1);
381 $form->field(name => "comments", type => "text", size => 80);
382 $form->field(name => "editcontent", type => "textarea", rows => 20,
384 $form->tmpl_param("can_commit", $config{rcs});
385 $form->tmpl_param("indexlink", indexlink());
386 $form->tmpl_param("helponformattinglink",
387 htmllink("", "", "ikiwiki/formatting",
389 linktext => "FormattingHelp"));
390 $form->tmpl_param("baseurl", baseurl());
392 if ($form->submitted eq "Cancel") {
393 if ($form->field("do") eq "create" && defined $from) {
394 redirect($q, "$config{url}/".htmlpage($from));
396 elsif ($form->field("do") eq "create") {
397 redirect($q, $config{url});
400 redirect($q, "$config{url}/".htmlpage($page));
404 elsif ($form->submitted eq "Preview") {
405 my $content=$form->field('editcontent');
406 run_hooks(editcontent => sub {
414 $form->tmpl_param("page_preview",
415 htmlize($page, $type,
417 preprocess($page, $page,
418 filter($page, $page, $content), 0, 1))));
420 elsif ($form->submitted eq "Save Page") {
421 $form->tmpl_param("page_preview", "");
423 $form->tmpl_param("page_conflict", "");
425 if ($form->submitted ne "Save Page" || ! $form->validate) {
426 if ($form->field("do") eq "create") {
429 if (! defined $from || ! length $from ||
430 $from ne $form->field('from') ||
431 file_pruned($from, $config{srcdir}) ||
433 $form->submitted eq "Preview") {
434 @page_locs=$best_loc=$page;
440 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
441 $page eq gettext('discussion')) {
442 $best_loc="$from/$page";
445 $best_loc=$dir.$page;
448 push @page_locs, $dir.$page;
449 push @page_locs, "$from/$page";
450 while (length $dir) {
452 push @page_locs, $dir.$page;
455 push @page_locs, "$config{userdir}/$page"
456 if length $config{userdir};
459 ! exists $pagecase{lc $_}
462 # hmm, someone else made the page in the
464 redirect($q, "$config{url}/".htmlpage($page));
468 my @editable_locs = grep {
469 check_canedit($_, $q, $session, 1)
471 if (! @editable_locs) {
472 # let it throw an error this time
473 map { check_canedit($_, $q, $session) } @page_locs;
477 if (exists $hooks{htmlize}) {
478 @page_types=keys %{$hooks{htmlize}};
481 $form->tmpl_param("page_select", 1);
482 $form->field(name => "page", type => 'select',
483 options => [ map { pagetitle($_, 1) } @editable_locs ],
484 value => pagetitle($best_loc, 1));
485 $form->field(name => "type", type => 'select',
486 options => \@page_types);
487 $form->title(sprintf(gettext("creating %s"), pagetitle($page)));
490 elsif ($form->field("do") eq "edit") {
491 check_canedit($page, $q, $session);
492 if (! defined $form->field('editcontent') ||
493 ! length $form->field('editcontent')) {
495 if (exists $pagesources{$page}) {
496 $content=readfile(srcfile($pagesources{$page}));
497 $content=~s/\n/\r\n/g;
499 $form->field(name => "editcontent", value => $content,
502 $form->tmpl_param("page_select", 0);
503 $form->field(name => "page", type => 'hidden');
504 $form->field(name => "type", type => 'hidden');
505 $form->title(sprintf(gettext("editing %s"), pagetitle($page)));
508 showform($form, \@buttons, $session, $q);
513 check_canedit($page, $q, $session);
515 my $exists=-e "$config{srcdir}/$file";
517 if ($form->field("do") ne "create" && ! $exists &&
518 ! eval { srcfile($file) }) {
519 $form->tmpl_param("page_gone", 1);
520 $form->field(name => "do", value => "create", force => 1);
521 $form->tmpl_param("page_select", 0);
522 $form->field(name => "page", type => 'hidden');
523 $form->field(name => "type", type => 'hidden');
524 $form->title(sprintf(gettext("editing %s"), $page));
525 showform($form, \@buttons, $session, $q);
528 elsif ($form->field("do") eq "create" && $exists) {
529 $form->tmpl_param("creation_conflict", 1);
530 $form->field(name => "do", value => "edit", force => 1);
531 $form->tmpl_param("page_select", 0);
532 $form->field(name => "page", type => 'hidden');
533 $form->field(name => "type", type => 'hidden');
534 $form->title(sprintf(gettext("editing %s"), $page));
535 $form->field("editcontent",
536 value => readfile("$config{srcdir}/$file").
537 "\n\n\n".$form->field("editcontent"),
539 showform($form, \@buttons, $session, $q);
543 my $content=$form->field('editcontent');
544 run_hooks(editcontent => sub {
552 $content=~s/\r\n/\n/g;
554 $content.="\n" if $content !~ /\n$/;
556 $config{cgi}=0; # avoid cgi error message
557 eval { writefile($file, $config{srcdir}, $content) };
560 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
562 $form->tmpl_param("failed_save", 1);
563 $form->tmpl_param("error_message", $@);
564 $form->field("editcontent", value => $content, force => 1);
565 $form->tmpl_param("page_select", 0);
566 $form->field(name => "page", type => 'hidden');
567 $form->field(name => "type", type => 'hidden');
568 $form->title(sprintf(gettext("editing %s"), $page));
569 showform($form, \@buttons, $session, $q);
576 if (defined $form->field('comments') &&
577 length $form->field('comments')) {
578 $message=$form->field('comments');
585 # Prevent deadlock with post-commit hook by
586 # signaling to it that it should not try to
587 # do anything (except send commit mails).
588 disable_commit_hook();
589 $conflict=rcs_commit($file, $message,
590 $form->field("rcsinfo"),
591 $session->param("name"), $ENV{REMOTE_ADDR});
592 enable_commit_hook();
596 # Refresh even if there was a conflict, since other changes
597 # may have been committed while the post-commit hook was
599 require IkiWiki::Render;
600 # Reload index, since the first time it's loaded is before
601 # the wiki is locked, and things may have changed in the
607 if (defined $conflict) {
608 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
610 $form->tmpl_param("page_conflict", 1);
611 $form->field("editcontent", value => $conflict, force => 1);
612 $form->field("do", "edit", force => 1);
613 $form->tmpl_param("page_select", 0);
614 $form->field(name => "page", type => 'hidden');
615 $form->field(name => "type", type => 'hidden');
616 $form->title(sprintf(gettext("editing %s"), $page));
617 showform($form, \@buttons, $session, $q);
621 # The trailing question mark tries to avoid broken
622 # caches and get the most recent version of the page.
623 redirect($q, "$config{url}/".htmlpage($page)."?updated");
628 sub cgi_getsession ($) { #{{{
631 eval q{use CGI::Session};
632 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
634 my $oldmask=umask(077);
635 my $session = CGI::Session->new("driver:DB_File", $q,
636 { FileName => "$config{wikistatedir}/sessions.db" });
642 sub cgi_savesession ($) { #{{{
645 # Force session flush with safe umask.
646 my $oldmask=umask(077);
661 run_hooks(cgi => sub { shift->($q) });
664 my $do=$q->param('do');
665 if (! defined $do || ! length $do) {
666 my $error = $q->cgi_error;
668 error("Request not processed: $error");
671 error("\"do\" parameter missing");
675 # Things that do not need a session.
676 if ($do eq 'recentchanges') {
677 cgi_recentchanges($q);
681 # Need to lock the wiki before getting a session.
685 $session=cgi_getsession($q);
688 # Auth hooks can sign a user in.
689 if ($do ne 'signin' && ! defined $session->param("name")) {
690 run_hooks(auth => sub {
691 shift->($q, $session)
693 if (defined $session->param("name")) {
694 # Make sure whatever user was authed is in the
696 if (! userinfo_get($session->param("name"), "regdate")) {
697 userinfo_setall($session->param("name"), {
701 }) || error("failed adding user");
706 if (defined $session->param("name") &&
707 userinfo_get($session->param("name"), "banned")) {
708 print $q->header(-status => "403 Forbidden");
710 print gettext("You are banned.");
711 cgi_savesession($session);
714 run_hooks(sessioncgi => sub { shift->($q, $session) });
716 if ($do eq 'signin') {
717 cgi_signin($q, $session);
718 cgi_savesession($session);
720 elsif (defined $session->param("postsignin")) {
721 cgi_postsignin($q, $session);
723 elsif ($do eq 'prefs') {
724 cgi_prefs($q, $session);
726 elsif ($do eq 'create' || $do eq 'edit') {
727 cgi_editpage($q, $session);
729 elsif ($do eq 'postsignin') {
730 error(gettext("login failed, perhaps you need to turn on cookies?"));
733 error("unknown do parameter");
737 sub userlink ($) { #{{{
740 eval q{use CGI 'escapeHTML'};
742 if ($user =~ m!^https?://! &&
743 eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
744 # Munge user-urls, as used by eg, OpenID.
745 my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
746 my $display=$oid->display;
747 # Convert "user.somehost.com" to "user [somehost.com]".
748 if ($display !~ /\[/) {
749 $display=~s/^(.*?)\.([^.]+\.[a-z]+)$/$1 [$2]/;
751 # Convert "http://somehost.com/user" to "user [somehost.com]".
752 if ($display !~ /\[/) {
753 $display=~s/^https?:\/\/(.+)\/([^\/]+)$/$2 [$1]/;
755 $display=~s!^https?://!!; # make sure this is removed
756 return "<a href=\"$user\">".escapeHTML($display)."</a>";
759 return htmllink("", "", escapeHTML(
760 length $config{userdir} ? $config{userdir}."/".$user : $user
761 ), noimageinline => 1);