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 page_locked ($$;$) { #{{{
41 my $user=$session->param("name");
42 return if defined $user && is_admin($user);
44 foreach my $admin (@{$config{adminuser}}) {
45 my $locked_pages=userinfo_get($admin, "locked_pages");
46 if (pagespec_match($page, userinfo_get($admin, "locked_pages"))) {
47 return 1 if $nonfatal;
49 error(sprintf(gettext("%s is locked by %s and cannot be edited"),
50 htmllink("", "", $page, 1),
58 sub decode_form_utf8 ($) { #{{{
60 foreach my $f ($form->field) {
61 next if Encode::is_utf8(scalar $form->field($f));
62 $form->field(name => $f,
63 value => decode_utf8($form->field($f)),
69 sub cgi_recentchanges ($) { #{{{
72 # Optimisation: building recentchanges means calculating lots of
73 # links. Memoizing htmllink speeds it up a lot (can't be memoized
74 # during page builds as the return values may change, but they
80 eval q{use Time::Duration};
83 my $changelog=[rcs_recentchanges(100)];
84 foreach my $change (@$changelog) {
85 $change->{when} = concise(ago($change->{when}));
87 $change->{user} = userlink($change->{user});
89 my $is_excess = exists $change->{pages}[10]; # limit pages to first 10
90 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
93 $_->{link} = htmllink("", "", $_->{page}, 1);
97 push @{$change->{pages}}, { link => '...' } if $is_excess;
100 my $template=template("recentchanges.tmpl");
102 title => "RecentChanges",
103 indexlink => indexlink(),
104 wikiname => $config{wikiname},
105 changelog => $changelog,
106 baseurl => baseurl(),
108 run_hooks(pagetemplate => sub {
109 shift->(page => "", destpage => "", template => $template);
111 print $q->header(-charset => 'utf-8'), $template->output;
114 sub cgi_signin ($$) { #{{{
118 eval q{use CGI::FormBuilder};
120 my $form = CGI::FormBuilder->new(
128 action => $config{cgiurl},
130 template => (-e "$config{templatedir}/signin.tmpl" ?
131 {template_params("signin.tmpl")} : ""),
132 stylesheet => baseurl()."style.css",
134 my $buttons=["Login"];
136 $form->field(name => "do", type => "hidden");
138 if ($q->param("do") ne "signin" && !$form->submitted) {
139 $form->text(gettext("You need to log in first."));
142 run_hooks(formbuilder_setup => sub {
143 shift->(form => $form, cgi => $q, session => $session);
146 decode_form_utf8($form);
148 if (exists $hooks{formbuilder}) {
149 run_hooks(formbuilder => sub {
150 shift->(form => $form, cgi => $q, session => $session,
151 buttons => $buttons);
155 if ($form->submitted) {
158 printheader($session);
159 print misctemplate($form->title, $form->render(submit => $buttons));
163 sub cgi_postsignin ($$) { #{{{
167 # Continue with whatever was being done before the signin process.
168 if (defined $q->param("do") && $q->param("do") ne "signin" &&
169 defined $session->param("postsignin")) {
170 my $postsignin=CGI->new($session->param("postsignin"));
171 $session->clear("postsignin");
172 cgi($postsignin, $session);
173 cgi_savesession($session);
177 redirect($q, $config{url});
181 sub cgi_prefs ($$) { #{{{
185 eval q{use CGI::FormBuilder};
187 my $form = CGI::FormBuilder->new(
188 title => "preferences",
198 action => $config{cgiurl},
199 template => (-e "$config{templatedir}/prefs.tmpl" ?
200 {template_params("prefs.tmpl")} : ""),
201 stylesheet => baseurl()."style.css",
203 my $buttons=["Save Preferences", "Logout", "Cancel"];
205 run_hooks(formbuilder_setup => sub {
206 shift->(form => $form, cgi => $q, session => $session);
209 $form->field(name => "do", type => "hidden");
210 $form->field(name => "email", size => 50);
211 $form->field(name => "subscriptions", size => 50,
212 comment => "(".htmllink("", "", "PageSpec", 1).")");
213 $form->field(name => "locked_pages", size => 50,
214 comment => "(".htmllink("", "", "PageSpec", 1).")");
215 $form->field(name => "banned_users", size => 50);
217 my $user_name=$session->param("name");
218 if (! is_admin($user_name)) {
219 $form->field(name => "locked_pages", type => "hidden");
220 $form->field(name => "banned_users", type => "hidden");
223 if (! $form->submitted) {
224 $form->field(name => "email", force => 1,
225 value => userinfo_get($user_name, "email"));
226 $form->field(name => "subscriptions", force => 1,
227 value => userinfo_get($user_name, "subscriptions"));
228 $form->field(name => "locked_pages", force => 1,
229 value => userinfo_get($user_name, "locked_pages"));
230 if (is_admin($user_name)) {
231 $form->field(name => "banned_users", force => 1,
232 value => join(" ", get_banned_users()));
236 decode_form_utf8($form);
238 if ($form->submitted eq 'Logout') {
240 redirect($q, $config{url});
243 elsif ($form->submitted eq 'Cancel') {
244 redirect($q, $config{url});
247 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
248 foreach my $field (qw(email subscriptions locked_pages)) {
249 if (defined $form->field($field) && length $form->field($field)) {
250 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
253 if (is_admin($user_name)) {
254 set_banned_users(grep { ! is_admin($_) }
255 split(' ', $form->field("banned_users")));
257 $form->text(gettext("Preferences saved."));
260 if (exists $hooks{formbuilder}) {
261 run_hooks(formbuilder => sub {
262 shift->(form => $form, cgi => $q, session => $session,
263 buttons => $buttons);
267 printheader($session);
268 print misctemplate($form->title, $form->render(submit => $buttons));
272 sub cgi_editpage ($$) { #{{{
276 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
277 my @buttons=("Save Page", "Preview", "Cancel");
279 eval q{use CGI::FormBuilder};
281 my $form = CGI::FormBuilder->new(
287 editcontent => '/.+/',
289 required => [qw{editcontent}],
292 action => $config{cgiurl},
294 template => (-e "$config{templatedir}/editpage.tmpl" ?
295 {template_params("editpage.tmpl")} : ""),
298 run_hooks(formbuilder_setup => sub {
299 shift->(form => $form, cgi => $q, session => $session);
302 decode_form_utf8($form);
304 # This untaint is safe because titlepage removes any problematic
306 my ($page)=$form->field('page');
307 $page=titlepage(possibly_foolish_untaint($page));
308 if (! defined $page || ! length $page || file_pruned($page, $config{srcdir}) || $page=~/^\//) {
309 error("bad page name");
313 if (defined $form->field('from')) {
314 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
319 if (exists $pagesources{$page}) {
320 $file=$pagesources{$page};
321 $type=pagetype($file);
324 $type=$form->param('type');
325 if (defined $type && length $type && $hooks{htmlize}{$type}) {
326 $type=possibly_foolish_untaint($type);
328 elsif (defined $from) {
329 # favor the type of linking page
330 $type=pagetype($pagesources{$from});
332 $type=$config{default_pageext} unless defined $type;
333 $file=$page.".".$type;
337 if (! -e "$config{srcdir}/$file") {
341 $form->field(name => "do", type => 'hidden');
342 $form->field(name => "from", type => 'hidden');
343 $form->field(name => "rcsinfo", type => 'hidden');
344 $form->field(name => "subpage", type => 'hidden');
345 $form->field(name => "page", value => $page, force => 1);
346 $form->field(name => "type", value => $type, force => 1);
347 $form->field(name => "comments", type => "text", size => 80);
348 $form->field(name => "editcontent", type => "textarea", rows => 20,
350 $form->tmpl_param("can_commit", $config{rcs});
351 $form->tmpl_param("indexlink", indexlink());
352 $form->tmpl_param("helponformattinglink",
353 htmllink("", "", "HelpOnFormatting", 1));
354 $form->tmpl_param("baseurl", baseurl());
355 if (! $form->submitted) {
356 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
360 if ($form->submitted eq "Cancel") {
361 if ($newfile && defined $from) {
362 redirect($q, "$config{url}/".htmlpage($from));
365 redirect($q, $config{url});
368 redirect($q, "$config{url}/".htmlpage($page));
372 elsif ($form->submitted eq "Preview") {
373 my $content=$form->field('editcontent');
374 my $comments=$form->field('comments');
375 $form->field(name => "editcontent",
376 value => $content, force => 1);
377 $form->field(name => "comments",
378 value => $comments, force => 1);
379 $config{rss}=$config{atom}=0; # avoid preview writing a feed!
380 $form->tmpl_param("page_preview",
381 htmlize($page, $type,
383 preprocess($page, $page,
384 filter($page, $content)))));
387 $form->tmpl_param("page_preview", "");
389 $form->tmpl_param("page_conflict", "");
391 if (! $form->submitted || $form->submitted eq "Preview" ||
393 if ($form->field("do") eq "create") {
396 if (! defined $from || ! length $from ||
397 $from ne $form->field('from') ||
398 file_pruned($from, $config{srcdir}) ||
400 $form->submitted eq "Preview") {
401 @page_locs=$best_loc=$page;
407 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
408 $page eq gettext('discussion')) {
409 $best_loc="$from/$page";
412 $best_loc=$dir.$page;
415 push @page_locs, $dir.$page;
416 push @page_locs, "$from/$page";
417 while (length $dir) {
419 push @page_locs, $dir.$page;
422 push @page_locs, "$config{userdir}/$page"
423 if length $config{userdir};
426 ! exists $pagecase{lc $_} &&
427 ! page_locked($_, $session, 1)
431 # hmm, someone else made the page in the
433 redirect($q, "$config{url}/".htmlpage($page));
438 if (exists $hooks{htmlize}) {
439 @page_types=keys %{$hooks{htmlize}};
442 $form->tmpl_param("page_select", 1);
443 $form->field(name => "page", type => 'select',
444 options => \@page_locs, value => $best_loc);
445 $form->field(name => "type", type => 'select',
446 options => \@page_types);
447 $form->title(sprintf(gettext("creating %s"), pagetitle($page)));
449 elsif ($form->field("do") eq "edit") {
450 page_locked($page, $session);
451 if (! defined $form->field('editcontent') ||
452 ! length $form->field('editcontent')) {
454 if (exists $pagesources{$page}) {
455 $content=readfile(srcfile($pagesources{$page}));
456 $content=~s/\n/\r\n/g;
458 $form->field(name => "editcontent", value => $content,
461 $form->tmpl_param("page_select", 0);
462 $form->field(name => "page", type => 'hidden');
463 $form->field(name => "type", type => 'hidden');
464 $form->title(sprintf(gettext("editing %s"), pagetitle($page)));
467 print $form->render(submit => \@buttons);
471 page_locked($page, $session);
473 my $content=$form->field('editcontent');
475 $content=~s/\r\n/\n/g;
477 writefile($file, $config{srcdir}, $content);
481 if (defined $form->field('comments') &&
482 length $form->field('comments')) {
483 $message=$form->field('comments');
489 # prevent deadlock with post-commit hook
491 # presumably the commit will trigger an update
493 my $conflict=rcs_commit($file, $message,
494 $form->field("rcsinfo"),
495 $session->param("name"), $ENV{REMOTE_ADDR});
497 if (defined $conflict) {
498 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
500 $form->tmpl_param("page_conflict", 1);
501 $form->field("editcontent", value => $conflict, force => 1);
502 $form->field(name => "comments", value => $form->field('comments'), force => 1);
503 $form->field("do", "edit)");
504 $form->tmpl_param("page_select", 0);
505 $form->field(name => "page", type => 'hidden');
506 $form->field(name => "type", type => 'hidden');
507 $form->title(sprintf(gettext("editing %s"), $page));
508 print $form->render(submit => \@buttons);
513 require IkiWiki::Render;
518 # The trailing question mark tries to avoid broken
519 # caches and get the most recent version of the page.
520 redirect($q, "$config{url}/".htmlpage($page)."?updated");
524 sub cgi_getsession ($) { #{{{
527 eval q{use CGI::Session};
528 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
530 my $oldmask=umask(077);
531 my $session = CGI::Session->new("driver:DB_File", $q,
532 { FileName => "$config{wikistatedir}/sessions.db" });
538 sub cgi_savesession ($) { #{{{
541 # Force session flush with safe umask.
542 my $oldmask=umask(077);
557 run_hooks(cgi => sub { shift->($q) });
560 my $do=$q->param('do');
561 if (! defined $do || ! length $do) {
562 my $error = $q->cgi_error;
564 error("Request not processed: $error");
567 error("\"do\" parameter missing");
571 # Things that do not need a session.
572 if ($do eq 'recentchanges') {
573 cgi_recentchanges($q);
576 elsif ($do eq 'hyperestraier') {
580 # Need to lock the wiki before getting a session.
584 $session=cgi_getsession($q);
587 # Auth hooks can sign a user in.
588 if ($do ne 'signin' && ! defined $session->param("name")) {
589 run_hooks(auth => sub {
590 shift->($q, $session)
592 if (defined $session->param("name")) {
593 # Make sure whatever user was authed is in the
595 if (! userinfo_get($session->param("name"), "regdate")) {
596 userinfo_setall($session->param("name"), {
605 # Everything below this point needs the user to be signed in.
606 if (((! $config{anonok} || $do eq 'prefs') &&
607 (! defined $session->param("name") ||
608 ! userinfo_get($session->param("name"), "regdate")))
609 || $do eq 'signin') {
610 if ($do ne 'signin' && ! defined $session->param("postsignin")) {
611 $session->param(postsignin => $ENV{QUERY_STRING});
613 cgi_signin($q, $session);
614 cgi_savesession($session);
617 elsif (defined $session->param("postsignin")) {
618 cgi_postsignin($q, $session);
621 if (defined $session->param("name") && userinfo_get($session->param("name"), "banned")) {
622 print $q->header(-status => "403 Forbidden");
624 print gettext("You are banned.");
625 cgi_savesession($session);
629 if ($do eq 'create' || $do eq 'edit') {
630 cgi_editpage($q, $session);
632 elsif ($do eq 'prefs') {
633 cgi_prefs($q, $session);
635 elsif ($do eq 'blog') {
636 my $page=titlepage(decode_utf8($q->param('title')));
637 $page=~s/(\/)/"__".ord($1)."__"/eg; # escape slashes too
638 # if the page already exists, munge it to be unique
639 my $from=$q->param('from');
641 while (exists $pagecase{lc "$from/$page$add"}) {
642 $add=1 unless length $add;
645 $q->param('page', $page.$add);
646 # now run same as create
647 $q->param('do', 'create');
648 cgi_editpage($q, $session);
651 error("unknown do parameter");
655 sub userlink ($) { #{{{
658 eval q{use CGI 'escapeHTML'};
660 if ($user =~ m!^https?://! &&
661 eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
662 # Munge user-urls, as used by eg, OpenID.
663 my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
664 my $display=$oid->display;
665 # Convert "user.somehost.com" to "user [somehost.com]".
666 if ($display !~ /\[/) {
667 $display=~s/^(.*?)\.([^.]+\.[a-z]+)$/$1 [$2]/;
669 # Convert "http://somehost.com/user" to "user [somehost.com]".
670 if ($display !~ /\[/) {
671 $display=~s/^https?:\/\/(.+)\/([^\/]+)$/$2 [$1]/;
673 $display=~s!^https?://!!; # make sure this is removed
674 return "<a href=\"$user\">".escapeHTML($display)."</a>";
677 return htmllink("", "", escapeHTML(
678 length $config{userdir} ? $config{userdir}."/".$user : $user