+ $form->field(name => "password", validate => 'VALUE');
+ }
+ # And make sure the entered name exists when logging
+ # in or sending email, and does not when registering.
+ if ($form->submitted eq 'Register') {
+ $form->field(
+ name => "name",
+ validate => sub {
+ my $name=shift;
+ length $name &&
+ ! userinfo_get($name, "regdate");
+ },
+ );
+ }
+ else {
+ $form->field(
+ name => "name",
+ validate => sub {
+ my $name=shift;
+ length $name &&
+ userinfo_get($name, "regdate");
+ },
+ );
+ }
+ }
+ else {
+ # First time settings.
+ $form->field(name => "name", comment => "use FirstnameLastName");
+ $form->field(name => "confirm_password", comment => "(only needed");
+ $form->field(name => "email", comment => "for registration)");
+ if ($session->param("name")) {
+ $form->field(name => "name", value => $session->param("name"));
+ }
+ }
+
+ if ($form->submitted && $form->validate) {
+ if ($form->submitted eq 'Login') {
+ $session->param("name", $form->field("name"));
+ if (defined $form->field("do") &&
+ $form->field("do") ne 'signin') {
+ print $q->redirect(
+ "$config{cgiurl}?do=".$form->field("do").
+ "&page=".$form->field("page").
+ "&from=".$form->field("from"));;
+ }
+ else {
+ print $q->redirect($config{url});
+ }
+ }
+ elsif ($form->submitted eq 'Register') {
+ my $user_name=$form->field('name');
+ if (userinfo_set($user_name, {
+ 'email' => $form->field('email'),
+ 'password' => $form->field('password'),
+ 'regdate' => time
+ })) {
+ $form->field(name => "confirm_password", type => "hidden");
+ $form->field(name => "email", type => "hidden");
+ $form->text("Registration successful. Now you can Login.");
+ print $session->header();
+ print misctemplate($form->title, $form->render(submit => ["Login"]));
+ }
+ else {
+ error("Error saving registration.");
+ }
+ }
+ elsif ($form->submitted eq 'Mail Password') {
+ my $user_name=$form->field("name");
+ my $template=HTML::Template->new(
+ filename => "$config{templatedir}/passwordmail.tmpl"
+ );
+ $template->param(
+ user_name => $user_name,
+ user_password => userinfo_get($user_name, "password"),
+ wikiurl => $config{url},
+ wikiname => $config{wikiname},
+ REMOTE_ADDR => $ENV{REMOTE_ADDR},
+ );
+
+ eval q{use Mail::Sendmail};
+ my ($fromhost) = $config{cgiurl} =~ m!/([^/]+)!;
+ sendmail(
+ To => userinfo_get($user_name, "email"),
+ From => "$config{wikiname} admin <".(getpwuid($>))[0]."@".$fromhost.">",
+ Subject => "$config{wikiname} information",
+ Message => $template->output,
+ ) or error("Failed to send mail");
+
+ $form->text("Your password has been emailed to you.");
+ $form->field(name => "name", required => 0);
+ print $session->header();
+ print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
+ }
+ }
+ else {
+ print $session->header();
+ print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
+ }
+} #}}}
+
+sub cgi_editpage ($$) { #{{{
+ my $q=shift;
+ my $session=shift;
+
+ eval q{use CGI::FormBuilder};
+ my $form = CGI::FormBuilder->new(
+ fields => [qw(do from page content comments)],
+ header => 1,
+ method => 'POST',
+ validate => {
+ content => '/.+/',
+ },
+ required => [qw{content}],
+ javascript => 0,
+ params => $q,
+ action => $q->request_uri,
+ table => 0,
+ template => "$config{templatedir}/editpage.tmpl"
+ );
+
+ my ($page)=$form->param('page')=~/$config{wiki_file_regexp}/;
+ if (! defined $page || ! length $page || $page ne $q->param('page') ||
+ $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
+ error("bad page name");
+ }
+ $page=lc($page);
+
+ $form->field(name => "do", type => 'hidden');
+ $form->field(name => "from", type => 'hidden');
+ $form->field(name => "page", value => "$page", force => 1);
+ $form->field(name => "comments", type => "text", size => 80);
+ $form->field(name => "content", type => "textarea", rows => 20,
+ cols => 80);
+
+ if ($form->submitted eq "Cancel") {
+ print $q->redirect("$config{url}/".htmlpage($page));
+ return;
+ }
+ if (! $form->submitted || ! $form->validate) {
+ if ($form->field("do") eq "create") {
+ if (exists $pagesources{lc($page)}) {
+ # hmm, someone else made the page in the
+ # meantime?
+ print $q->redirect("$config{url}/".htmlpage($page));
+ return;
+ }
+
+ my @page_locs;
+ my $best_loc;
+ my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
+ if (! defined $from || ! length $from ||
+ $from ne $form->param('from') ||
+ $from=~/$config{wiki_file_prune_regexp}/ || $from=~/^\//) {
+ @page_locs=$best_loc=$page;
+ }
+ else {
+ my $dir=$from."/";