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 (! $form->submitted) {
319 $form->field(name => "email", force => 1,
320 value => userinfo_get($user_name, "email"));
321 $form->field(name => "subscriptions", force => 1,
322 value => userinfo_get($user_name, "subscriptions"));
323 $form->field(name => "locked_pages", force => 1,
324 value => userinfo_get($user_name, "locked_pages"));
327 decode_form_utf8($form);
329 if ($form->submitted eq 'Logout') {
331 redirect($q, $config{url});
334 elsif ($form->submitted eq 'Cancel') {
335 redirect($q, $config{url});
338 elsif ($form->submitted eq "Save Preferences" && $form->validate) {
339 foreach my $field (qw(password email subscriptions locked_pages)) {
340 if (length $form->field($field)) {
341 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
344 $form->text("Preferences saved.");
347 printheader($session);
348 print misctemplate($form->title, $form->render(submit => \@buttons));
351 sub cgi_editpage ($$) { #{{{
355 my @fields=qw(do rcsinfo subpage from page type editcontent comments);
356 my @buttons=("Save Page", "Preview", "Cancel");
358 eval q{use CGI::FormBuilder; use CGI::FormBuilder::Template::HTML};
359 my $renderer=CGI::FormBuilder::Template::HTML->new(
361 template_params("editpage.tmpl"),
363 run_hooks(pagetemplate => sub {
364 shift->(page => "", destpage => "", template => $renderer->engine);
366 my $form = CGI::FormBuilder->new(
372 editcontent => '/.+/',
374 required => [qw{editcontent}],
377 action => $config{cgiurl},
379 template => $renderer,
382 decode_form_utf8($form);
384 # This untaint is safe because titlepage removes any problematic
386 my ($page)=$form->field('page');
387 $page=titlepage(possibly_foolish_untaint($page));
388 if (! defined $page || ! length $page ||
389 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
390 error("bad page name");
394 if (defined $form->field('from')) {
395 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
400 if (exists $pagesources{$page}) {
401 $file=$pagesources{$page};
402 $type=pagetype($file);
405 $type=$form->param('type');
406 if (defined $type && length $type && $hooks{htmlize}{$type}) {
407 $type=possibly_foolish_untaint($type);
409 elsif (defined $from) {
410 # favor the type of linking page
411 $type=pagetype($pagesources{$from});
413 $type=$config{default_pageext} unless defined $type;
414 $file=$page.".".$type;
418 if (! -e "$config{srcdir}/$file") {
422 $form->field(name => "do", type => 'hidden');
423 $form->field(name => "from", type => 'hidden');
424 $form->field(name => "rcsinfo", type => 'hidden');
425 $form->field(name => "subpage", type => 'hidden');
426 $form->field(name => "page", value => $page, force => 1);
427 $form->field(name => "type", value => $type, force => 1);
428 $form->field(name => "comments", type => "text", size => 80);
429 $form->field(name => "editcontent", type => "textarea", rows => 20,
431 $form->tmpl_param("can_commit", $config{rcs});
432 $form->tmpl_param("indexlink", indexlink());
433 $form->tmpl_param("helponformattinglink",
434 htmllink("", "", "HelpOnFormatting", 1));
435 $form->tmpl_param("baseurl", baseurl());
436 if (! $form->submitted) {
437 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
441 if ($form->submitted eq "Cancel") {
442 if ($newfile && defined $from) {
443 redirect($q, "$config{url}/".htmlpage($from));
446 redirect($q, $config{url});
449 redirect($q, "$config{url}/".htmlpage($page));
453 elsif ($form->submitted eq "Preview") {
454 my $content=$form->field('editcontent');
455 my $comments=$form->field('comments');
456 $form->field(name => "editcontent",
457 value => $content, force => 1);
458 $form->field(name => "comments",
459 value => $comments, force => 1);
460 $config{rss}=0; # avoid preview writing an rss feed!
461 $form->tmpl_param("page_preview",
462 htmlize($page, $type,
464 preprocess($page, $page,
465 filter($page, $content)))));
468 $form->tmpl_param("page_preview", "");
470 $form->tmpl_param("page_conflict", "");
472 if (! $form->submitted || $form->submitted eq "Preview" ||
474 if ($form->field("do") eq "create") {
477 if (! defined $from || ! length $from ||
478 $from ne $form->field('from') ||
479 $from=~/$config{wiki_file_prune_regexp}/ ||
481 $form->submitted eq "Preview") {
482 @page_locs=$best_loc=$page;
488 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
489 $page eq 'discussion') {
490 $best_loc="$from/$page";
493 $best_loc=$dir.$page;
496 push @page_locs, $dir.$page;
497 push @page_locs, "$from/$page";
498 while (length $dir) {
500 push @page_locs, $dir.$page;
505 ! exists $pagecase{lc $_} &&
506 ! page_locked($_, $session, 1)
510 # hmm, someone else made the page in the
512 redirect($q, "$config{url}/".htmlpage($page));
517 if (exists $hooks{htmlize}) {
518 @page_types=keys %{$hooks{htmlize}};
521 $form->tmpl_param("page_select", 1);
522 $form->field(name => "page", type => 'select',
523 options => \@page_locs, value => $best_loc);
524 $form->field(name => "type", type => 'select',
525 options => \@page_types);
526 $form->title("creating ".pagetitle($page));
528 elsif ($form->field("do") eq "edit") {
529 page_locked($page, $session);
530 if (! defined $form->field('editcontent') ||
531 ! length $form->field('editcontent')) {
533 if (exists $pagesources{$page}) {
534 $content=readfile(srcfile($pagesources{$page}));
535 $content=~s/\n/\r\n/g;
537 $form->field(name => "editcontent", value => $content,
540 $form->tmpl_param("page_select", 0);
541 $form->field(name => "page", type => 'hidden');
542 $form->field(name => "type", type => 'hidden');
543 $form->title("editing ".pagetitle($page));
546 print $form->render(submit => \@buttons);
550 page_locked($page, $session);
552 my $content=$form->field('editcontent');
554 $content=~s/\r\n/\n/g;
556 writefile($file, $config{srcdir}, $content);
558 my $message="web commit ";
559 if (defined $session->param("name") &&
560 length $session->param("name")) {
561 $message.="by ".$session->param("name");
564 $message.="from $ENV{REMOTE_ADDR}";
566 if (defined $form->field('comments') &&
567 length $form->field('comments')) {
568 $message.=": ".$form->field('comments');
575 # prevent deadlock with post-commit hook
577 # presumably the commit will trigger an update
579 my $conflict=rcs_commit($file, $message,
580 $form->field("rcsinfo"));
582 if (defined $conflict) {
583 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
585 $form->tmpl_param("page_conflict", 1);
586 $form->field("editcontent", value => $conflict, force => 1);
587 $form->field(name => "comments", value => $form->field('comments'), force => 1);
588 $form->field("do", "edit)");
589 $form->tmpl_param("page_select", 0);
590 $form->field(name => "page", type => 'hidden');
591 $form->field(name => "type", type => 'hidden');
592 $form->title("editing $page");
593 print $form->render(submit => \@buttons);
598 require IkiWiki::Render;
603 # The trailing question mark tries to avoid broken
604 # caches and get the most recent version of the page.
605 redirect($q, "$config{url}/".htmlpage($page)."?updated");
611 eval q{use CGI::Session};
615 run_hooks(cgi => sub { shift->($q) });
617 my $do=$q->param('do');
618 if (! defined $do || ! length $do) {
619 my $error = $q->cgi_error;
621 error("Request not processed: $error");
624 error("\"do\" parameter missing");
628 # Things that do not need a session.
629 if ($do eq 'recentchanges') {
630 cgi_recentchanges($q);
633 elsif ($do eq 'hyperestraier') {
637 CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
639 my $oldmask=umask(077);
640 my $session = CGI::Session->new("driver:DB_File", $q,
641 { FileName => "$config{wikistatedir}/sessions.db" });
644 # Everything below this point needs the user to be signed in.
645 if (((! $config{anonok} || $do eq 'prefs') &&
646 (! defined $session->param("name") ||
647 ! userinfo_get($session->param("name"), "regdate"))) || $do eq 'signin') {
648 cgi_signin($q, $session);
650 # Force session flush with safe umask.
651 my $oldmask=umask(077);
658 if ($do eq 'create' || $do eq 'edit') {
659 cgi_editpage($q, $session);
661 elsif ($do eq 'prefs') {
662 cgi_prefs($q, $session);
664 elsif ($do eq 'blog') {
665 my $page=titlepage(decode_utf8($q->param('title')));
666 # if the page already exists, munge it to be unique
667 my $from=$q->param('from');
669 while (exists $pagecase{lc "$from/$page$add"}) {
670 $add=1 unless length $add;
673 $q->param('page', $page.$add);
674 # now run same as create
675 $q->param('do', 'create');
676 cgi_editpage($q, $session);
679 error("unknown do parameter");