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
79 eval q{use Time::Duration};
80 eval q{use CGI 'escapeHTML'};
82 my $changelog=[rcs_recentchanges(100)];
83 foreach my $change (@$changelog) {
84 $change->{when} = concise(ago($change->{when}));
85 $change->{user} = htmllink("", "", escapeHTML($change->{user}), 1);
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};
117 my $form = CGI::FormBuilder->new(
119 fields => [qw(do title page subpage from name password)],
124 confirm_password => {
125 perl => q{eq $form->field("password")},
132 action => $config{cgiurl},
134 template => (-e "$config{templatedir}/signin.tmpl" ?
135 {template_params("signin.tmpl")} : ""),
136 stylesheet => baseurl()."style.css",
139 decode_form_utf8($form);
141 $form->field(name => "name", required => 0);
142 $form->field(name => "do", type => "hidden");
143 $form->field(name => "page", type => "hidden");
144 $form->field(name => "title", type => "hidden");
145 $form->field(name => "from", type => "hidden");
146 $form->field(name => "subpage", type => "hidden");
147 $form->field(name => "password", type => "password", required => 0);
148 if ($form->submitted eq "Register" || $form->submitted eq "Create Account") {
149 $form->title("register");
151 $form->field(name => "name", comment => "use FirstnameLastName");
152 $form->fields(qw(do title page subpage from name password confirm_password email));
153 $form->field(name => "confirm_password", type => "password");
154 $form->field(name => "email", type => "text");
156 if ($q->param("do") ne "signin" && !$form->submitted) {
157 $form->text("You need to log in first.");
160 if ($form->submitted) {
161 # Set required fields based on how form was submitted.
163 "Login" => [qw(name password)],
165 "Create Account" => [qw(name password confirm_password email)],
166 "Mail Password" => [qw(name)],
168 foreach my $opt (@{$required{$form->submitted}}) {
169 $form->field(name => $opt, required => 1);
172 # Validate password differently depending on how
173 # form was submitted.
174 if ($form->submitted eq 'Login') {
178 length $form->field("name") &&
179 shift eq userinfo_get($form->field("name"), 'password');
182 $form->field(name => "name", validate => '/^\w+$/');
185 $form->field(name => "password", validate => 'VALUE');
187 # And make sure the entered name exists when logging
188 # in or sending email, and does not when registering.
189 if ($form->submitted eq 'Create Account') {
195 $name=~/$config{wiki_file_regexp}/ &&
196 ! userinfo_get($name, "regdate");
206 userinfo_get($name, "regdate");
212 # First time settings.
213 $form->field(name => "name", comment => "use FirstnameLastName");
214 if ($session->param("name")) {
215 $form->field(name => "name", value => $session->param("name"));
219 if ($form->submitted && $form->validate) {
220 if ($form->submitted eq 'Login') {
221 $session->param("name", $form->field("name"));
222 if (defined $form->field("do") &&
223 $form->field("do") ne 'signin') {
225 do => $form->field("do"),
226 page => $form->field("page"),
227 title => $form->field("title"),
228 subpage => $form->field("subpage"),
229 from => $form->field("from"),
233 redirect($q, $config{url});
236 elsif ($form->submitted eq 'Create Account') {
237 my $user_name=$form->field('name');
238 if (userinfo_setall($user_name, {
239 'email' => $form->field('email'),
240 'password' => $form->field('password'),
243 $form->field(name => "confirm_password", type => "hidden");
244 $form->field(name => "email", type => "hidden");
245 $form->text("Account creation successful. Now you can Login.");
246 printheader($session);
247 print misctemplate($form->title, $form->render(submit => ["Login"]));
250 error("Error creating account.");
253 elsif ($form->submitted eq 'Mail Password') {
254 my $user_name=$form->field("name");
255 my $template=template("passwordmail.tmpl");
257 user_name => $user_name,
258 user_password => userinfo_get($user_name, "password"),
259 wikiurl => $config{url},
260 wikiname => $config{wikiname},
261 REMOTE_ADDR => $ENV{REMOTE_ADDR},
264 eval q{use Mail::Sendmail};
266 To => userinfo_get($user_name, "email"),
267 From => "$config{wikiname} admin <$config{adminemail}>",
268 Subject => "$config{wikiname} information",
269 Message => $template->output,
270 ) or error("Failed to send mail");
272 $form->text("Your password has been emailed to you.");
273 $form->field(name => "name", required => 0);
274 printheader($session);
275 print misctemplate($form->title, $form->render(submit => ["Login", "Mail Password"]));
277 elsif ($form->submitted eq "Register") {
278 printheader($session);
279 print misctemplate($form->title, $form->render(submit => ["Create Account"]));
282 elsif ($form->submitted eq "Create Account") {
283 printheader($session);
284 print misctemplate($form->title, $form->render(submit => ["Create Account"]));
287 printheader($session);
288 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
292 sub cgi_prefs ($$) { #{{{
296 eval q{use CGI::FormBuilder};
297 my $form = CGI::FormBuilder->new(
298 title => "preferences",
299 fields => [qw(do name password confirm_password email
300 subscriptions locked_pages)],
305 confirm_password => {
306 perl => q{eq $form->field("password")},
313 action => $config{cgiurl},
314 template => (-e "$config{templatedir}/prefs.tmpl" ?
315 {template_params("prefs.tmpl")} : ""),
316 stylesheet => baseurl()."style.css",
318 my @buttons=("Save Preferences", "Logout", "Cancel");
320 my $user_name=$session->param("name");
321 $form->field(name => "do", type => "hidden");
322 $form->field(name => "name", disabled => 1,
323 value => $user_name, force => 1);
324 $form->field(name => "password", type => "password");
325 $form->field(name => "confirm_password", type => "password");
326 $form->field(name => "subscriptions", size => 50,
327 comment => "(".htmllink("", "", "PageSpec", 1).")");
328 $form->field(name => "locked_pages", size => 50,
329 comment => "(".htmllink("", "", "PageSpec", 1).")");
330 $form->field(name => "banned_users", size => 50);
332 if (! is_admin($user_name)) {
333 $form->field(name => "locked_pages", type => "hidden");
334 $form->field(name => "banned_users", type => "hidden");
337 if ($config{httpauth}) {
338 $form->field(name => "password", type => "hidden");
339 $form->field(name => "confirm_password", type => "hidden");
342 if (! $form->submitted) {
343 $form->field(name => "email", force => 1,
344 value => userinfo_get($user_name, "email"));
345 $form->field(name => "subscriptions", force => 1,
346 value => userinfo_get($user_name, "subscriptions"));
347 $form->field(name => "locked_pages", force => 1,
348 value => userinfo_get($user_name, "locked_pages"));
349 if (is_admin($user_name)) {
350 $form->field(name => "banned_users", force => 1,
351 value => join(" ", get_banned_users()));
355 decode_form_utf8($form);
357 if ($form->submitted eq 'Logout') {
359 redirect($q, $config{url});
362 elsif ($form->submitted eq 'Cancel') {
363 redirect($q, $config{url});
366 elsif ($form->submitted eq "Save Preferences" && $form->validate) {
367 foreach my $field (qw(password email subscriptions locked_pages)) {
368 if (length $form->field($field)) {
369 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
372 if (is_admin($user_name)) {
373 set_banned_users(grep { ! is_admin($_) }
374 split(' ', $form->field("banned_users")));
376 $form->text("Preferences saved.");
379 printheader($session);
380 print misctemplate($form->title, $form->render(submit => \@buttons));
383 sub cgi_editpage ($$) { #{{{
387 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
388 my @buttons=("Save Page", "Preview", "Cancel");
390 eval q{use CGI::FormBuilder; use CGI::FormBuilder::Template::HTML};
391 my $renderer=CGI::FormBuilder::Template::HTML->new(
393 template_params("editpage.tmpl"),
395 run_hooks(pagetemplate => sub {
396 shift->(page => "", destpage => "", template => $renderer->engine);
398 my $form = CGI::FormBuilder->new(
404 editcontent => '/.+/',
406 required => [qw{editcontent}],
409 action => $config{cgiurl},
411 template => $renderer,
414 decode_form_utf8($form);
416 # This untaint is safe because titlepage removes any problematic
418 my ($page)=$form->field('page');
419 $page=titlepage(possibly_foolish_untaint($page));
420 if (! defined $page || ! length $page ||
421 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
422 error("bad page name");
426 if (defined $form->field('from')) {
427 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
432 if (exists $pagesources{$page}) {
433 $file=$pagesources{$page};
434 $type=pagetype($file);
437 $type=$form->param('type');
438 if (defined $type && length $type && $hooks{htmlize}{$type}) {
439 $type=possibly_foolish_untaint($type);
441 elsif (defined $from) {
442 # favor the type of linking page
443 $type=pagetype($pagesources{$from});
445 $type=$config{default_pageext} unless defined $type;
446 $file=$page.".".$type;
450 if (! -e "$config{srcdir}/$file") {
454 $form->field(name => "do", type => 'hidden');
455 $form->field(name => "from", type => 'hidden');
456 $form->field(name => "rcsinfo", type => 'hidden');
457 $form->field(name => "subpage", type => 'hidden');
458 $form->field(name => "page", value => $page, force => 1);
459 $form->field(name => "type", value => $type, force => 1);
460 $form->field(name => "comments", type => "text", size => 80);
461 $form->field(name => "editcontent", type => "textarea", rows => 20,
463 $form->tmpl_param("can_commit", $config{rcs});
464 $form->tmpl_param("indexlink", indexlink());
465 $form->tmpl_param("helponformattinglink",
466 htmllink("", "", "HelpOnFormatting", 1));
467 $form->tmpl_param("baseurl", baseurl());
468 if (! $form->submitted) {
469 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
473 if ($form->submitted eq "Cancel") {
474 if ($newfile && defined $from) {
475 redirect($q, "$config{url}/".htmlpage($from));
478 redirect($q, $config{url});
481 redirect($q, "$config{url}/".htmlpage($page));
485 elsif ($form->submitted eq "Preview") {
486 my $content=$form->field('editcontent');
487 my $comments=$form->field('comments');
488 $form->field(name => "editcontent",
489 value => $content, force => 1);
490 $form->field(name => "comments",
491 value => $comments, force => 1);
492 $config{rss}=$config{atom}=0; # avoid preview writing a feed!
493 $form->tmpl_param("page_preview",
494 htmlize($page, $type,
496 preprocess($page, $page,
497 filter($page, $content)))));
500 $form->tmpl_param("page_preview", "");
502 $form->tmpl_param("page_conflict", "");
504 if (! $form->submitted || $form->submitted eq "Preview" ||
506 if ($form->field("do") eq "create") {
509 if (! defined $from || ! length $from ||
510 $from ne $form->field('from') ||
511 $from=~/$config{wiki_file_prune_regexp}/ ||
513 $form->submitted eq "Preview") {
514 @page_locs=$best_loc=$page;
520 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
521 $page eq 'discussion') {
522 $best_loc="$from/$page";
525 $best_loc=$dir.$page;
528 push @page_locs, $dir.$page;
529 push @page_locs, "$from/$page";
530 while (length $dir) {
532 push @page_locs, $dir.$page;
537 ! exists $pagecase{lc $_} &&
538 ! page_locked($_, $session, 1)
542 # hmm, someone else made the page in the
544 redirect($q, "$config{url}/".htmlpage($page));
549 if (exists $hooks{htmlize}) {
550 @page_types=keys %{$hooks{htmlize}};
553 $form->tmpl_param("page_select", 1);
554 $form->field(name => "page", type => 'select',
555 options => \@page_locs, value => $best_loc);
556 $form->field(name => "type", type => 'select',
557 options => \@page_types);
558 $form->title("creating ".pagetitle($page));
560 elsif ($form->field("do") eq "edit") {
561 page_locked($page, $session);
562 if (! defined $form->field('editcontent') ||
563 ! length $form->field('editcontent')) {
565 if (exists $pagesources{$page}) {
566 $content=readfile(srcfile($pagesources{$page}));
567 $content=~s/\n/\r\n/g;
569 $form->field(name => "editcontent", value => $content,
572 $form->tmpl_param("page_select", 0);
573 $form->field(name => "page", type => 'hidden');
574 $form->field(name => "type", type => 'hidden');
575 $form->title("editing ".pagetitle($page));
578 print $form->render(submit => \@buttons);
582 page_locked($page, $session);
584 my $content=$form->field('editcontent');
586 $content=~s/\r\n/\n/g;
588 writefile($file, $config{srcdir}, $content);
590 my $message="web commit ";
591 if (defined $session->param("name") &&
592 length $session->param("name")) {
593 $message.="by ".$session->param("name");
596 $message.="from $ENV{REMOTE_ADDR}";
598 if (defined $form->field('comments') &&
599 length $form->field('comments')) {
600 $message.=": ".$form->field('comments');
607 # prevent deadlock with post-commit hook
609 # presumably the commit will trigger an update
611 my $conflict=rcs_commit($file, $message,
612 $form->field("rcsinfo"));
614 if (defined $conflict) {
615 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
617 $form->tmpl_param("page_conflict", 1);
618 $form->field("editcontent", value => $conflict, force => 1);
619 $form->field(name => "comments", value => $form->field('comments'), force => 1);
620 $form->field("do", "edit)");
621 $form->tmpl_param("page_select", 0);
622 $form->field(name => "page", type => 'hidden');
623 $form->field(name => "type", type => 'hidden');
624 $form->title("editing $page");
625 print $form->render(submit => \@buttons);
630 require IkiWiki::Render;
635 # The trailing question mark tries to avoid broken
636 # caches and get the most recent version of the page.
637 redirect($q, "$config{url}/".htmlpage($page)."?updated");
643 eval q{use CGI::Session};
647 run_hooks(cgi => sub { shift->($q) });
649 my $do=$q->param('do');
650 if (! defined $do || ! length $do) {
651 my $error = $q->cgi_error;
653 error("Request not processed: $error");
656 error("\"do\" parameter missing");
660 # Things that do not need a session.
661 if ($do eq 'recentchanges') {
662 cgi_recentchanges($q);
665 elsif ($do eq 'hyperestraier') {
669 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
671 my $oldmask=umask(077);
672 my $session = CGI::Session->new("driver:DB_File", $q,
673 { FileName => "$config{wikistatedir}/sessions.db" });
676 # Everything below this point needs the user to be signed in.
677 if (((! $config{anonok} || $do eq 'prefs') &&
678 (! $config{httpauth}) &&
679 (! defined $session->param("name") ||
680 ! userinfo_get($session->param("name"), "regdate"))) || $do eq 'signin') {
681 cgi_signin($q, $session);
683 # Force session flush with safe umask.
684 my $oldmask=umask(077);
691 if ($config{httpauth} && (! defined $session->param("name"))) {
692 if (! defined $q->remote_user()) {
693 error("Could not determine authenticated username.");
696 $session->param("name", $q->remote_user());
697 if (! userinfo_get($session->param("name"), "regdate")) {
698 userinfo_setall($session->param("name"), {
707 if (userinfo_get($session->param("name"), "banned")) {
708 print $q->header(-status => "403 Forbidden");
710 print "You are banned.";
714 if ($do eq 'create' || $do eq 'edit') {
715 cgi_editpage($q, $session);
717 elsif ($do eq 'prefs') {
718 cgi_prefs($q, $session);
720 elsif ($do eq 'blog') {
721 my $page=titlepage(decode_utf8($q->param('title')));
722 # if the page already exists, munge it to be unique
723 my $from=$q->param('from');
725 while (exists $pagecase{lc "$from/$page$add"}) {
726 $add=1 unless length $add;
729 $q->param('page', $page.$add);
730 # now run same as create
731 $q->param('do', 'create');
732 cgi_editpage($q, $session);
735 error("unknown do parameter");