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(
147 action => $config{cgiurl},
149 template => {type => 'div'},
150 stylesheet => baseurl()."style.css",
152 my $buttons=["Login"];
154 if ($q->param("do") ne "signin" && !$form->submitted) {
155 $form->text(gettext("You need to log in first."));
157 $form->field(name => "do", type => "hidden", value => "signin",
160 run_hooks(formbuilder_setup => sub {
161 shift->(form => $form, cgi => $q, session => $session);
164 decode_form_utf8($form);
166 if (exists $hooks{formbuilder}) {
167 run_hooks(formbuilder => sub {
168 shift->(form => $form, cgi => $q, session => $session,
169 buttons => $buttons);
173 if ($form->submitted) {
176 printheader($session);
177 print misctemplate($form->title, $form->render(submit => $buttons));
181 sub cgi_postsignin ($$) { #{{{
185 # Continue with whatever was being done before the signin process.
186 if (defined $session->param("postsignin")) {
187 my $postsignin=CGI->new($session->param("postsignin"));
188 $session->clear("postsignin");
189 cgi($postsignin, $session);
190 cgi_savesession($session);
194 # This can occur, for example, if a user went to the signin
195 # url via a bookmark.
196 redirect($q, $config{url});
200 sub cgi_prefs ($$) { #{{{
204 needsignin($q, $session);
206 eval q{use CGI::FormBuilder};
208 my $form = CGI::FormBuilder->new(
209 title => "preferences",
210 name => "preferences",
220 action => $config{cgiurl},
221 template => {type => 'div'},
222 stylesheet => baseurl()."style.css",
224 [login => gettext("Login")],
225 [preferences => gettext("Preferences")],
226 [admin => gettext("Admin")]
229 my $buttons=["Save Preferences", "Logout", "Cancel"];
231 run_hooks(formbuilder_setup => sub {
232 shift->(form => $form, cgi => $q, session => $session);
235 $form->field(name => "do", type => "hidden");
236 $form->field(name => "email", size => 50, fieldset => "preferences");
237 $form->field(name => "subscriptions", size => 50,
238 fieldset => "preferences",
239 comment => "(".htmllink("", "", "PageSpec", noimageinline => 1).")");
240 $form->field(name => "banned_users", size => 50,
241 fieldset => "admin");
243 my $user_name=$session->param("name");
244 if (! is_admin($user_name)) {
245 $form->field(name => "banned_users", type => "hidden");
248 if (! $form->submitted) {
249 $form->field(name => "email", force => 1,
250 value => userinfo_get($user_name, "email"));
251 $form->field(name => "subscriptions", force => 1,
252 value => userinfo_get($user_name, "subscriptions"));
253 if (is_admin($user_name)) {
254 $form->field(name => "banned_users", force => 1,
255 value => join(" ", get_banned_users()));
259 decode_form_utf8($form);
261 if ($form->submitted eq 'Logout') {
263 redirect($q, $config{url});
266 elsif ($form->submitted eq 'Cancel') {
267 redirect($q, $config{url});
270 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
271 foreach my $field (qw(email subscriptions)) {
272 if (defined $form->field($field) && length $form->field($field)) {
273 userinfo_set($user_name, $field, $form->field($field)) ||
274 error("failed to set $field");
277 if (is_admin($user_name)) {
278 set_banned_users(grep { ! is_admin($_) }
280 $form->field("banned_users"))) ||
281 error("failed saving changes");
283 $form->text(gettext("Preferences saved."));
286 if (exists $hooks{formbuilder}) {
287 run_hooks(formbuilder => sub {
288 shift->(form => $form, cgi => $q, session => $session,
289 buttons => $buttons);
293 printheader($session);
294 print misctemplate($form->title, $form->render(submit => $buttons));
298 sub cgi_editpage ($$) { #{{{
302 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
303 my @buttons=("Save Page", "Preview", "Cancel");
305 eval q{use CGI::FormBuilder};
307 my $form = CGI::FormBuilder->new(
313 editcontent => '/.+/',
315 required => [qw{editcontent}],
318 action => $config{cgiurl},
320 template => scalar template_params("editpage.tmpl"),
323 run_hooks(formbuilder_setup => sub {
324 shift->(form => $form, cgi => $q, session => $session);
327 decode_form_utf8($form);
329 # This untaint is safe because titlepage removes any problematic
331 my ($page)=$form->field('page');
332 $page=titlepage(possibly_foolish_untaint($page));
333 if (! defined $page || ! length $page || file_pruned($page, $config{srcdir}) || $page=~/^\//) {
334 error("bad page name");
338 if (defined $form->field('from')) {
339 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
344 if (exists $pagesources{$page} && $form->field("do") ne "create") {
345 $file=$pagesources{$page};
346 $type=pagetype($file);
347 if (! defined $type) {
348 error(sprintf(gettext("%s is not an editable page"), $page));
350 if (! $form->submitted) {
351 $form->field(name => "rcsinfo",
352 value => rcs_prepedit($file), force => 1);
356 $type=$form->param('type');
357 if (defined $type && length $type && $hooks{htmlize}{$type}) {
358 $type=possibly_foolish_untaint($type);
360 elsif (defined $from) {
361 # favor the type of linking page
362 $type=pagetype($pagesources{$from});
364 $type=$config{default_pageext} unless defined $type;
365 $file=$page.".".$type;
366 if (! $form->submitted) {
367 $form->field(name => "rcsinfo", value => "", force => 1);
371 $form->field(name => "do", type => 'hidden');
372 $form->field(name => "from", type => 'hidden');
373 $form->field(name => "rcsinfo", type => 'hidden');
374 $form->field(name => "subpage", type => 'hidden');
375 $form->field(name => "page", value => pagetitle($page, 1), force => 1);
376 $form->field(name => "type", value => $type, force => 1);
377 $form->field(name => "comments", type => "text", size => 80);
378 $form->field(name => "editcontent", type => "textarea", rows => 20,
380 $form->tmpl_param("can_commit", $config{rcs});
381 $form->tmpl_param("indexlink", indexlink());
382 $form->tmpl_param("helponformattinglink",
383 htmllink("", "", "HelpOnFormatting", noimageinline => 1));
384 $form->tmpl_param("baseurl", baseurl());
386 if ($form->submitted eq "Cancel") {
387 if ($form->field("do") eq "create" && defined $from) {
388 redirect($q, "$config{url}/".htmlpage($from));
390 elsif ($form->field("do") eq "create") {
391 redirect($q, $config{url});
394 redirect($q, "$config{url}/".htmlpage($page));
398 elsif ($form->submitted eq "Preview") {
399 $form->tmpl_param("page_preview",
400 htmlize($page, $type,
402 preprocess($page, $page,
403 filter($page, $form->field('editcontent')), 0, 1))));
406 $form->tmpl_param("page_preview", "");
408 $form->tmpl_param("page_conflict", "");
410 if (! $form->submitted || $form->submitted eq "Preview" ||
412 if ($form->field("do") eq "create") {
415 if (! defined $from || ! length $from ||
416 $from ne $form->field('from') ||
417 file_pruned($from, $config{srcdir}) ||
419 $form->submitted eq "Preview") {
420 @page_locs=$best_loc=$page;
426 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
427 $page eq gettext('discussion')) {
428 $best_loc="$from/$page";
431 $best_loc=$dir.$page;
434 push @page_locs, $dir.$page;
435 push @page_locs, "$from/$page";
436 while (length $dir) {
438 push @page_locs, $dir.$page;
441 push @page_locs, "$config{userdir}/$page"
442 if length $config{userdir};
445 ! exists $pagecase{lc $_}
448 # hmm, someone else made the page in the
450 redirect($q, "$config{url}/".htmlpage($page));
454 my @editable_locs = grep {
455 check_canedit($_, $q, $session, 1)
457 if (! @editable_locs) {
458 # let it throw an error this time
459 map { check_canedit($_, $q, $session) } @page_locs;
463 if (exists $hooks{htmlize}) {
464 @page_types=keys %{$hooks{htmlize}};
467 $form->tmpl_param("page_select", 1);
468 $form->field(name => "page", type => 'select',
469 options => [ map { pagetitle($_, 1) } @editable_locs ],
470 value => pagetitle($best_loc, 1));
471 $form->field(name => "type", type => 'select',
472 options => \@page_types);
473 $form->title(sprintf(gettext("creating %s"), pagetitle($page)));
475 elsif ($form->field("do") eq "edit") {
476 check_canedit($page, $q, $session);
477 if (! defined $form->field('editcontent') ||
478 ! length $form->field('editcontent')) {
480 if (exists $pagesources{$page}) {
481 $content=readfile(srcfile($pagesources{$page}));
482 $content=~s/\n/\r\n/g;
484 $form->field(name => "editcontent", value => $content,
487 $form->tmpl_param("page_select", 0);
488 $form->field(name => "page", type => 'hidden');
489 $form->field(name => "type", type => 'hidden');
490 $form->title(sprintf(gettext("editing %s"), pagetitle($page)));
493 print $form->render(submit => \@buttons);
497 check_canedit($page, $q, $session);
498 if (! -e "$config{srcdir}/$file" &&
499 $form->field("do") ne "create") {
500 $form->tmpl_param("page_gone", 1);
501 $form->field(name => "do", value => "create", force => 1);
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"), $page));
506 print $form->render(submit => \@buttons);
509 elsif (-e "$config{srcdir}/$file" &&
510 $form->field("do") eq "create") {
511 $form->tmpl_param("creation_conflict", 1);
512 $form->field(name => "do", value => "edit", force => 1);
513 $form->tmpl_param("page_select", 0);
514 $form->field(name => "page", type => 'hidden');
515 $form->field(name => "type", type => 'hidden');
516 $form->title(sprintf(gettext("editing %s"), $page));
517 $form->field("editcontent",
518 value => readfile("$config{srcdir}/$file").
519 "\n\n\n".$form->field("editcontent"),
521 print $form->render(submit => \@buttons);
525 my $content=$form->field('editcontent');
527 $content=~s/\r\n/\n/g;
530 $config{cgi}=0; # avoid cgi error message
531 eval { writefile($file, $config{srcdir}, $content) };
534 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
536 $form->tmpl_param("failed_save", 1);
537 $form->tmpl_param("error_message", $@);
538 $form->field("editcontent", value => $content, force => 1);
539 $form->tmpl_param("page_select", 0);
540 $form->field(name => "page", type => 'hidden');
541 $form->field(name => "type", type => 'hidden');
542 $form->title(sprintf(gettext("editing %s"), $page));
543 print $form->render(submit => \@buttons);
550 if (defined $form->field('comments') &&
551 length $form->field('comments')) {
552 $message=$form->field('comments');
555 if ($form->field("do") eq "create") {
559 # Prevent deadlock with post-commit hook by
560 # signaling to it that it should not try to
561 # do anything (except send commit mails).
562 disable_commit_hook();
563 $conflict=rcs_commit($file, $message,
564 $form->field("rcsinfo"),
565 $session->param("name"), $ENV{REMOTE_ADDR});
566 enable_commit_hook();
570 # Refresh even if there was a conflict, since other changes
571 # may have been committed while the post-commit hook was
573 require IkiWiki::Render;
577 if (defined $conflict) {
578 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
580 $form->tmpl_param("page_conflict", 1);
581 $form->field("editcontent", value => $conflict, force => 1);
582 $form->field("do", "edit", force => 1);
583 $form->tmpl_param("page_select", 0);
584 $form->field(name => "page", type => 'hidden');
585 $form->field(name => "type", type => 'hidden');
586 $form->title(sprintf(gettext("editing %s"), $page));
587 print $form->render(submit => \@buttons);
591 # The trailing question mark tries to avoid broken
592 # caches and get the most recent version of the page.
593 redirect($q, "$config{url}/".htmlpage($page)."?updated");
598 sub cgi_getsession ($) { #{{{
601 eval q{use CGI::Session};
602 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
604 my $oldmask=umask(077);
605 my $session = CGI::Session->new("driver:DB_File", $q,
606 { FileName => "$config{wikistatedir}/sessions.db" });
612 sub cgi_savesession ($) { #{{{
615 # Force session flush with safe umask.
616 my $oldmask=umask(077);
631 run_hooks(cgi => sub { shift->($q) });
634 my $do=$q->param('do');
635 if (! defined $do || ! length $do) {
636 my $error = $q->cgi_error;
638 error("Request not processed: $error");
641 error("\"do\" parameter missing");
645 # Things that do not need a session.
646 if ($do eq 'recentchanges') {
647 cgi_recentchanges($q);
650 elsif ($do eq 'hyperestraier') {
654 # Need to lock the wiki before getting a session.
658 $session=cgi_getsession($q);
661 # Auth hooks can sign a user in.
662 if ($do ne 'signin' && ! defined $session->param("name")) {
663 run_hooks(auth => sub {
664 shift->($q, $session)
666 if (defined $session->param("name")) {
667 # Make sure whatever user was authed is in the
669 if (! userinfo_get($session->param("name"), "regdate")) {
670 userinfo_setall($session->param("name"), {
674 }) || error("failed adding user");
679 if (defined $session->param("name") &&
680 userinfo_get($session->param("name"), "banned")) {
681 print $q->header(-status => "403 Forbidden");
683 print gettext("You are banned.");
684 cgi_savesession($session);
686 elsif ($do eq 'signin') {
687 cgi_signin($q, $session);
688 cgi_savesession($session);
690 elsif (defined $session->param("postsignin")) {
691 cgi_postsignin($q, $session);
693 elsif ($do eq 'prefs') {
694 cgi_prefs($q, $session);
696 elsif ($do eq 'create' || $do eq 'edit') {
697 cgi_editpage($q, $session);
699 elsif ($do eq 'blog') {
700 my $page=decode_utf8($q->param('title'));
701 $page=~s/\///g; # no slashes in blog posts
702 # if the page already exists, munge it to be unique
703 my $from=$q->param('from');
705 while (exists $pagecase{lc($from."/".titlepage($page).$add)}) {
706 $add=1 unless length $add;
709 $q->param('page', $page.$add);
710 # now run same as create
711 $q->param('do', 'create');
712 cgi_editpage($q, $session);
714 elsif ($do eq 'postsignin') {
715 error(gettext("login failed, perhaps you need to turn on cookies?"));
718 error("unknown do parameter");
722 sub userlink ($) { #{{{
725 eval q{use CGI 'escapeHTML'};
727 if ($user =~ m!^https?://! &&
728 eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
729 # Munge user-urls, as used by eg, OpenID.
730 my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
731 my $display=$oid->display;
732 # Convert "user.somehost.com" to "user [somehost.com]".
733 if ($display !~ /\[/) {
734 $display=~s/^(.*?)\.([^.]+\.[a-z]+)$/$1 [$2]/;
736 # Convert "http://somehost.com/user" to "user [somehost.com]".
737 if ($display !~ /\[/) {
738 $display=~s/^https?:\/\/(.+)\/([^\/]+)$/$2 [$1]/;
740 $display=~s!^https?://!!; # make sure this is removed
741 return "<a href=\"$user\">".escapeHTML($display)."</a>";
744 return htmllink("", "", escapeHTML(
745 length $config{userdir} ? $config{userdir}."/".$user : $user
746 ), noimageinline => 1);