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}, 1, 0, pagetitle($_->{page}));
96 push @{$change->{pages}}, { link => '...' } if $is_excess;
99 my $template=template("recentchanges.tmpl");
101 title => "RecentChanges",
102 indexlink => indexlink(),
103 wikiname => $config{wikiname},
104 changelog => $changelog,
105 baseurl => baseurl(),
107 run_hooks(pagetemplate => sub {
108 shift->(page => "", destpage => "", template => $template);
110 print $q->header(-charset => 'utf-8'), $template->output;
113 # Check if the user is signed in. If not, redirect to the signin form and
114 # save their place to return to later.
115 sub needsignin ($$) { #{{{
119 if (! defined $session->param("name") ||
120 ! userinfo_get($session->param("name"), "regdate")) {
121 if (! defined $session->param("postsignin")) {
122 $session->param(postsignin => $ENV{QUERY_STRING});
124 cgi_signin($q, $session);
125 cgi_savesession($session);
130 sub cgi_signin ($$) { #{{{
134 eval q{use CGI::FormBuilder};
136 my $form = CGI::FormBuilder->new(
144 action => $config{cgiurl},
146 template => scalar template_params("signin.tmpl"),
147 stylesheet => baseurl()."style.css",
149 my $buttons=["Login"];
151 if ($q->param("do") ne "signin" && !$form->submitted) {
152 $form->text(gettext("You need to log in first."));
154 $form->field(name => "do", type => "hidden", value => "signin",
157 run_hooks(formbuilder_setup => sub {
158 shift->(form => $form, cgi => $q, session => $session);
161 decode_form_utf8($form);
163 if (exists $hooks{formbuilder}) {
164 run_hooks(formbuilder => sub {
165 shift->(form => $form, cgi => $q, session => $session,
166 buttons => $buttons);
170 if ($form->submitted) {
173 printheader($session);
174 print misctemplate($form->title, $form->render(submit => $buttons));
178 sub cgi_postsignin ($$) { #{{{
182 # Continue with whatever was being done before the signin process.
183 my $postsignin=CGI->new($session->param("postsignin"));
184 $session->clear("postsignin");
185 cgi($postsignin, $session);
186 cgi_savesession($session);
190 sub cgi_prefs ($$) { #{{{
194 needsignin($q, $session);
196 eval q{use CGI::FormBuilder};
198 my $form = CGI::FormBuilder->new(
199 title => "preferences",
209 action => $config{cgiurl},
210 template => scalar template_params("prefs.tmpl"),
211 stylesheet => baseurl()."style.css",
213 my $buttons=["Save Preferences", "Logout", "Cancel"];
215 run_hooks(formbuilder_setup => sub {
216 shift->(form => $form, cgi => $q, session => $session);
219 $form->field(name => "do", type => "hidden");
220 $form->field(name => "email", size => 50);
221 $form->field(name => "subscriptions", size => 50,
222 comment => "(".htmllink("", "", "PageSpec", 1).")");
223 $form->field(name => "banned_users", size => 50);
225 my $user_name=$session->param("name");
226 if (! is_admin($user_name)) {
227 $form->field(name => "banned_users", type => "hidden");
230 if (! $form->submitted) {
231 $form->field(name => "email", force => 1,
232 value => userinfo_get($user_name, "email"));
233 $form->field(name => "subscriptions", force => 1,
234 value => userinfo_get($user_name, "subscriptions"));
235 if (is_admin($user_name)) {
236 $form->field(name => "banned_users", force => 1,
237 value => join(" ", get_banned_users()));
241 decode_form_utf8($form);
243 if ($form->submitted eq 'Logout') {
245 redirect($q, $config{url});
248 elsif ($form->submitted eq 'Cancel') {
249 redirect($q, $config{url});
252 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
253 foreach my $field (qw(email subscriptions)) {
254 if (defined $form->field($field) && length $form->field($field)) {
255 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
258 if (is_admin($user_name)) {
259 set_banned_users(grep { ! is_admin($_) }
260 split(' ', $form->field("banned_users")));
262 $form->text(gettext("Preferences saved."));
265 if (exists $hooks{formbuilder}) {
266 run_hooks(formbuilder => sub {
267 shift->(form => $form, cgi => $q, session => $session,
268 buttons => $buttons);
272 printheader($session);
273 print misctemplate($form->title, $form->render(submit => $buttons));
277 sub cgi_editpage ($$) { #{{{
281 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
282 my @buttons=("Save Page", "Preview", "Cancel");
284 eval q{use CGI::FormBuilder};
286 my $form = CGI::FormBuilder->new(
292 editcontent => '/.+/',
294 required => [qw{editcontent}],
297 action => $config{cgiurl},
299 template => scalar template_params("editpage.tmpl"),
302 run_hooks(formbuilder_setup => sub {
303 shift->(form => $form, cgi => $q, session => $session);
306 decode_form_utf8($form);
308 # This untaint is safe because titlepage removes any problematic
310 my ($page)=$form->field('page');
311 $page=titlepage(possibly_foolish_untaint($page));
312 if (! defined $page || ! length $page || file_pruned($page, $config{srcdir}) || $page=~/^\//) {
313 error("bad page name");
317 if (defined $form->field('from')) {
318 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
323 if (exists $pagesources{$page}) {
324 $file=$pagesources{$page};
325 $type=pagetype($file);
326 if (! defined $type) {
327 error(sprintf(gettext("%s is not an editable page"), $page));
331 $type=$form->param('type');
332 if (defined $type && length $type && $hooks{htmlize}{$type}) {
333 $type=possibly_foolish_untaint($type);
335 elsif (defined $from) {
336 # favor the type of linking page
337 $type=pagetype($pagesources{$from});
339 $type=$config{default_pageext} unless defined $type;
340 $file=$page.".".$type;
344 if (! -e "$config{srcdir}/$file") {
348 $form->field(name => "do", type => 'hidden');
349 $form->field(name => "from", type => 'hidden');
350 $form->field(name => "rcsinfo", type => 'hidden');
351 $form->field(name => "subpage", type => 'hidden');
352 $form->field(name => "page", value => $page, force => 1);
353 $form->field(name => "type", value => $type, force => 1);
354 $form->field(name => "comments", type => "text", size => 80);
355 $form->field(name => "editcontent", type => "textarea", rows => 20,
357 $form->tmpl_param("can_commit", $config{rcs});
358 $form->tmpl_param("indexlink", indexlink());
359 $form->tmpl_param("helponformattinglink",
360 htmllink("", "", "HelpOnFormatting", 1));
361 $form->tmpl_param("baseurl", baseurl());
362 if (! $form->submitted) {
363 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
367 if ($form->submitted eq "Cancel") {
368 if ($newfile && defined $from) {
369 redirect($q, "$config{url}/".htmlpage($from));
372 redirect($q, $config{url});
375 redirect($q, "$config{url}/".htmlpage($page));
379 elsif ($form->submitted eq "Preview") {
380 my $content=$form->field('editcontent');
381 my $comments=$form->field('comments');
382 $form->field(name => "editcontent",
383 value => $content, force => 1);
384 $form->field(name => "comments",
385 value => $comments, force => 1);
386 $config{rss}=$config{atom}=0; # avoid preview writing a feed!
387 $form->tmpl_param("page_preview",
388 htmlize($page, $type,
390 preprocess($page, $page,
391 filter($page, $content)))));
394 $form->tmpl_param("page_preview", "");
396 $form->tmpl_param("page_conflict", "");
398 if (! $form->submitted || $form->submitted eq "Preview" ||
400 if ($form->field("do") eq "create") {
403 if (! defined $from || ! length $from ||
404 $from ne $form->field('from') ||
405 file_pruned($from, $config{srcdir}) ||
407 $form->submitted eq "Preview") {
408 @page_locs=$best_loc=$page;
414 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
415 $page eq gettext('discussion')) {
416 $best_loc="$from/$page";
419 $best_loc=$dir.$page;
422 push @page_locs, $dir.$page;
423 push @page_locs, "$from/$page";
424 while (length $dir) {
426 push @page_locs, $dir.$page;
429 push @page_locs, "$config{userdir}/$page"
430 if length $config{userdir};
433 ! exists $pagecase{lc $_}
436 # hmm, someone else made the page in the
438 redirect($q, "$config{url}/".htmlpage($page));
442 my @editable_locs = grep {
443 check_canedit($_, $q, $session, 1)
445 if (! @editable_locs) {
446 # let it throw an error this time
447 map { check_canedit($_, $q, $session) } @page_locs;
451 if (exists $hooks{htmlize}) {
452 @page_types=keys %{$hooks{htmlize}};
455 $form->tmpl_param("page_select", 1);
456 $form->field(name => "page", type => 'select',
457 options => \@editable_locs, value => $best_loc);
458 $form->field(name => "type", type => 'select',
459 options => \@page_types);
460 $form->title(sprintf(gettext("creating %s"), pagetitle($page)));
462 elsif ($form->field("do") eq "edit") {
463 check_canedit($page, $q, $session);
464 if (! defined $form->field('editcontent') ||
465 ! length $form->field('editcontent')) {
467 if (exists $pagesources{$page}) {
468 $content=readfile(srcfile($pagesources{$page}));
469 $content=~s/\n/\r\n/g;
471 $form->field(name => "editcontent", value => $content,
474 $form->tmpl_param("page_select", 0);
475 $form->field(name => "page", type => 'hidden');
476 $form->field(name => "type", type => 'hidden');
477 $form->title(sprintf(gettext("editing %s"), pagetitle($page)));
480 print $form->render(submit => \@buttons);
484 check_canedit($page, $q, $session);
486 my $content=$form->field('editcontent');
488 $content=~s/\r\n/\n/g;
490 writefile($file, $config{srcdir}, $content);
494 if (defined $form->field('comments') &&
495 length $form->field('comments')) {
496 $message=$form->field('comments');
502 # prevent deadlock with post-commit hook
504 # presumably the commit will trigger an update
506 my $conflict=rcs_commit($file, $message,
507 $form->field("rcsinfo"),
508 $session->param("name"), $ENV{REMOTE_ADDR});
510 if (defined $conflict) {
511 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
513 $form->tmpl_param("page_conflict", 1);
514 $form->field("editcontent", value => $conflict, force => 1);
515 $form->field(name => "comments", value => $form->field('comments'), force => 1);
516 $form->field("do", "edit)");
517 $form->tmpl_param("page_select", 0);
518 $form->field(name => "page", type => 'hidden');
519 $form->field(name => "type", type => 'hidden');
520 $form->title(sprintf(gettext("editing %s"), $page));
521 print $form->render(submit => \@buttons);
525 # Make sure that the repo is up-to-date;
526 # locking prevents the post-commit hook
532 require IkiWiki::Render;
537 # The trailing question mark tries to avoid broken
538 # caches and get the most recent version of the page.
539 redirect($q, "$config{url}/".htmlpage($page)."?updated");
543 sub cgi_getsession ($) { #{{{
546 eval q{use CGI::Session};
547 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
549 my $oldmask=umask(077);
550 my $session = CGI::Session->new("driver:DB_File", $q,
551 { FileName => "$config{wikistatedir}/sessions.db" });
557 sub cgi_savesession ($) { #{{{
560 # Force session flush with safe umask.
561 my $oldmask=umask(077);
576 run_hooks(cgi => sub { shift->($q) });
579 my $do=$q->param('do');
580 if (! defined $do || ! length $do) {
581 my $error = $q->cgi_error;
583 error("Request not processed: $error");
586 error("\"do\" parameter missing");
590 # Things that do not need a session.
591 if ($do eq 'recentchanges') {
592 cgi_recentchanges($q);
595 elsif ($do eq 'hyperestraier') {
599 # Need to lock the wiki before getting a session.
603 $session=cgi_getsession($q);
606 # Auth hooks can sign a user in.
607 if ($do ne 'signin' && ! defined $session->param("name")) {
608 run_hooks(auth => sub {
609 shift->($q, $session)
611 if (defined $session->param("name")) {
612 # Make sure whatever user was authed is in the
614 if (! userinfo_get($session->param("name"), "regdate")) {
615 userinfo_setall($session->param("name"), {
624 if (defined $session->param("name") &&
625 userinfo_get($session->param("name"), "banned")) {
626 print $q->header(-status => "403 Forbidden");
628 print gettext("You are banned.");
629 cgi_savesession($session);
631 elsif ($do eq 'signin') {
632 cgi_signin($q, $session);
633 cgi_savesession($session);
635 elsif (defined $session->param("postsignin")) {
636 cgi_postsignin($q, $session);
638 elsif ($do eq 'prefs') {
639 cgi_prefs($q, $session);
641 elsif ($do eq 'create' || $do eq 'edit') {
642 cgi_editpage($q, $session);
644 elsif ($do eq 'blog') {
645 my $page=titlepage(decode_utf8($q->param('title')));
646 $page=~s/(\/)/"__".ord($1)."__"/eg; # escape slashes too
647 # if the page already exists, munge it to be unique
648 my $from=$q->param('from');
650 while (exists $pagecase{lc "$from/$page$add"}) {
651 $add=1 unless length $add;
654 $q->param('page', $page.$add);
655 # now run same as create
656 $q->param('do', 'create');
657 cgi_editpage($q, $session);
659 elsif ($do eq 'postsignin') {
660 error(gettext("login failed, perhaps you need to turn on cookies?"));
663 error("unknown do parameter");
667 sub userlink ($) { #{{{
670 eval q{use CGI 'escapeHTML'};
672 if ($user =~ m!^https?://! &&
673 eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
674 # Munge user-urls, as used by eg, OpenID.
675 my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
676 my $display=$oid->display;
677 # Convert "user.somehost.com" to "user [somehost.com]".
678 if ($display !~ /\[/) {
679 $display=~s/^(.*?)\.([^.]+\.[a-z]+)$/$1 [$2]/;
681 # Convert "http://somehost.com/user" to "user [somehost.com]".
682 if ($display !~ /\[/) {
683 $display=~s/^https?:\/\/(.+)\/([^\/]+)$/$2 [$1]/;
685 $display=~s!^https?://!!; # make sure this is removed
686 return "<a href=\"$user\">".escapeHTML($display)."</a>";
689 return htmllink("", "", escapeHTML(
690 length $config{userdir} ? $config{userdir}."/".$user : $user