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 redirect ($$) { #{{{
27 if (! $config{w3mmode}) {
28 print $q->redirect($url);
31 print "Content-type: text/plain\n";
32 print "W3m-control: GOTO $url\n\n";
36 sub check_canedit ($$$;$) { #{{{
43 run_hooks(canedit => sub {
44 return if defined $canedit;
45 my $ret=shift->($page, $q, $session);
46 if (defined $ret && $ret eq "") {
49 elsif (defined $ret) {
51 error($ret) unless $nonfatal;
57 sub decode_form_utf8 ($) { #{{{
59 foreach my $f ($form->field) {
60 next if Encode::is_utf8(scalar $form->field($f));
61 $form->field(name => $f,
62 value => decode_utf8($form->field($f)),
68 sub cgi_recentchanges ($) { #{{{
71 # Optimisation: building recentchanges means calculating lots of
72 # links. Memoizing htmllink speeds it up a lot (can't be memoized
73 # during page builds as the return values may change, but they
79 eval q{use Time::Duration};
82 my $changelog=[rcs_recentchanges(100)];
83 foreach my $change (@$changelog) {
84 $change->{when} = concise(ago($change->{when}));
86 $change->{user} = userlink($change->{user});
88 my $is_excess = exists $change->{pages}[10]; # limit pages to first 10
89 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
92 $_->{link} = htmllink("", "", $_->{page},
94 linktext => pagetitle($_->{page}));
98 push @{$change->{pages}}, { link => '...' } if $is_excess;
101 my $template=template("recentchanges.tmpl");
103 title => "RecentChanges",
104 indexlink => indexlink(),
105 wikiname => $config{wikiname},
106 changelog => $changelog,
107 baseurl => baseurl(),
109 run_hooks(pagetemplate => sub {
110 shift->(page => "", destpage => "", template => $template);
112 print $q->header(-charset => 'utf-8'), $template->output;
115 # Check if the user is signed in. If not, redirect to the signin form and
116 # save their place to return to later.
117 sub needsignin ($$) { #{{{
121 if (! defined $session->param("name") ||
122 ! userinfo_get($session->param("name"), "regdate")) {
123 if (! defined $session->param("postsignin")) {
124 $session->param(postsignin => $ENV{QUERY_STRING});
126 cgi_signin($q, $session);
127 cgi_savesession($session);
132 sub cgi_signin ($$) { #{{{
136 eval q{use CGI::FormBuilder};
138 my $form = CGI::FormBuilder->new(
146 action => $config{cgiurl},
148 template => scalar template_params("signin.tmpl"),
149 stylesheet => baseurl()."style.css",
151 my $buttons=["Login"];
153 if ($q->param("do") ne "signin" && !$form->submitted) {
154 $form->text(gettext("You need to log in first."));
156 $form->field(name => "do", type => "hidden", value => "signin",
159 run_hooks(formbuilder_setup => sub {
160 shift->(form => $form, cgi => $q, session => $session);
163 decode_form_utf8($form);
165 if (exists $hooks{formbuilder}) {
166 run_hooks(formbuilder => sub {
167 shift->(form => $form, cgi => $q, session => $session,
168 buttons => $buttons);
172 if ($form->submitted) {
175 printheader($session);
176 print misctemplate($form->title, $form->render(submit => $buttons));
180 sub cgi_postsignin ($$) { #{{{
184 # Continue with whatever was being done before the signin process.
185 if (defined $session->param("postsignin")) {
186 my $postsignin=CGI->new($session->param("postsignin"));
187 $session->clear("postsignin");
188 cgi($postsignin, $session);
189 cgi_savesession($session);
193 # This can occur, for example, if a user went to the signin
194 # url via a bookmark.
195 redirect($q, $config{url});
199 sub cgi_prefs ($$) { #{{{
203 needsignin($q, $session);
205 eval q{use CGI::FormBuilder};
207 my $form = CGI::FormBuilder->new(
208 title => "preferences",
218 action => $config{cgiurl},
219 template => scalar template_params("prefs.tmpl"),
220 stylesheet => baseurl()."style.css",
222 my $buttons=["Save Preferences", "Logout", "Cancel"];
224 run_hooks(formbuilder_setup => sub {
225 shift->(form => $form, cgi => $q, session => $session);
228 $form->field(name => "do", type => "hidden");
229 $form->field(name => "email", size => 50);
230 $form->field(name => "subscriptions", size => 50,
231 comment => "(".htmllink("", "", "PageSpec", noimageinline => 1).")");
232 $form->field(name => "banned_users", size => 50);
234 my $user_name=$session->param("name");
235 if (! is_admin($user_name)) {
236 $form->field(name => "banned_users", type => "hidden");
239 if (! $form->submitted) {
240 $form->field(name => "email", force => 1,
241 value => userinfo_get($user_name, "email"));
242 $form->field(name => "subscriptions", force => 1,
243 value => userinfo_get($user_name, "subscriptions"));
244 if (is_admin($user_name)) {
245 $form->field(name => "banned_users", force => 1,
246 value => join(" ", get_banned_users()));
250 decode_form_utf8($form);
252 if ($form->submitted eq 'Logout') {
254 redirect($q, $config{url});
257 elsif ($form->submitted eq 'Cancel') {
258 redirect($q, $config{url});
261 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
262 foreach my $field (qw(email subscriptions)) {
263 if (defined $form->field($field) && length $form->field($field)) {
264 userinfo_set($user_name, $field, $form->field($field)) ||
265 error("failed to set $field");
268 if (is_admin($user_name)) {
269 set_banned_users(grep { ! is_admin($_) }
271 $form->field("banned_users"))) ||
272 error("failed saving changes");
274 $form->text(gettext("Preferences saved."));
277 if (exists $hooks{formbuilder}) {
278 run_hooks(formbuilder => sub {
279 shift->(form => $form, cgi => $q, session => $session,
280 buttons => $buttons);
284 printheader($session);
285 print misctemplate($form->title, $form->render(submit => $buttons));
289 sub cgi_editpage ($$;$) { #{{{
294 my @fields=qw(do rcsinfo subpage from page type editcontent comments
296 my @buttons=("Save Page", "Preview", "Cancel");
298 eval q{use CGI::FormBuilder};
300 my $form = CGI::FormBuilder->new(
306 editcontent => '/.+/',
308 required => [qw{editcontent}],
311 action => $config{cgiurl},
313 template => scalar template_params("editpage.tmpl"),
316 run_hooks(formbuilder_setup => sub {
317 shift->(form => $form, cgi => $q, session => $session);
320 decode_form_utf8($form);
322 # This untaint is safe because titlepage removes any problematic
324 my ($page)=$form->field('page');
325 $page=titlepage(possibly_foolish_untaint($page));
327 $page=~s/(\/)/"__".ord($1)."__"/eg;
329 if (! defined $page || ! length $page || file_pruned($page, $config{srcdir}) || $page=~/^\//) {
330 error("bad page name");
334 if (defined $form->field('from')) {
335 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
340 if (exists $pagesources{$page}) {
341 $file=$pagesources{$page};
342 $type=pagetype($file);
343 if (! defined $type) {
344 error(sprintf(gettext("%s is not an editable page"), $page));
348 $type=$form->param('type');
349 if (defined $type && length $type && $hooks{htmlize}{$type}) {
350 $type=possibly_foolish_untaint($type);
352 elsif (defined $from) {
353 # favor the type of linking page
354 $type=pagetype($pagesources{$from});
356 $type=$config{default_pageext} unless defined $type;
357 $file=$page.".".$type;
361 $form->field(name => "do", type => 'hidden');
362 $form->field(name => "from", type => 'hidden');
363 $form->field(name => "rcsinfo", type => 'hidden');
364 $form->field(name => "subpage", type => 'hidden');
365 $form->field(name => "page", value => $page, force => 1);
366 $form->field(name => "type", value => $type, force => 1);
367 $form->field(name => "comments", type => "text", size => 80);
368 $form->field(name => "editcontent", type => "textarea", rows => 20,
370 $form->field(name => "newfile", type => 'hidden');
371 $form->tmpl_param("can_commit", $config{rcs});
372 $form->tmpl_param("indexlink", indexlink());
373 $form->tmpl_param("helponformattinglink",
374 htmllink("", "", "HelpOnFormatting", noimageinline => 1));
375 $form->tmpl_param("baseurl", baseurl());
376 if (! $form->submitted) {
377 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
379 $form->field(name => "newfile",
380 value => ! -e "$config{srcdir}/$file",
384 if ($form->submitted eq "Cancel") {
385 if ($form->field(name => "newfile") && defined $from) {
386 redirect($q, "$config{url}/".htmlpage($from));
388 elsif ($form->field(name => "newfile")) {
389 redirect($q, $config{url});
392 redirect($q, "$config{url}/".htmlpage($page));
396 elsif ($form->submitted eq "Preview") {
397 my $content=$form->field('editcontent');
398 my $comments=$form->field('comments');
399 $form->field(name => "editcontent",
400 value => $content, force => 1);
401 $form->field(name => "comments",
402 value => $comments, force => 1);
403 $form->tmpl_param("page_preview",
404 htmlize($page, $type,
406 preprocess($page, $page,
407 filter($page, $content), 0, 1))));
410 $form->tmpl_param("page_preview", "");
412 $form->tmpl_param("page_conflict", "");
414 if (! $form->submitted || $form->submitted eq "Preview" ||
416 if ($form->field("do") eq "create") {
419 if (! defined $from || ! length $from ||
420 $from ne $form->field('from') ||
421 file_pruned($from, $config{srcdir}) ||
423 $form->submitted eq "Preview") {
424 @page_locs=$best_loc=$page;
430 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
431 $page eq gettext('discussion')) {
432 $best_loc="$from/$page";
435 $best_loc=$dir.$page;
438 push @page_locs, $dir.$page;
439 push @page_locs, "$from/$page";
440 while (length $dir) {
442 push @page_locs, $dir.$page;
445 push @page_locs, "$config{userdir}/$page"
446 if length $config{userdir};
449 ! exists $pagecase{lc $_}
452 # hmm, someone else made the page in the
454 redirect($q, "$config{url}/".htmlpage($page));
458 my @editable_locs = grep {
459 check_canedit($_, $q, $session, 1)
461 if (! @editable_locs) {
462 # let it throw an error this time
463 map { check_canedit($_, $q, $session) } @page_locs;
467 if (exists $hooks{htmlize}) {
468 @page_types=keys %{$hooks{htmlize}};
471 $form->tmpl_param("page_select", 1);
472 $form->field(name => "page", type => 'select',
473 options => [ map { pagetitle($_, 1) } @editable_locs ],
474 value => pagetitle($best_loc, 1));
475 $form->field(name => "type", type => 'select',
476 options => \@page_types);
477 $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 print $form->render(submit => \@buttons);
501 check_canedit($page, $q, $session);
502 if (! -e "$config{srcdir}/$file" && ! $form->field(name => 'newfile')){
503 $form->tmpl_param("page_gone", 1);
504 $form->field(name => "newfile",
505 value => 1, force => 1);
506 $form->tmpl_param("page_select", 0);
507 $form->field(name => "page", type => 'hidden');
508 $form->field(name => "type", type => 'hidden');
509 print $form->render(submit => \@buttons);
513 my $content=$form->field('editcontent');
515 $content=~s/\r\n/\n/g;
518 $config{cgi}=0; # avoid cgi error message
519 eval { writefile($file, $config{srcdir}, $content) };
522 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
524 $form->tmpl_param("failed_save", 1);
525 $form->tmpl_param("error_message", $@);
526 $form->field("editcontent", value => $content, force => 1);
527 $form->tmpl_param("page_select", 0);
528 $form->field(name => "page", type => 'hidden');
529 $form->field(name => "type", type => 'hidden');
530 $form->title(sprintf(gettext("editing %s"), $page));
531 print $form->render(submit => \@buttons);
538 if (defined $form->field('comments') &&
539 length $form->field('comments')) {
540 $message=$form->field('comments');
543 if ($form->field(name => "newfile")) {
547 # Prevent deadlock with post-commit hook by
548 # signaling to it that it should not try to
549 # do anything (except send commit mails).
550 disable_commit_hook();
551 $conflict=rcs_commit($file, $message,
552 $form->field("rcsinfo"),
553 $session->param("name"), $ENV{REMOTE_ADDR});
554 enable_commit_hook();
558 # Refresh even if there was a conflict, since other changes
559 # may have been committed while the post-commit hook was
561 require IkiWiki::Render;
565 if (defined $conflict) {
566 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
568 $form->tmpl_param("page_conflict", 1);
569 $form->field("editcontent", value => $conflict, force => 1);
570 $form->field("do", "edit", force => 1);
571 $form->tmpl_param("page_select", 0);
572 $form->field(name => "page", type => 'hidden');
573 $form->field(name => "type", type => 'hidden');
574 $form->title(sprintf(gettext("editing %s"), $page));
575 print $form->render(submit => \@buttons);
579 # The trailing question mark tries to avoid broken
580 # caches and get the most recent version of the page.
581 redirect($q, "$config{url}/".htmlpage($page)."?updated");
586 sub cgi_getsession ($) { #{{{
589 eval q{use CGI::Session};
590 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
592 my $oldmask=umask(077);
593 my $session = CGI::Session->new("driver:DB_File", $q,
594 { FileName => "$config{wikistatedir}/sessions.db" });
600 sub cgi_savesession ($) { #{{{
603 # Force session flush with safe umask.
604 my $oldmask=umask(077);
619 run_hooks(cgi => sub { shift->($q) });
622 my $do=$q->param('do');
623 if (! defined $do || ! length $do) {
624 my $error = $q->cgi_error;
626 error("Request not processed: $error");
629 error("\"do\" parameter missing");
633 # Things that do not need a session.
634 if ($do eq 'recentchanges') {
635 cgi_recentchanges($q);
638 elsif ($do eq 'hyperestraier') {
642 # Need to lock the wiki before getting a session.
646 $session=cgi_getsession($q);
649 # Auth hooks can sign a user in.
650 if ($do ne 'signin' && ! defined $session->param("name")) {
651 run_hooks(auth => sub {
652 shift->($q, $session)
654 if (defined $session->param("name")) {
655 # Make sure whatever user was authed is in the
657 if (! userinfo_get($session->param("name"), "regdate")) {
658 userinfo_setall($session->param("name"), {
662 }) || error("failed adding user");
667 if (defined $session->param("name") &&
668 userinfo_get($session->param("name"), "banned")) {
669 print $q->header(-status => "403 Forbidden");
671 print gettext("You are banned.");
672 cgi_savesession($session);
674 elsif ($do eq 'signin') {
675 cgi_signin($q, $session);
676 cgi_savesession($session);
678 elsif (defined $session->param("postsignin")) {
679 cgi_postsignin($q, $session);
681 elsif ($do eq 'prefs') {
682 cgi_prefs($q, $session);
684 elsif ($do eq 'create' || $do eq 'edit') {
685 cgi_editpage($q, $session);
687 elsif ($do eq 'blog') {
688 my $page=decode_utf8($q->param('title'));
689 # if the page already exists, munge it to be unique
690 my $from=$q->param('from');
692 while (exists $pagecase{lc "$from/$page$add"}) {
693 $add=1 unless length $add;
696 $q->param('page', $page.$add);
697 # now run same as create, except escape slashes too
698 $q->param('do', 'create');
699 cgi_editpage($q, $session, 1);
701 elsif ($do eq 'postsignin') {
702 error(gettext("login failed, perhaps you need to turn on cookies?"));
705 error("unknown do parameter");
709 sub userlink ($) { #{{{
712 eval q{use CGI 'escapeHTML'};
714 if ($user =~ m!^https?://! &&
715 eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
716 # Munge user-urls, as used by eg, OpenID.
717 my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
718 my $display=$oid->display;
719 # Convert "user.somehost.com" to "user [somehost.com]".
720 if ($display !~ /\[/) {
721 $display=~s/^(.*?)\.([^.]+\.[a-z]+)$/$1 [$2]/;
723 # Convert "http://somehost.com/user" to "user [somehost.com]".
724 if ($display !~ /\[/) {
725 $display=~s/^https?:\/\/(.+)\/([^\/]+)$/$2 [$1]/;
727 $display=~s!^https?://!!; # make sure this is removed
728 return "<a href=\"$user\">".escapeHTML($display)."</a>";
731 return htmllink("", "", escapeHTML(
732 length $config{userdir} ? $config{userdir}."/".$user : $user
733 ), noimageinline => 1);