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 print $q->header(-charset => 'utf-8'), $template->output;
105 sub cgi_signin ($$) { #{{{
109 eval q{use CGI::FormBuilder};
110 my $form = CGI::FormBuilder->new(
112 fields => [qw(do title page subpage from name password confirm_password email)],
117 confirm_password => {
118 perl => q{eq $form->field("password")},
125 action => $config{cgiurl},
127 template => (-e "$config{templatedir}/signin.tmpl" ?
128 {template_params("signin.tmpl")} : ""),
129 stylesheet => baseurl()."style.css",
132 decode_form_utf8($form);
134 $form->field(name => "name", required => 0);
135 $form->field(name => "do", type => "hidden");
136 $form->field(name => "page", type => "hidden");
137 $form->field(name => "title", type => "hidden");
138 $form->field(name => "from", type => "hidden");
139 $form->field(name => "subpage", type => "hidden");
140 $form->field(name => "password", type => "password", required => 0);
141 $form->field(name => "confirm_password", type => "password", required => 0);
142 $form->field(name => "email", required => 0);
143 if ($q->param("do") ne "signin" && !$form->submitted) {
144 $form->text("You need to log in first.");
147 if ($form->submitted) {
148 # Set required fields based on how form was submitted.
150 "Login" => [qw(name password)],
151 "Register" => [qw(name password confirm_password email)],
152 "Mail Password" => [qw(name)],
154 foreach my $opt (@{$required{$form->submitted}}) {
155 $form->field(name => $opt, required => 1);
158 # Validate password differently depending on how
159 # form was submitted.
160 if ($form->submitted eq 'Login') {
164 length $form->field("name") &&
165 shift eq userinfo_get($form->field("name"), 'password');
168 $form->field(name => "name", validate => '/^\w+$/');
171 $form->field(name => "password", validate => 'VALUE');
173 # And make sure the entered name exists when logging
174 # in or sending email, and does not when registering.
175 if ($form->submitted eq 'Register') {
181 $name=~/$config{wiki_file_regexp}/ &&
182 ! userinfo_get($name, "regdate");
192 userinfo_get($name, "regdate");
198 # First time settings.
199 $form->field(name => "name", comment => "use FirstnameLastName");
200 $form->field(name => "confirm_password", comment => "(only needed");
201 $form->field(name => "email", comment => "for registration)");
202 if ($session->param("name")) {
203 $form->field(name => "name", value => $session->param("name"));
207 if ($form->submitted && $form->validate) {
208 if ($form->submitted eq 'Login') {
209 $session->param("name", $form->field("name"));
210 if (defined $form->field("do") &&
211 $form->field("do") ne 'signin') {
213 do => $form->field("do"),
214 page => $form->field("page"),
215 title => $form->field("title"),
216 subpage => $form->field("subpage"),
217 from => $form->field("from"),
221 redirect($q, $config{url});
224 elsif ($form->submitted eq 'Register') {
225 my $user_name=$form->field('name');
226 if (userinfo_setall($user_name, {
227 'email' => $form->field('email'),
228 'password' => $form->field('password'),
231 $form->field(name => "confirm_password", type => "hidden");
232 $form->field(name => "email", type => "hidden");
233 $form->text("Registration successful. Now you can Login.");
234 printheader($session);
235 print misctemplate($form->title, $form->render(submit => ["Login"]));
238 error("Error saving registration.");
241 elsif ($form->submitted eq 'Mail Password') {
242 my $user_name=$form->field("name");
243 my $template=template("passwordmail.tmpl");
245 user_name => $user_name,
246 user_password => userinfo_get($user_name, "password"),
247 wikiurl => $config{url},
248 wikiname => $config{wikiname},
249 REMOTE_ADDR => $ENV{REMOTE_ADDR},
252 eval q{use Mail::Sendmail};
254 To => userinfo_get($user_name, "email"),
255 From => "$config{wikiname} admin <$config{adminemail}>",
256 Subject => "$config{wikiname} information",
257 Message => $template->output,
258 ) or error("Failed to send mail");
260 $form->text("Your password has been emailed to you.");
261 $form->field(name => "name", required => 0);
262 printheader($session);
263 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
267 printheader($session);
268 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
272 sub cgi_prefs ($$) { #{{{
276 eval q{use CGI::FormBuilder};
277 my $form = CGI::FormBuilder->new(
278 title => "preferences",
279 fields => [qw(do name password confirm_password email
280 subscriptions locked_pages)],
285 confirm_password => {
286 perl => q{eq $form->field("password")},
293 action => $config{cgiurl},
294 template => (-e "$config{templatedir}/prefs.tmpl" ?
295 {template_params("prefs.tmpl")} : ""),
296 stylesheet => baseurl()."style.css",
298 my @buttons=("Save Preferences", "Logout", "Cancel");
300 my $user_name=$session->param("name");
301 $form->field(name => "do", type => "hidden");
302 $form->field(name => "name", disabled => 1,
303 value => $user_name, force => 1);
304 $form->field(name => "password", type => "password");
305 $form->field(name => "confirm_password", type => "password");
306 $form->field(name => "subscriptions", size => 50,
307 comment => "(".htmllink("", "", "PageSpec", 1).")");
308 $form->field(name => "locked_pages", size => 50,
309 comment => "(".htmllink("", "", "PageSpec", 1).")");
311 if (! is_admin($user_name)) {
312 $form->field(name => "locked_pages", type => "hidden");
315 if (! $form->submitted) {
316 $form->field(name => "email", force => 1,
317 value => userinfo_get($user_name, "email"));
318 $form->field(name => "subscriptions", force => 1,
319 value => userinfo_get($user_name, "subscriptions"));
320 $form->field(name => "locked_pages", force => 1,
321 value => userinfo_get($user_name, "locked_pages"));
324 decode_form_utf8($form);
326 if ($form->submitted eq 'Logout') {
328 redirect($q, $config{url});
331 elsif ($form->submitted eq 'Cancel') {
332 redirect($q, $config{url});
335 elsif ($form->submitted eq "Save Preferences" && $form->validate) {
336 foreach my $field (qw(password email subscriptions locked_pages)) {
337 if (length $form->field($field)) {
338 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
341 $form->text("Preferences saved.");
344 printheader($session);
345 print misctemplate($form->title, $form->render(submit => \@buttons));
348 sub cgi_editpage ($$) { #{{{
352 eval q{use CGI::FormBuilder};
353 my $form = CGI::FormBuilder->new(
354 fields => [qw(do rcsinfo subpage from page type editcontent comments)],
359 editcontent => '/.+/',
361 required => [qw{editcontent}],
364 action => $config{cgiurl},
366 template => {template_params("editpage.tmpl")},
368 my @buttons=("Save Page", "Preview", "Cancel");
370 decode_form_utf8($form);
372 # This untaint is safe because titlepage removes any problematic
374 my ($page)=$form->field('page');
375 $page=titlepage(possibly_foolish_untaint($page));
376 if (! defined $page || ! length $page ||
377 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
378 error("bad page name");
382 if (defined $form->field('from')) {
383 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
388 if (exists $pagesources{$page}) {
389 $file=$pagesources{$page};
390 $type=pagetype($file);
393 $type=$form->param('type');
394 if (defined $type && length $type && $hooks{htmlize}{$type}) {
395 $type=possibly_foolish_untaint($type);
397 elsif (defined $from) {
398 # favor the type of linking page
399 $type=pagetype($pagesources{$from});
401 $type=$config{default_pageext} unless defined $type;
402 $file=$page.".".$type;
406 if (! -e "$config{srcdir}/$file") {
410 $form->field(name => "do", type => 'hidden');
411 $form->field(name => "from", type => 'hidden');
412 $form->field(name => "rcsinfo", type => 'hidden');
413 $form->field(name => "subpage", type => 'hidden');
414 $form->field(name => "page", value => $page, force => 1);
415 $form->field(name => "type", value => $type, force => 1);
416 $form->field(name => "comments", type => "text", size => 80);
417 $form->field(name => "editcontent", type => "textarea", rows => 20,
419 $form->tmpl_param("can_commit", $config{rcs});
420 $form->tmpl_param("indexlink", indexlink());
421 $form->tmpl_param("helponformattinglink",
422 htmllink("", "", "HelpOnFormatting", 1));
423 $form->tmpl_param("baseurl", baseurl());
424 if (! $form->submitted) {
425 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
429 if ($form->submitted eq "Cancel") {
430 redirect($q, "$config{url}/".htmlpage($page));
433 elsif ($form->submitted eq "Preview") {
434 require IkiWiki::Render;
435 my $content=$form->field('editcontent');
436 my $comments=$form->field('comments');
437 $form->field(name => "editcontent",
438 value => $content, force => 1);
439 $form->field(name => "comments",
440 value => $comments, force => 1);
441 $config{rss}=0; # avoid preview writing an rss feed!
442 $form->tmpl_param("page_preview",
443 htmlize($page, $type,
445 preprocess($page, $page,
446 filter($page, $content)))));
449 $form->tmpl_param("page_preview", "");
451 $form->tmpl_param("page_conflict", "");
453 if (! $form->submitted || $form->submitted eq "Preview" ||
455 if ($form->field("do") eq "create") {
458 if (! defined $from || ! length $from ||
459 $from ne $form->field('from') ||
460 $from=~/$config{wiki_file_prune_regexp}/ ||
462 $form->submitted eq "Preview") {
463 @page_locs=$best_loc=$page;
469 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
470 $page eq 'discussion') {
471 $best_loc="$from/$page";
474 $best_loc=$dir.$page;
477 push @page_locs, $dir.$page;
478 push @page_locs, "$from/$page";
479 while (length $dir) {
481 push @page_locs, $dir.$page;
486 ! exists $pagecase{lc $_} &&
487 ! page_locked($_, $session, 1)
491 # hmm, someone else made the page in the
493 redirect($q, "$config{url}/".htmlpage($page));
498 if (exists $hooks{htmlize}) {
499 @page_types=keys %{$hooks{htmlize}};
502 $form->tmpl_param("page_select", 1);
503 $form->field(name => "page", type => 'select',
504 options => \@page_locs, value => $best_loc);
505 $form->field(name => "type", type => 'select',
506 options => \@page_types);
507 $form->title("creating ".pagetitle($page));
509 elsif ($form->field("do") eq "edit") {
510 page_locked($page, $session);
511 if (! defined $form->field('editcontent') ||
512 ! length $form->field('editcontent')) {
514 if (exists $pagesources{$page}) {
515 $content=readfile(srcfile($pagesources{$page}));
516 $content=~s/\n/\r\n/g;
518 $form->field(name => "editcontent", value => $content,
521 $form->tmpl_param("page_select", 0);
522 $form->field(name => "page", type => 'hidden');
523 $form->field(name => "type", type => 'hidden');
524 $form->title("editing ".pagetitle($page));
527 print $form->render(submit => \@buttons);
531 page_locked($page, $session);
533 my $content=$form->field('editcontent');
535 $content=~s/\r\n/\n/g;
537 writefile($file, $config{srcdir}, $content);
539 my $message="web commit ";
540 if (defined $session->param("name") &&
541 length $session->param("name")) {
542 $message.="by ".$session->param("name");
545 $message.="from $ENV{REMOTE_ADDR}";
547 if (defined $form->field('comments') &&
548 length $form->field('comments')) {
549 $message.=": ".$form->field('comments');
556 # prevent deadlock with post-commit hook
558 # presumably the commit will trigger an update
560 my $conflict=rcs_commit($file, $message,
561 $form->field("rcsinfo"));
563 if (defined $conflict) {
564 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
566 $form->tmpl_param("page_conflict", 1);
567 $form->field("editcontent", value => $conflict, force => 1);
568 $form->field(name => "comments", value => $form->field('comments'), force => 1);
569 $form->field("do", "edit)");
570 $form->tmpl_param("page_select", 0);
571 $form->field(name => "page", type => 'hidden');
572 $form->field(name => "type", type => 'hidden');
573 $form->title("editing $page");
574 print $form->render(submit => \@buttons);
579 require IkiWiki::Render;
584 # The trailing question mark tries to avoid broken
585 # caches and get the most recent version of the page.
586 redirect($q, "$config{url}/".htmlpage($page)."?updated");
592 eval q{use CGI::Session};
596 run_hooks(cgi => sub { shift->($q) });
598 my $do=$q->param('do');
599 if (! defined $do || ! length $do) {
600 my $error = $q->cgi_error;
602 error("Request not processed: $error");
605 error("\"do\" parameter missing");
609 # Things that do not need a session.
610 if ($do eq 'recentchanges') {
611 cgi_recentchanges($q);
614 elsif ($do eq 'hyperestraier') {
618 CGI::Session->name("ikiwiki_session_$config{wikiname}");
620 my $oldmask=umask(077);
621 my $session = CGI::Session->new("driver:DB_File", $q,
622 { FileName => "$config{wikistatedir}/sessions.db" });
625 # Everything below this point needs the user to be signed in.
626 if (((! $config{anonok} || $do eq 'prefs') &&
627 (! defined $session->param("name") ||
628 ! userinfo_get($session->param("name"), "regdate"))) || $do eq 'signin') {
629 cgi_signin($q, $session);
631 # Force session flush with safe umask.
632 my $oldmask=umask(077);
639 if ($do eq 'create' || $do eq 'edit') {
640 cgi_editpage($q, $session);
642 elsif ($do eq 'prefs') {
643 cgi_prefs($q, $session);
645 elsif ($do eq 'blog') {
646 my $page=titlepage(decode_utf8($q->param('title')));
647 # if the page already exists, munge it to be unique
648 my $from=$q->param('from');
650 while (exists $pagecase{lc "$from/$page$add"}) {
651 $add=1 unless length $add;
654 $q->param('page', $page.$add);
655 # now run same as create
656 $q->param('do', 'create');
657 cgi_editpage($q, $session);
660 error("unknown do parameter");