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 ($) { #{{{
72 # Optimisation: building recentchanges means calculating lots of
73 # links. Memoizing htmllink speeds it up a lot (can't be memoized
74 # during page builds as the return values may change, but they
80 eval q{use Time::Duration};
82 eval q{use CGI 'escapeHTML'};
85 my $changelog=[rcs_recentchanges(100)];
86 foreach my $change (@$changelog) {
87 $change->{when} = concise(ago($change->{when}));
89 if ($change->{user} =~ m!^https?://! &&
90 eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
91 # Munge user-urls, as used by eg, OpenID.
92 my $oid=Net::OpenID::VerifiedIdentity->new(identity => $change->{user});
93 $change->{user} = "<a href=\"".$change->{user}."\">".escapeHTML($oid->display)."</a>";
96 $change->{user} = htmllink("", "", escapeHTML($change->{user}), 1);
99 my $is_excess = exists $change->{pages}[10]; # limit pages to first 10
100 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
103 $_->{link} = htmllink("", "", $_->{page}, 1);
105 } @{$change->{pages}}
107 push @{$change->{pages}}, { link => '...' } if $is_excess;
110 my $template=template("recentchanges.tmpl");
112 title => "RecentChanges",
113 indexlink => indexlink(),
114 wikiname => $config{wikiname},
115 changelog => $changelog,
116 baseurl => baseurl(),
118 run_hooks(pagetemplate => sub {
119 shift->(page => "", destpage => "", template => $template);
121 print $q->header(-charset => 'utf-8'), $template->output;
124 sub cgi_signin ($$) { #{{{
128 eval q{use CGI::FormBuilder};
130 my $form = CGI::FormBuilder->new(
132 fields => [qw(do title page subpage from name password openid_url)],
137 confirm_password => {
138 perl => q{eq $form->field("password")},
145 action => $config{cgiurl},
147 template => (-e "$config{templatedir}/signin.tmpl" ?
148 {template_params("signin.tmpl")} : ""),
149 stylesheet => baseurl()."style.css",
152 decode_form_utf8($form);
154 $form->field(name => "name", required => 0);
155 $form->field(name => "do", type => "hidden");
156 $form->field(name => "page", type => "hidden");
157 $form->field(name => "title", type => "hidden");
158 $form->field(name => "from", type => "hidden");
159 $form->field(name => "subpage", type => "hidden");
160 $form->field(name => "password", type => "password", required => 0);
161 if ($config{openid}) {
162 $form->field(name => "openid_url", label => "OpenID",
163 comment => '('.htmllink("", "", "OpenID", 1, 0, "What's this?").')');
166 $form->field(name => "openid_url", type => "hidden");
168 if ($form->submitted eq "Register" || $form->submitted eq "Create Account") {
169 $form->title("register");
171 $form->fields(qw(do title page subpage from name password confirm_password email));
172 $form->field(name => "confirm_password", type => "password");
173 $form->field(name => "email", type => "text");
174 $form->field(name => "openid_url", type => "hidden");
176 if ($q->param("do") ne "signin" && !$form->submitted) {
177 $form->text("You need to log in first.");
180 if ($form->submitted) {
181 my $submittype=$form->submitted;
182 # OpenID login uses the Login button, but validates
184 if ($submittype eq "Login" && $config{openid} &&
185 length $form->field("openid_url")) {
186 $submittype="OpenID";
189 name => "openid_url",
192 IkiWiki::Plugin::openid::validate($q, $session, $form, shift);
197 # Set required fields based on how form was submitted.
199 "Login" => [qw(name password)],
201 "Create Account" => [qw(name password confirm_password email)],
202 "Mail Password" => [qw(name)],
203 "OpenID" => [qw(openid_url)],
205 foreach my $opt (@{$required{$submittype}}) {
206 $form->field(name => $opt, required => 1);
209 # Validate password differently depending on how
210 # form was submitted.
211 if ($submittype eq 'Login') {
215 length $form->field("name") &&
216 shift eq userinfo_get($form->field("name"), 'password');
219 $form->field(name => "name", validate => '/^\w+$/');
221 elsif ($submittype ne 'OpenID') {
222 $form->field(name => "password", validate => 'VALUE');
224 # And make sure the entered name exists when logging
225 # in or sending email, and does not when registering.
226 if ($submittype eq 'Create Account' ||
227 $submittype eq 'Register') {
233 $name=~/$config{wiki_file_regexp}/ &&
234 ! userinfo_get($name, "regdate");
238 elsif ($submittype ne 'OpenID') {
244 userinfo_get($name, "regdate");
250 # First time settings.
251 $form->field(name => "name", comment => "use FirstnameLastName");
252 if ($session->param("name")) {
253 $form->field(name => "name", value => $session->param("name"));
257 if ($form->submitted && $form->validate) {
258 if ($form->submitted eq 'Login') {
259 $session->param("name", $form->field("name"));
260 if (defined $form->field("do") &&
261 $form->field("do") ne 'signin') {
263 do => $form->field("do"),
264 page => $form->field("page"),
265 title => $form->field("title"),
266 from => $form->field("from"),
267 subpage => $form->field("subpage"),
271 redirect($q, $config{url});
274 elsif ($form->submitted eq 'Create Account') {
275 my $user_name=$form->field('name');
276 if (userinfo_setall($user_name, {
277 'email' => $form->field('email'),
278 'password' => $form->field('password'),
281 $form->field(name => "confirm_password", type => "hidden");
282 $form->field(name => "email", type => "hidden");
283 $form->text("Account creation successful. Now you can Login.");
284 printheader($session);
285 print misctemplate($form->title, $form->render(submit => ["Login"]));
288 error("Error creating account.");
291 elsif ($form->submitted eq 'Mail Password') {
292 my $user_name=$form->field("name");
293 my $template=template("passwordmail.tmpl");
295 user_name => $user_name,
296 user_password => userinfo_get($user_name, "password"),
297 wikiurl => $config{url},
298 wikiname => $config{wikiname},
299 REMOTE_ADDR => $ENV{REMOTE_ADDR},
302 eval q{use Mail::Sendmail};
305 To => userinfo_get($user_name, "email"),
306 From => "$config{wikiname} admin <$config{adminemail}>",
307 Subject => "$config{wikiname} information",
308 Message => $template->output,
309 ) or error("Failed to send mail");
311 $form->text("Your password has been emailed to you.");
312 $form->field(name => "name", required => 0);
313 printheader($session);
314 print misctemplate($form->title, $form->render(submit => ["Login", "Mail Password"]));
316 elsif ($form->submitted eq "Register") {
317 printheader($session);
318 print misctemplate($form->title, $form->render(submit => ["Create Account"]));
321 elsif ($form->submitted eq "Create Account") {
322 printheader($session);
323 print misctemplate($form->title, $form->render(submit => ["Create Account"]));
326 printheader($session);
327 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
331 sub cgi_prefs ($$) { #{{{
335 eval q{use CGI::FormBuilder};
337 my $form = CGI::FormBuilder->new(
338 title => "preferences",
339 fields => [qw(do name password confirm_password email
340 subscriptions locked_pages)],
345 confirm_password => {
346 perl => q{eq $form->field("password")},
353 action => $config{cgiurl},
354 template => (-e "$config{templatedir}/prefs.tmpl" ?
355 {template_params("prefs.tmpl")} : ""),
356 stylesheet => baseurl()."style.css",
358 my @buttons=("Save Preferences", "Logout", "Cancel");
360 my $user_name=$session->param("name");
361 $form->field(name => "do", type => "hidden");
362 $form->field(name => "name", disabled => 1,
363 value => $user_name, force => 1);
364 $form->field(name => "password", type => "password");
365 $form->field(name => "confirm_password", type => "password");
366 $form->field(name => "subscriptions", size => 50,
367 comment => "(".htmllink("", "", "PageSpec", 1).")");
368 $form->field(name => "locked_pages", size => 50,
369 comment => "(".htmllink("", "", "PageSpec", 1).")");
370 $form->field(name => "banned_users", size => 50);
372 if (! is_admin($user_name)) {
373 $form->field(name => "locked_pages", type => "hidden");
374 $form->field(name => "banned_users", type => "hidden");
377 if ($config{httpauth}) {
378 $form->field(name => "password", type => "hidden");
379 $form->field(name => "confirm_password", type => "hidden");
382 if (! $form->submitted) {
383 $form->field(name => "email", force => 1,
384 value => userinfo_get($user_name, "email"));
385 $form->field(name => "subscriptions", force => 1,
386 value => userinfo_get($user_name, "subscriptions"));
387 $form->field(name => "locked_pages", force => 1,
388 value => userinfo_get($user_name, "locked_pages"));
389 if (is_admin($user_name)) {
390 $form->field(name => "banned_users", force => 1,
391 value => join(" ", get_banned_users()));
395 decode_form_utf8($form);
397 if ($form->submitted eq 'Logout') {
399 redirect($q, $config{url});
402 elsif ($form->submitted eq 'Cancel') {
403 redirect($q, $config{url});
406 elsif ($form->submitted eq "Save Preferences" && $form->validate) {
407 foreach my $field (qw(password email subscriptions locked_pages)) {
408 if (length $form->field($field)) {
409 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
412 if (is_admin($user_name)) {
413 set_banned_users(grep { ! is_admin($_) }
414 split(' ', $form->field("banned_users")));
416 $form->text("Preferences saved.");
419 printheader($session);
420 print misctemplate($form->title, $form->render(submit => \@buttons));
423 sub cgi_editpage ($$) { #{{{
427 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
428 my @buttons=("Save Page", "Preview", "Cancel");
430 eval q{use CGI::FormBuilder; use CGI::FormBuilder::Template::HTML};
432 my $renderer=CGI::FormBuilder::Template::HTML->new(
434 template_params("editpage.tmpl"),
436 run_hooks(pagetemplate => sub {
437 shift->(page => "", destpage => "", template => $renderer->engine);
439 my $form = CGI::FormBuilder->new(
445 editcontent => '/.+/',
447 required => [qw{editcontent}],
450 action => $config{cgiurl},
452 template => $renderer,
455 decode_form_utf8($form);
457 # This untaint is safe because titlepage removes any problematic
459 my ($page)=$form->field('page');
460 $page=titlepage(possibly_foolish_untaint($page));
461 if (! defined $page || ! length $page ||
462 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
463 error("bad page name");
467 if (defined $form->field('from')) {
468 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
473 if (exists $pagesources{$page}) {
474 $file=$pagesources{$page};
475 $type=pagetype($file);
478 $type=$form->param('type');
479 if (defined $type && length $type && $hooks{htmlize}{$type}) {
480 $type=possibly_foolish_untaint($type);
482 elsif (defined $from) {
483 # favor the type of linking page
484 $type=pagetype($pagesources{$from});
486 $type=$config{default_pageext} unless defined $type;
487 $file=$page.".".$type;
491 if (! -e "$config{srcdir}/$file") {
495 $form->field(name => "do", type => 'hidden');
496 $form->field(name => "from", type => 'hidden');
497 $form->field(name => "rcsinfo", type => 'hidden');
498 $form->field(name => "subpage", type => 'hidden');
499 $form->field(name => "page", value => $page, force => 1);
500 $form->field(name => "type", value => $type, force => 1);
501 $form->field(name => "comments", type => "text", size => 80);
502 $form->field(name => "editcontent", type => "textarea", rows => 20,
504 $form->tmpl_param("can_commit", $config{rcs});
505 $form->tmpl_param("indexlink", indexlink());
506 $form->tmpl_param("helponformattinglink",
507 htmllink("", "", "HelpOnFormatting", 1));
508 $form->tmpl_param("baseurl", baseurl());
509 if (! $form->submitted) {
510 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
514 if ($form->submitted eq "Cancel") {
515 if ($newfile && defined $from) {
516 redirect($q, "$config{url}/".htmlpage($from));
519 redirect($q, $config{url});
522 redirect($q, "$config{url}/".htmlpage($page));
526 elsif ($form->submitted eq "Preview") {
527 my $content=$form->field('editcontent');
528 my $comments=$form->field('comments');
529 $form->field(name => "editcontent",
530 value => $content, force => 1);
531 $form->field(name => "comments",
532 value => $comments, force => 1);
533 $config{rss}=$config{atom}=0; # avoid preview writing a feed!
534 $form->tmpl_param("page_preview",
535 htmlize($page, $type,
537 preprocess($page, $page,
538 filter($page, $content)))));
541 $form->tmpl_param("page_preview", "");
543 $form->tmpl_param("page_conflict", "");
545 if (! $form->submitted || $form->submitted eq "Preview" ||
547 if ($form->field("do") eq "create") {
550 if (! defined $from || ! length $from ||
551 $from ne $form->field('from') ||
552 $from=~/$config{wiki_file_prune_regexp}/ ||
554 $form->submitted eq "Preview") {
555 @page_locs=$best_loc=$page;
561 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
562 $page eq 'discussion') {
563 $best_loc="$from/$page";
566 $best_loc=$dir.$page;
569 push @page_locs, $dir.$page;
570 push @page_locs, "$from/$page";
571 while (length $dir) {
573 push @page_locs, $dir.$page;
578 ! exists $pagecase{lc $_} &&
579 ! page_locked($_, $session, 1)
583 # hmm, someone else made the page in the
585 redirect($q, "$config{url}/".htmlpage($page));
590 if (exists $hooks{htmlize}) {
591 @page_types=keys %{$hooks{htmlize}};
594 $form->tmpl_param("page_select", 1);
595 $form->field(name => "page", type => 'select',
596 options => \@page_locs, value => $best_loc);
597 $form->field(name => "type", type => 'select',
598 options => \@page_types);
599 $form->title("creating ".pagetitle($page));
601 elsif ($form->field("do") eq "edit") {
602 page_locked($page, $session);
603 if (! defined $form->field('editcontent') ||
604 ! length $form->field('editcontent')) {
606 if (exists $pagesources{$page}) {
607 $content=readfile(srcfile($pagesources{$page}));
608 $content=~s/\n/\r\n/g;
610 $form->field(name => "editcontent", value => $content,
613 $form->tmpl_param("page_select", 0);
614 $form->field(name => "page", type => 'hidden');
615 $form->field(name => "type", type => 'hidden');
616 $form->title("editing ".pagetitle($page));
619 print $form->render(submit => \@buttons);
623 page_locked($page, $session);
625 my $content=$form->field('editcontent');
627 $content=~s/\r\n/\n/g;
629 writefile($file, $config{srcdir}, $content);
631 my $message="web commit ";
632 if (defined $session->param("name") &&
633 length $session->param("name")) {
634 $message.="by ".$session->param("name");
637 $message.="from $ENV{REMOTE_ADDR}";
639 if (defined $form->field('comments') &&
640 length $form->field('comments')) {
641 $message.=": ".$form->field('comments');
648 # prevent deadlock with post-commit hook
650 # presumably the commit will trigger an update
652 my $conflict=rcs_commit($file, $message,
653 $form->field("rcsinfo"));
655 if (defined $conflict) {
656 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
658 $form->tmpl_param("page_conflict", 1);
659 $form->field("editcontent", value => $conflict, force => 1);
660 $form->field(name => "comments", value => $form->field('comments'), force => 1);
661 $form->field("do", "edit)");
662 $form->tmpl_param("page_select", 0);
663 $form->field(name => "page", type => 'hidden');
664 $form->field(name => "type", type => 'hidden');
665 $form->title("editing $page");
666 print $form->render(submit => \@buttons);
671 require IkiWiki::Render;
676 # The trailing question mark tries to avoid broken
677 # caches and get the most recent version of the page.
678 redirect($q, "$config{url}/".htmlpage($page)."?updated");
683 eval q{use CGI; use CGI::Session};
688 run_hooks(cgi => sub { shift->($q) });
690 my $do=$q->param('do');
691 if (! defined $do || ! length $do) {
692 my $error = $q->cgi_error;
694 error("Request not processed: $error");
697 error("\"do\" parameter missing");
701 # Things that do not need a session.
702 if ($do eq 'recentchanges') {
703 cgi_recentchanges($q);
706 elsif ($do eq 'hyperestraier') {
710 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
712 my $oldmask=umask(077);
713 my $session = CGI::Session->new("driver:DB_File", $q,
714 { FileName => "$config{wikistatedir}/sessions.db" });
717 # Auth hooks can sign a user in.
718 if ($do ne 'signin' && ! defined $session->param("name")) {
719 run_hooks(auth => sub {
720 shift->($q, $session)
722 if (defined $session->param("name")) {
723 # Make sure whatever user was authed is in the
725 if (! userinfo_get($session->param("name"), "regdate")) {
726 userinfo_setall($session->param("name"), {
735 # Everything below this point needs the user to be signed in.
736 if (((! $config{anonok} || $do eq 'prefs') &&
737 (! $config{httpauth}) &&
738 (! defined $session->param("name") ||
739 ! userinfo_get($session->param("name"), "regdate")))
740 || $do eq 'signin') {
741 cgi_signin($q, $session);
743 # Force session flush with safe umask.
744 my $oldmask=umask(077);
751 if (defined $session->param("name") && userinfo_get($session->param("name"), "banned")) {
752 print $q->header(-status => "403 Forbidden");
754 print "You are banned.";
758 if ($do eq 'create' || $do eq 'edit') {
759 cgi_editpage($q, $session);
761 elsif ($do eq 'prefs') {
762 cgi_prefs($q, $session);
764 elsif ($do eq 'blog') {
765 my $page=titlepage(decode_utf8($q->param('title')));
766 # if the page already exists, munge it to be unique
767 my $from=$q->param('from');
769 while (exists $pagecase{lc "$from/$page$add"}) {
770 $add=1 unless length $add;
773 $q->param('page', $page.$add);
774 # now run same as create
775 $q->param('do', 'create');
776 cgi_editpage($q, $session);
779 error("unknown do parameter");