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;
48 error(htmllink("", "", $page, 1)." is locked by ".
49 userlink($admin)." and cannot be edited.");
56 sub decode_form_utf8 ($) { #{{{
58 foreach my $f ($form->field) {
59 next if Encode::is_utf8(scalar $form->field($f));
60 $form->field(name => $f,
61 value => decode_utf8($form->field($f)),
67 sub cgi_recentchanges ($) { #{{{
70 # Optimisation: building recentchanges means calculating lots of
71 # links. Memoizing htmllink speeds it up a lot (can't be memoized
72 # during page builds as the return values may change, but they
78 eval q{use Time::Duration};
81 my $changelog=[rcs_recentchanges(100)];
82 foreach my $change (@$changelog) {
83 $change->{when} = concise(ago($change->{when}));
85 $change->{user} = userlink($change->{user});
87 my $is_excess = exists $change->{pages}[10]; # limit pages to first 10
88 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
91 $_->{link} = htmllink("", "", $_->{page}, 1);
95 push @{$change->{pages}}, { link => '...' } if $is_excess;
98 my $template=template("recentchanges.tmpl");
100 title => "RecentChanges",
101 indexlink => indexlink(),
102 wikiname => $config{wikiname},
103 changelog => $changelog,
104 baseurl => baseurl(),
106 run_hooks(pagetemplate => sub {
107 shift->(page => "", destpage => "", template => $template);
109 print $q->header(-charset => 'utf-8'), $template->output;
112 sub cgi_signin ($$) { #{{{
116 eval q{use CGI::FormBuilder};
118 my $form = CGI::FormBuilder->new(
126 action => $config{cgiurl},
128 template => (-e "$config{templatedir}/signin.tmpl" ?
129 {template_params("signin.tmpl")} : ""),
130 stylesheet => baseurl()."style.css",
132 my $buttons=["Login"];
134 $form->field(name => "do", type => "hidden");
136 if ($q->param("do") ne "signin" && !$form->submitted) {
137 $form->text("You need to log in first.");
140 run_hooks(formbuilder_setup => sub {
141 shift->(form => $form, cgi => $q, session => $session);
144 decode_form_utf8($form);
146 if (exists $hooks{formbuilder}) {
147 run_hooks(formbuilder => sub {
148 shift->(form => $form, cgi => $q, session => $session,
149 buttons => $buttons);
153 if ($form->submitted) {
156 printheader($session);
157 print misctemplate($form->title, $form->render(submit => $buttons));
161 sub cgi_postsignin ($$) { #{{{
165 # Continue with whatever was being done before the signin process.
166 if (defined $q->param("do") && $q->param("do") ne "signin" &&
167 defined $session->param("postsignin")) {
168 my $postsignin=CGI->new($session->param("postsignin"));
169 $session->clear("postsignin");
170 cgi($postsignin, $session);
171 cgi_savesession($session);
175 redirect($q, $config{url});
179 sub cgi_prefs ($$) { #{{{
183 eval q{use CGI::FormBuilder};
185 my $form = CGI::FormBuilder->new(
186 title => "preferences",
196 action => $config{cgiurl},
197 template => (-e "$config{templatedir}/prefs.tmpl" ?
198 {template_params("prefs.tmpl")} : ""),
199 stylesheet => baseurl()."style.css",
201 my $buttons=["Save Preferences", "Logout", "Cancel"];
203 run_hooks(formbuilder_setup => sub {
204 shift->(form => $form, cgi => $q, session => $session);
207 $form->field(name => "do", type => "hidden");
208 $form->field(name => "email", size => 50);
209 $form->field(name => "subscriptions", size => 50,
210 comment => "(".htmllink("", "", "PageSpec", 1).")");
211 $form->field(name => "locked_pages", size => 50,
212 comment => "(".htmllink("", "", "PageSpec", 1).")");
213 $form->field(name => "banned_users", size => 50);
215 my $user_name=$session->param("name");
216 if (! is_admin($user_name)) {
217 $form->field(name => "locked_pages", type => "hidden");
218 $form->field(name => "banned_users", type => "hidden");
221 if (! $form->submitted) {
222 $form->field(name => "email", force => 1,
223 value => userinfo_get($user_name, "email"));
224 $form->field(name => "subscriptions", force => 1,
225 value => userinfo_get($user_name, "subscriptions"));
226 $form->field(name => "locked_pages", force => 1,
227 value => userinfo_get($user_name, "locked_pages"));
228 if (is_admin($user_name)) {
229 $form->field(name => "banned_users", force => 1,
230 value => join(" ", get_banned_users()));
234 decode_form_utf8($form);
236 if ($form->submitted eq 'Logout') {
238 redirect($q, $config{url});
241 elsif ($form->submitted eq 'Cancel') {
242 redirect($q, $config{url});
245 elsif ($form->submitted eq "Save Preferences" && $form->validate) {
246 foreach my $field (qw(email subscriptions locked_pages)) {
247 if (defined $form->field($field) && length $form->field($field)) {
248 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
251 if (is_admin($user_name)) {
252 set_banned_users(grep { ! is_admin($_) }
253 split(' ', $form->field("banned_users")));
255 $form->text("Preferences saved.");
258 if (exists $hooks{formbuilder}) {
259 run_hooks(formbuilder => sub {
260 shift->(form => $form, cgi => $q, session => $session,
261 buttons => $buttons);
265 printheader($session);
266 print misctemplate($form->title, $form->render(submit => $buttons));
270 sub cgi_editpage ($$) { #{{{
274 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
275 my @buttons=("Save Page", "Preview", "Cancel");
277 eval q{use CGI::FormBuilder};
279 my $form = CGI::FormBuilder->new(
285 editcontent => '/.+/',
287 required => [qw{editcontent}],
290 action => $config{cgiurl},
292 template => (-e "$config{templatedir}/editpage.tmpl" ?
293 {template_params("editpage.tmpl")} : ""),
296 run_hooks(formbuilder_setup => sub {
297 shift->(form => $form, cgi => $q, session => $session);
300 decode_form_utf8($form);
302 # This untaint is safe because titlepage removes any problematic
304 my ($page)=$form->field('page');
305 $page=titlepage(possibly_foolish_untaint($page));
306 if (! defined $page || ! length $page || file_pruned($page, $config{srcdir}) || $page=~/^\//) {
307 error("bad page name");
311 if (defined $form->field('from')) {
312 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
317 if (exists $pagesources{$page}) {
318 $file=$pagesources{$page};
319 $type=pagetype($file);
322 $type=$form->param('type');
323 if (defined $type && length $type && $hooks{htmlize}{$type}) {
324 $type=possibly_foolish_untaint($type);
326 elsif (defined $from) {
327 # favor the type of linking page
328 $type=pagetype($pagesources{$from});
330 $type=$config{default_pageext} unless defined $type;
331 $file=$page.".".$type;
335 if (! -e "$config{srcdir}/$file") {
339 $form->field(name => "do", type => 'hidden');
340 $form->field(name => "from", type => 'hidden');
341 $form->field(name => "rcsinfo", type => 'hidden');
342 $form->field(name => "subpage", type => 'hidden');
343 $form->field(name => "page", value => $page, force => 1);
344 $form->field(name => "type", value => $type, force => 1);
345 $form->field(name => "comments", type => "text", size => 80);
346 $form->field(name => "editcontent", type => "textarea", rows => 20,
348 $form->tmpl_param("can_commit", $config{rcs});
349 $form->tmpl_param("indexlink", indexlink());
350 $form->tmpl_param("helponformattinglink",
351 htmllink("", "", "HelpOnFormatting", 1));
352 $form->tmpl_param("baseurl", baseurl());
353 if (! $form->submitted) {
354 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
358 if ($form->submitted eq "Cancel") {
359 if ($newfile && defined $from) {
360 redirect($q, "$config{url}/".htmlpage($from));
363 redirect($q, $config{url});
366 redirect($q, "$config{url}/".htmlpage($page));
370 elsif ($form->submitted eq "Preview") {
371 my $content=$form->field('editcontent');
372 my $comments=$form->field('comments');
373 $form->field(name => "editcontent",
374 value => $content, force => 1);
375 $form->field(name => "comments",
376 value => $comments, force => 1);
377 $config{rss}=$config{atom}=0; # avoid preview writing a feed!
378 $form->tmpl_param("page_preview",
379 htmlize($page, $type,
381 preprocess($page, $page,
382 filter($page, $content)))));
385 $form->tmpl_param("page_preview", "");
387 $form->tmpl_param("page_conflict", "");
389 if (! $form->submitted || $form->submitted eq "Preview" ||
391 if ($form->field("do") eq "create") {
394 if (! defined $from || ! length $from ||
395 $from ne $form->field('from') ||
396 file_pruned($from, $config{srcdir}) ||
398 $form->submitted eq "Preview") {
399 @page_locs=$best_loc=$page;
405 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
406 $page eq 'discussion') {
407 $best_loc="$from/$page";
410 $best_loc=$dir.$page;
413 push @page_locs, $dir.$page;
414 push @page_locs, "$from/$page";
415 while (length $dir) {
417 push @page_locs, $dir.$page;
422 ! exists $pagecase{lc $_} &&
423 ! page_locked($_, $session, 1)
427 # hmm, someone else made the page in the
429 redirect($q, "$config{url}/".htmlpage($page));
434 if (exists $hooks{htmlize}) {
435 @page_types=keys %{$hooks{htmlize}};
438 $form->tmpl_param("page_select", 1);
439 $form->field(name => "page", type => 'select',
440 options => \@page_locs, value => $best_loc);
441 $form->field(name => "type", type => 'select',
442 options => \@page_types);
443 $form->title("creating ".pagetitle($page));
445 elsif ($form->field("do") eq "edit") {
446 page_locked($page, $session);
447 if (! defined $form->field('editcontent') ||
448 ! length $form->field('editcontent')) {
450 if (exists $pagesources{$page}) {
451 $content=readfile(srcfile($pagesources{$page}));
452 $content=~s/\n/\r\n/g;
454 $form->field(name => "editcontent", value => $content,
457 $form->tmpl_param("page_select", 0);
458 $form->field(name => "page", type => 'hidden');
459 $form->field(name => "type", type => 'hidden');
460 $form->title("editing ".pagetitle($page));
463 print $form->render(submit => \@buttons);
467 page_locked($page, $session);
469 my $content=$form->field('editcontent');
471 $content=~s/\r\n/\n/g;
473 writefile($file, $config{srcdir}, $content);
477 if (defined $form->field('comments') &&
478 length $form->field('comments')) {
479 $message=$form->field('comments');
485 # prevent deadlock with post-commit hook
487 # presumably the commit will trigger an update
489 my $conflict=rcs_commit($file, $message,
490 $form->field("rcsinfo"),
491 $session->param("name"), $ENV{REMOTE_ADDR});
493 if (defined $conflict) {
494 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
496 $form->tmpl_param("page_conflict", 1);
497 $form->field("editcontent", value => $conflict, force => 1);
498 $form->field(name => "comments", value => $form->field('comments'), force => 1);
499 $form->field("do", "edit)");
500 $form->tmpl_param("page_select", 0);
501 $form->field(name => "page", type => 'hidden');
502 $form->field(name => "type", type => 'hidden');
503 $form->title("editing $page");
504 print $form->render(submit => \@buttons);
509 require IkiWiki::Render;
514 # The trailing question mark tries to avoid broken
515 # caches and get the most recent version of the page.
516 redirect($q, "$config{url}/".htmlpage($page)."?updated");
520 sub cgi_getsession ($) { #{{{
523 eval q{use CGI::Session};
524 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
526 my $oldmask=umask(077);
527 my $session = CGI::Session->new("driver:DB_File", $q,
528 { FileName => "$config{wikistatedir}/sessions.db" });
534 sub cgi_savesession ($) { #{{{
537 # Force session flush with safe umask.
538 my $oldmask=umask(077);
553 run_hooks(cgi => sub { shift->($q) });
556 my $do=$q->param('do');
557 if (! defined $do || ! length $do) {
558 my $error = $q->cgi_error;
560 error("Request not processed: $error");
563 error("\"do\" parameter missing");
567 # Things that do not need a session.
568 if ($do eq 'recentchanges') {
569 cgi_recentchanges($q);
572 elsif ($do eq 'hyperestraier') {
576 # Need to lock the wiki before getting a session.
580 $session=cgi_getsession($q);
583 # Auth hooks can sign a user in.
584 if ($do ne 'signin' && ! defined $session->param("name")) {
585 run_hooks(auth => sub {
586 shift->($q, $session)
588 if (defined $session->param("name")) {
589 # Make sure whatever user was authed is in the
591 if (! userinfo_get($session->param("name"), "regdate")) {
592 userinfo_setall($session->param("name"), {
601 # Everything below this point needs the user to be signed in.
602 if (((! $config{anonok} || $do eq 'prefs') &&
603 (! defined $session->param("name") ||
604 ! userinfo_get($session->param("name"), "regdate")))
605 || $do eq 'signin') {
606 if ($do ne 'signin' && ! defined $session->param("postsignin")) {
607 $session->param(postsignin => $ENV{QUERY_STRING});
609 cgi_signin($q, $session);
610 cgi_savesession($session);
613 elsif (defined $session->param("postsignin")) {
614 cgi_postsignin($q, $session);
617 if (defined $session->param("name") && userinfo_get($session->param("name"), "banned")) {
618 print $q->header(-status => "403 Forbidden");
620 print "You are banned.";
621 cgi_savesession($session);
625 if ($do eq 'create' || $do eq 'edit') {
626 cgi_editpage($q, $session);
628 elsif ($do eq 'prefs') {
629 cgi_prefs($q, $session);
631 elsif ($do eq 'blog') {
632 my $page=titlepage(decode_utf8($q->param('title')));
633 # if the page already exists, munge it to be unique
634 my $from=$q->param('from');
636 while (exists $pagecase{lc "$from/$page$add"}) {
637 $add=1 unless length $add;
640 $q->param('page', $page.$add);
641 # now run same as create
642 $q->param('do', 'create');
643 cgi_editpage($q, $session);
646 error("unknown do parameter");
650 sub userlink ($) { #{{{
653 eval q{use CGI 'escapeHTML'};
655 if ($user =~ m!^https?://! &&
656 eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
657 # Munge user-urls, as used by eg, OpenID.
658 my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
659 my $display=$oid->display;
660 # Convert "user.somehost.com" to "user [somehost.com]".
661 if ($display !~ /\[/) {
662 $display=~s/^(.*?)\.([^.]+\.[a-z]+)$/$1 [$2]/;
664 $display=~s!^https?://!!; # make sure this is removed
665 return "<a href=\"$user\">".escapeHTML($display)."</a>";
668 return htmllink("", "", escapeHTML(
669 length $config{userdir} ? $config{userdir}."/".$user : $user