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 #translators: The first parameter is a page name,
50 #translators: second is the user who locked it.
51 error(sprintf(gettext("%s is locked by %s and cannot be edited"),
52 htmllink("", "", $page, 1),
60 sub decode_form_utf8 ($) { #{{{
62 foreach my $f ($form->field) {
63 next if Encode::is_utf8(scalar $form->field($f));
64 $form->field(name => $f,
65 value => decode_utf8($form->field($f)),
71 sub cgi_recentchanges ($) { #{{{
74 # Optimisation: building recentchanges means calculating lots of
75 # links. Memoizing htmllink speeds it up a lot (can't be memoized
76 # during page builds as the return values may change, but they
82 eval q{use Time::Duration};
85 my $changelog=[rcs_recentchanges(100)];
86 foreach my $change (@$changelog) {
87 $change->{when} = concise(ago($change->{when}));
89 $change->{user} = userlink($change->{user});
91 my $is_excess = exists $change->{pages}[10]; # limit pages to first 10
92 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
95 $_->{link} = htmllink("", "", $_->{page}, 1);
99 push @{$change->{pages}}, { link => '...' } if $is_excess;
102 my $template=template("recentchanges.tmpl");
104 title => "RecentChanges",
105 indexlink => indexlink(),
106 wikiname => $config{wikiname},
107 changelog => $changelog,
108 baseurl => baseurl(),
110 run_hooks(pagetemplate => sub {
111 shift->(page => "", destpage => "", template => $template);
113 print $q->header(-charset => 'utf-8'), $template->output;
116 sub cgi_signin ($$) { #{{{
120 eval q{use CGI::FormBuilder};
122 my $form = CGI::FormBuilder->new(
130 action => $config{cgiurl},
132 template => (-e "$config{templatedir}/signin.tmpl" ?
133 {template_params("signin.tmpl")} : ""),
134 stylesheet => baseurl()."style.css",
136 my $buttons=["Login"];
138 $form->field(name => "do", type => "hidden");
140 if ($q->param("do") ne "signin" && !$form->submitted) {
141 $form->text(gettext("You need to log in first."));
144 run_hooks(formbuilder_setup => sub {
145 shift->(form => $form, cgi => $q, session => $session);
148 decode_form_utf8($form);
150 if (exists $hooks{formbuilder}) {
151 run_hooks(formbuilder => sub {
152 shift->(form => $form, cgi => $q, session => $session,
153 buttons => $buttons);
157 if ($form->submitted) {
160 printheader($session);
161 print misctemplate($form->title, $form->render(submit => $buttons));
165 sub cgi_postsignin ($$) { #{{{
169 # Continue with whatever was being done before the signin process.
170 if (defined $q->param("do") && $q->param("do") ne "signin" &&
171 defined $session->param("postsignin")) {
172 my $postsignin=CGI->new($session->param("postsignin"));
173 $session->clear("postsignin");
174 cgi($postsignin, $session);
175 cgi_savesession($session);
179 redirect($q, $config{url});
183 sub cgi_prefs ($$) { #{{{
187 eval q{use CGI::FormBuilder};
189 my $form = CGI::FormBuilder->new(
190 title => "preferences",
200 action => $config{cgiurl},
201 template => (-e "$config{templatedir}/prefs.tmpl" ?
202 {template_params("prefs.tmpl")} : ""),
203 stylesheet => baseurl()."style.css",
205 my $buttons=["Save Preferences", "Logout", "Cancel"];
207 run_hooks(formbuilder_setup => sub {
208 shift->(form => $form, cgi => $q, session => $session);
211 $form->field(name => "do", type => "hidden");
212 $form->field(name => "email", size => 50);
213 $form->field(name => "subscriptions", size => 50,
214 comment => "(".htmllink("", "", "PageSpec", 1).")");
215 $form->field(name => "locked_pages", size => 50,
216 comment => "(".htmllink("", "", "PageSpec", 1).")");
217 $form->field(name => "banned_users", size => 50);
219 my $user_name=$session->param("name");
220 if (! is_admin($user_name)) {
221 $form->field(name => "locked_pages", type => "hidden");
222 $form->field(name => "banned_users", type => "hidden");
225 if (! $form->submitted) {
226 $form->field(name => "email", force => 1,
227 value => userinfo_get($user_name, "email"));
228 $form->field(name => "subscriptions", force => 1,
229 value => userinfo_get($user_name, "subscriptions"));
230 $form->field(name => "locked_pages", force => 1,
231 value => userinfo_get($user_name, "locked_pages"));
232 if (is_admin($user_name)) {
233 $form->field(name => "banned_users", force => 1,
234 value => join(" ", get_banned_users()));
238 decode_form_utf8($form);
240 if ($form->submitted eq 'Logout') {
242 redirect($q, $config{url});
245 elsif ($form->submitted eq 'Cancel') {
246 redirect($q, $config{url});
249 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
250 foreach my $field (qw(email subscriptions locked_pages)) {
251 if (defined $form->field($field) && length $form->field($field)) {
252 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
255 if (is_admin($user_name)) {
256 set_banned_users(grep { ! is_admin($_) }
257 split(' ', $form->field("banned_users")));
259 $form->text(gettext("Preferences saved."));
262 if (exists $hooks{formbuilder}) {
263 run_hooks(formbuilder => sub {
264 shift->(form => $form, cgi => $q, session => $session,
265 buttons => $buttons);
269 printheader($session);
270 print misctemplate($form->title, $form->render(submit => $buttons));
274 sub cgi_editpage ($$) { #{{{
278 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
279 my @buttons=("Save Page", "Preview", "Cancel");
281 eval q{use CGI::FormBuilder};
283 my $form = CGI::FormBuilder->new(
289 editcontent => '/.+/',
291 required => [qw{editcontent}],
294 action => $config{cgiurl},
296 template => (-e "$config{templatedir}/editpage.tmpl" ?
297 {template_params("editpage.tmpl")} : ""),
300 run_hooks(formbuilder_setup => sub {
301 shift->(form => $form, cgi => $q, session => $session);
304 decode_form_utf8($form);
306 # This untaint is safe because titlepage removes any problematic
308 my ($page)=$form->field('page');
309 $page=titlepage(possibly_foolish_untaint($page));
310 if (! defined $page || ! length $page || file_pruned($page, $config{srcdir}) || $page=~/^\//) {
311 error("bad page name");
315 if (defined $form->field('from')) {
316 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
321 if (exists $pagesources{$page}) {
322 $file=$pagesources{$page};
323 $type=pagetype($file);
326 $type=$form->param('type');
327 if (defined $type && length $type && $hooks{htmlize}{$type}) {
328 $type=possibly_foolish_untaint($type);
330 elsif (defined $from) {
331 # favor the type of linking page
332 $type=pagetype($pagesources{$from});
334 $type=$config{default_pageext} unless defined $type;
335 $file=$page.".".$type;
339 if (! -e "$config{srcdir}/$file") {
343 $form->field(name => "do", type => 'hidden');
344 $form->field(name => "from", type => 'hidden');
345 $form->field(name => "rcsinfo", type => 'hidden');
346 $form->field(name => "subpage", type => 'hidden');
347 $form->field(name => "page", value => $page, force => 1);
348 $form->field(name => "type", value => $type, force => 1);
349 $form->field(name => "comments", type => "text", size => 80);
350 $form->field(name => "editcontent", type => "textarea", rows => 20,
352 $form->tmpl_param("can_commit", $config{rcs});
353 $form->tmpl_param("indexlink", indexlink());
354 $form->tmpl_param("helponformattinglink",
355 htmllink("", "", "HelpOnFormatting", 1));
356 $form->tmpl_param("baseurl", baseurl());
357 if (! $form->submitted) {
358 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
362 if ($form->submitted eq "Cancel") {
363 if ($newfile && defined $from) {
364 redirect($q, "$config{url}/".htmlpage($from));
367 redirect($q, $config{url});
370 redirect($q, "$config{url}/".htmlpage($page));
374 elsif ($form->submitted eq "Preview") {
375 my $content=$form->field('editcontent');
376 my $comments=$form->field('comments');
377 $form->field(name => "editcontent",
378 value => $content, force => 1);
379 $form->field(name => "comments",
380 value => $comments, force => 1);
381 $config{rss}=$config{atom}=0; # avoid preview writing a feed!
382 $form->tmpl_param("page_preview",
383 htmlize($page, $type,
385 preprocess($page, $page,
386 filter($page, $content)))));
389 $form->tmpl_param("page_preview", "");
391 $form->tmpl_param("page_conflict", "");
393 if (! $form->submitted || $form->submitted eq "Preview" ||
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};
428 ! exists $pagecase{lc $_} &&
429 ! page_locked($_, $session, 1)
433 # hmm, someone else made the page in the
435 redirect($q, "$config{url}/".htmlpage($page));
440 if (exists $hooks{htmlize}) {
441 @page_types=keys %{$hooks{htmlize}};
444 $form->tmpl_param("page_select", 1);
445 $form->field(name => "page", type => 'select',
446 options => \@page_locs, value => $best_loc);
447 $form->field(name => "type", type => 'select',
448 options => \@page_types);
449 $form->title(sprintf(gettext("creating %s"), pagetitle($page)));
451 elsif ($form->field("do") eq "edit") {
452 page_locked($page, $session);
453 if (! defined $form->field('editcontent') ||
454 ! length $form->field('editcontent')) {
456 if (exists $pagesources{$page}) {
457 $content=readfile(srcfile($pagesources{$page}));
458 $content=~s/\n/\r\n/g;
460 $form->field(name => "editcontent", value => $content,
463 $form->tmpl_param("page_select", 0);
464 $form->field(name => "page", type => 'hidden');
465 $form->field(name => "type", type => 'hidden');
466 $form->title(sprintf(gettext("editing %s"), pagetitle($page)));
469 print $form->render(submit => \@buttons);
473 page_locked($page, $session);
475 my $content=$form->field('editcontent');
477 $content=~s/\r\n/\n/g;
479 writefile($file, $config{srcdir}, $content);
483 if (defined $form->field('comments') &&
484 length $form->field('comments')) {
485 $message=$form->field('comments');
491 # prevent deadlock with post-commit hook
493 # presumably the commit will trigger an update
495 my $conflict=rcs_commit($file, $message,
496 $form->field("rcsinfo"),
497 $session->param("name"), $ENV{REMOTE_ADDR});
499 if (defined $conflict) {
500 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
502 $form->tmpl_param("page_conflict", 1);
503 $form->field("editcontent", value => $conflict, force => 1);
504 $form->field(name => "comments", value => $form->field('comments'), force => 1);
505 $form->field("do", "edit)");
506 $form->tmpl_param("page_select", 0);
507 $form->field(name => "page", type => 'hidden');
508 $form->field(name => "type", type => 'hidden');
509 $form->title(sprintf(gettext("editing %s"), $page));
510 print $form->render(submit => \@buttons);
515 require IkiWiki::Render;
520 # The trailing question mark tries to avoid broken
521 # caches and get the most recent version of the page.
522 redirect($q, "$config{url}/".htmlpage($page)."?updated");
526 sub cgi_getsession ($) { #{{{
529 eval q{use CGI::Session};
530 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
532 my $oldmask=umask(077);
533 my $session = CGI::Session->new("driver:DB_File", $q,
534 { FileName => "$config{wikistatedir}/sessions.db" });
540 sub cgi_savesession ($) { #{{{
543 # Force session flush with safe umask.
544 my $oldmask=umask(077);
559 run_hooks(cgi => sub { shift->($q) });
562 my $do=$q->param('do');
563 if (! defined $do || ! length $do) {
564 my $error = $q->cgi_error;
566 error("Request not processed: $error");
569 error("\"do\" parameter missing");
573 # Things that do not need a session.
574 if ($do eq 'recentchanges') {
575 cgi_recentchanges($q);
578 elsif ($do eq 'hyperestraier') {
582 # Need to lock the wiki before getting a session.
586 $session=cgi_getsession($q);
589 # Auth hooks can sign a user in.
590 if ($do ne 'signin' && ! defined $session->param("name")) {
591 run_hooks(auth => sub {
592 shift->($q, $session)
594 if (defined $session->param("name")) {
595 # Make sure whatever user was authed is in the
597 if (! userinfo_get($session->param("name"), "regdate")) {
598 userinfo_setall($session->param("name"), {
607 # Everything below this point needs the user to be signed in.
608 if (((! $config{anonok} || $do eq 'prefs') &&
609 (! defined $session->param("name") ||
610 ! userinfo_get($session->param("name"), "regdate")))
611 || $do eq 'signin') {
612 if ($do ne 'signin' && ! defined $session->param("postsignin")) {
613 $session->param(postsignin => $ENV{QUERY_STRING});
615 cgi_signin($q, $session);
616 cgi_savesession($session);
619 elsif (defined $session->param("postsignin")) {
620 cgi_postsignin($q, $session);
623 if (defined $session->param("name") && userinfo_get($session->param("name"), "banned")) {
624 print $q->header(-status => "403 Forbidden");
626 print gettext("You are banned.");
627 cgi_savesession($session);
631 if ($do eq 'create' || $do eq 'edit') {
632 cgi_editpage($q, $session);
634 elsif ($do eq 'prefs') {
635 cgi_prefs($q, $session);
637 elsif ($do eq 'blog') {
638 my $page=titlepage(decode_utf8($q->param('title')));
639 $page=~s/(\/)/"__".ord($1)."__"/eg; # escape slashes too
640 # if the page already exists, munge it to be unique
641 my $from=$q->param('from');
643 while (exists $pagecase{lc "$from/$page$add"}) {
644 $add=1 unless length $add;
647 $q->param('page', $page.$add);
648 # now run same as create
649 $q->param('do', 'create');
650 cgi_editpage($q, $session);
653 error("unknown do parameter");
657 sub userlink ($) { #{{{
660 eval q{use CGI 'escapeHTML'};
662 if ($user =~ m!^https?://! &&
663 eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
664 # Munge user-urls, as used by eg, OpenID.
665 my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
666 my $display=$oid->display;
667 # Convert "user.somehost.com" to "user [somehost.com]".
668 if ($display !~ /\[/) {
669 $display=~s/^(.*?)\.([^.]+\.[a-z]+)$/$1 [$2]/;
671 # Convert "http://somehost.com/user" to "user [somehost.com]".
672 if ($display !~ /\[/) {
673 $display=~s/^https?:\/\/(.+)\/([^\/]+)$/$2 [$1]/;
675 $display=~s!^https?://!!; # make sure this is removed
676 return "<a href=\"$user\">".escapeHTML($display)."</a>";
679 return htmllink("", "", escapeHTML(
680 length $config{userdir} ? $config{userdir}."/".$user : $user