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));
42 sub redirect ($$) { #{{{
45 if (! $config{w3mmode}) {
46 print $q->redirect($url);
49 print "Content-type: text/plain\n";
50 print "W3m-control: GOTO $url\n\n";
54 sub check_canedit ($$$;$) { #{{{
61 run_hooks(canedit => sub {
62 return if defined $canedit;
63 my $ret=shift->($page, $q, $session);
64 if (defined $ret && $ret eq "") {
67 elsif (defined $ret) {
69 error($ret) unless $nonfatal;
75 sub decode_form_utf8 ($) { #{{{
77 foreach my $f ($form->field) {
78 next if Encode::is_utf8(scalar $form->field($f));
79 $form->field(name => $f,
80 value => decode_utf8($form->field($f)),
86 sub cgi_recentchanges ($) { #{{{
89 # Optimisation: building recentchanges means calculating lots of
90 # links. Memoizing htmllink speeds it up a lot (can't be memoized
91 # during page builds as the return values may change, but they
97 eval q{use Time::Duration};
100 my $changelog=[rcs_recentchanges(100)];
101 foreach my $change (@$changelog) {
102 $change->{when} = concise(ago($change->{when}));
104 $change->{user} = userlink($change->{user});
106 my $is_excess = exists $change->{pages}[10]; # limit pages to first 10
107 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
110 $_->{link} = htmllink("", "", $_->{page},
112 linktext => pagetitle($_->{page}));
114 } @{$change->{pages}}
116 push @{$change->{pages}}, { link => '...' } if $is_excess;
119 my $template=template("recentchanges.tmpl");
121 title => "RecentChanges",
122 indexlink => indexlink(),
123 wikiname => $config{wikiname},
124 changelog => $changelog,
125 baseurl => baseurl(),
127 run_hooks(pagetemplate => sub {
128 shift->(page => "", destpage => "", template => $template);
130 print $q->header(-charset => 'utf-8'), $template->output;
133 # Check if the user is signed in. If not, redirect to the signin form and
134 # save their place to return to later.
135 sub needsignin ($$) { #{{{
139 if (! defined $session->param("name") ||
140 ! userinfo_get($session->param("name"), "regdate")) {
141 if (! defined $session->param("postsignin")) {
142 $session->param(postsignin => $ENV{QUERY_STRING});
144 cgi_signin($q, $session);
145 cgi_savesession($session);
150 sub cgi_signin ($$) { #{{{
154 eval q{use CGI::FormBuilder};
156 my $form = CGI::FormBuilder->new(
164 action => $config{cgiurl},
166 template => {type => 'div'},
167 stylesheet => baseurl()."style.css",
169 my $buttons=["Login"];
171 if ($q->param("do") ne "signin" && !$form->submitted) {
172 $form->text(gettext("You need to log in first."));
174 $form->field(name => "do", type => "hidden", value => "signin",
177 decode_form_utf8($form);
179 run_hooks(formbuilder_setup => sub {
180 shift->(form => $form, cgi => $q, session => $session,
181 buttons => $buttons);
184 if ($form->submitted) {
188 showform($form, $buttons, $session, $q);
191 sub cgi_postsignin ($$) { #{{{
195 # Continue with whatever was being done before the signin process.
196 if (defined $session->param("postsignin")) {
197 my $postsignin=CGI->new($session->param("postsignin"));
198 $session->clear("postsignin");
199 cgi($postsignin, $session);
200 cgi_savesession($session);
204 # This can occur, for example, if a user went to the signin
205 # url via a bookmark.
206 redirect($q, $config{url});
210 sub cgi_prefs ($$) { #{{{
214 needsignin($q, $session);
216 eval q{use CGI::FormBuilder};
218 my $form = CGI::FormBuilder->new(
219 title => "preferences",
220 name => "preferences",
230 action => $config{cgiurl},
231 template => {type => 'div'},
232 stylesheet => baseurl()."style.css",
234 [login => gettext("Login")],
235 [preferences => gettext("Preferences")],
236 [admin => gettext("Admin")]
239 my $buttons=["Save Preferences", "Logout", "Cancel"];
241 decode_form_utf8($form);
243 run_hooks(formbuilder_setup => sub {
244 shift->(form => $form, cgi => $q, session => $session,
245 buttons => $buttons);
248 $form->field(name => "do", type => "hidden");
249 $form->field(name => "email", size => 50, fieldset => "preferences");
250 $form->field(name => "subscriptions", size => 50,
251 fieldset => "preferences",
252 comment => "(".htmllink("", "", "PageSpec", noimageinline => 1).")");
253 $form->field(name => "banned_users", size => 50,
254 fieldset => "admin");
256 my $user_name=$session->param("name");
257 if (! is_admin($user_name)) {
258 $form->field(name => "banned_users", type => "hidden");
261 if (! $form->submitted) {
262 $form->field(name => "email", force => 1,
263 value => userinfo_get($user_name, "email"));
264 $form->field(name => "subscriptions", force => 1,
265 value => userinfo_get($user_name, "subscriptions"));
266 if (is_admin($user_name)) {
267 $form->field(name => "banned_users", force => 1,
268 value => join(" ", get_banned_users()));
272 if ($form->submitted eq 'Logout') {
274 redirect($q, $config{url});
277 elsif ($form->submitted eq 'Cancel') {
278 redirect($q, $config{url});
281 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
282 foreach my $field (qw(email subscriptions)) {
283 if (defined $form->field($field) && length $form->field($field)) {
284 userinfo_set($user_name, $field, $form->field($field)) ||
285 error("failed to set $field");
288 if (is_admin($user_name)) {
289 set_banned_users(grep { ! is_admin($_) }
291 $form->field("banned_users"))) ||
292 error("failed saving changes");
294 $form->text(gettext("Preferences saved."));
297 showform($form, $buttons, $session, $q);
300 sub cgi_editpage ($$) { #{{{
304 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
305 my @buttons=("Save Page", "Preview", "Cancel");
307 eval q{use CGI::FormBuilder};
309 my $form = CGI::FormBuilder->new(
315 editcontent => '/.+/',
317 required => [qw{editcontent}],
320 action => $config{cgiurl},
323 template => scalar template_params("editpage.tmpl"),
324 wikiname => $config{wikiname},
327 decode_form_utf8($form);
329 run_hooks(formbuilder_setup => sub {
330 shift->(form => $form, cgi => $q, session => $session,
331 buttons => \@buttons);
334 # This untaint is safe because titlepage removes any problematic
336 my ($page)=$form->field('page');
337 $page=titlepage(possibly_foolish_untaint($page));
338 if (! defined $page || ! length $page ||
339 file_pruned($page, $config{srcdir}) || $page=~/^\//) {
340 error("bad page name");
344 if (defined $form->field('from')) {
345 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
350 if (exists $pagesources{$page} && $form->field("do") ne "create") {
351 $file=$pagesources{$page};
352 $type=pagetype($file);
353 if (! defined $type) {
354 error(sprintf(gettext("%s is not an editable page"), $page));
356 if (! $form->submitted) {
357 $form->field(name => "rcsinfo",
358 value => rcs_prepedit($file), force => 1);
362 $type=$form->param('type');
363 if (defined $type && length $type && $hooks{htmlize}{$type}) {
364 $type=possibly_foolish_untaint($type);
366 elsif (defined $from) {
367 # favor the type of linking page
368 $type=pagetype($pagesources{$from});
370 $type=$config{default_pageext} unless defined $type;
371 $file=$page.".".$type;
372 if (! $form->submitted) {
373 $form->field(name => "rcsinfo", value => "", force => 1);
377 $form->field(name => "do", type => 'hidden');
378 $form->field(name => "from", type => 'hidden');
379 $form->field(name => "rcsinfo", type => 'hidden');
380 $form->field(name => "subpage", type => 'hidden');
381 $form->field(name => "page", value => pagetitle($page, 1), force => 1);
382 $form->field(name => "type", value => $type, force => 1);
383 $form->field(name => "comments", type => "text", size => 80);
384 $form->field(name => "editcontent", type => "textarea", rows => 20,
386 $form->tmpl_param("can_commit", $config{rcs});
387 $form->tmpl_param("indexlink", indexlink());
388 $form->tmpl_param("helponformattinglink",
389 htmllink("", "", "HelpOnFormatting", noimageinline => 1));
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;
555 $config{cgi}=0; # avoid cgi error message
556 eval { writefile($file, $config{srcdir}, $content) };
559 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
561 $form->tmpl_param("failed_save", 1);
562 $form->tmpl_param("error_message", $@);
563 $form->field("editcontent", value => $content, force => 1);
564 $form->tmpl_param("page_select", 0);
565 $form->field(name => "page", type => 'hidden');
566 $form->field(name => "type", type => 'hidden');
567 $form->title(sprintf(gettext("editing %s"), $page));
568 showform($form, \@buttons, $session, $q);
575 if (defined $form->field('comments') &&
576 length $form->field('comments')) {
577 $message=$form->field('comments');
584 # Prevent deadlock with post-commit hook by
585 # signaling to it that it should not try to
586 # do anything (except send commit mails).
587 disable_commit_hook();
588 $conflict=rcs_commit($file, $message,
589 $form->field("rcsinfo"),
590 $session->param("name"), $ENV{REMOTE_ADDR});
591 enable_commit_hook();
595 # Refresh even if there was a conflict, since other changes
596 # may have been committed while the post-commit hook was
598 require IkiWiki::Render;
599 # Reload index, since the first time it's loaded is before
600 # the wiki is locked, and things may have changed in the
606 if (defined $conflict) {
607 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
609 $form->tmpl_param("page_conflict", 1);
610 $form->field("editcontent", value => $conflict, force => 1);
611 $form->field("do", "edit", force => 1);
612 $form->tmpl_param("page_select", 0);
613 $form->field(name => "page", type => 'hidden');
614 $form->field(name => "type", type => 'hidden');
615 $form->title(sprintf(gettext("editing %s"), $page));
616 showform($form, \@buttons, $session, $q);
620 # The trailing question mark tries to avoid broken
621 # caches and get the most recent version of the page.
622 redirect($q, "$config{url}/".htmlpage($page)."?updated");
627 sub cgi_getsession ($) { #{{{
630 eval q{use CGI::Session};
631 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
633 my $oldmask=umask(077);
634 my $session = CGI::Session->new("driver:DB_File", $q,
635 { FileName => "$config{wikistatedir}/sessions.db" });
641 sub cgi_savesession ($) { #{{{
644 # Force session flush with safe umask.
645 my $oldmask=umask(077);
660 run_hooks(cgi => sub { shift->($q) });
663 my $do=$q->param('do');
664 if (! defined $do || ! length $do) {
665 my $error = $q->cgi_error;
667 error("Request not processed: $error");
670 error("\"do\" parameter missing");
674 # Things that do not need a session.
675 if ($do eq 'recentchanges') {
676 cgi_recentchanges($q);
680 # Need to lock the wiki before getting a session.
684 $session=cgi_getsession($q);
687 # Auth hooks can sign a user in.
688 if ($do ne 'signin' && ! defined $session->param("name")) {
689 run_hooks(auth => sub {
690 shift->($q, $session)
692 if (defined $session->param("name")) {
693 # Make sure whatever user was authed is in the
695 if (! userinfo_get($session->param("name"), "regdate")) {
696 userinfo_setall($session->param("name"), {
700 }) || error("failed adding user");
705 if (defined $session->param("name") &&
706 userinfo_get($session->param("name"), "banned")) {
707 print $q->header(-status => "403 Forbidden");
709 print gettext("You are banned.");
710 cgi_savesession($session);
713 run_hooks(sessioncgi => sub { shift->($q, $session) });
715 if ($do eq 'signin') {
716 cgi_signin($q, $session);
717 cgi_savesession($session);
719 elsif (defined $session->param("postsignin")) {
720 cgi_postsignin($q, $session);
722 elsif ($do eq 'prefs') {
723 cgi_prefs($q, $session);
725 elsif ($do eq 'create' || $do eq 'edit') {
726 cgi_editpage($q, $session);
728 elsif ($do eq 'postsignin') {
729 error(gettext("login failed, perhaps you need to turn on cookies?"));
732 error("unknown do parameter");
736 sub userlink ($) { #{{{
739 eval q{use CGI 'escapeHTML'};
741 if ($user =~ m!^https?://! &&
742 eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
743 # Munge user-urls, as used by eg, OpenID.
744 my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
745 my $display=$oid->display;
746 # Convert "user.somehost.com" to "user [somehost.com]".
747 if ($display !~ /\[/) {
748 $display=~s/^(.*?)\.([^.]+\.[a-z]+)$/$1 [$2]/;
750 # Convert "http://somehost.com/user" to "user [somehost.com]".
751 if ($display !~ /\[/) {
752 $display=~s/^https?:\/\/(.+)\/([^\/]+)$/$2 [$1]/;
754 $display=~s!^https?://!!; # make sure this is removed
755 return "<a href=\"$user\">".escapeHTML($display)."</a>";
758 return htmllink("", "", escapeHTML(
759 length $config{userdir} ? $config{userdir}."/".$user : $user
760 ), noimageinline => 1);