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);
67 elsif (ref $ret eq 'CODE') {
68 $ret->() unless $nonfatal;
71 elsif (defined $ret) {
72 error($ret) unless $nonfatal;
80 sub decode_cgi_utf8 ($) { #{{{
82 foreach my $f ($cgi->param) {
83 $cgi->param($f, map { decode_utf8 $_ } $cgi->param($f));
87 sub cgi_recentchanges ($) { #{{{
90 # Optimisation: building recentchanges means calculating lots of
91 # links. Memoizing htmllink speeds it up a lot (can't be memoized
92 # during page builds as the return values may change, but they
98 eval q{use Time::Duration};
101 my $changelog=[rcs_recentchanges(100)];
102 foreach my $change (@$changelog) {
103 $change->{when} = concise(ago(time - $change->{when}));
105 $change->{user} = userlink($change->{user});
107 my $is_excess = exists $change->{pages}[10]; # limit pages to first 10
108 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
111 $_->{link} = htmllink("", "", $_->{page},
113 linktext => pagetitle($_->{page}));
115 } @{$change->{pages}}
117 push @{$change->{pages}}, { link => '...' } if $is_excess;
120 my $template=template("recentchanges.tmpl");
122 title => "RecentChanges",
123 indexlink => indexlink(),
124 wikiname => $config{wikiname},
125 changelog => $changelog,
126 baseurl => baseurl(),
128 run_hooks(pagetemplate => sub {
129 shift->(page => "", destpage => "", template => $template);
131 print $q->header(-charset => 'utf-8'), $template->output;
134 # Check if the user is signed in. If not, redirect to the signin form and
135 # save their place to return to later.
136 sub needsignin ($$) { #{{{
140 if (! defined $session->param("name") ||
141 ! userinfo_get($session->param("name"), "regdate")) {
142 $session->param(postsignin => $ENV{QUERY_STRING});
143 cgi_signin($q, $session);
144 cgi_savesession($session);
149 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 run_hooks(formbuilder_setup => sub {
178 shift->(form => $form, cgi => $q, session => $session,
179 buttons => $buttons);
182 if ($form->submitted) {
186 showform($form, $buttons, $session, $q);
189 sub cgi_postsignin ($$) { #{{{
193 # Continue with whatever was being done before the signin process.
194 if (defined $session->param("postsignin")) {
195 my $postsignin=CGI->new($session->param("postsignin"));
196 $session->clear("postsignin");
197 cgi($postsignin, $session);
198 cgi_savesession($session);
202 error(gettext("login failed, perhaps you need to turn on cookies?"));
206 sub cgi_prefs ($$) { #{{{
210 needsignin($q, $session);
213 eval q{use CGI::FormBuilder};
215 my $form = CGI::FormBuilder->new(
216 title => "preferences",
217 name => "preferences",
227 action => $config{cgiurl},
228 template => {type => 'div'},
229 stylesheet => baseurl()."style.css",
231 [login => gettext("Login")],
232 [preferences => gettext("Preferences")],
233 [admin => gettext("Admin")]
236 my $buttons=["Save Preferences", "Logout", "Cancel"];
238 run_hooks(formbuilder_setup => sub {
239 shift->(form => $form, cgi => $q, session => $session,
240 buttons => $buttons);
243 $form->field(name => "do", type => "hidden");
244 $form->field(name => "email", size => 50, fieldset => "preferences");
245 $form->field(name => "banned_users", size => 50,
246 fieldset => "admin");
248 my $user_name=$session->param("name");
249 if (! is_admin($user_name)) {
250 $form->field(name => "banned_users", type => "hidden");
253 if (! $form->submitted) {
254 $form->field(name => "email", force => 1,
255 value => userinfo_get($user_name, "email"));
256 if (is_admin($user_name)) {
257 $form->field(name => "banned_users", force => 1,
258 value => join(" ", get_banned_users()));
262 if ($form->submitted eq 'Logout') {
264 redirect($q, $config{url});
267 elsif ($form->submitted eq 'Cancel') {
268 redirect($q, $config{url});
271 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
272 if (defined $form->field('email')) {
273 userinfo_set($user_name, 'email', $form->field('email')) ||
274 error("failed to set email");
276 if (is_admin($user_name)) {
277 set_banned_users(grep { ! is_admin($_) }
279 $form->field("banned_users"))) ||
280 error("failed saving changes");
282 $form->text(gettext("Preferences saved."));
285 showform($form, $buttons, $session, $q);
288 sub cgi_editpage ($$) { #{{{
292 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
293 my @buttons=("Save Page", "Preview", "Cancel");
296 eval q{use CGI::FormBuilder};
298 my $form = CGI::FormBuilder->new(
303 required => [qw{editcontent}],
306 action => $config{cgiurl},
309 template => scalar template_params("editpage.tmpl"),
310 wikiname => $config{wikiname},
313 run_hooks(formbuilder_setup => sub {
314 shift->(form => $form, cgi => $q, session => $session,
315 buttons => \@buttons);
318 # This untaint is safe because titlepage removes any problematic
320 my ($page)=$form->field('page');
321 $page=titlepage(possibly_foolish_untaint($page));
322 if (! defined $page || ! length $page ||
323 file_pruned($page, $config{srcdir}) || $page=~/^\//) {
324 error("bad page name");
328 if (defined $form->field('from')) {
329 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
334 if (exists $pagesources{$page} && $form->field("do") ne "create") {
335 $file=$pagesources{$page};
336 $type=pagetype($file);
337 if (! defined $type || $type=~/^_/) {
338 error(sprintf(gettext("%s is not an editable page"), $page));
340 if (! $form->submitted) {
341 $form->field(name => "rcsinfo",
342 value => rcs_prepedit($file), force => 1);
344 $form->field(name => "editcontent", validate => '/.*/');
347 $type=$form->param('type');
348 if (defined $type && length $type && $hooks{htmlize}{$type}) {
349 $type=possibly_foolish_untaint($type);
351 elsif (defined $from && exists $pagesources{$from}) {
352 # favor the type of linking page
353 $type=pagetype($pagesources{$from});
355 $type=$config{default_pageext} unless defined $type;
356 $file=$page.".".$type;
357 if (! $form->submitted) {
358 $form->field(name => "rcsinfo", value => "", force => 1);
360 $form->field(name => "editcontent", validate => '/.+/');
363 $form->field(name => "do", type => 'hidden');
364 $form->field(name => "from", type => 'hidden');
365 $form->field(name => "rcsinfo", type => 'hidden');
366 $form->field(name => "subpage", type => 'hidden');
367 $form->field(name => "page", value => pagetitle($page, 1), force => 1);
368 $form->field(name => "type", value => $type, force => 1);
369 $form->field(name => "comments", type => "text", size => 80);
370 $form->field(name => "editcontent", type => "textarea", rows => 20,
372 $form->tmpl_param("can_commit", $config{rcs});
373 $form->tmpl_param("indexlink", indexlink());
374 $form->tmpl_param("helponformattinglink",
375 htmllink("", "", "ikiwiki/formatting",
377 linktext => "FormattingHelp"));
378 $form->tmpl_param("baseurl", baseurl());
380 if ($form->submitted eq "Cancel") {
381 if ($form->field("do") eq "create" && defined $from) {
382 redirect($q, "$config{url}/".htmlpage($from));
384 elsif ($form->field("do") eq "create") {
385 redirect($q, $config{url});
388 redirect($q, "$config{url}/".htmlpage($page));
392 elsif ($form->submitted eq "Preview") {
393 my $content=$form->field('editcontent');
394 run_hooks(editcontent => sub {
402 $form->tmpl_param("page_preview",
403 htmlize($page, $type,
405 preprocess($page, $page,
406 filter($page, $page, $content), 0, 1))));
408 elsif ($form->submitted eq "Save Page") {
409 $form->tmpl_param("page_preview", "");
411 $form->tmpl_param("page_conflict", "");
413 if ($form->submitted ne "Save Page" || ! $form->validate) {
414 if ($form->field("do") eq "create") {
417 if (! defined $from || ! length $from ||
418 $from ne $form->field('from') ||
419 file_pruned($from, $config{srcdir}) ||
421 $form->submitted eq "Preview") {
422 @page_locs=$best_loc=$page;
428 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
429 $page eq gettext('discussion')) {
430 $best_loc="$from/$page";
433 $best_loc=$dir.$page;
436 push @page_locs, $dir.$page;
437 push @page_locs, "$from/$page";
438 while (length $dir) {
440 push @page_locs, $dir.$page;
443 push @page_locs, "$config{userdir}/$page"
444 if length $config{userdir};
447 ! exists $pagecase{lc $_}
450 # hmm, someone else made the page in the
452 redirect($q, "$config{url}/".htmlpage($page));
456 my @editable_locs = grep {
457 check_canedit($_, $q, $session, 1)
459 if (! @editable_locs) {
460 # let it throw an error this time
461 map { check_canedit($_, $q, $session) } @page_locs;
465 if (exists $hooks{htmlize}) {
466 @page_types=grep { !/^_/ }
467 keys %{$hooks{htmlize}};
470 $form->tmpl_param("page_select", 1);
471 $form->field(name => "page", type => 'select',
472 options => [ map { pagetitle($_, 1) } @editable_locs ],
473 value => pagetitle($best_loc, 1));
474 $form->field(name => "type", type => 'select',
475 options => \@page_types);
476 $form->title(sprintf(gettext("creating %s"), pagetitle($page)));
479 elsif ($form->field("do") eq "edit") {
480 check_canedit($page, $q, $session);
481 if (! defined $form->field('editcontent') ||
482 ! length $form->field('editcontent')) {
484 if (exists $pagesources{$page}) {
485 $content=readfile(srcfile($pagesources{$page}));
486 $content=~s/\n/\r\n/g;
488 $form->field(name => "editcontent", value => $content,
491 $form->tmpl_param("page_select", 0);
492 $form->field(name => "page", type => 'hidden');
493 $form->field(name => "type", type => 'hidden');
494 $form->title(sprintf(gettext("editing %s"), pagetitle($page)));
497 showform($form, \@buttons, $session, $q);
502 check_canedit($page, $q, $session);
504 my $exists=-e "$config{srcdir}/$file";
506 if ($form->field("do") ne "create" && ! $exists &&
507 ! eval { srcfile($file) }) {
508 $form->tmpl_param("page_gone", 1);
509 $form->field(name => "do", value => "create", force => 1);
510 $form->tmpl_param("page_select", 0);
511 $form->field(name => "page", type => 'hidden');
512 $form->field(name => "type", type => 'hidden');
513 $form->title(sprintf(gettext("editing %s"), $page));
514 showform($form, \@buttons, $session, $q);
517 elsif ($form->field("do") eq "create" && $exists) {
518 $form->tmpl_param("creation_conflict", 1);
519 $form->field(name => "do", value => "edit", force => 1);
520 $form->tmpl_param("page_select", 0);
521 $form->field(name => "page", type => 'hidden');
522 $form->field(name => "type", type => 'hidden');
523 $form->title(sprintf(gettext("editing %s"), $page));
524 $form->field("editcontent",
525 value => readfile("$config{srcdir}/$file").
526 "\n\n\n".$form->field("editcontent"),
528 showform($form, \@buttons, $session, $q);
532 my $content=$form->field('editcontent');
533 run_hooks(editcontent => sub {
541 $content=~s/\r\n/\n/g;
543 $content.="\n" if $content !~ /\n$/;
545 $config{cgi}=0; # avoid cgi error message
546 eval { writefile($file, $config{srcdir}, $content) };
549 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
551 $form->tmpl_param("failed_save", 1);
552 $form->tmpl_param("error_message", $@);
553 $form->field("editcontent", value => $content, force => 1);
554 $form->tmpl_param("page_select", 0);
555 $form->field(name => "page", type => 'hidden');
556 $form->field(name => "type", type => 'hidden');
557 $form->title(sprintf(gettext("editing %s"), $page));
558 showform($form, \@buttons, $session, $q);
565 if (defined $form->field('comments') &&
566 length $form->field('comments')) {
567 $message=$form->field('comments');
574 # Prevent deadlock with post-commit hook by
575 # signaling to it that it should not try to
576 # do anything (except send commit mails).
577 disable_commit_hook();
578 $conflict=rcs_commit($file, $message,
579 $form->field("rcsinfo"),
580 $session->param("name"), $ENV{REMOTE_ADDR});
581 enable_commit_hook();
585 # Refresh even if there was a conflict, since other changes
586 # may have been committed while the post-commit hook was
588 require IkiWiki::Render;
589 # Reload index, since the first time it's loaded is before
590 # the wiki is locked, and things may have changed in the
596 if (defined $conflict) {
597 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
599 $form->tmpl_param("page_conflict", 1);
600 $form->field("editcontent", value => $conflict, force => 1);
601 $form->field("do", "edit", force => 1);
602 $form->tmpl_param("page_select", 0);
603 $form->field(name => "page", type => 'hidden');
604 $form->field(name => "type", type => 'hidden');
605 $form->title(sprintf(gettext("editing %s"), $page));
606 showform($form, \@buttons, $session, $q);
610 # The trailing question mark tries to avoid broken
611 # caches and get the most recent version of the page.
612 redirect($q, "$config{url}/".htmlpage($page)."?updated");
617 sub cgi_getsession ($) { #{{{
620 eval q{use CGI::Session};
621 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
623 my $oldmask=umask(077);
624 my $session = CGI::Session->new("driver:DB_File", $q,
625 { FileName => "$config{wikistatedir}/sessions.db" });
631 sub cgi_savesession ($) { #{{{
634 # Force session flush with safe umask.
635 my $oldmask=umask(077);
650 run_hooks(cgi => sub { shift->($q) });
653 my $do=$q->param('do');
654 if (! defined $do || ! length $do) {
655 my $error = $q->cgi_error;
657 error("Request not processed: $error");
660 error("\"do\" parameter missing");
664 # Things that do not need a session.
665 if ($do eq 'recentchanges') {
666 cgi_recentchanges($q);
670 # Need to lock the wiki before getting a session.
674 $session=cgi_getsession($q);
677 # Auth hooks can sign a user in.
678 if ($do ne 'signin' && ! defined $session->param("name")) {
679 run_hooks(auth => sub {
680 shift->($q, $session)
682 if (defined $session->param("name")) {
683 # Make sure whatever user was authed is in the
685 if (! userinfo_get($session->param("name"), "regdate")) {
686 userinfo_setall($session->param("name"), {
690 }) || error("failed adding user");
695 if (defined $session->param("name") &&
696 userinfo_get($session->param("name"), "banned")) {
697 print $q->header(-status => "403 Forbidden");
699 print gettext("You are banned.");
700 cgi_savesession($session);
703 run_hooks(sessioncgi => sub { shift->($q, $session) });
705 if ($do eq 'signin') {
706 cgi_signin($q, $session);
707 cgi_savesession($session);
709 elsif ($do eq 'prefs') {
710 cgi_prefs($q, $session);
712 elsif ($do eq 'create' || $do eq 'edit') {
713 cgi_editpage($q, $session);
715 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
716 cgi_postsignin($q, $session);
719 error("unknown do parameter");