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}));
88 $change->{user} = htmllink("", "", escapeHTML($change->{user}), 1);
90 my $is_excess = exists $change->{pages}[10]; # limit pages to first 10
91 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
94 $_->{link} = htmllink("", "", $_->{page}, 1);
98 push @{$change->{pages}}, { link => '...' } if $is_excess;
101 my $template=template("recentchanges.tmpl");
103 title => "RecentChanges",
104 indexlink => indexlink(),
105 wikiname => $config{wikiname},
106 changelog => $changelog,
107 baseurl => baseurl(),
109 run_hooks(pagetemplate => sub {
110 shift->(page => "", destpage => "", template => $template);
112 print $q->header(-charset => 'utf-8'), $template->output;
115 sub cgi_signin ($$) { #{{{
119 eval q{use CGI::FormBuilder};
121 my $form = CGI::FormBuilder->new(
123 fields => [qw(do title page subpage from name password openid_url)],
128 confirm_password => {
129 perl => q{eq $form->field("password")},
136 action => $config{cgiurl},
138 template => (-e "$config{templatedir}/signin.tmpl" ?
139 {template_params("signin.tmpl")} : ""),
140 stylesheet => baseurl()."style.css",
143 decode_form_utf8($form);
145 $form->field(name => "name", required => 0);
146 $form->field(name => "do", type => "hidden");
147 $form->field(name => "page", type => "hidden");
148 $form->field(name => "title", type => "hidden");
149 $form->field(name => "from", type => "hidden");
150 $form->field(name => "subpage", type => "hidden");
151 $form->field(name => "password", type => "password", required => 0);
152 if ($config{openid}) {
153 $form->field(name => "openid_url", label => "OpenID", comment => "to log in via OpenID");
156 $form->field(name => "openid_url", type => "hidden");
158 if ($form->submitted eq "Register" || $form->submitted eq "Create Account") {
159 $form->title("register");
161 $form->fields(qw(do title page subpage from name password confirm_password email));
162 $form->field(name => "confirm_password", type => "password");
163 $form->field(name => "email", type => "text");
164 $form->field(name => "openid_url", type => "hidden");
166 if ($q->param("do") ne "signin" && !$form->submitted) {
167 $form->text("You need to log in first.");
170 if ($form->submitted) {
171 my $submittype=$form->submitted;
172 # OpenID login uses the Login button, but validates
174 if ($submittype eq "Login" && $config{openid} &&
175 length $form->field("openid_url")) {
176 $submittype="OpenID";
179 name => "openid_url",
182 IkiWiki::Plugin::openid::validate($q, $session, $form, shift);
187 # Set required fields based on how form was submitted.
189 "Login" => [qw(name password)],
191 "Create Account" => [qw(name password confirm_password email)],
192 "Mail Password" => [qw(name)],
193 "OpenID" => [qw(openid_url)],
195 foreach my $opt (@{$required{$submittype}}) {
196 $form->field(name => $opt, required => 1);
199 # Validate password differently depending on how
200 # form was submitted.
201 if ($submittype eq 'Login') {
205 length $form->field("name") &&
206 shift eq userinfo_get($form->field("name"), 'password');
209 $form->field(name => "name", validate => '/^\w+$/');
211 elsif ($submittype ne 'OpenID') {
212 $form->field(name => "password", validate => 'VALUE');
214 # And make sure the entered name exists when logging
215 # in or sending email, and does not when registering.
216 if ($submittype eq 'Create Account' ||
217 $submittype eq 'Register') {
223 $name=~/$config{wiki_file_regexp}/ &&
224 ! userinfo_get($name, "regdate");
228 elsif ($submittype ne 'OpenID') {
234 userinfo_get($name, "regdate");
240 # First time settings.
241 $form->field(name => "name", comment => "use FirstnameLastName");
242 if ($session->param("name")) {
243 $form->field(name => "name", value => $session->param("name"));
247 if ($form->submitted && $form->validate) {
248 if ($form->submitted eq 'Login') {
249 $session->param("name", $form->field("name"));
250 if (defined $form->field("do") &&
251 $form->field("do") ne 'signin') {
253 do => $form->field("do"),
254 page => $form->field("page"),
255 title => $form->field("title"),
256 from => $form->field("from"),
257 subpage => $form->field("subpage"),
261 redirect($q, $config{url});
264 elsif ($form->submitted eq 'Create Account') {
265 my $user_name=$form->field('name');
266 if (userinfo_setall($user_name, {
267 'email' => $form->field('email'),
268 'password' => $form->field('password'),
271 $form->field(name => "confirm_password", type => "hidden");
272 $form->field(name => "email", type => "hidden");
273 $form->text("Account creation successful. Now you can Login.");
274 printheader($session);
275 print misctemplate($form->title, $form->render(submit => ["Login"]));
278 error("Error creating account.");
281 elsif ($form->submitted eq 'Mail Password') {
282 my $user_name=$form->field("name");
283 my $template=template("passwordmail.tmpl");
285 user_name => $user_name,
286 user_password => userinfo_get($user_name, "password"),
287 wikiurl => $config{url},
288 wikiname => $config{wikiname},
289 REMOTE_ADDR => $ENV{REMOTE_ADDR},
292 eval q{use Mail::Sendmail};
295 To => userinfo_get($user_name, "email"),
296 From => "$config{wikiname} admin <$config{adminemail}>",
297 Subject => "$config{wikiname} information",
298 Message => $template->output,
299 ) or error("Failed to send mail");
301 $form->text("Your password has been emailed to you.");
302 $form->field(name => "name", required => 0);
303 printheader($session);
304 print misctemplate($form->title, $form->render(submit => ["Login", "Mail Password"]));
306 elsif ($form->submitted eq "Register") {
307 printheader($session);
308 print misctemplate($form->title, $form->render(submit => ["Create Account"]));
311 elsif ($form->submitted eq "Create Account") {
312 printheader($session);
313 print misctemplate($form->title, $form->render(submit => ["Create Account"]));
316 printheader($session);
317 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
321 sub cgi_prefs ($$) { #{{{
325 eval q{use CGI::FormBuilder};
327 my $form = CGI::FormBuilder->new(
328 title => "preferences",
329 fields => [qw(do name password confirm_password email
330 subscriptions locked_pages)],
335 confirm_password => {
336 perl => q{eq $form->field("password")},
343 action => $config{cgiurl},
344 template => (-e "$config{templatedir}/prefs.tmpl" ?
345 {template_params("prefs.tmpl")} : ""),
346 stylesheet => baseurl()."style.css",
348 my @buttons=("Save Preferences", "Logout", "Cancel");
350 my $user_name=$session->param("name");
351 $form->field(name => "do", type => "hidden");
352 $form->field(name => "name", disabled => 1,
353 value => $user_name, force => 1);
354 $form->field(name => "password", type => "password");
355 $form->field(name => "confirm_password", type => "password");
356 $form->field(name => "subscriptions", size => 50,
357 comment => "(".htmllink("", "", "PageSpec", 1).")");
358 $form->field(name => "locked_pages", size => 50,
359 comment => "(".htmllink("", "", "PageSpec", 1).")");
360 $form->field(name => "banned_users", size => 50);
362 if (! is_admin($user_name)) {
363 $form->field(name => "locked_pages", type => "hidden");
364 $form->field(name => "banned_users", type => "hidden");
367 if ($config{httpauth}) {
368 $form->field(name => "password", type => "hidden");
369 $form->field(name => "confirm_password", type => "hidden");
372 if (! $form->submitted) {
373 $form->field(name => "email", force => 1,
374 value => userinfo_get($user_name, "email"));
375 $form->field(name => "subscriptions", force => 1,
376 value => userinfo_get($user_name, "subscriptions"));
377 $form->field(name => "locked_pages", force => 1,
378 value => userinfo_get($user_name, "locked_pages"));
379 if (is_admin($user_name)) {
380 $form->field(name => "banned_users", force => 1,
381 value => join(" ", get_banned_users()));
385 decode_form_utf8($form);
387 if ($form->submitted eq 'Logout') {
389 redirect($q, $config{url});
392 elsif ($form->submitted eq 'Cancel') {
393 redirect($q, $config{url});
396 elsif ($form->submitted eq "Save Preferences" && $form->validate) {
397 foreach my $field (qw(password email subscriptions locked_pages)) {
398 if (length $form->field($field)) {
399 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
402 if (is_admin($user_name)) {
403 set_banned_users(grep { ! is_admin($_) }
404 split(' ', $form->field("banned_users")));
406 $form->text("Preferences saved.");
409 printheader($session);
410 print misctemplate($form->title, $form->render(submit => \@buttons));
413 sub cgi_editpage ($$) { #{{{
417 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
418 my @buttons=("Save Page", "Preview", "Cancel");
420 eval q{use CGI::FormBuilder; use CGI::FormBuilder::Template::HTML};
422 my $renderer=CGI::FormBuilder::Template::HTML->new(
424 template_params("editpage.tmpl"),
426 run_hooks(pagetemplate => sub {
427 shift->(page => "", destpage => "", template => $renderer->engine);
429 my $form = CGI::FormBuilder->new(
435 editcontent => '/.+/',
437 required => [qw{editcontent}],
440 action => $config{cgiurl},
442 template => $renderer,
445 decode_form_utf8($form);
447 # This untaint is safe because titlepage removes any problematic
449 my ($page)=$form->field('page');
450 $page=titlepage(possibly_foolish_untaint($page));
451 if (! defined $page || ! length $page ||
452 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
453 error("bad page name");
457 if (defined $form->field('from')) {
458 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
463 if (exists $pagesources{$page}) {
464 $file=$pagesources{$page};
465 $type=pagetype($file);
468 $type=$form->param('type');
469 if (defined $type && length $type && $hooks{htmlize}{$type}) {
470 $type=possibly_foolish_untaint($type);
472 elsif (defined $from) {
473 # favor the type of linking page
474 $type=pagetype($pagesources{$from});
476 $type=$config{default_pageext} unless defined $type;
477 $file=$page.".".$type;
481 if (! -e "$config{srcdir}/$file") {
485 $form->field(name => "do", type => 'hidden');
486 $form->field(name => "from", type => 'hidden');
487 $form->field(name => "rcsinfo", type => 'hidden');
488 $form->field(name => "subpage", type => 'hidden');
489 $form->field(name => "page", value => $page, force => 1);
490 $form->field(name => "type", value => $type, force => 1);
491 $form->field(name => "comments", type => "text", size => 80);
492 $form->field(name => "editcontent", type => "textarea", rows => 20,
494 $form->tmpl_param("can_commit", $config{rcs});
495 $form->tmpl_param("indexlink", indexlink());
496 $form->tmpl_param("helponformattinglink",
497 htmllink("", "", "HelpOnFormatting", 1));
498 $form->tmpl_param("baseurl", baseurl());
499 if (! $form->submitted) {
500 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
504 if ($form->submitted eq "Cancel") {
505 if ($newfile && defined $from) {
506 redirect($q, "$config{url}/".htmlpage($from));
509 redirect($q, $config{url});
512 redirect($q, "$config{url}/".htmlpage($page));
516 elsif ($form->submitted eq "Preview") {
517 my $content=$form->field('editcontent');
518 my $comments=$form->field('comments');
519 $form->field(name => "editcontent",
520 value => $content, force => 1);
521 $form->field(name => "comments",
522 value => $comments, force => 1);
523 $config{rss}=$config{atom}=0; # avoid preview writing a feed!
524 $form->tmpl_param("page_preview",
525 htmlize($page, $type,
527 preprocess($page, $page,
528 filter($page, $content)))));
531 $form->tmpl_param("page_preview", "");
533 $form->tmpl_param("page_conflict", "");
535 if (! $form->submitted || $form->submitted eq "Preview" ||
537 if ($form->field("do") eq "create") {
540 if (! defined $from || ! length $from ||
541 $from ne $form->field('from') ||
542 $from=~/$config{wiki_file_prune_regexp}/ ||
544 $form->submitted eq "Preview") {
545 @page_locs=$best_loc=$page;
551 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
552 $page eq 'discussion') {
553 $best_loc="$from/$page";
556 $best_loc=$dir.$page;
559 push @page_locs, $dir.$page;
560 push @page_locs, "$from/$page";
561 while (length $dir) {
563 push @page_locs, $dir.$page;
568 ! exists $pagecase{lc $_} &&
569 ! page_locked($_, $session, 1)
573 # hmm, someone else made the page in the
575 redirect($q, "$config{url}/".htmlpage($page));
580 if (exists $hooks{htmlize}) {
581 @page_types=keys %{$hooks{htmlize}};
584 $form->tmpl_param("page_select", 1);
585 $form->field(name => "page", type => 'select',
586 options => \@page_locs, value => $best_loc);
587 $form->field(name => "type", type => 'select',
588 options => \@page_types);
589 $form->title("creating ".pagetitle($page));
591 elsif ($form->field("do") eq "edit") {
592 page_locked($page, $session);
593 if (! defined $form->field('editcontent') ||
594 ! length $form->field('editcontent')) {
596 if (exists $pagesources{$page}) {
597 $content=readfile(srcfile($pagesources{$page}));
598 $content=~s/\n/\r\n/g;
600 $form->field(name => "editcontent", value => $content,
603 $form->tmpl_param("page_select", 0);
604 $form->field(name => "page", type => 'hidden');
605 $form->field(name => "type", type => 'hidden');
606 $form->title("editing ".pagetitle($page));
609 print $form->render(submit => \@buttons);
613 page_locked($page, $session);
615 my $content=$form->field('editcontent');
617 $content=~s/\r\n/\n/g;
619 writefile($file, $config{srcdir}, $content);
621 my $message="web commit ";
622 if (defined $session->param("name") &&
623 length $session->param("name")) {
624 $message.="by ".$session->param("name");
627 $message.="from $ENV{REMOTE_ADDR}";
629 if (defined $form->field('comments') &&
630 length $form->field('comments')) {
631 $message.=": ".$form->field('comments');
638 # prevent deadlock with post-commit hook
640 # presumably the commit will trigger an update
642 my $conflict=rcs_commit($file, $message,
643 $form->field("rcsinfo"));
645 if (defined $conflict) {
646 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
648 $form->tmpl_param("page_conflict", 1);
649 $form->field("editcontent", value => $conflict, force => 1);
650 $form->field(name => "comments", value => $form->field('comments'), force => 1);
651 $form->field("do", "edit)");
652 $form->tmpl_param("page_select", 0);
653 $form->field(name => "page", type => 'hidden');
654 $form->field(name => "type", type => 'hidden');
655 $form->title("editing $page");
656 print $form->render(submit => \@buttons);
661 require IkiWiki::Render;
666 # The trailing question mark tries to avoid broken
667 # caches and get the most recent version of the page.
668 redirect($q, "$config{url}/".htmlpage($page)."?updated");
673 eval q{use CGI; use CGI::Session};
678 run_hooks(cgi => sub { shift->($q) });
680 my $do=$q->param('do');
681 if (! defined $do || ! length $do) {
682 my $error = $q->cgi_error;
684 error("Request not processed: $error");
687 error("\"do\" parameter missing");
691 # Things that do not need a session.
692 if ($do eq 'recentchanges') {
693 cgi_recentchanges($q);
696 elsif ($do eq 'hyperestraier') {
700 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
702 my $oldmask=umask(077);
703 my $session = CGI::Session->new("driver:DB_File", $q,
704 { FileName => "$config{wikistatedir}/sessions.db" });
707 # Auth hooks can sign a user in.
708 if ($do ne 'signin' && ! defined $session->param("name")) {
709 run_hooks(auth => sub {
710 shift->($q, $session)
712 if (defined $session->param("name")) {
713 # Make sure whatever user was authed is in the
715 if (! userinfo_get($session->param("name"), "regdate")) {
716 userinfo_setall($session->param("name"), {
725 # Everything below this point needs the user to be signed in.
726 if (((! $config{anonok} || $do eq 'prefs') &&
727 (! $config{httpauth}) &&
728 (! defined $session->param("name") ||
729 ! userinfo_get($session->param("name"), "regdate")))
730 || $do eq 'signin') {
731 cgi_signin($q, $session);
733 # Force session flush with safe umask.
734 my $oldmask=umask(077);
741 if (defined $session->param("name") && userinfo_get($session->param("name"), "banned")) {
742 print $q->header(-status => "403 Forbidden");
744 print "You are banned.";
748 if ($do eq 'create' || $do eq 'edit') {
749 cgi_editpage($q, $session);
751 elsif ($do eq 'prefs') {
752 cgi_prefs($q, $session);
754 elsif ($do eq 'blog') {
755 my $page=titlepage(decode_utf8($q->param('title')));
756 # if the page already exists, munge it to be unique
757 my $from=$q->param('from');
759 while (exists $pagecase{lc "$from/$page$add"}) {
760 $add=1 unless length $add;
763 $q->param('page', $page.$add);
764 # now run same as create
765 $q->param('do', 'create');
766 cgi_editpage($q, $session);
769 error("unknown do parameter");