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_cgi_utf8 ($) { #{{{
76 foreach my $f ($cgi->param) {
77 $cgi->param($f, map { decode_utf8 $_ } $cgi->param($f));
81 sub cgi_recentchanges ($) { #{{{
84 # Optimisation: building recentchanges means calculating lots of
85 # links. Memoizing htmllink speeds it up a lot (can't be memoized
86 # during page builds as the return values may change, but they
92 eval q{use Time::Duration};
95 my $changelog=[rcs_recentchanges(100)];
96 foreach my $change (@$changelog) {
97 $change->{when} = concise(ago($change->{when}));
99 $change->{user} = userlink($change->{user});
101 my $is_excess = exists $change->{pages}[10]; # limit pages to first 10
102 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
105 $_->{link} = htmllink("", "", $_->{page},
107 linktext => pagetitle($_->{page}));
109 } @{$change->{pages}}
111 push @{$change->{pages}}, { link => '...' } if $is_excess;
114 my $template=template("recentchanges.tmpl");
116 title => "RecentChanges",
117 indexlink => indexlink(),
118 wikiname => $config{wikiname},
119 changelog => $changelog,
120 baseurl => baseurl(),
122 run_hooks(pagetemplate => sub {
123 shift->(page => "", destpage => "", template => $template);
125 print $q->header(-charset => 'utf-8'), $template->output;
128 # Check if the user is signed in. If not, redirect to the signin form and
129 # save their place to return to later.
130 sub needsignin ($$) { #{{{
134 if (! defined $session->param("name") ||
135 ! userinfo_get($session->param("name"), "regdate")) {
136 if (! defined $session->param("postsignin")) {
137 $session->param(postsignin => $ENV{QUERY_STRING});
139 cgi_signin($q, $session);
140 cgi_savesession($session);
145 sub cgi_signin ($$) { #{{{
150 eval q{use CGI::FormBuilder};
152 my $form = CGI::FormBuilder->new(
160 action => $config{cgiurl},
162 template => {type => 'div'},
163 stylesheet => baseurl()."style.css",
165 my $buttons=["Login"];
167 if ($q->param("do") ne "signin" && !$form->submitted) {
168 $form->text(gettext("You need to log in first."));
170 $form->field(name => "do", type => "hidden", value => "signin",
173 run_hooks(formbuilder_setup => sub {
174 shift->(form => $form, cgi => $q, session => $session,
175 buttons => $buttons);
178 if ($form->submitted) {
182 showform($form, $buttons, $session, $q);
185 sub cgi_postsignin ($$) { #{{{
189 # Continue with whatever was being done before the signin process.
190 if (defined $session->param("postsignin")) {
191 my $postsignin=CGI->new($session->param("postsignin"));
192 $session->clear("postsignin");
193 cgi($postsignin, $session);
194 cgi_savesession($session);
198 # This can occur, for example, if a user went to the signin
199 # url via a bookmark.
200 redirect($q, $config{url});
204 sub cgi_prefs ($$) { #{{{
208 needsignin($q, $session);
211 eval q{use CGI::FormBuilder};
213 my $form = CGI::FormBuilder->new(
214 title => "preferences",
215 name => "preferences",
225 action => $config{cgiurl},
226 template => {type => 'div'},
227 stylesheet => baseurl()."style.css",
229 [login => gettext("Login")],
230 [preferences => gettext("Preferences")],
231 [admin => gettext("Admin")]
234 my $buttons=["Save Preferences", "Logout", "Cancel"];
236 run_hooks(formbuilder_setup => sub {
237 shift->(form => $form, cgi => $q, session => $session,
238 buttons => $buttons);
241 $form->field(name => "do", type => "hidden");
242 $form->field(name => "email", size => 50, fieldset => "preferences");
243 $form->field(name => "subscriptions", size => 50,
244 fieldset => "preferences",
245 comment => "(".htmllink("", "", "ikiwiki/PageSpec", noimageinline => 1).")");
246 $form->field(name => "banned_users", size => 50,
247 fieldset => "admin");
249 my $user_name=$session->param("name");
250 if (! is_admin($user_name)) {
251 $form->field(name => "banned_users", type => "hidden");
254 if (! $form->submitted) {
255 $form->field(name => "email", force => 1,
256 value => userinfo_get($user_name, "email"));
257 $form->field(name => "subscriptions", force => 1,
258 value => userinfo_get($user_name, "subscriptions"));
259 if (is_admin($user_name)) {
260 $form->field(name => "banned_users", force => 1,
261 value => join(" ", get_banned_users()));
265 if ($form->submitted eq 'Logout') {
267 redirect($q, $config{url});
270 elsif ($form->submitted eq 'Cancel') {
271 redirect($q, $config{url});
274 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
275 foreach my $field (qw(email subscriptions)) {
276 if (defined $form->field($field) && length $form->field($field)) {
277 userinfo_set($user_name, $field, $form->field($field)) ||
278 error("failed to set $field");
281 if (is_admin($user_name)) {
282 set_banned_users(grep { ! is_admin($_) }
284 $form->field("banned_users"))) ||
285 error("failed saving changes");
287 $form->text(gettext("Preferences saved."));
290 showform($form, $buttons, $session, $q);
293 sub cgi_editpage ($$) { #{{{
297 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
298 my @buttons=("Save Page", "Preview", "Cancel");
301 eval q{use CGI::FormBuilder};
303 my $form = CGI::FormBuilder->new(
308 required => [qw{editcontent}],
311 action => $config{cgiurl},
314 template => scalar template_params("editpage.tmpl"),
315 wikiname => $config{wikiname},
318 run_hooks(formbuilder_setup => sub {
319 shift->(form => $form, cgi => $q, session => $session,
320 buttons => \@buttons);
323 # This untaint is safe because titlepage removes any problematic
325 my ($page)=$form->field('page');
326 $page=titlepage(possibly_foolish_untaint($page));
327 if (! defined $page || ! length $page ||
328 file_pruned($page, $config{srcdir}) || $page=~/^\//) {
329 error("bad page name");
333 if (defined $form->field('from')) {
334 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
339 if (exists $pagesources{$page} && $form->field("do") ne "create") {
340 $file=$pagesources{$page};
341 $type=pagetype($file);
342 if (! defined $type) {
343 error(sprintf(gettext("%s is not an editable page"), $page));
345 if (! $form->submitted) {
346 $form->field(name => "rcsinfo",
347 value => rcs_prepedit($file), force => 1);
349 $form->field(name => "editcontent", validate => '/.*/');
352 $type=$form->param('type');
353 if (defined $type && length $type && $hooks{htmlize}{$type}) {
354 $type=possibly_foolish_untaint($type);
356 elsif (defined $from) {
357 # favor the type of linking page
358 $type=pagetype($pagesources{$from});
360 $type=$config{default_pageext} unless defined $type;
361 $file=$page.".".$type;
362 if (! $form->submitted) {
363 $form->field(name => "rcsinfo", value => "", force => 1);
365 $form->field(name => "editcontent", validate => '/.+/');
368 $form->field(name => "do", type => 'hidden');
369 $form->field(name => "from", type => 'hidden');
370 $form->field(name => "rcsinfo", type => 'hidden');
371 $form->field(name => "subpage", type => 'hidden');
372 $form->field(name => "page", value => pagetitle($page, 1), force => 1);
373 $form->field(name => "type", value => $type, force => 1);
374 $form->field(name => "comments", type => "text", size => 80);
375 $form->field(name => "editcontent", type => "textarea", rows => 20,
377 $form->tmpl_param("can_commit", $config{rcs});
378 $form->tmpl_param("indexlink", indexlink());
379 $form->tmpl_param("helponformattinglink",
380 htmllink("", "", "ikiwiki/formatting",
382 linktext => "FormattingHelp"));
383 $form->tmpl_param("baseurl", baseurl());
385 if ($form->submitted eq "Cancel") {
386 if ($form->field("do") eq "create" && defined $from) {
387 redirect($q, "$config{url}/".htmlpage($from));
389 elsif ($form->field("do") eq "create") {
390 redirect($q, $config{url});
393 redirect($q, "$config{url}/".htmlpage($page));
397 elsif ($form->submitted eq "Preview") {
398 my $content=$form->field('editcontent');
399 run_hooks(editcontent => sub {
407 $form->tmpl_param("page_preview",
408 htmlize($page, $type,
410 preprocess($page, $page,
411 filter($page, $page, $content), 0, 1))));
413 elsif ($form->submitted eq "Save Page") {
414 $form->tmpl_param("page_preview", "");
416 $form->tmpl_param("page_conflict", "");
418 if ($form->submitted ne "Save Page" || ! $form->validate) {
419 if ($form->field("do") eq "create") {
422 if (! defined $from || ! length $from ||
423 $from ne $form->field('from') ||
424 file_pruned($from, $config{srcdir}) ||
426 $form->submitted eq "Preview") {
427 @page_locs=$best_loc=$page;
433 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
434 $page eq gettext('discussion')) {
435 $best_loc="$from/$page";
438 $best_loc=$dir.$page;
441 push @page_locs, $dir.$page;
442 push @page_locs, "$from/$page";
443 while (length $dir) {
445 push @page_locs, $dir.$page;
448 push @page_locs, "$config{userdir}/$page"
449 if length $config{userdir};
452 ! exists $pagecase{lc $_}
455 # hmm, someone else made the page in the
457 redirect($q, "$config{url}/".htmlpage($page));
461 my @editable_locs = grep {
462 check_canedit($_, $q, $session, 1)
464 if (! @editable_locs) {
465 # let it throw an error this time
466 map { check_canedit($_, $q, $session) } @page_locs;
470 if (exists $hooks{htmlize}) {
471 @page_types=keys %{$hooks{htmlize}};
474 $form->tmpl_param("page_select", 1);
475 $form->field(name => "page", type => 'select',
476 options => [ map { pagetitle($_, 1) } @editable_locs ],
477 value => pagetitle($best_loc, 1));
478 $form->field(name => "type", type => 'select',
479 options => \@page_types);
480 $form->title(sprintf(gettext("creating %s"), pagetitle($page)));
483 elsif ($form->field("do") eq "edit") {
484 check_canedit($page, $q, $session);
485 if (! defined $form->field('editcontent') ||
486 ! length $form->field('editcontent')) {
488 if (exists $pagesources{$page}) {
489 $content=readfile(srcfile($pagesources{$page}));
490 $content=~s/\n/\r\n/g;
492 $form->field(name => "editcontent", value => $content,
495 $form->tmpl_param("page_select", 0);
496 $form->field(name => "page", type => 'hidden');
497 $form->field(name => "type", type => 'hidden');
498 $form->title(sprintf(gettext("editing %s"), pagetitle($page)));
501 showform($form, \@buttons, $session, $q);
506 check_canedit($page, $q, $session);
508 my $exists=-e "$config{srcdir}/$file";
510 if ($form->field("do") ne "create" && ! $exists &&
511 ! eval { srcfile($file) }) {
512 $form->tmpl_param("page_gone", 1);
513 $form->field(name => "do", value => "create", force => 1);
514 $form->tmpl_param("page_select", 0);
515 $form->field(name => "page", type => 'hidden');
516 $form->field(name => "type", type => 'hidden');
517 $form->title(sprintf(gettext("editing %s"), $page));
518 showform($form, \@buttons, $session, $q);
521 elsif ($form->field("do") eq "create" && $exists) {
522 $form->tmpl_param("creation_conflict", 1);
523 $form->field(name => "do", value => "edit", force => 1);
524 $form->tmpl_param("page_select", 0);
525 $form->field(name => "page", type => 'hidden');
526 $form->field(name => "type", type => 'hidden');
527 $form->title(sprintf(gettext("editing %s"), $page));
528 $form->field("editcontent",
529 value => readfile("$config{srcdir}/$file").
530 "\n\n\n".$form->field("editcontent"),
532 showform($form, \@buttons, $session, $q);
536 my $content=$form->field('editcontent');
537 run_hooks(editcontent => sub {
545 $content=~s/\r\n/\n/g;
547 $content.="\n" if $content !~ /\n$/;
549 $config{cgi}=0; # avoid cgi error message
550 eval { writefile($file, $config{srcdir}, $content) };
553 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
555 $form->tmpl_param("failed_save", 1);
556 $form->tmpl_param("error_message", $@);
557 $form->field("editcontent", value => $content, force => 1);
558 $form->tmpl_param("page_select", 0);
559 $form->field(name => "page", type => 'hidden');
560 $form->field(name => "type", type => 'hidden');
561 $form->title(sprintf(gettext("editing %s"), $page));
562 showform($form, \@buttons, $session, $q);
569 if (defined $form->field('comments') &&
570 length $form->field('comments')) {
571 $message=$form->field('comments');
578 # Prevent deadlock with post-commit hook by
579 # signaling to it that it should not try to
580 # do anything (except send commit mails).
581 disable_commit_hook();
582 $conflict=rcs_commit($file, $message,
583 $form->field("rcsinfo"),
584 $session->param("name"), $ENV{REMOTE_ADDR});
585 enable_commit_hook();
589 # Refresh even if there was a conflict, since other changes
590 # may have been committed while the post-commit hook was
592 require IkiWiki::Render;
593 # Reload index, since the first time it's loaded is before
594 # the wiki is locked, and things may have changed in the
600 if (defined $conflict) {
601 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
603 $form->tmpl_param("page_conflict", 1);
604 $form->field("editcontent", value => $conflict, force => 1);
605 $form->field("do", "edit", force => 1);
606 $form->tmpl_param("page_select", 0);
607 $form->field(name => "page", type => 'hidden');
608 $form->field(name => "type", type => 'hidden');
609 $form->title(sprintf(gettext("editing %s"), $page));
610 showform($form, \@buttons, $session, $q);
614 # The trailing question mark tries to avoid broken
615 # caches and get the most recent version of the page.
616 redirect($q, "$config{url}/".htmlpage($page)."?updated");
621 sub cgi_getsession ($) { #{{{
624 eval q{use CGI::Session};
625 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
627 my $oldmask=umask(077);
628 my $session = CGI::Session->new("driver:DB_File", $q,
629 { FileName => "$config{wikistatedir}/sessions.db" });
635 sub cgi_savesession ($) { #{{{
638 # Force session flush with safe umask.
639 my $oldmask=umask(077);
654 run_hooks(cgi => sub { shift->($q) });
657 my $do=$q->param('do');
658 if (! defined $do || ! length $do) {
659 my $error = $q->cgi_error;
661 error("Request not processed: $error");
664 error("\"do\" parameter missing");
668 # Things that do not need a session.
669 if ($do eq 'recentchanges') {
670 cgi_recentchanges($q);
674 # Need to lock the wiki before getting a session.
678 $session=cgi_getsession($q);
681 # Auth hooks can sign a user in.
682 if ($do ne 'signin' && ! defined $session->param("name")) {
683 run_hooks(auth => sub {
684 shift->($q, $session)
686 if (defined $session->param("name")) {
687 # Make sure whatever user was authed is in the
689 if (! userinfo_get($session->param("name"), "regdate")) {
690 userinfo_setall($session->param("name"), {
694 }) || error("failed adding user");
699 if (defined $session->param("name") &&
700 userinfo_get($session->param("name"), "banned")) {
701 print $q->header(-status => "403 Forbidden");
703 print gettext("You are banned.");
704 cgi_savesession($session);
707 run_hooks(sessioncgi => sub { shift->($q, $session) });
709 if ($do eq 'signin') {
710 cgi_signin($q, $session);
711 cgi_savesession($session);
713 elsif (defined $session->param("postsignin")) {
714 cgi_postsignin($q, $session);
716 elsif ($do eq 'prefs') {
717 cgi_prefs($q, $session);
719 elsif ($do eq 'create' || $do eq 'edit') {
720 cgi_editpage($q, $session);
722 elsif ($do eq 'postsignin') {
723 error(gettext("login failed, perhaps you need to turn on cookies?"));
726 error("unknown do parameter");
730 sub userlink ($) { #{{{
733 eval q{use CGI 'escapeHTML'};
735 if ($user =~ m!^https?://! &&
736 eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
737 # Munge user-urls, as used by eg, OpenID.
738 my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
739 my $display=$oid->display;
740 # Convert "user.somehost.com" to "user [somehost.com]".
741 if ($display !~ /\[/) {
742 $display=~s/^(.*?)\.([^.]+\.[a-z]+)$/$1 [$2]/;
744 # Convert "http://somehost.com/user" to "user [somehost.com]".
745 if ($display !~ /\[/) {
746 $display=~s/^https?:\/\/(.+)\/([^\/]+)$/$2 [$1]/;
748 $display=~s!^https?://!!; # make sure this is removed
749 return "<a href=\"$user\">".escapeHTML($display)."</a>";
752 return htmllink("", "", escapeHTML(
753 length $config{userdir} ? $config{userdir}."/".$user : $user
754 ), noimageinline => 1);