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)],
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 ($form->submitted eq "Register" || $form->submitted eq "Create Account") {
153 $form->title("register");
155 $form->fields(qw(do title page subpage from name password confirm_password email));
156 $form->field(name => "confirm_password", type => "password");
157 $form->field(name => "email", type => "text");
159 if ($q->param("do") ne "signin" && !$form->submitted) {
160 $form->text("You need to log in first.");
163 if ($form->submitted) {
164 # Set required fields based on how form was submitted.
166 "Login" => [qw(name password)],
168 "Create Account" => [qw(name password confirm_password email)],
169 "Mail Password" => [qw(name)],
171 foreach my $opt (@{$required{$form->submitted}}) {
172 $form->field(name => $opt, required => 1);
175 # Validate password differently depending on how
176 # form was submitted.
177 if ($form->submitted eq 'Login') {
181 length $form->field("name") &&
182 shift eq userinfo_get($form->field("name"), 'password');
185 $form->field(name => "name", validate => '/^\w+$/');
188 $form->field(name => "password", validate => 'VALUE');
190 # And make sure the entered name exists when logging
191 # in or sending email, and does not when registering.
192 if ($form->submitted eq 'Create Account' ||
193 $form->submitted eq 'Register') {
199 $name=~/$config{wiki_file_regexp}/ &&
200 ! userinfo_get($name, "regdate");
210 userinfo_get($name, "regdate");
216 # First time settings.
217 $form->field(name => "name", comment => "use FirstnameLastName");
218 if ($session->param("name")) {
219 $form->field(name => "name", value => $session->param("name"));
223 if ($form->submitted && $form->validate) {
224 if ($form->submitted eq 'Login') {
225 $session->param("name", $form->field("name"));
226 if (defined $form->field("do") &&
227 $form->field("do") ne 'signin') {
229 do => $form->field("do"),
230 page => $form->field("page"),
231 title => $form->field("title"),
232 subpage => $form->field("subpage"),
233 from => $form->field("from"),
237 redirect($q, $config{url});
240 elsif ($form->submitted eq 'Create Account') {
241 my $user_name=$form->field('name');
242 if (userinfo_setall($user_name, {
243 'email' => $form->field('email'),
244 'password' => $form->field('password'),
247 $form->field(name => "confirm_password", type => "hidden");
248 $form->field(name => "email", type => "hidden");
249 $form->text("Account creation successful. Now you can Login.");
250 printheader($session);
251 print misctemplate($form->title, $form->render(submit => ["Login"]));
254 error("Error creating account.");
257 elsif ($form->submitted eq 'Mail Password') {
258 my $user_name=$form->field("name");
259 my $template=template("passwordmail.tmpl");
261 user_name => $user_name,
262 user_password => userinfo_get($user_name, "password"),
263 wikiurl => $config{url},
264 wikiname => $config{wikiname},
265 REMOTE_ADDR => $ENV{REMOTE_ADDR},
268 eval q{use Mail::Sendmail};
271 To => userinfo_get($user_name, "email"),
272 From => "$config{wikiname} admin <$config{adminemail}>",
273 Subject => "$config{wikiname} information",
274 Message => $template->output,
275 ) or error("Failed to send mail");
277 $form->text("Your password has been emailed to you.");
278 $form->field(name => "name", required => 0);
279 printheader($session);
280 print misctemplate($form->title, $form->render(submit => ["Login", "Mail Password"]));
282 elsif ($form->submitted eq "Register") {
283 printheader($session);
284 print misctemplate($form->title, $form->render(submit => ["Create Account"]));
287 elsif ($form->submitted eq "Create Account") {
288 printheader($session);
289 print misctemplate($form->title, $form->render(submit => ["Create Account"]));
292 printheader($session);
293 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
297 sub cgi_prefs ($$) { #{{{
301 eval q{use CGI::FormBuilder};
303 my $form = CGI::FormBuilder->new(
304 title => "preferences",
305 fields => [qw(do name password confirm_password email
306 subscriptions locked_pages)],
311 confirm_password => {
312 perl => q{eq $form->field("password")},
319 action => $config{cgiurl},
320 template => (-e "$config{templatedir}/prefs.tmpl" ?
321 {template_params("prefs.tmpl")} : ""),
322 stylesheet => baseurl()."style.css",
324 my @buttons=("Save Preferences", "Logout", "Cancel");
326 my $user_name=$session->param("name");
327 $form->field(name => "do", type => "hidden");
328 $form->field(name => "name", disabled => 1,
329 value => $user_name, force => 1);
330 $form->field(name => "password", type => "password");
331 $form->field(name => "confirm_password", type => "password");
332 $form->field(name => "subscriptions", size => 50,
333 comment => "(".htmllink("", "", "PageSpec", 1).")");
334 $form->field(name => "locked_pages", size => 50,
335 comment => "(".htmllink("", "", "PageSpec", 1).")");
336 $form->field(name => "banned_users", size => 50);
338 if (! is_admin($user_name)) {
339 $form->field(name => "locked_pages", type => "hidden");
340 $form->field(name => "banned_users", type => "hidden");
343 if ($config{httpauth}) {
344 $form->field(name => "password", type => "hidden");
345 $form->field(name => "confirm_password", type => "hidden");
348 if (! $form->submitted) {
349 $form->field(name => "email", force => 1,
350 value => userinfo_get($user_name, "email"));
351 $form->field(name => "subscriptions", force => 1,
352 value => userinfo_get($user_name, "subscriptions"));
353 $form->field(name => "locked_pages", force => 1,
354 value => userinfo_get($user_name, "locked_pages"));
355 if (is_admin($user_name)) {
356 $form->field(name => "banned_users", force => 1,
357 value => join(" ", get_banned_users()));
361 decode_form_utf8($form);
363 if ($form->submitted eq 'Logout') {
365 redirect($q, $config{url});
368 elsif ($form->submitted eq 'Cancel') {
369 redirect($q, $config{url});
372 elsif ($form->submitted eq "Save Preferences" && $form->validate) {
373 foreach my $field (qw(password email subscriptions locked_pages)) {
374 if (length $form->field($field)) {
375 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
378 if (is_admin($user_name)) {
379 set_banned_users(grep { ! is_admin($_) }
380 split(' ', $form->field("banned_users")));
382 $form->text("Preferences saved.");
385 printheader($session);
386 print misctemplate($form->title, $form->render(submit => \@buttons));
389 sub cgi_editpage ($$) { #{{{
393 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
394 my @buttons=("Save Page", "Preview", "Cancel");
396 eval q{use CGI::FormBuilder; use CGI::FormBuilder::Template::HTML};
398 my $renderer=CGI::FormBuilder::Template::HTML->new(
400 template_params("editpage.tmpl"),
402 run_hooks(pagetemplate => sub {
403 shift->(page => "", destpage => "", template => $renderer->engine);
405 my $form = CGI::FormBuilder->new(
411 editcontent => '/.+/',
413 required => [qw{editcontent}],
416 action => $config{cgiurl},
418 template => $renderer,
421 decode_form_utf8($form);
423 # This untaint is safe because titlepage removes any problematic
425 my ($page)=$form->field('page');
426 $page=titlepage(possibly_foolish_untaint($page));
427 if (! defined $page || ! length $page ||
428 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
429 error("bad page name");
433 if (defined $form->field('from')) {
434 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
439 if (exists $pagesources{$page}) {
440 $file=$pagesources{$page};
441 $type=pagetype($file);
444 $type=$form->param('type');
445 if (defined $type && length $type && $hooks{htmlize}{$type}) {
446 $type=possibly_foolish_untaint($type);
448 elsif (defined $from) {
449 # favor the type of linking page
450 $type=pagetype($pagesources{$from});
452 $type=$config{default_pageext} unless defined $type;
453 $file=$page.".".$type;
457 if (! -e "$config{srcdir}/$file") {
461 $form->field(name => "do", type => 'hidden');
462 $form->field(name => "from", type => 'hidden');
463 $form->field(name => "rcsinfo", type => 'hidden');
464 $form->field(name => "subpage", type => 'hidden');
465 $form->field(name => "page", value => $page, force => 1);
466 $form->field(name => "type", value => $type, force => 1);
467 $form->field(name => "comments", type => "text", size => 80);
468 $form->field(name => "editcontent", type => "textarea", rows => 20,
470 $form->tmpl_param("can_commit", $config{rcs});
471 $form->tmpl_param("indexlink", indexlink());
472 $form->tmpl_param("helponformattinglink",
473 htmllink("", "", "HelpOnFormatting", 1));
474 $form->tmpl_param("baseurl", baseurl());
475 if (! $form->submitted) {
476 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
480 if ($form->submitted eq "Cancel") {
481 if ($newfile && defined $from) {
482 redirect($q, "$config{url}/".htmlpage($from));
485 redirect($q, $config{url});
488 redirect($q, "$config{url}/".htmlpage($page));
492 elsif ($form->submitted eq "Preview") {
493 my $content=$form->field('editcontent');
494 my $comments=$form->field('comments');
495 $form->field(name => "editcontent",
496 value => $content, force => 1);
497 $form->field(name => "comments",
498 value => $comments, force => 1);
499 $config{rss}=$config{atom}=0; # avoid preview writing a feed!
500 $form->tmpl_param("page_preview",
501 htmlize($page, $type,
503 preprocess($page, $page,
504 filter($page, $content)))));
507 $form->tmpl_param("page_preview", "");
509 $form->tmpl_param("page_conflict", "");
511 if (! $form->submitted || $form->submitted eq "Preview" ||
513 if ($form->field("do") eq "create") {
516 if (! defined $from || ! length $from ||
517 $from ne $form->field('from') ||
518 $from=~/$config{wiki_file_prune_regexp}/ ||
520 $form->submitted eq "Preview") {
521 @page_locs=$best_loc=$page;
527 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
528 $page eq 'discussion') {
529 $best_loc="$from/$page";
532 $best_loc=$dir.$page;
535 push @page_locs, $dir.$page;
536 push @page_locs, "$from/$page";
537 while (length $dir) {
539 push @page_locs, $dir.$page;
544 ! exists $pagecase{lc $_} &&
545 ! page_locked($_, $session, 1)
549 # hmm, someone else made the page in the
551 redirect($q, "$config{url}/".htmlpage($page));
556 if (exists $hooks{htmlize}) {
557 @page_types=keys %{$hooks{htmlize}};
560 $form->tmpl_param("page_select", 1);
561 $form->field(name => "page", type => 'select',
562 options => \@page_locs, value => $best_loc);
563 $form->field(name => "type", type => 'select',
564 options => \@page_types);
565 $form->title("creating ".pagetitle($page));
567 elsif ($form->field("do") eq "edit") {
568 page_locked($page, $session);
569 if (! defined $form->field('editcontent') ||
570 ! length $form->field('editcontent')) {
572 if (exists $pagesources{$page}) {
573 $content=readfile(srcfile($pagesources{$page}));
574 $content=~s/\n/\r\n/g;
576 $form->field(name => "editcontent", value => $content,
579 $form->tmpl_param("page_select", 0);
580 $form->field(name => "page", type => 'hidden');
581 $form->field(name => "type", type => 'hidden');
582 $form->title("editing ".pagetitle($page));
585 print "Content-Type: text/html\n\n";
586 print $form->render(submit => \@buttons);
590 page_locked($page, $session);
592 my $content=$form->field('editcontent');
594 $content=~s/\r\n/\n/g;
596 writefile($file, $config{srcdir}, $content);
598 my $message="web commit ";
599 if (defined $session->param("name") &&
600 length $session->param("name")) {
601 $message.="by ".$session->param("name");
604 $message.="from $ENV{REMOTE_ADDR}";
606 if (defined $form->field('comments') &&
607 length $form->field('comments')) {
608 $message.=": ".$form->field('comments');
615 # prevent deadlock with post-commit hook
617 # presumably the commit will trigger an update
619 my $conflict=rcs_commit($file, $message,
620 $form->field("rcsinfo"));
622 if (defined $conflict) {
623 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
625 $form->tmpl_param("page_conflict", 1);
626 $form->field("editcontent", value => $conflict, force => 1);
627 $form->field(name => "comments", value => $form->field('comments'), force => 1);
628 $form->field("do", "edit)");
629 $form->tmpl_param("page_select", 0);
630 $form->field(name => "page", type => 'hidden');
631 $form->field(name => "type", type => 'hidden');
632 $form->title("editing $page");
633 print $form->render(submit => \@buttons);
638 require IkiWiki::Render;
643 # The trailing question mark tries to avoid broken
644 # caches and get the most recent version of the page.
645 redirect($q, "$config{url}/".htmlpage($page)."?updated");
650 eval q{use CGI; use CGI::Session};
655 run_hooks(cgi => sub { shift->($q) });
657 my $do=$q->param('do');
658 if (! defined $do || ! length $do) {
659 my $error = $q->cgi_error;
661 error("Request not processed: $error");
664 error("\"do\" parameter missing");
668 # Things that do not need a session.
669 if ($do eq 'recentchanges') {
670 cgi_recentchanges($q);
673 elsif ($do eq 'hyperestraier') {
677 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
679 my $oldmask=umask(077);
680 my $session = CGI::Session->new("driver:DB_File", $q,
681 { FileName => "$config{wikistatedir}/sessions.db" });
684 # Everything below this point needs the user to be signed in.
685 if (((! $config{anonok} || $do eq 'prefs') &&
686 (! $config{httpauth}) &&
687 (! defined $session->param("name") ||
688 ! userinfo_get($session->param("name"), "regdate"))) || $do eq 'signin') {
689 cgi_signin($q, $session);
691 # Force session flush with safe umask.
692 my $oldmask=umask(077);
699 if ($config{httpauth} && (! defined $session->param("name"))) {
700 if (! defined $q->remote_user()) {
701 error("Could not determine authenticated username.");
704 $session->param("name", $q->remote_user());
705 if (! userinfo_get($session->param("name"), "regdate")) {
706 userinfo_setall($session->param("name"), {
715 if (userinfo_get($session->param("name"), "banned")) {
716 print $q->header(-status => "403 Forbidden");
718 print "You are banned.";
722 if ($do eq 'create' || $do eq 'edit') {
723 cgi_editpage($q, $session);
725 elsif ($do eq 'prefs') {
726 cgi_prefs($q, $session);
728 elsif ($do eq 'blog') {
729 my $page=titlepage(decode_utf8($q->param('title')));
730 # if the page already exists, munge it to be unique
731 my $from=$q->param('from');
733 while (exists $pagecase{lc "$from/$page$add"}) {
734 $add=1 unless length $add;
737 $q->param('page', $page.$add);
738 # now run same as create
739 $q->param('do', 'create');
740 cgi_editpage($q, $session);
743 error("unknown do parameter");