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);
88 $_->{link} = htmllink("", "", $_->{page}, 1);
94 my $template=template("recentchanges.tmpl");
96 title => "RecentChanges",
97 indexlink => indexlink(),
98 wikiname => $config{wikiname},
99 changelog => $changelog,
100 baseurl => baseurl(),
102 run_hooks(pagetemplate => sub {
103 shift->(page => "", destpage => "", template => $template);
105 print $q->header(-charset => 'utf-8'), $template->output;
108 sub cgi_signin ($$) { #{{{
112 eval q{use CGI::FormBuilder};
113 my $form = CGI::FormBuilder->new(
115 fields => [qw(do title page subpage from name password confirm_password email)],
120 confirm_password => {
121 perl => q{eq $form->field("password")},
128 action => $config{cgiurl},
130 template => (-e "$config{templatedir}/signin.tmpl" ?
131 {template_params("signin.tmpl")} : ""),
132 stylesheet => baseurl()."style.css",
135 decode_form_utf8($form);
137 $form->field(name => "name", required => 0);
138 $form->field(name => "do", type => "hidden");
139 $form->field(name => "page", type => "hidden");
140 $form->field(name => "title", type => "hidden");
141 $form->field(name => "from", type => "hidden");
142 $form->field(name => "subpage", type => "hidden");
143 $form->field(name => "password", type => "password", required => 0);
144 $form->field(name => "confirm_password", type => "password", required => 0);
145 $form->field(name => "email", required => 0);
146 if ($q->param("do") ne "signin" && !$form->submitted) {
147 $form->text("You need to log in first.");
150 if ($form->submitted) {
151 # Set required fields based on how form was submitted.
153 "Login" => [qw(name password)],
154 "Register" => [qw(name password confirm_password email)],
155 "Mail Password" => [qw(name)],
157 foreach my $opt (@{$required{$form->submitted}}) {
158 $form->field(name => $opt, required => 1);
161 # Validate password differently depending on how
162 # form was submitted.
163 if ($form->submitted eq 'Login') {
167 length $form->field("name") &&
168 shift eq userinfo_get($form->field("name"), 'password');
171 $form->field(name => "name", validate => '/^\w+$/');
174 $form->field(name => "password", validate => 'VALUE');
176 # And make sure the entered name exists when logging
177 # in or sending email, and does not when registering.
178 if ($form->submitted eq 'Register') {
184 $name=~/$config{wiki_file_regexp}/ &&
185 ! userinfo_get($name, "regdate");
195 userinfo_get($name, "regdate");
201 # First time settings.
202 $form->field(name => "name", comment => "use FirstnameLastName");
203 $form->field(name => "confirm_password", comment => "(only needed");
204 $form->field(name => "email", comment => "for registration)");
205 if ($session->param("name")) {
206 $form->field(name => "name", value => $session->param("name"));
210 if ($form->submitted && $form->validate) {
211 if ($form->submitted eq 'Login') {
212 $session->param("name", $form->field("name"));
213 if (defined $form->field("do") &&
214 $form->field("do") ne 'signin') {
216 do => $form->field("do"),
217 page => $form->field("page"),
218 title => $form->field("title"),
219 subpage => $form->field("subpage"),
220 from => $form->field("from"),
224 redirect($q, $config{url});
227 elsif ($form->submitted eq 'Register') {
228 my $user_name=$form->field('name');
229 if (userinfo_setall($user_name, {
230 'email' => $form->field('email'),
231 'password' => $form->field('password'),
234 $form->field(name => "confirm_password", type => "hidden");
235 $form->field(name => "email", type => "hidden");
236 $form->text("Registration successful. Now you can Login.");
237 printheader($session);
238 print misctemplate($form->title, $form->render(submit => ["Login"]));
241 error("Error saving registration.");
244 elsif ($form->submitted eq 'Mail Password') {
245 my $user_name=$form->field("name");
246 my $template=template("passwordmail.tmpl");
248 user_name => $user_name,
249 user_password => userinfo_get($user_name, "password"),
250 wikiurl => $config{url},
251 wikiname => $config{wikiname},
252 REMOTE_ADDR => $ENV{REMOTE_ADDR},
255 eval q{use Mail::Sendmail};
257 To => userinfo_get($user_name, "email"),
258 From => "$config{wikiname} admin <$config{adminemail}>",
259 Subject => "$config{wikiname} information",
260 Message => $template->output,
261 ) or error("Failed to send mail");
263 $form->text("Your password has been emailed to you.");
264 $form->field(name => "name", required => 0);
265 printheader($session);
266 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
270 printheader($session);
271 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
275 sub cgi_prefs ($$) { #{{{
279 eval q{use CGI::FormBuilder};
280 my $form = CGI::FormBuilder->new(
281 title => "preferences",
282 fields => [qw(do name password confirm_password email
283 subscriptions locked_pages)],
288 confirm_password => {
289 perl => q{eq $form->field("password")},
296 action => $config{cgiurl},
297 template => (-e "$config{templatedir}/prefs.tmpl" ?
298 {template_params("prefs.tmpl")} : ""),
299 stylesheet => baseurl()."style.css",
301 my @buttons=("Save Preferences", "Logout", "Cancel");
303 my $user_name=$session->param("name");
304 $form->field(name => "do", type => "hidden");
305 $form->field(name => "name", disabled => 1,
306 value => $user_name, force => 1);
307 $form->field(name => "password", type => "password");
308 $form->field(name => "confirm_password", type => "password");
309 $form->field(name => "subscriptions", size => 50,
310 comment => "(".htmllink("", "", "PageSpec", 1).")");
311 $form->field(name => "locked_pages", size => 50,
312 comment => "(".htmllink("", "", "PageSpec", 1).")");
314 if (! is_admin($user_name)) {
315 $form->field(name => "locked_pages", type => "hidden");
318 if ($config{httpauth}) {
319 $form->field(name => "password", type => "hidden");
320 $form->field(name => "confirm_password", type => "hidden");
323 if (! $form->submitted) {
324 $form->field(name => "email", force => 1,
325 value => userinfo_get($user_name, "email"));
326 $form->field(name => "subscriptions", force => 1,
327 value => userinfo_get($user_name, "subscriptions"));
328 $form->field(name => "locked_pages", force => 1,
329 value => userinfo_get($user_name, "locked_pages"));
332 decode_form_utf8($form);
334 if ($form->submitted eq 'Logout') {
336 redirect($q, $config{url});
339 elsif ($form->submitted eq 'Cancel') {
340 redirect($q, $config{url});
343 elsif ($form->submitted eq "Save Preferences" && $form->validate) {
344 foreach my $field (qw(password email subscriptions locked_pages)) {
345 if (length $form->field($field)) {
346 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
349 $form->text("Preferences saved.");
352 printheader($session);
353 print misctemplate($form->title, $form->render(submit => \@buttons));
356 sub cgi_editpage ($$) { #{{{
360 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
361 my @buttons=("Save Page", "Preview", "Cancel");
363 eval q{use CGI::FormBuilder; use CGI::FormBuilder::Template::HTML};
364 my $renderer=CGI::FormBuilder::Template::HTML->new(
366 template_params("editpage.tmpl"),
368 run_hooks(pagetemplate => sub {
369 shift->(page => "", destpage => "", template => $renderer->engine);
371 my $form = CGI::FormBuilder->new(
377 editcontent => '/.+/',
379 required => [qw{editcontent}],
382 action => $config{cgiurl},
384 template => $renderer,
387 decode_form_utf8($form);
389 # This untaint is safe because titlepage removes any problematic
391 my ($page)=$form->field('page');
392 $page=titlepage(possibly_foolish_untaint($page));
393 if (! defined $page || ! length $page ||
394 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
395 error("bad page name");
399 if (defined $form->field('from')) {
400 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
405 if (exists $pagesources{$page}) {
406 $file=$pagesources{$page};
407 $type=pagetype($file);
410 $type=$form->param('type');
411 if (defined $type && length $type && $hooks{htmlize}{$type}) {
412 $type=possibly_foolish_untaint($type);
414 elsif (defined $from) {
415 # favor the type of linking page
416 $type=pagetype($pagesources{$from});
418 $type=$config{default_pageext} unless defined $type;
419 $file=$page.".".$type;
423 if (! -e "$config{srcdir}/$file") {
427 $form->field(name => "do", type => 'hidden');
428 $form->field(name => "from", type => 'hidden');
429 $form->field(name => "rcsinfo", type => 'hidden');
430 $form->field(name => "subpage", type => 'hidden');
431 $form->field(name => "page", value => $page, force => 1);
432 $form->field(name => "type", value => $type, force => 1);
433 $form->field(name => "comments", type => "text", size => 80);
434 $form->field(name => "editcontent", type => "textarea", rows => 20,
436 $form->tmpl_param("can_commit", $config{rcs});
437 $form->tmpl_param("indexlink", indexlink());
438 $form->tmpl_param("helponformattinglink",
439 htmllink("", "", "HelpOnFormatting", 1));
440 $form->tmpl_param("baseurl", baseurl());
441 if (! $form->submitted) {
442 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
446 if ($form->submitted eq "Cancel") {
447 if ($newfile && defined $from) {
448 redirect($q, "$config{url}/".htmlpage($from));
451 redirect($q, $config{url});
454 redirect($q, "$config{url}/".htmlpage($page));
458 elsif ($form->submitted eq "Preview") {
459 my $content=$form->field('editcontent');
460 my $comments=$form->field('comments');
461 $form->field(name => "editcontent",
462 value => $content, force => 1);
463 $form->field(name => "comments",
464 value => $comments, force => 1);
465 $config{rss}=$config{atom}=0; # avoid preview writing a feed!
466 $form->tmpl_param("page_preview",
467 htmlize($page, $type,
469 preprocess($page, $page,
470 filter($page, $content)))));
473 $form->tmpl_param("page_preview", "");
475 $form->tmpl_param("page_conflict", "");
477 if (! $form->submitted || $form->submitted eq "Preview" ||
479 if ($form->field("do") eq "create") {
482 if (! defined $from || ! length $from ||
483 $from ne $form->field('from') ||
484 $from=~/$config{wiki_file_prune_regexp}/ ||
486 $form->submitted eq "Preview") {
487 @page_locs=$best_loc=$page;
493 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
494 $page eq 'discussion') {
495 $best_loc="$from/$page";
498 $best_loc=$dir.$page;
501 push @page_locs, $dir.$page;
502 push @page_locs, "$from/$page";
503 while (length $dir) {
505 push @page_locs, $dir.$page;
510 ! exists $pagecase{lc $_} &&
511 ! page_locked($_, $session, 1)
515 # hmm, someone else made the page in the
517 redirect($q, "$config{url}/".htmlpage($page));
522 if (exists $hooks{htmlize}) {
523 @page_types=keys %{$hooks{htmlize}};
526 $form->tmpl_param("page_select", 1);
527 $form->field(name => "page", type => 'select',
528 options => \@page_locs, value => $best_loc);
529 $form->field(name => "type", type => 'select',
530 options => \@page_types);
531 $form->title("creating ".pagetitle($page));
533 elsif ($form->field("do") eq "edit") {
534 page_locked($page, $session);
535 if (! defined $form->field('editcontent') ||
536 ! length $form->field('editcontent')) {
538 if (exists $pagesources{$page}) {
539 $content=readfile(srcfile($pagesources{$page}));
540 $content=~s/\n/\r\n/g;
542 $form->field(name => "editcontent", value => $content,
545 $form->tmpl_param("page_select", 0);
546 $form->field(name => "page", type => 'hidden');
547 $form->field(name => "type", type => 'hidden');
548 $form->title("editing ".pagetitle($page));
551 print $form->render(submit => \@buttons);
555 page_locked($page, $session);
557 my $content=$form->field('editcontent');
559 $content=~s/\r\n/\n/g;
561 writefile($file, $config{srcdir}, $content);
563 my $message="web commit ";
564 if (defined $session->param("name") &&
565 length $session->param("name")) {
566 $message.="by ".$session->param("name");
569 $message.="from $ENV{REMOTE_ADDR}";
571 if (defined $form->field('comments') &&
572 length $form->field('comments')) {
573 $message.=": ".$form->field('comments');
580 # prevent deadlock with post-commit hook
582 # presumably the commit will trigger an update
584 my $conflict=rcs_commit($file, $message,
585 $form->field("rcsinfo"));
587 if (defined $conflict) {
588 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
590 $form->tmpl_param("page_conflict", 1);
591 $form->field("editcontent", value => $conflict, force => 1);
592 $form->field(name => "comments", value => $form->field('comments'), force => 1);
593 $form->field("do", "edit)");
594 $form->tmpl_param("page_select", 0);
595 $form->field(name => "page", type => 'hidden');
596 $form->field(name => "type", type => 'hidden');
597 $form->title("editing $page");
598 print $form->render(submit => \@buttons);
603 require IkiWiki::Render;
608 # The trailing question mark tries to avoid broken
609 # caches and get the most recent version of the page.
610 redirect($q, "$config{url}/".htmlpage($page)."?updated");
616 eval q{use CGI::Session};
620 run_hooks(cgi => sub { shift->($q) });
622 my $do=$q->param('do');
623 if (! defined $do || ! length $do) {
624 my $error = $q->cgi_error;
626 error("Request not processed: $error");
629 error("\"do\" parameter missing");
633 # Things that do not need a session.
634 if ($do eq 'recentchanges') {
635 cgi_recentchanges($q);
638 elsif ($do eq 'hyperestraier') {
642 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
644 my $oldmask=umask(077);
645 my $session = CGI::Session->new("driver:DB_File", $q,
646 { FileName => "$config{wikistatedir}/sessions.db" });
649 # Everything below this point needs the user to be signed in.
650 if (((! $config{anonok} || $do eq 'prefs') &&
651 (! $config{httpauth}) &&
652 (! defined $session->param("name") ||
653 ! userinfo_get($session->param("name"), "regdate"))) || $do eq 'signin') {
654 cgi_signin($q, $session);
656 # Force session flush with safe umask.
657 my $oldmask=umask(077);
664 if ($config{httpauth} && (! defined $session->param("name"))) {
665 if (! defined $q->remote_user()) {
666 error("Could not determine authenticated username.");
669 $session->param("name", $q->remote_user());
670 if (!userinfo_get($session->param("name"),"regdate")) {
671 userinfo_setall($session->param("name"), {
680 if ($do eq 'create' || $do eq 'edit') {
681 cgi_editpage($q, $session);
683 elsif ($do eq 'prefs') {
684 cgi_prefs($q, $session);
686 elsif ($do eq 'blog') {
687 my $page=titlepage(decode_utf8($q->param('title')));
688 # if the page already exists, munge it to be unique
689 my $from=$q->param('from');
691 while (exists $pagecase{lc "$from/$page$add"}) {
692 $add=1 unless length $add;
695 $q->param('page', $page.$add);
696 # now run same as create
697 $q->param('do', 'create');
698 cgi_editpage($q, $session);
701 error("unknown do parameter");