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 # Check if the user is signed in. If not, redirect to the signin form and
88 # save their place to return to later.
89 sub needsignin ($$) { #{{{
93 if (! defined $session->param("name") ||
94 ! userinfo_get($session->param("name"), "regdate")) {
95 $session->param(postsignin => $ENV{QUERY_STRING});
96 cgi_signin($q, $session);
97 cgi_savesession($session);
102 sub cgi_signin ($$) { #{{{
107 eval q{use CGI::FormBuilder};
109 my $form = CGI::FormBuilder->new(
117 action => $config{cgiurl},
119 template => {type => 'div'},
120 stylesheet => baseurl()."style.css",
122 my $buttons=["Login"];
124 if ($q->param("do") ne "signin" && !$form->submitted) {
125 $form->text(gettext("You need to log in first."));
127 $form->field(name => "do", type => "hidden", value => "signin",
130 run_hooks(formbuilder_setup => sub {
131 shift->(form => $form, cgi => $q, session => $session,
132 buttons => $buttons);
135 if ($form->submitted) {
139 showform($form, $buttons, $session, $q);
142 sub cgi_postsignin ($$) { #{{{
146 # Continue with whatever was being done before the signin process.
147 if (defined $session->param("postsignin")) {
148 my $postsignin=CGI->new($session->param("postsignin"));
149 $session->clear("postsignin");
150 cgi($postsignin, $session);
151 cgi_savesession($session);
155 error(gettext("login failed, perhaps you need to turn on cookies?"));
159 sub cgi_prefs ($$) { #{{{
163 needsignin($q, $session);
166 # The session id is stored on the form and checked to
167 # guard against CSRF.
168 my $sid=$q->param('sid');
169 if (! defined $sid) {
172 elsif ($sid ne $session->id) {
173 error(gettext("Your login session has expired."));
176 eval q{use CGI::FormBuilder};
178 my $form = CGI::FormBuilder->new(
179 title => "preferences",
180 name => "preferences",
190 action => $config{cgiurl},
191 template => {type => 'div'},
192 stylesheet => baseurl()."style.css",
194 [login => gettext("Login")],
195 [preferences => gettext("Preferences")],
196 [admin => gettext("Admin")]
199 my $buttons=["Save Preferences", "Logout", "Cancel"];
201 run_hooks(formbuilder_setup => sub {
202 shift->(form => $form, cgi => $q, session => $session,
203 buttons => $buttons);
206 $form->field(name => "do", type => "hidden", value => "prefs",
208 $form->field(name => "sid", type => "hidden", value => $session->id,
210 $form->field(name => "email", size => 50, fieldset => "preferences");
211 $form->field(name => "banned_users", size => 50,
212 fieldset => "admin");
214 my $user_name=$session->param("name");
215 if (! is_admin($user_name)) {
216 $form->field(name => "banned_users", type => "hidden");
219 if (! $form->submitted) {
220 $form->field(name => "email", force => 1,
221 value => userinfo_get($user_name, "email"));
222 if (is_admin($user_name)) {
223 $form->field(name => "banned_users", force => 1,
224 value => join(" ", get_banned_users()));
228 if ($form->submitted eq 'Logout') {
230 redirect($q, $config{url});
233 elsif ($form->submitted eq 'Cancel') {
234 redirect($q, $config{url});
237 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
238 if (defined $form->field('email')) {
239 userinfo_set($user_name, 'email', $form->field('email')) ||
240 error("failed to set email");
242 if (is_admin($user_name)) {
243 set_banned_users(grep { ! is_admin($_) }
245 $form->field("banned_users"))) ||
246 error("failed saving changes");
248 $form->text(gettext("Preferences saved."));
251 showform($form, $buttons, $session, $q);
254 sub cgi_editpage ($$) { #{{{
260 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
261 my @buttons=("Save Page", "Preview", "Cancel");
262 eval q{use CGI::FormBuilder};
264 my $form = CGI::FormBuilder->new(
269 required => [qw{editcontent}],
272 action => $config{cgiurl},
275 template => scalar template_params("editpage.tmpl"),
276 wikiname => $config{wikiname},
279 run_hooks(formbuilder_setup => sub {
280 shift->(form => $form, cgi => $q, session => $session,
281 buttons => \@buttons);
284 # This untaint is safe because titlepage removes any problematic
286 my ($page)=$form->field('page');
287 $page=titlepage(possibly_foolish_untaint($page));
288 if (! defined $page || ! length $page ||
289 file_pruned($page, $config{srcdir}) || $page=~/^\//) {
290 error("bad page name");
293 my $baseurl=$config{url}."/".htmlpage($page);
296 if (defined $form->field('from')) {
297 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
302 if (exists $pagesources{$page} && $form->field("do") ne "create") {
303 $file=$pagesources{$page};
304 $type=pagetype($file);
305 if (! defined $type || $type=~/^_/) {
306 error(sprintf(gettext("%s is not an editable page"), $page));
308 if (! $form->submitted) {
309 $form->field(name => "rcsinfo",
310 value => rcs_prepedit($file), force => 1);
312 $form->field(name => "editcontent", validate => '/.*/');
315 $type=$form->param('type');
316 if (defined $type && length $type && $hooks{htmlize}{$type}) {
317 $type=possibly_foolish_untaint($type);
319 elsif (defined $from && exists $pagesources{$from}) {
320 # favor the type of linking page
321 $type=pagetype($pagesources{$from});
323 $type=$config{default_pageext} unless defined $type;
324 $file=$page.".".$type;
325 if (! $form->submitted) {
326 $form->field(name => "rcsinfo", value => "", force => 1);
328 $form->field(name => "editcontent", validate => '/.+/');
331 $form->field(name => "do", type => 'hidden');
332 $form->field(name => "sid", type => "hidden", value => $session->id,
334 $form->field(name => "from", type => 'hidden');
335 $form->field(name => "rcsinfo", type => 'hidden');
336 $form->field(name => "subpage", type => 'hidden');
337 $form->field(name => "page", value => pagetitle($page, 1), force => 1);
338 $form->field(name => "type", value => $type, force => 1);
339 $form->field(name => "comments", type => "text", size => 80);
340 $form->field(name => "editcontent", type => "textarea", rows => 20,
342 $form->tmpl_param("can_commit", $config{rcs});
343 $form->tmpl_param("indexlink", indexlink());
344 $form->tmpl_param("helponformattinglink",
345 htmllink($page, $page, "ikiwiki/formatting",
347 linktext => "FormattingHelp"));
349 if ($form->submitted eq "Cancel") {
350 if ($form->field("do") eq "create" && defined $from) {
351 redirect($q, "$config{url}/".htmlpage($from));
353 elsif ($form->field("do") eq "create") {
354 redirect($q, $config{url});
357 redirect($q, "$config{url}/".htmlpage($page));
361 elsif ($form->submitted eq "Preview") {
362 my $new=not exists $pagesources{$page};
364 # temporarily record its type
365 $pagesources{$page}=$page.".".$type;
368 my $content=$form->field('editcontent');
369 run_hooks(editcontent => sub {
377 $form->tmpl_param("page_preview",
378 htmlize($page, $type,
379 linkify($page, $page,
380 preprocess($page, $page,
381 filter($page, $page, $content), 0, 1))));
384 delete $pagesources{$page};
386 # previewing may have created files on disk
389 elsif ($form->submitted eq "Save Page") {
390 $form->tmpl_param("page_preview", "");
392 $form->tmpl_param("page_conflict", "");
394 if ($form->submitted ne "Save Page" || ! $form->validate) {
395 if ($form->field("do") eq "create") {
398 if (! defined $from || ! length $from ||
399 $from ne $form->field('from') ||
400 file_pruned($from, $config{srcdir}) ||
402 $form->submitted eq "Preview") {
403 @page_locs=$best_loc=$page;
409 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
410 $page eq gettext('discussion')) {
411 $best_loc="$from/$page";
414 $best_loc=$dir.$page;
417 push @page_locs, $dir.$page;
418 push @page_locs, "$from/$page";
419 while (length $dir) {
421 push @page_locs, $dir.$page;
424 push @page_locs, "$config{userdir}/$page"
425 if length $config{userdir};
429 ! exists $pagecase{lc $_}
432 # hmm, someone else made the page in the
434 if ($form->submitted eq "Preview") {
435 # let them go ahead with the edit
436 # and resolve the conflict at save
441 redirect($q, "$config{url}/".htmlpage($page));
446 my @editable_locs = grep {
447 check_canedit($_, $q, $session, 1)
449 if (! @editable_locs) {
450 # let it throw an error this time
451 map { check_canedit($_, $q, $session) } @page_locs;
455 if (exists $hooks{htmlize}) {
456 @page_types=grep { !/^_/ }
457 keys %{$hooks{htmlize}};
460 $form->tmpl_param("page_select", 1);
461 $form->field(name => "page", type => 'select',
462 options => [ map { pagetitle($_, 1) } @editable_locs ],
463 value => pagetitle($best_loc, 1));
464 $form->field(name => "type", type => 'select',
465 options => \@page_types);
466 $form->title(sprintf(gettext("creating %s"), pagetitle($page)));
469 elsif ($form->field("do") eq "edit") {
470 check_canedit($page, $q, $session);
471 if (! defined $form->field('editcontent') ||
472 ! length $form->field('editcontent')) {
474 if (exists $pagesources{$page}) {
475 $content=readfile(srcfile($pagesources{$page}));
476 $content=~s/\n/\r\n/g;
478 $form->field(name => "editcontent", value => $content,
481 $form->tmpl_param("page_select", 0);
482 $form->field(name => "page", type => 'hidden');
483 $form->field(name => "type", type => 'hidden');
484 $form->title(sprintf(gettext("editing %s"), pagetitle($page)));
487 showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
491 check_canedit($page, $q, $session);
493 # The session id is stored on the form and checked to
494 # guard against CSRF. But only if the user is logged in,
495 # as anonok can allow anonymous edits.
496 if (defined $session->param("name")) {
497 my $sid=$q->param('sid');
498 if (! defined $sid || $sid ne $session->id) {
499 error(gettext("Your login session has expired."));
503 my $exists=-e "$config{srcdir}/$file";
505 if ($form->field("do") ne "create" && ! $exists &&
506 ! eval { srcfile($file) }) {
507 $form->tmpl_param("page_gone", 1);
508 $form->field(name => "do", value => "create", force => 1);
509 $form->tmpl_param("page_select", 0);
510 $form->field(name => "page", type => 'hidden');
511 $form->field(name => "type", type => 'hidden');
512 $form->title(sprintf(gettext("editing %s"), $page));
513 showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
516 elsif ($form->field("do") eq "create" && $exists) {
517 $form->tmpl_param("creation_conflict", 1);
518 $form->field(name => "do", value => "edit", force => 1);
519 $form->tmpl_param("page_select", 0);
520 $form->field(name => "page", type => 'hidden');
521 $form->field(name => "type", type => 'hidden');
522 $form->title(sprintf(gettext("editing %s"), $page));
523 $form->field("editcontent",
524 value => readfile("$config{srcdir}/$file").
525 "\n\n\n".$form->field("editcontent"),
527 showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
531 my $content=$form->field('editcontent');
532 run_hooks(editcontent => sub {
540 $content=~s/\r\n/\n/g;
542 $content.="\n" if $content !~ /\n$/;
544 $config{cgi}=0; # avoid cgi error message
545 eval { writefile($file, $config{srcdir}, $content) };
548 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
550 $form->tmpl_param("failed_save", 1);
551 $form->tmpl_param("error_message", $@);
552 $form->field("editcontent", value => $content, force => 1);
553 $form->tmpl_param("page_select", 0);
554 $form->field(name => "page", type => 'hidden');
555 $form->field(name => "type", type => 'hidden');
556 $form->title(sprintf(gettext("editing %s"), $page));
557 showform($form, \@buttons, $session, $q,
558 forcebaseurl => $baseurl);
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
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;
592 if (defined $conflict) {
593 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
595 $form->tmpl_param("page_conflict", 1);
596 $form->field("editcontent", value => $conflict, force => 1);
597 $form->field("do", "edit", force => 1);
598 $form->tmpl_param("page_select", 0);
599 $form->field(name => "page", type => 'hidden');
600 $form->field(name => "type", type => 'hidden');
601 $form->title(sprintf(gettext("editing %s"), $page));
602 showform($form, \@buttons, $session, $q,
603 forcebaseurl => $baseurl);
607 # The trailing question mark tries to avoid broken
608 # caches and get the most recent version of the page.
609 redirect($q, "$config{url}/".htmlpage($page)."?updated");
614 sub cgi_getsession ($) { #{{{
617 eval q{use CGI::Session};
618 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
620 my $oldmask=umask(077);
621 my $session = CGI::Session->new("driver:DB_File", $q,
622 { FileName => "$config{wikistatedir}/sessions.db" });
628 sub cgi_savesession ($) { #{{{
631 # Force session flush with safe umask.
632 my $oldmask=umask(077);
647 run_hooks(cgi => sub { shift->($q) });
650 my $do=$q->param('do');
651 if (! defined $do || ! length $do) {
652 my $error = $q->cgi_error;
654 error("Request not processed: $error");
657 error("\"do\" parameter missing");
661 # Need to lock the wiki before getting a session.
666 $session=cgi_getsession($q);
669 # Auth hooks can sign a user in.
670 if ($do ne 'signin' && ! defined $session->param("name")) {
671 run_hooks(auth => sub {
672 shift->($q, $session)
674 if (defined $session->param("name")) {
675 # Make sure whatever user was authed is in the
677 if (! userinfo_get($session->param("name"), "regdate")) {
678 userinfo_setall($session->param("name"), {
682 }) || error("failed adding user");
687 if (defined $session->param("name") &&
688 userinfo_get($session->param("name"), "banned")) {
689 print $q->header(-status => "403 Forbidden");
691 print gettext("You are banned.");
692 cgi_savesession($session);
695 run_hooks(sessioncgi => sub { shift->($q, $session) });
697 if ($do eq 'signin') {
698 cgi_signin($q, $session);
699 cgi_savesession($session);
701 elsif ($do eq 'prefs') {
702 cgi_prefs($q, $session);
704 elsif ($do eq 'create' || $do eq 'edit') {
705 cgi_editpage($q, $session);
707 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
708 cgi_postsignin($q, $session);
711 error("unknown do parameter");