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);
174 redirect($q, $config{url});
178 sub cgi_prefs ($$) { #{{{
182 eval q{use CGI::FormBuilder};
184 my $form = CGI::FormBuilder->new(
185 title => "preferences",
195 action => $config{cgiurl},
196 template => (-e "$config{templatedir}/prefs.tmpl" ?
197 {template_params("prefs.tmpl")} : ""),
198 stylesheet => baseurl()."style.css",
200 my $buttons=["Save Preferences", "Logout", "Cancel"];
202 run_hooks(formbuilder_setup => sub {
203 shift->(form => $form, cgi => $q, session => $session);
206 $form->field(name => "do", type => "hidden");
207 $form->field(name => "email", size => 50);
208 $form->field(name => "subscriptions", size => 50,
209 comment => "(".htmllink("", "", "PageSpec", 1).")");
210 $form->field(name => "locked_pages", size => 50,
211 comment => "(".htmllink("", "", "PageSpec", 1).")");
212 $form->field(name => "banned_users", size => 50);
214 my $user_name=$session->param("name");
215 if (! is_admin($user_name)) {
216 $form->field(name => "locked_pages", type => "hidden");
217 $form->field(name => "banned_users", type => "hidden");
220 if (! $form->submitted) {
221 $form->field(name => "email", force => 1,
222 value => userinfo_get($user_name, "email"));
223 $form->field(name => "subscriptions", force => 1,
224 value => userinfo_get($user_name, "subscriptions"));
225 $form->field(name => "locked_pages", force => 1,
226 value => userinfo_get($user_name, "locked_pages"));
227 if (is_admin($user_name)) {
228 $form->field(name => "banned_users", force => 1,
229 value => join(" ", get_banned_users()));
233 decode_form_utf8($form);
235 if ($form->submitted eq 'Logout') {
237 redirect($q, $config{url});
240 elsif ($form->submitted eq 'Cancel') {
241 redirect($q, $config{url});
244 elsif ($form->submitted eq "Save Preferences" && $form->validate) {
245 foreach my $field (qw(email subscriptions locked_pages)) {
246 if (defined $form->field($field) && length $form->field($field)) {
247 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
250 if (is_admin($user_name)) {
251 set_banned_users(grep { ! is_admin($_) }
252 split(' ', $form->field("banned_users")));
254 $form->text("Preferences saved.");
257 if (exists $hooks{formbuilder}) {
258 run_hooks(formbuilder => sub {
259 shift->(form => $form, cgi => $q, session => $session,
260 buttons => $buttons);
264 printheader($session);
265 print misctemplate($form->title, $form->render(submit => $buttons));
269 sub cgi_editpage ($$) { #{{{
273 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
274 my @buttons=("Save Page", "Preview", "Cancel");
276 eval q{use CGI::FormBuilder; use CGI::FormBuilder::Template::HTML};
278 my $renderer=CGI::FormBuilder::Template::HTML->new(
280 template_params("editpage.tmpl"),
282 run_hooks(pagetemplate => sub {
283 shift->(page => "", destpage => "", template => $renderer->engine);
285 my $form = CGI::FormBuilder->new(
291 editcontent => '/.+/',
293 required => [qw{editcontent}],
296 action => $config{cgiurl},
298 template => $renderer,
301 run_hooks(formbuilder_setup => sub {
302 shift->(form => $form, cgi => $q, session => $session);
305 decode_form_utf8($form);
307 # This untaint is safe because titlepage removes any problematic
309 my ($page)=$form->field('page');
310 $page=titlepage(possibly_foolish_untaint($page));
311 if (! defined $page || ! length $page ||
312 $page=~/$config{wiki_file_prune_regexp}/ || $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);
328 $type=$form->param('type');
329 if (defined $type && length $type && $hooks{htmlize}{$type}) {
330 $type=possibly_foolish_untaint($type);
332 elsif (defined $from) {
333 # favor the type of linking page
334 $type=pagetype($pagesources{$from});
336 $type=$config{default_pageext} unless defined $type;
337 $file=$page.".".$type;
341 if (! -e "$config{srcdir}/$file") {
345 $form->field(name => "do", type => 'hidden');
346 $form->field(name => "from", type => 'hidden');
347 $form->field(name => "rcsinfo", type => 'hidden');
348 $form->field(name => "subpage", type => 'hidden');
349 $form->field(name => "page", value => $page, force => 1);
350 $form->field(name => "type", value => $type, force => 1);
351 $form->field(name => "comments", type => "text", size => 80);
352 $form->field(name => "editcontent", type => "textarea", rows => 20,
354 $form->tmpl_param("can_commit", $config{rcs});
355 $form->tmpl_param("indexlink", indexlink());
356 $form->tmpl_param("helponformattinglink",
357 htmllink("", "", "HelpOnFormatting", 1));
358 $form->tmpl_param("baseurl", baseurl());
359 if (! $form->submitted) {
360 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
364 if ($form->submitted eq "Cancel") {
365 if ($newfile && defined $from) {
366 redirect($q, "$config{url}/".htmlpage($from));
369 redirect($q, $config{url});
372 redirect($q, "$config{url}/".htmlpage($page));
376 elsif ($form->submitted eq "Preview") {
377 my $content=$form->field('editcontent');
378 my $comments=$form->field('comments');
379 $form->field(name => "editcontent",
380 value => $content, force => 1);
381 $form->field(name => "comments",
382 value => $comments, force => 1);
383 $config{rss}=$config{atom}=0; # avoid preview writing a feed!
384 $form->tmpl_param("page_preview",
385 htmlize($page, $type,
387 preprocess($page, $page,
388 filter($page, $content)))));
391 $form->tmpl_param("page_preview", "");
393 $form->tmpl_param("page_conflict", "");
395 if (! $form->submitted || $form->submitted eq "Preview" ||
397 if ($form->field("do") eq "create") {
400 if (! defined $from || ! length $from ||
401 $from ne $form->field('from') ||
402 $from=~/$config{wiki_file_prune_regexp}/ ||
404 $form->submitted eq "Preview") {
405 @page_locs=$best_loc=$page;
411 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
412 $page eq 'discussion') {
413 $best_loc="$from/$page";
416 $best_loc=$dir.$page;
419 push @page_locs, $dir.$page;
420 push @page_locs, "$from/$page";
421 while (length $dir) {
423 push @page_locs, $dir.$page;
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("creating ".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("editing ".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);
481 my $message="web commit ";
482 if (defined $session->param("name") &&
483 length $session->param("name")) {
484 $message.="by ".$session->param("name");
487 $message.="from $ENV{REMOTE_ADDR}";
489 if (defined $form->field('comments') &&
490 length $form->field('comments')) {
491 $message.=": ".$form->field('comments');
498 # prevent deadlock with post-commit hook
500 # presumably the commit will trigger an update
502 my $conflict=rcs_commit($file, $message,
503 $form->field("rcsinfo"));
505 if (defined $conflict) {
506 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
508 $form->tmpl_param("page_conflict", 1);
509 $form->field("editcontent", value => $conflict, force => 1);
510 $form->field(name => "comments", value => $form->field('comments'), force => 1);
511 $form->field("do", "edit)");
512 $form->tmpl_param("page_select", 0);
513 $form->field(name => "page", type => 'hidden');
514 $form->field(name => "type", type => 'hidden');
515 $form->title("editing $page");
516 print $form->render(submit => \@buttons);
521 require IkiWiki::Render;
526 # The trailing question mark tries to avoid broken
527 # caches and get the most recent version of the page.
528 redirect($q, "$config{url}/".htmlpage($page)."?updated");
537 eval q{use CGI; use CGI::Session};
542 run_hooks(cgi => sub { shift->($q) });
545 my $do=$q->param('do');
546 if (! defined $do || ! length $do) {
547 my $error = $q->cgi_error;
549 error("Request not processed: $error");
552 error("\"do\" parameter missing");
556 # Things that do not need a session.
557 if ($do eq 'recentchanges') {
558 cgi_recentchanges($q);
561 elsif ($do eq 'hyperestraier') {
565 # Need to lock the wiki before getting a session.
569 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
571 my $oldmask=umask(077);
572 $session = CGI::Session->new("driver:DB_File", $q,
573 { FileName => "$config{wikistatedir}/sessions.db" });
577 # Auth hooks can sign a user in.
578 if ($do ne 'signin' && ! defined $session->param("name")) {
579 run_hooks(auth => sub {
580 shift->($q, $session)
582 if (defined $session->param("name")) {
583 # Make sure whatever user was authed is in the
585 if (! userinfo_get($session->param("name"), "regdate")) {
586 userinfo_setall($session->param("name"), {
595 # Everything below this point needs the user to be signed in.
596 if (((! $config{anonok} || $do eq 'prefs') &&
597 (! defined $session->param("name") ||
598 ! userinfo_get($session->param("name"), "regdate")))
599 || $do eq 'signin') {
600 if ($do ne 'signin' && ! defined $session->param("postsignin")) {
601 $session->param(postsignin => $ENV{QUERY_STRING});
603 cgi_signin($q, $session);
605 # Force session flush with safe umask.
606 my $oldmask=umask(077);
612 elsif (defined $session->param("postsignin")) {
613 cgi_postsignin($q, $session);
616 if (defined $session->param("name") && userinfo_get($session->param("name"), "banned")) {
617 print $q->header(-status => "403 Forbidden");
619 print "You are banned.";
623 if ($do eq 'create' || $do eq 'edit') {
624 cgi_editpage($q, $session);
626 elsif ($do eq 'prefs') {
627 cgi_prefs($q, $session);
629 elsif ($do eq 'blog') {
630 my $page=titlepage(decode_utf8($q->param('title')));
631 # if the page already exists, munge it to be unique
632 my $from=$q->param('from');
634 while (exists $pagecase{lc "$from/$page$add"}) {
635 $add=1 unless length $add;
638 $q->param('page', $page.$add);
639 # now run same as create
640 $q->param('do', 'create');
641 cgi_editpage($q, $session);
644 error("unknown do parameter");
648 sub userlink ($) { #{{{
651 eval q{use CGI 'escapeHTML'};
653 if ($user =~ m!^https?://! &&
654 eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
655 # Munge user-urls, as used by eg, OpenID.
656 my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
657 return "<a href=\"$user\">".escapeHTML($oid->display)."</a>";
660 return htmllink("", "", escapeHTML($user), 1);