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 my $postsignin=CGI->new($session->param("postsignin"));
186 $session->clear("postsignin");
187 cgi($postsignin, $session);
188 cgi_savesession($session);
192 sub cgi_prefs ($$) { #{{{
196 needsignin($q, $session);
198 eval q{use CGI::FormBuilder};
200 my $form = CGI::FormBuilder->new(
201 title => "preferences",
211 action => $config{cgiurl},
212 template => scalar template_params("prefs.tmpl"),
213 stylesheet => baseurl()."style.css",
215 my $buttons=["Save Preferences", "Logout", "Cancel"];
217 run_hooks(formbuilder_setup => sub {
218 shift->(form => $form, cgi => $q, session => $session);
221 $form->field(name => "do", type => "hidden");
222 $form->field(name => "email", size => 50);
223 $form->field(name => "subscriptions", size => 50,
224 comment => "(".htmllink("", "", "PageSpec", noimageinline => 1).")");
225 $form->field(name => "banned_users", size => 50);
227 my $user_name=$session->param("name");
228 if (! is_admin($user_name)) {
229 $form->field(name => "banned_users", type => "hidden");
232 if (! $form->submitted) {
233 $form->field(name => "email", force => 1,
234 value => userinfo_get($user_name, "email"));
235 $form->field(name => "subscriptions", force => 1,
236 value => userinfo_get($user_name, "subscriptions"));
237 if (is_admin($user_name)) {
238 $form->field(name => "banned_users", force => 1,
239 value => join(" ", get_banned_users()));
243 decode_form_utf8($form);
245 if ($form->submitted eq 'Logout') {
247 redirect($q, $config{url});
250 elsif ($form->submitted eq 'Cancel') {
251 redirect($q, $config{url});
254 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
255 foreach my $field (qw(email subscriptions)) {
256 if (defined $form->field($field) && length $form->field($field)) {
257 userinfo_set($user_name, $field, $form->field($field)) ||
258 error("failed to set $field");
261 if (is_admin($user_name)) {
262 set_banned_users(grep { ! is_admin($_) }
264 $form->field("banned_users"))) ||
265 error("failed saving changes");
267 $form->text(gettext("Preferences saved."));
270 if (exists $hooks{formbuilder}) {
271 run_hooks(formbuilder => sub {
272 shift->(form => $form, cgi => $q, session => $session,
273 buttons => $buttons);
277 printheader($session);
278 print misctemplate($form->title, $form->render(submit => $buttons));
282 sub cgi_editpage ($$) { #{{{
286 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
287 my @buttons=("Save Page", "Preview", "Cancel");
289 eval q{use CGI::FormBuilder};
291 my $form = CGI::FormBuilder->new(
297 editcontent => '/.+/',
299 required => [qw{editcontent}],
302 action => $config{cgiurl},
304 template => scalar template_params("editpage.tmpl"),
307 run_hooks(formbuilder_setup => sub {
308 shift->(form => $form, cgi => $q, session => $session);
311 decode_form_utf8($form);
313 # This untaint is safe because titlepage removes any problematic
315 my ($page)=$form->field('page');
316 $page=titlepage(possibly_foolish_untaint($page));
317 if (! defined $page || ! length $page || file_pruned($page, $config{srcdir}) || $page=~/^\//) {
318 error("bad page name");
322 if (defined $form->field('from')) {
323 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
328 if (exists $pagesources{$page}) {
329 $file=$pagesources{$page};
330 $type=pagetype($file);
331 if (! defined $type) {
332 error(sprintf(gettext("%s is not an editable page"), $page));
336 $type=$form->param('type');
337 if (defined $type && length $type && $hooks{htmlize}{$type}) {
338 $type=possibly_foolish_untaint($type);
340 elsif (defined $from) {
341 # favor the type of linking page
342 $type=pagetype($pagesources{$from});
344 $type=$config{default_pageext} unless defined $type;
345 $file=$page.".".$type;
349 if (! -e "$config{srcdir}/$file") {
353 $form->field(name => "do", type => 'hidden');
354 $form->field(name => "from", type => 'hidden');
355 $form->field(name => "rcsinfo", type => 'hidden');
356 $form->field(name => "subpage", type => 'hidden');
357 $form->field(name => "page", value => $page, force => 1);
358 $form->field(name => "type", value => $type, force => 1);
359 $form->field(name => "comments", type => "text", size => 80);
360 $form->field(name => "editcontent", type => "textarea", rows => 20,
362 $form->tmpl_param("can_commit", $config{rcs});
363 $form->tmpl_param("indexlink", indexlink());
364 $form->tmpl_param("helponformattinglink",
365 htmllink("", "", "HelpOnFormatting", noimageinline => 1));
366 $form->tmpl_param("baseurl", baseurl());
367 if (! $form->submitted) {
368 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
372 if ($form->submitted eq "Cancel") {
373 if ($newfile && defined $from) {
374 redirect($q, "$config{url}/".htmlpage($from));
377 redirect($q, $config{url});
380 redirect($q, "$config{url}/".htmlpage($page));
384 elsif ($form->submitted eq "Preview") {
385 my $content=$form->field('editcontent');
386 my $comments=$form->field('comments');
387 $form->field(name => "editcontent",
388 value => $content, force => 1);
389 $form->field(name => "comments",
390 value => $comments, force => 1);
391 $config{rss}=$config{atom}=0; # avoid preview writing a feed!
392 $form->tmpl_param("page_preview",
393 htmlize($page, $type,
395 preprocess($page, $page,
396 filter($page, $content)))));
399 $form->tmpl_param("page_preview", "");
401 $form->tmpl_param("page_conflict", "");
403 if (! $form->submitted || $form->submitted eq "Preview" ||
405 if ($form->field("do") eq "create") {
408 if (! defined $from || ! length $from ||
409 $from ne $form->field('from') ||
410 file_pruned($from, $config{srcdir}) ||
412 $form->submitted eq "Preview") {
413 @page_locs=$best_loc=$page;
419 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
420 $page eq gettext('discussion')) {
421 $best_loc="$from/$page";
424 $best_loc=$dir.$page;
427 push @page_locs, $dir.$page;
428 push @page_locs, "$from/$page";
429 while (length $dir) {
431 push @page_locs, $dir.$page;
434 push @page_locs, "$config{userdir}/$page"
435 if length $config{userdir};
438 ! exists $pagecase{lc $_}
441 # hmm, someone else made the page in the
443 redirect($q, "$config{url}/".htmlpage($page));
447 my @editable_locs = grep {
448 check_canedit($_, $q, $session, 1)
450 if (! @editable_locs) {
451 # let it throw an error this time
452 map { check_canedit($_, $q, $session) } @page_locs;
456 if (exists $hooks{htmlize}) {
457 @page_types=keys %{$hooks{htmlize}};
460 $form->tmpl_param("page_select", 1);
461 $form->field(name => "page", type => 'select',
462 options => \@editable_locs, value => $best_loc);
463 $form->field(name => "type", type => 'select',
464 options => \@page_types);
465 $form->title(sprintf(gettext("creating %s"), pagetitle($page)));
467 elsif ($form->field("do") eq "edit") {
468 check_canedit($page, $q, $session);
469 if (! defined $form->field('editcontent') ||
470 ! length $form->field('editcontent')) {
472 if (exists $pagesources{$page}) {
473 $content=readfile(srcfile($pagesources{$page}));
474 $content=~s/\n/\r\n/g;
476 $form->field(name => "editcontent", value => $content,
479 $form->tmpl_param("page_select", 0);
480 $form->field(name => "page", type => 'hidden');
481 $form->field(name => "type", type => 'hidden');
482 $form->title(sprintf(gettext("editing %s"), pagetitle($page)));
485 print $form->render(submit => \@buttons);
489 check_canedit($page, $q, $session);
491 my $content=$form->field('editcontent');
493 $content=~s/\r\n/\n/g;
496 $config{cgi}=0; # avoid cgi error message
497 eval { writefile($file, $config{srcdir}, $content) };
500 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
502 $form->tmpl_param("failed_save", 1);
503 $form->tmpl_param("error_message", $@);
504 $form->field("editcontent", value => $content, force => 1);
505 $form->field(name => "comments", value => $form->field('comments'), force => 1);
506 $form->field("do", "edit)");
507 $form->tmpl_param("page_select", 0);
508 $form->field(name => "page", type => 'hidden');
509 $form->field(name => "type", type => 'hidden');
510 $form->title(sprintf(gettext("editing %s"), $page));
511 print $form->render(submit => \@buttons);
517 if (defined $form->field('comments') &&
518 length $form->field('comments')) {
519 $message=$form->field('comments');
525 # prevent deadlock with post-commit hook
527 # presumably the commit will trigger an update
529 my $conflict=rcs_commit($file, $message,
530 $form->field("rcsinfo"),
531 $session->param("name"), $ENV{REMOTE_ADDR});
533 if (defined $conflict) {
534 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
536 $form->tmpl_param("page_conflict", 1);
537 $form->field("editcontent", value => $conflict, force => 1);
538 $form->field(name => "comments", value => $form->field('comments'), force => 1);
539 $form->field("do", "edit)");
540 $form->tmpl_param("page_select", 0);
541 $form->field(name => "page", type => 'hidden');
542 $form->field(name => "type", type => 'hidden');
543 $form->title(sprintf(gettext("editing %s"), $page));
544 print $form->render(submit => \@buttons);
548 # Make sure that the repo is up-to-date;
549 # locking prevents the post-commit hook
555 require IkiWiki::Render;
560 # The trailing question mark tries to avoid broken
561 # caches and get the most recent version of the page.
562 redirect($q, "$config{url}/".htmlpage($page)."?updated");
566 sub cgi_getsession ($) { #{{{
569 eval q{use CGI::Session};
570 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
572 my $oldmask=umask(077);
573 my $session = CGI::Session->new("driver:DB_File", $q,
574 { FileName => "$config{wikistatedir}/sessions.db" });
580 sub cgi_savesession ($) { #{{{
583 # Force session flush with safe umask.
584 my $oldmask=umask(077);
599 run_hooks(cgi => sub { shift->($q) });
602 my $do=$q->param('do');
603 if (! defined $do || ! length $do) {
604 my $error = $q->cgi_error;
606 error("Request not processed: $error");
609 error("\"do\" parameter missing");
613 # Things that do not need a session.
614 if ($do eq 'recentchanges') {
615 cgi_recentchanges($q);
618 elsif ($do eq 'hyperestraier') {
622 # Need to lock the wiki before getting a session.
626 $session=cgi_getsession($q);
629 # Auth hooks can sign a user in.
630 if ($do ne 'signin' && ! defined $session->param("name")) {
631 run_hooks(auth => sub {
632 shift->($q, $session)
634 if (defined $session->param("name")) {
635 # Make sure whatever user was authed is in the
637 if (! userinfo_get($session->param("name"), "regdate")) {
638 userinfo_setall($session->param("name"), {
642 }) || error("failed adding user");
647 if (defined $session->param("name") &&
648 userinfo_get($session->param("name"), "banned")) {
649 print $q->header(-status => "403 Forbidden");
651 print gettext("You are banned.");
652 cgi_savesession($session);
654 elsif ($do eq 'signin') {
655 cgi_signin($q, $session);
656 cgi_savesession($session);
658 elsif (defined $session->param("postsignin")) {
659 cgi_postsignin($q, $session);
661 elsif ($do eq 'prefs') {
662 cgi_prefs($q, $session);
664 elsif ($do eq 'create' || $do eq 'edit') {
665 cgi_editpage($q, $session);
667 elsif ($do eq 'blog') {
668 my $page=titlepage(decode_utf8($q->param('title')));
669 $page=~s/(\/)/"__".ord($1)."__"/eg; # escape slashes too
670 # if the page already exists, munge it to be unique
671 my $from=$q->param('from');
673 while (exists $pagecase{lc "$from/$page$add"}) {
674 $add=1 unless length $add;
677 $q->param('page', $page.$add);
678 # now run same as create
679 $q->param('do', 'create');
680 cgi_editpage($q, $session);
682 elsif ($do eq 'postsignin') {
683 error(gettext("login failed, perhaps you need to turn on cookies?"));
686 error("unknown do parameter");
690 sub userlink ($) { #{{{
693 eval q{use CGI 'escapeHTML'};
695 if ($user =~ m!^https?://! &&
696 eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
697 # Munge user-urls, as used by eg, OpenID.
698 my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
699 my $display=$oid->display;
700 # Convert "user.somehost.com" to "user [somehost.com]".
701 if ($display !~ /\[/) {
702 $display=~s/^(.*?)\.([^.]+\.[a-z]+)$/$1 [$2]/;
704 # Convert "http://somehost.com/user" to "user [somehost.com]".
705 if ($display !~ /\[/) {
706 $display=~s/^https?:\/\/(.+)\/([^\/]+)$/$2 [$1]/;
708 $display=~s!^https?://!!; # make sure this is removed
709 return "<a href=\"$user\">".escapeHTML($display)."</a>";
712 return htmllink("", "", escapeHTML(
713 length $config{userdir} ? $config{userdir}."/".$user : $user
714 ), noimageinline => 1);