<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
# Ikiwiki password authentication.
package IkiWiki::Plugin::passwordauth;

use warnings;
use strict;
use IkiWiki;

sub import { #{{{
        hook(type =&gt; "formbuilder_setup", id =&gt; "passwordauth",
		call =&gt; \&amp;formbuilder_setup);
        hook(type =&gt; "formbuilder", id =&gt; "passwordauth",
		call =&gt; \&amp;formbuilder);
} # }}}

sub formbuilder_setup (@) { #{{{
	my %params=@_;

	my $form=$params{form};
	my $session=$params{session};
	my $cgi=$params{cgi};

	if ($form-&gt;title eq "signin" || $form-&gt;title eq "register") {
		$form-&gt;field(name =&gt; "name", required =&gt; 0, size =&gt; 50);
		$form-&gt;field(name =&gt; "password", type =&gt; "password", required =&gt; 0);
		
		if ($form-&gt;submitted eq "Register" || $form-&gt;submitted eq "Create Account") {
			$form-&gt;field(name =&gt; "confirm_password", type =&gt; "password");
			$form-&gt;field(name =&gt; "email", size =&gt; 50);
			$form-&gt;title("register");
			$form-&gt;text("");
		}

		if ($form-&gt;submitted) {
			my $submittype=$form-&gt;submitted;
			# Set required fields based on how form was submitted.
			my %required=(
				"Login" =&gt; [qw(name password)],
				"Register" =&gt; [],
				"Create Account" =&gt; [qw(name password confirm_password email)],
				"Mail Password" =&gt; [qw(name)],
			);
			foreach my $opt (@{$required{$submittype}}) {
				$form-&gt;field(name =&gt; $opt, required =&gt; 1);
			}
	
			if ($submittype eq "Create Account") {
				$form-&gt;field(
					name =&gt; "confirm_password",
					validate =&gt; sub {
						shift eq $form-&gt;field("password");
					},
				);
				$form-&gt;field(
					name =&gt; "email",
					validate =&gt; "EMAIL",
				);
			}

			# Validate password against name for Login.
			if ($submittype eq "Login") {
				$form-&gt;field(
					name =&gt; "password",
					validate =&gt; sub {
						length $form-&gt;field("name") &amp;&amp;
						shift eq IkiWiki::userinfo_get($form-&gt;field("name"), 'password');
					},
				);
			}
			elsif ($submittype eq "Register" ||
			       $submittype eq "Create Account" ||
			       $submittype eq "Mail Password") {
				$form-&gt;field(name =&gt; "password", validate =&gt; 'VALUE');
			}
			
			# And make sure the entered name exists when logging
			# in or sending email, and does not when registering.
			if ($submittype eq 'Create Account' ||
			    $submittype eq 'Register') {
				$form-&gt;field(
					name =&gt; "name",
					validate =&gt; sub {
						my $name=shift;
						length $name &amp;&amp;
						$name=~/$config{wiki_file_regexp}/ &amp;&amp;
						! IkiWiki::userinfo_get($name, "regdate");
					},
				);
			}
			elsif ($submittype eq "Login" ||
			       $submittype eq "Mail Password") {
				$form-&gt;field( 
					name =&gt; "name",
					validate =&gt; sub {
						my $name=shift;
						length $name &amp;&amp;
						IkiWiki::userinfo_get($name, "regdate");
					},
				);
			}
		}
		else {
			# First time settings.
			$form-&gt;field(name =&gt; "name", comment =&gt; gettext("(use FirstnameLastName)"));
			if ($session-&gt;param("name")) {
				$form-&gt;field(name =&gt; "name", value =&gt; $session-&gt;param("name"));
			}
		}
	}
	elsif ($form-&gt;title eq "preferences") {
		$form-&gt;field(name =&gt; "name", disabled =&gt; 1, value =&gt;
			$session-&gt;param("name"), force =&gt; 1);
		$form-&gt;field(name =&gt; "password", type =&gt; "password");
		$form-&gt;field(name =&gt; "confirm_password", type =&gt; "password",
			validate =&gt; sub {
				shift eq $form-&gt;field("password");
			});
		
	}
}

sub formbuilder (@) { #{{{
	my %params=@_;

	my $form=$params{form};
	my $session=$params{session};
	my $cgi=$params{cgi};
	my $buttons=$params{buttons};

	if ($form-&gt;title eq "signin" || $form-&gt;title eq "register") {
		if ($form-&gt;submitted &amp;&amp; $form-&gt;validate) {
			if ($form-&gt;submitted eq 'Login') {
				$session-&gt;param("name", $form-&gt;field("name"));
				IkiWiki::cgi_postsignin($cgi, $session);
			}
			elsif ($form-&gt;submitted eq 'Create Account') {
				my $user_name=$form-&gt;field('name');
				if (IkiWiki::userinfo_setall($user_name, {
				    	'email' =&gt; $form-&gt;field('email'),
					'password' =&gt; $form-&gt;field('password'),
					'regdate' =&gt; time})) {
					$form-&gt;field(name =&gt; "confirm_password", type =&gt; "hidden");
					$form-&gt;field(name =&gt; "email", type =&gt; "hidden");
					$form-&gt;text(gettext("Account creation successful. Now you can Login."));
				}
				else {
					error(gettext("Error creating account."));
				}
			}
			elsif ($form-&gt;submitted eq 'Mail Password') {
				my $user_name=$form-&gt;field("name");
				my $template=template("passwordmail.tmpl");
				$template-&gt;param(
					user_name =&gt; $user_name,
					user_password =&gt; IkiWiki::userinfo_get($user_name, "password"),
					wikiurl =&gt; $config{url},
					wikiname =&gt; $config{wikiname},
					REMOTE_ADDR =&gt; $ENV{REMOTE_ADDR},
				);
			
				eval q{use Mail::Sendmail};
				error($@) if $@;
				sendmail(
					To =&gt; IkiWiki::userinfo_get($user_name, "email"),
					From =&gt; "$config{wikiname} admin &lt;$config{adminemail}&gt;",
					Subject =&gt; "$config{wikiname} information",
					Message =&gt; $template-&gt;output,
				) or error(gettext("Failed to send mail"));
			
				$form-&gt;text(gettext("Your password has been emailed to you."));
				$form-&gt;field(name =&gt; "name", required =&gt; 0);
				push @$buttons, "Mail Password";
			}
			elsif ($form-&gt;submitted eq "Register") {
				@$buttons="Create Account";
			}
		}
		elsif ($form-&gt;submitted eq "Create Account") {
			@$buttons="Create Account";
		}
		else {
			push @$buttons, "Register", "Mail Password";
		}
	}
	elsif ($form-&gt;title eq "preferences") {
		if ($form-&gt;submitted eq "Save Preferences" &amp;&amp; $form-&gt;validate) {
			my $user_name=$form-&gt;field('name');
	                foreach my $field (qw(password)) {
        	                if (defined $form-&gt;field($field) &amp;&amp; length $form-&gt;field($field)) {
					IkiWiki::userinfo_set($user_name, $field, $form-&gt;field($field)) ||
						error("failed to set $field");
	                        }
	                }
		}
	}
	
	IkiWiki::printheader($session);
	print IkiWiki::misctemplate($form-&gt;title, $form-&gt;render(submit =&gt; $buttons));
} #}}}

1
</pre></body></html>