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 htmllink("", "", $admin, 1)." 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};
80 eval q{use CGI 'escapeHTML'};
83 my $changelog=[rcs_recentchanges(100)];
84 foreach my $change (@$changelog) {
85 $change->{when} = concise(ago($change->{when}));
87 if ($change->{user} =~ m!^https?://! &&
88 eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
89 # Munge user-urls, as used by eg, OpenID.
90 my $oid=Net::OpenID::VerifiedIdentity->new(identity => $change->{user});
91 $change->{user} = "<a href=\"".$change->{user}."\">".escapeHTML($oid->display)."</a>";
94 $change->{user} = htmllink("", "", escapeHTML($change->{user}), 1);
97 my $is_excess = exists $change->{pages}[10]; # limit pages to first 10
98 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
101 $_->{link} = htmllink("", "", $_->{page}, 1);
103 } @{$change->{pages}}
105 push @{$change->{pages}}, { link => '...' } if $is_excess;
108 my $template=template("recentchanges.tmpl");
110 title => "RecentChanges",
111 indexlink => indexlink(),
112 wikiname => $config{wikiname},
113 changelog => $changelog,
114 baseurl => baseurl(),
116 run_hooks(pagetemplate => sub {
117 shift->(page => "", destpage => "", template => $template);
119 print $q->header(-charset => 'utf-8'), $template->output;
122 sub cgi_signin ($$) { #{{{
126 eval q{use CGI::FormBuilder};
128 my $form = CGI::FormBuilder->new(
130 fields => [qw(do name password openid_url)],
135 confirm_password => {
136 perl => q{eq $form->field("password")},
143 action => $config{cgiurl},
145 template => (-e "$config{templatedir}/signin.tmpl" ?
146 {template_params("signin.tmpl")} : ""),
147 stylesheet => baseurl()."style.css",
150 decode_form_utf8($form);
152 $form->field(name => "name", required => 0, size => 30);
153 $form->field(name => "do", type => "hidden");
154 $form->field(name => "password", type => "password", required => 0);
155 if ($config{openid}) {
156 $form->field(name => "openid_url", label => "OpenID", size => 30,
158 htmllink("", "", "OpenID", 1, 0, "What's this?")
159 .($config{openidsignup} ? " | <a href=\"$config{openidsignup}\">Get an OpenID</a>" : "")
163 $form->field(name => "openid_url", type => "hidden");
165 if ($form->submitted eq "Register" || $form->submitted eq "Create Account") {
166 $form->title("register");
168 $form->fields(qw(do name password confirm_password email));
169 $form->field(name => "confirm_password", type => "password");
170 $form->field(name => "email", type => "text");
171 $form->field(name => "openid_url", type => "hidden");
173 if ($q->param("do") ne "signin" && !$form->submitted) {
174 $form->text("You need to log in first.");
177 if ($form->submitted) {
178 my $submittype=$form->submitted;
179 # OpenID login uses the Login button, but validates
181 if ($submittype eq "Login" && $config{openid} &&
182 length $form->field("openid_url")) {
183 $submittype="OpenID";
186 name => "openid_url",
189 IkiWiki::Plugin::openid::validate($q, $session, shift, $form);
194 # Set required fields based on how form was submitted.
196 "Login" => [qw(name password)],
198 "Create Account" => [qw(name password confirm_password email)],
199 "Mail Password" => [qw(name)],
200 "OpenID" => [qw(openid_url)],
202 foreach my $opt (@{$required{$submittype}}) {
203 $form->field(name => $opt, required => 1);
206 # Validate password differently depending on how
207 # form was submitted.
208 if ($submittype eq 'Login') {
212 length $form->field("name") &&
213 shift eq userinfo_get($form->field("name"), 'password');
216 $form->field(name => "name", validate => '/^\w+$/');
218 elsif ($submittype ne 'OpenID') {
219 $form->field(name => "password", validate => 'VALUE');
221 # And make sure the entered name exists when logging
222 # in or sending email, and does not when registering.
223 if ($submittype eq 'Create Account' ||
224 $submittype eq 'Register') {
230 $name=~/$config{wiki_file_regexp}/ &&
231 ! userinfo_get($name, "regdate");
235 elsif ($submittype ne 'OpenID') {
241 userinfo_get($name, "regdate");
247 # First time settings.
248 $form->field(name => "name", comment => "use FirstnameLastName");
249 if ($session->param("name")) {
250 $form->field(name => "name", value => $session->param("name"));
254 if ($form->submitted && $form->validate) {
255 if ($form->submitted eq 'Login') {
256 $session->param("name", $form->field("name"));
257 cgi_postsignin($q, $session);
259 elsif ($form->submitted eq 'Create Account') {
260 my $user_name=$form->field('name');
261 if (userinfo_setall($user_name, {
262 'email' => $form->field('email'),
263 'password' => $form->field('password'),
266 $form->field(name => "confirm_password", type => "hidden");
267 $form->field(name => "email", type => "hidden");
268 $form->text("Account creation successful. Now you can Login.");
269 printheader($session);
270 print misctemplate($form->title, $form->render(submit => ["Login"]));
273 error("Error creating account.");
276 elsif ($form->submitted eq 'Mail Password') {
277 my $user_name=$form->field("name");
278 my $template=template("passwordmail.tmpl");
280 user_name => $user_name,
281 user_password => userinfo_get($user_name, "password"),
282 wikiurl => $config{url},
283 wikiname => $config{wikiname},
284 REMOTE_ADDR => $ENV{REMOTE_ADDR},
287 eval q{use Mail::Sendmail};
290 To => userinfo_get($user_name, "email"),
291 From => "$config{wikiname} admin <$config{adminemail}>",
292 Subject => "$config{wikiname} information",
293 Message => $template->output,
294 ) or error("Failed to send mail");
296 $form->text("Your password has been emailed to you.");
297 $form->field(name => "name", required => 0);
298 printheader($session);
299 print misctemplate($form->title, $form->render(submit => ["Login", "Mail Password"]));
301 elsif ($form->submitted eq "Register") {
302 printheader($session);
303 print misctemplate($form->title, $form->render(submit => ["Create Account"]));
306 elsif ($form->submitted eq "Create Account") {
307 printheader($session);
308 print misctemplate($form->title, $form->render(submit => ["Create Account"]));
311 printheader($session);
312 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
316 sub cgi_postsignin ($$) { #{{{
320 # Continue with whatever was being done before the signin process.
321 if (defined $q->param("do") && $q->param("do") ne "signin" &&
322 defined $session->param("postsignin")) {
323 my $postsignin=CGI->new($session->param("postsignin"));
324 $session->clear("postsignin");
325 cgi($postsignin, $session);
329 redirect($q, $config{url});
333 sub cgi_prefs ($$) { #{{{
337 eval q{use CGI::FormBuilder};
339 my $form = CGI::FormBuilder->new(
340 title => "preferences",
341 fields => [qw(do name password confirm_password email
342 subscriptions locked_pages)],
347 confirm_password => {
348 perl => q{eq $form->field("password")},
355 action => $config{cgiurl},
356 template => (-e "$config{templatedir}/prefs.tmpl" ?
357 {template_params("prefs.tmpl")} : ""),
358 stylesheet => baseurl()."style.css",
360 my @buttons=("Save Preferences", "Logout", "Cancel");
362 my $user_name=$session->param("name");
363 $form->field(name => "do", type => "hidden");
364 $form->field(name => "name", disabled => 1,
365 value => $user_name, force => 1, size => 30);
366 $form->field(name => "password", type => "password");
367 $form->field(name => "confirm_password", type => "password");
368 $form->field(name => "subscriptions", size => 50,
369 comment => "(".htmllink("", "", "PageSpec", 1).")");
370 $form->field(name => "locked_pages", size => 50,
371 comment => "(".htmllink("", "", "PageSpec", 1).")");
372 $form->field(name => "banned_users", size => 50);
374 if (! is_admin($user_name)) {
375 $form->field(name => "locked_pages", type => "hidden");
376 $form->field(name => "banned_users", type => "hidden");
379 if ($config{httpauth}) {
380 $form->field(name => "password", type => "hidden");
381 $form->field(name => "confirm_password", type => "hidden");
384 if (! $form->submitted) {
385 $form->field(name => "email", force => 1,
386 value => userinfo_get($user_name, "email"));
387 $form->field(name => "subscriptions", force => 1,
388 value => userinfo_get($user_name, "subscriptions"));
389 $form->field(name => "locked_pages", force => 1,
390 value => userinfo_get($user_name, "locked_pages"));
391 if (is_admin($user_name)) {
392 $form->field(name => "banned_users", force => 1,
393 value => join(" ", get_banned_users()));
397 decode_form_utf8($form);
399 if ($form->submitted eq 'Logout') {
401 redirect($q, $config{url});
404 elsif ($form->submitted eq 'Cancel') {
405 redirect($q, $config{url});
408 elsif ($form->submitted eq "Save Preferences" && $form->validate) {
409 foreach my $field (qw(password email subscriptions locked_pages)) {
410 if (length $form->field($field)) {
411 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
414 if (is_admin($user_name)) {
415 set_banned_users(grep { ! is_admin($_) }
416 split(' ', $form->field("banned_users")));
418 $form->text("Preferences saved.");
421 printheader($session);
422 print misctemplate($form->title, $form->render(submit => \@buttons));
425 sub cgi_editpage ($$) { #{{{
429 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
430 my @buttons=("Save Page", "Preview", "Cancel");
432 eval q{use CGI::FormBuilder; use CGI::FormBuilder::Template::HTML};
434 my $renderer=CGI::FormBuilder::Template::HTML->new(
436 template_params("editpage.tmpl"),
438 run_hooks(pagetemplate => sub {
439 shift->(page => "", destpage => "", template => $renderer->engine);
441 my $form = CGI::FormBuilder->new(
447 editcontent => '/.+/',
449 required => [qw{editcontent}],
452 action => $config{cgiurl},
454 template => $renderer,
457 decode_form_utf8($form);
459 # This untaint is safe because titlepage removes any problematic
461 my ($page)=$form->field('page');
462 $page=titlepage(possibly_foolish_untaint($page));
463 if (! defined $page || ! length $page ||
464 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
465 error("bad page name");
469 if (defined $form->field('from')) {
470 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
475 if (exists $pagesources{$page}) {
476 $file=$pagesources{$page};
477 $type=pagetype($file);
480 $type=$form->param('type');
481 if (defined $type && length $type && $hooks{htmlize}{$type}) {
482 $type=possibly_foolish_untaint($type);
484 elsif (defined $from) {
485 # favor the type of linking page
486 $type=pagetype($pagesources{$from});
488 $type=$config{default_pageext} unless defined $type;
489 $file=$page.".".$type;
493 if (! -e "$config{srcdir}/$file") {
497 $form->field(name => "do", type => 'hidden');
498 $form->field(name => "from", type => 'hidden');
499 $form->field(name => "rcsinfo", type => 'hidden');
500 $form->field(name => "subpage", type => 'hidden');
501 $form->field(name => "page", value => $page, force => 1);
502 $form->field(name => "type", value => $type, force => 1);
503 $form->field(name => "comments", type => "text", size => 80);
504 $form->field(name => "editcontent", type => "textarea", rows => 20,
506 $form->tmpl_param("can_commit", $config{rcs});
507 $form->tmpl_param("indexlink", indexlink());
508 $form->tmpl_param("helponformattinglink",
509 htmllink("", "", "HelpOnFormatting", 1));
510 $form->tmpl_param("baseurl", baseurl());
511 if (! $form->submitted) {
512 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
516 if ($form->submitted eq "Cancel") {
517 if ($newfile && defined $from) {
518 redirect($q, "$config{url}/".htmlpage($from));
521 redirect($q, $config{url});
524 redirect($q, "$config{url}/".htmlpage($page));
528 elsif ($form->submitted eq "Preview") {
529 my $content=$form->field('editcontent');
530 my $comments=$form->field('comments');
531 $form->field(name => "editcontent",
532 value => $content, force => 1);
533 $form->field(name => "comments",
534 value => $comments, force => 1);
535 $config{rss}=$config{atom}=0; # avoid preview writing a feed!
536 $form->tmpl_param("page_preview",
537 htmlize($page, $type,
539 preprocess($page, $page,
540 filter($page, $content)))));
543 $form->tmpl_param("page_preview", "");
545 $form->tmpl_param("page_conflict", "");
547 if (! $form->submitted || $form->submitted eq "Preview" ||
549 if ($form->field("do") eq "create") {
552 if (! defined $from || ! length $from ||
553 $from ne $form->field('from') ||
554 $from=~/$config{wiki_file_prune_regexp}/ ||
556 $form->submitted eq "Preview") {
557 @page_locs=$best_loc=$page;
563 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
564 $page eq 'discussion') {
565 $best_loc="$from/$page";
568 $best_loc=$dir.$page;
571 push @page_locs, $dir.$page;
572 push @page_locs, "$from/$page";
573 while (length $dir) {
575 push @page_locs, $dir.$page;
580 ! exists $pagecase{lc $_} &&
581 ! page_locked($_, $session, 1)
585 # hmm, someone else made the page in the
587 redirect($q, "$config{url}/".htmlpage($page));
592 if (exists $hooks{htmlize}) {
593 @page_types=keys %{$hooks{htmlize}};
596 $form->tmpl_param("page_select", 1);
597 $form->field(name => "page", type => 'select',
598 options => \@page_locs, value => $best_loc);
599 $form->field(name => "type", type => 'select',
600 options => \@page_types);
601 $form->title("creating ".pagetitle($page));
603 elsif ($form->field("do") eq "edit") {
604 page_locked($page, $session);
605 if (! defined $form->field('editcontent') ||
606 ! length $form->field('editcontent')) {
608 if (exists $pagesources{$page}) {
609 $content=readfile(srcfile($pagesources{$page}));
610 $content=~s/\n/\r\n/g;
612 $form->field(name => "editcontent", value => $content,
615 $form->tmpl_param("page_select", 0);
616 $form->field(name => "page", type => 'hidden');
617 $form->field(name => "type", type => 'hidden');
618 $form->title("editing ".pagetitle($page));
621 print $form->render(submit => \@buttons);
625 page_locked($page, $session);
627 my $content=$form->field('editcontent');
629 $content=~s/\r\n/\n/g;
631 writefile($file, $config{srcdir}, $content);
633 my $message="web commit ";
634 if (defined $session->param("name") &&
635 length $session->param("name")) {
636 $message.="by ".$session->param("name");
639 $message.="from $ENV{REMOTE_ADDR}";
641 if (defined $form->field('comments') &&
642 length $form->field('comments')) {
643 $message.=": ".$form->field('comments');
650 # prevent deadlock with post-commit hook
652 # presumably the commit will trigger an update
654 my $conflict=rcs_commit($file, $message,
655 $form->field("rcsinfo"));
657 if (defined $conflict) {
658 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
660 $form->tmpl_param("page_conflict", 1);
661 $form->field("editcontent", value => $conflict, force => 1);
662 $form->field(name => "comments", value => $form->field('comments'), force => 1);
663 $form->field("do", "edit)");
664 $form->tmpl_param("page_select", 0);
665 $form->field(name => "page", type => 'hidden');
666 $form->field(name => "type", type => 'hidden');
667 $form->title("editing $page");
668 print $form->render(submit => \@buttons);
673 require IkiWiki::Render;
678 # The trailing question mark tries to avoid broken
679 # caches and get the most recent version of the page.
680 redirect($q, "$config{url}/".htmlpage($page)."?updated");
689 eval q{use CGI; use CGI::Session};
694 run_hooks(cgi => sub { shift->($q) });
697 my $do=$q->param('do');
698 if (! defined $do || ! length $do) {
699 my $error = $q->cgi_error;
701 error("Request not processed: $error");
704 error("\"do\" parameter missing");
708 # Things that do not need a session.
709 if ($do eq 'recentchanges') {
710 cgi_recentchanges($q);
713 elsif ($do eq 'hyperestraier') {
717 # Need to lock the wiki before getting a session.
721 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
723 my $oldmask=umask(077);
724 $session = CGI::Session->new("driver:DB_File", $q,
725 { FileName => "$config{wikistatedir}/sessions.db" });
729 # Auth hooks can sign a user in.
730 if ($do ne 'signin' && ! defined $session->param("name")) {
731 run_hooks(auth => sub {
732 shift->($q, $session)
734 if (defined $session->param("name")) {
735 # Make sure whatever user was authed is in the
737 if (! userinfo_get($session->param("name"), "regdate")) {
738 userinfo_setall($session->param("name"), {
747 # Everything below this point needs the user to be signed in.
748 if (((! $config{anonok} || $do eq 'prefs') &&
749 (! defined $session->param("name") ||
750 ! userinfo_get($session->param("name"), "regdate")))
751 || $do eq 'signin') {
752 if ($do ne 'signin' && ! defined $session->param("postsignin")) {
753 $session->param(postsignin => $ENV{QUERY_STRING});
755 cgi_signin($q, $session);
757 # Force session flush with safe umask.
758 my $oldmask=umask(077);
764 elsif (defined $session->param("postsignin")) {
765 cgi_postsignin($q, $session);
768 if (defined $session->param("name") && userinfo_get($session->param("name"), "banned")) {
769 print $q->header(-status => "403 Forbidden");
771 print "You are banned.";
775 if ($do eq 'create' || $do eq 'edit') {
776 cgi_editpage($q, $session);
778 elsif ($do eq 'prefs') {
779 cgi_prefs($q, $session);
781 elsif ($do eq 'blog') {
782 my $page=titlepage(decode_utf8($q->param('title')));
783 # if the page already exists, munge it to be unique
784 my $from=$q->param('from');
786 while (exists $pagecase{lc "$from/$page$add"}) {
787 $add=1 unless length $add;
790 $q->param('page', $page.$add);
791 # now run same as create
792 $q->param('do', 'create');
793 cgi_editpage($q, $session);
796 error("unknown do parameter");