###############################################################################
# Register.pl                                                                 #
###############################################################################
# YaBB: Yet another Bulletin Board                                            #
# Open-Source Community Software for Webmasters                               #
# Version:        YaBB 2.3.1                                                  #
# Packaged:       January 04, 2009                                            #
# Distributed by: http://www.yabbforum.com                                    #
# =========================================================================== #
# Copyright (c) 2000-2009 YaBB (www.yabbforum.com) - All Rights Reserved.     #
# Software by:  The YaBB Development Team                                     #
#               with assistance from the YaBB community.                      #
# Sponsored by: Xnull Internet Media, Inc. - http://www.ximinc.com            #
#               Your source for web hosting, web design, and domains.         #
###############################################################################

$registerplver = 'YaBB 2.3.1 $Revision: 1.57.2.23.2.26 $';
if ($action eq 'detailedversion') { return 1; }

if ($regcheck) { require "$sourcedir/Decoder.pl"; }
require "$sourcedir/Mailer.pl";
&LoadLanguage('Register');

if ($^O =~ /Win/) {
	my $regstyle = qq~ style="text-transform: lowercase"~;
} else {
	my $regstyle = '';
}

sub Register {
	if ($regtype == 0 && $iamguest) { &fatal_error("registration_disabled"); }
	my ($tmpregname, $tmprealname, $tmpregemail, $tmpregpasswrd1, $tmpregpasswrd2, $hidechecked);
	$yytitle = $register_txt{'97'};
	$yynavigation = qq~&rsaquo; $register_txt{'97'}~;
	if ($FORM{'reglanguage'}) {
		$language = $FORM{'reglanguage'};
		#&LoadLanguage('Main');
		#&LoadLanguage('Menu');
		&LoadLanguage('Register');
	}
	if ($FORM{'regusername'}) { $tmpregname     = $FORM{'regusername'}; }
	if ($FORM{'regrealname'}) { $tmprealname    = $FORM{'regrealname'}; }
	if ($FORM{'email'})       { $tmpregemail    = $FORM{'email'}; }
	if ($FORM{'hideemail'} || !exists $FORM{'hideemail'}) { $hidechecked = qq~ checked="checked"~; }
	if ($FORM{'passwrd1'})    { $tmpregpasswrd1 = $FORM{'passwrd1'}; }
	if ($FORM{'passwrd2'})    { $tmpregpasswrd2 = $FORM{'passwrd2'}; }
	if ($FORM{'reason'})      { $reason         = $FORM{'reason'}; }

	## moved langopt setup to subs.pl
	if (!$langopt) { &guestLangSel; }

	if ($regcheck) { &validation_code; }

	if (!$iamguest) { &fatal_error("no_registration_logged_in"); }
	if (-e "$vardir/email_domain_filter.txt" ) { require "$vardir/email_domain_filter.txt"; }
	if ($adomains) {
		@domains = split (/\,/, $adomains);
		$aedomains = qq~<table border="0" width="100%" cellspacing="0" cellpadding="0"><tr><td><input type="text" maxlength="30" name="email" value="$tmpregemail" size="15" /></td><td><select name="domain">~;
		foreach (@domains) { $aedomains .= ($_ =~ m/\@/) ? qq~<option value="$_">$_</option>~ : qq~<option value="\@$_">&#64;$_</option>~; }
		$aedomains .= qq~</select></td></tr></table>~;

	} else {
		$aedomains .= qq~<input type="text" maxlength="60" name="email" value="$tmpregemail" size="45" />~;
	}

	$yymain .= qq~
<div class="bordercolor" style="padding: 1px;">
<form action="$scripturl?action=register2" method="post" name="creator" onsubmit="return CheckRegFields();">
<table border="0" width="100%" cellspacing="0" class="bordercolor" cellpadding="4">
	<colgroup>
		<col width="80%" />
		<col width="*" />
	</colgroup>
	<tr>
		<td class="titlebg">
			<img src="$imagesdir/register.gif" alt="$register_txt{'97'}" title="$register_txt{'97'}" border="0" />
			<span class="text1"><b>$register_txt{'97'}</b> $register_txt{'517'}</span>
		</td>
		<td class="titlebg">
			&nbsp;
		</td>
	</tr>
	<tr>
		<td class="windowbg2" colspan="2" align="center">
			$register_txt{'97a'}
		</td>
	</tr>~;

	if ($morelang > 1) {
		$yymain .= qq~
	<tr>
		<td class="windowbg" align="left" valign="top">
			<b>$register_txt{'101'}</b>
		</td>
		<td class="windowbg" align="left" valign="top">
			<select name="reglanguage" onchange="document.creator.action='$scripturl?action=register'; document.creator.submit();">
			$langopt
			</select>
			<noscript><input type="submit" value="$maintxt{'32'}" class="button" /></noscript>
		</td>
	</tr>~;
	}
	$yymain .= qq~
	<tr>
		<td class="windowbg" align="left" valign="top">
			* <b>$register_txt{'98'}:</b><br />
			<span class="small">$register_txt{'520'}</span>
		</td>
		<td class="windowbg" align="left" valign="top">
			<input type="text" name="regusername" size="30" value="$tmpregname" maxlength="18"$regstyle />
			<input type="hidden" name="language" id="language" value="$language" />
		</td>
	</tr>
	<tr>
		<td class="windowbg" align="left" valign="top">
			* <b>$register_txt{'98a'}:</b>~;
	if ($name_cannot_be_userid) {
		$yymain .= qq~
			<br /><span class="small">$register_txt{'521'}</span>~;
	}
	$yymain .= qq~
		</td>
		<td class="windowbg" align="left" valign="top">
			<input type="text" name="regrealname" size="30" value="$tmprealname" maxlength="30" />
		</td>
	</tr>
	<tr>
		<td class="windowbg" align="left" valign="top">* <b>$register_txt{'69'}:</b>
			<br /><span class="small">$register_txt{'679'}</span>
		</td>
		<td class="windowbg" align="left" valign="top">
			$aedomains
	~;
	if ($allow_hide_email == 1) {
		$yymain .= qq~
			<br /><input type="checkbox" name="hideemail" value="checked"$hidechecked /> $register_txt{'721'}
		~;
	} 
	$yymain .= qq~
		</td>
	</tr>
	~;
	unless ($emailpassword) {
		$yymain .= qq~
	<tr>
		<td class="windowbg" align="left" valign="top">
			<div style="float:left; width: 75%;">* <b>$register_txt{'81'}:</b></div>
			<div style="float:left; width: 25%; height: 20px;">
				<div id="password-strength-meter" style="background: transparent url($imagesdir/empty_bar.gif) repeat-x center left; height: 4px"></div>
				<div class="pstrength-bar" id="passwrd1_bar" style="border: 1px solid #FFFFFF; height: 4px"></div>
				<div class="pstrength-info" id="passwrd1_text">&nbsp;</div>
			</div>
		</td>
		<td class="windowbg" align="left" valign="top">
			<script language="JavaScript1.2" type="text/javascript">
			<!--
				// Password_strength_meter start
				var verdects = new Array("$pwstrengthmeter_txt{'1'}","$pwstrengthmeter_txt{'2'}","$pwstrengthmeter_txt{'3'}","$pwstrengthmeter_txt{'4'}","$pwstrengthmeter_txt{'5'}","$pwstrengthmeter_txt{'6'}","$pwstrengthmeter_txt{'7'}","$pwstrengthmeter_txt{'8'}");
				var colors = new Array("#8F8F8F","#BF0000","#FF0000","#00A0FF","#33EE00","#339900");
				var scores = new Array($pwstrengthmeter_scores);
				var common = new Array($pwstrengthmeter_common);
				var minchar = $pwstrengthmeter_minchar;

				function runPassword(D) {
					var nPerc = checkPassword(D);
					if (nPerc > -199 && nPerc < 0) {
						strColor = colors[0];
						strText = verdects[1];
						strWidth = "5%";
					} else if (nPerc == -200) {
						strColor = colors[1];
						strText = verdects[0];
						strWidth = "0%";
					} else if (scores[0] == -1 && scores[1] == -1 && scores[2] == -1 && scores[3] == -1) {
						strColor = colors[4];
						strText = verdects[7];
						strWidth = "100%";
					} else if (nPerc <= scores[0]) {
						strColor = colors[1];
						strText = verdects[2];
						strWidth = "10%";
					} else if (nPerc > scores[0] && nPerc <= scores[1]) {
						strColor = colors[2];
						strText = verdects[3];
						strWidth = "25%";
					} else if (nPerc > scores[1] && nPerc <= scores[2]) {
						strColor = colors[3];
						strText = verdects[4];
						strWidth = "50%";
					} else if (nPerc > scores[2] && nPerc <= scores[3]) {
						strColor = colors[4];
						strText = verdects[5];
						strWidth = "75%";
					} else {
						strColor = colors[5];
						strText = verdects[6];
						strWidth = "100%";
					}
					document.getElementById("passwrd1_bar").style.width = strWidth;
					document.getElementById("passwrd1_bar").style.backgroundColor = strColor;
					document.getElementById("passwrd1_text").style.color = strColor;
					document.getElementById("passwrd1_text").childNodes[0].nodeValue = strText;
				}

				function checkPassword(C) {
					if (C.length == 0 || C.length < minchar) return -100;

					for (var D = 0; D < common.length; D++) {
						if (C.toLowerCase() == common[D]) return -200;
					}

					var F = 0;
					if (C.length >= minchar && C.length <= (minchar+2)) {
						F = (F + 6)
					} else if (C.length >= (minchar + 3) && C.length <= (minchar + 4)) {
						F = (F + 12)
					} else if (C.length >= (minchar + 5)) {
						F = (F + 18)
					}

					if (C.match(/[a-z]/)) {
						F = (F + 1)
					}
					if (C.match(/[A-Z]/)) {
						F = (F + 5)
					}
					if (C.match(/d+/)) {
						F = (F + 5)
					}
					if (C.match(/(.*[0-9].*[0-9].*[0-9])/)) {
						F = (F + 7)
					}
					if (C.match(/.[!,\@,#,\$,\%,^,&,*,?,_,\~]/)) {
						F = (F + 5)
					}
					if (C.match(/(.*[!,\@,#,\$,\%,^,&,*,?,_,\~].*[!,\@,#,\$,\%,^,&,*,?,_,\~])/)) {
						F = (F + 7)
					}
					if (C.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)){
						F = (F + 2)
					}
					if (C.match(/([a-zA-Z])/) && C.match(/([0-9])/)) {
						F = (F + 3)
					}
					if (C.match(/([a-zA-Z0-9].*[!,\@,#,\$,\%,^,&,*,?,_,\~])|([!,\@,#,\$,\%,^,&,*,?,_,\~].*[a-zA-Z0-9])/)) {
						F = (F + 3)
					}
					return F;
				}
				// Password_strength_meter end
			// -->
			</script>
			<input type="password" maxlength="30" name="passwrd1" id="passwrd1" value="$tmpregpasswrd1" size="30" onkeyup="runPassword(this.value);" />
		</td>
	</tr>
	<tr>
		<td class="windowbg" align="left" valign="top">
			* <b>$register_txt{'82'}:</b>
		</td>
		<td class="windowbg" align="left" valign="top">
			<input type="password" maxlength="30" name="passwrd2" value="$tmpregpasswrd2" size="30" />
		</td>
	</tr>~;
	}

	if ($addmemgroup_enabled == 1 || $addmemgroup_enabled == 3) {
		foreach (@nopostorder) {
			chomp $_;
			($title, undef, undef, undef, undef, undef, undef, undef, undef, undef, $additional) = split(/\|/, $NoPost{$_});
			if ($additional == 1) {
				$addmemgroup .= qq~<option value="$_">$title</option>~;
			}
		}
		my $additional_explain = $register_txt{'766'};
		if ($addmemgroup_enabled == 3) { $additional_explain = $register_txt{'767'}; }

		$yymain .= qq~
	<tr>
		<td class="windowbg" align="left" valign="top">
			<b>$register_txt{'765'}:</b>
			<br /><span class="small">$additional_explain</span>
		</td>
		<td class="windowbg" align="left" valign="top">
			<select name="joinmemgroup">
			<option value=""></option>
			$addmemgroup
			</select>
		</td>
	</tr>~;
	}

	if ($regcheck) {
		$yymain .= qq~
	<tr>
		<td class="windowbg" align="left" valign="top">
			* <b>$floodtxt{'1'}:</b><br />
			<span class="small">$floodtxt{'casewarning'}</span>
		</td>
		<td class="windowbg" align="left" valign="top">
			$showcheck
		</td>
	</tr>
	<tr>
		<td class="windowbg" align="left" valign="top">
			* <b>$floodtxt{'3'}:</b>
		</td>
		<td class="windowbg" align="left" valign="top">
			<input type="text" maxlength="30" name="verification" id="verification" size="30" />
		</td>
	</tr>~;
	}

	if ($regtype == 1) {
		$yymain .= qq~
	<tr>
		<td class="windowbg" align="left" valign="top">
			* <b>$prereg_txt{'regreason'}:</b><br />
			<span class="small">$prereg_txt{'reason_exp'}</span><br /><br />
		</td>
		<td class="windowbg" align="left" valign="top">
			<textarea cols="60" rows="7" name="reason" id="reason">$reason</textarea><br />
			<span class="small">$prereg_txt{'16'} <input value="$RegReasonSymbols" size="3" name="msgCL" class="windowbg" style="border: 0px; width: 40px; padding: 1px; font-size: 11px;" readonly="readonly" /></span>
		</td>
	</tr>

	<script type="text/javascript" language="JavaScript">
	<!--
	var supportsKeys = false;
	function tick() {
		calcCharLeft(document.forms[0]);
		if (!supportsKeys) { timerID = setTimeout("tick()",$RegReasonSymbols); }
	}
	function calcCharLeft(sig) {
		clipped = false;
		maxLength = $RegReasonSymbols;
		if (document.creator.reason.value.length > maxLength) {
			document.creator.reason.value = document.creator.reason.value.substring(0,maxLength);
			charleft = 0;
			clipped = true;
		} else {
			charleft = maxLength - document.creator.reason.value.length;
		}
		document.creator.msgCL.value = charleft;
		return clipped;
	}
	tick();
	//-->
	</script>
		~;
	}
	if ($extendedprofiles) {
		require "$sourcedir/ExtendedProfiles.pl";
		$yymain .= &ext_register;
	}
	if ($RegAgree) {
		if ($language) {
			fopen(AGREE, "$langdir/$language/agreement.txt");
		} else {
			fopen(AGREE, "$langdir/$lang/agreement.txt");
		}
		@agreement = <AGREE>;
		fclose(AGREE);
		$fullagree = join("", @agreement);
		$fullagree =~ s/\n/<br \/>/g;
		$yymain .= qq~
	<tr>
		<td colspan="2" class="titlebg">
			<img src="$imagesdir/xx.gif" alt="$register_txt{'764a'}" title="$register_txt{'764a'}" border="0" /> <b>$register_txt{'764a'}</b>
		</td>
	</tr>
	<tr>
		<td colspan="2" class="windowbg2">
			<span style="float: left; padding: 5px;"><br />$fullagree<br /><br /></span>
		</td>
	</tr>
	<tr>
		<td colspan="2" class="catbg" align="center">
			<b>$register_txt{'585'}</b> <input type="radio" name="regagree" value="yes" /> &nbsp;&nbsp;&nbsp; <b>$register_txt{'586'}</b> <input type="radio" name="regagree" value="no" />
		</td>
	</tr>~;
	}
	$yymain .= qq~
	<tr>
		<td colspan="2" align="center" class="windowbg">
			$register_txt{'95'}
		</td>
	</tr>
	<tr class="titlebg">
		<td colspan="2" align="center">
			<br /><input type="submit" value="$register_txt{'97'}" class="button" /><br /><br />
		</td>
	</tr>
</table>
</form>
</div>
<script type="text/javascript" language="JavaScript">
<!--
	document.creator.regusername.focus();

	function CheckRegFields() {
		if (document.creator.regusername.value == '') {
			alert("$register_txt{'error_username'}");
			document.creator.regusername.focus();
			return false;
		}
		if (document.creator.regusername.value == document.creator.passwrd1.value || document.creator.regrealname.value == document.creator.passwrd1.value) {
			alert("$register_txt{'error_usernameispass'}");
			document.creator.regusername.focus();
			return false;
		}
		if (document.creator.regrealname.value == '') {
			alert("$register_txt{'error_realname'}");
			document.creator.regrealname.focus();
			return false;
		}~ .

		($name_cannot_be_userid ? qq~
		if (document.creator.regusername.value == document.creator.regrealname.value) {
			alert("$register_txt{'error_name_cannot_be_userid'}");
			document.creator.regrealname.focus();
			return false;
		}~ : '')

		. qq~
		if (document.creator.email.value == '') {
			alert("$register_txt{'error_email'}");
			document.creator.email.focus();
			return false;
		}
		if ($emailpassword == 0) {
			if (document.creator.passwrd1.value == '' || document.creator.passwrd2.value == '') {
				alert("$register_txt{'error_pass1'}");
				document.creator.passwrd1.focus();
				return false;
			}
			if (document.creator.passwrd1.value != document.creator.passwrd2.value) {
				alert("$register_txt{'error_pass2'}");
				document.creator.passwrd1.focus();
				return false;
			}
		}
		if ($regcheck > 0 && document.creator.verification.value == '') {
			alert("$register_txt{'error_verification'}");
			document.creator.verification.focus();
			return false;
		}
		if ($regtype == 1 && document.creator.reason.value == '') {
			alert("$register_txt{'error_reason'}");
			document.creator.reason.focus();
			return false;
		}
		if ($RegAgree > 0 && document.creator.regagree[0].checked != true) {
			alert("$register_txt{'error_agree'}");
			return false;
		}

		return true;
	}
//-->
</script>
	~;
	&template;
}

sub Register2 { 
	if (!$regtype) { &fatal_error("registration_disabled"); } 
	if ($RegAgree && $FORM{'regagree'} ne 'yes') { &fatal_error('no_regagree'); }
	my %member;
	while (($key, $value) = each(%FORM)) {
		$value =~ s~\A\s+~~;
		$value =~ s~\s+\Z~~;
		unless($key eq "reason") {$value =~ s~[\n\r]~~g;}
		$member{$key} = $value;
	}
	if ($member{'domain'}) {$member{'email'} = qq($member{'email'}$member{'domain'});} 
	&email_domain_check($member{'email'});
	$member{'regusername'} =~ s/\s/_/g;

	# Make sure users can't register with banned details
	&banning("$member{'regusername'}", "$member{'email'}");

	# check if there is a system hash named like this by checking existence through size
	&fatal_error("system_prohibited_id", "($member{'regusername'})") if keys(%{ $member{'regusername'} }) > 0;
	&fatal_error("id_to_long","($member{'regusername'})") if length($member{'regusername'}) > 25;
	&fatal_error("email_to_long","($member{'email'})") if length($member{'email'}) > 60;
	&fatal_error("realname_to_long","($member{'regrealname'})") if length($member{'regrealname'}) > 30;
	&fatal_error("no_username","($member{'regusername'})") if $member{'regusername'} eq '';
	&fatal_error("id_alfa_only","($member{'regusername'})") if $member{'regusername'} eq '_';
	&fatal_error("id_reserved","$member{'regusername'}") if $member{'regusername'} =~ /guest/i;
	&fatal_error("invalid_character","$register_txt{'35'} $register_txt{'241re'}") if $member{'regusername'} =~ /[^\w\+\-\.\@]/;
	&fatal_error("no_email","($member{'regusername'})") if $member{'email'} eq "";
	&fatal_error("id_taken","($member{'regusername'})") if -e ("$memberdir/$member{'regusername'}.vars");
	&fatal_error("password_is_userid") if $member{'regusername'} eq $member{'passwrd1'};
	&fatal_error("no_reg_reason") if $member{'reason'} eq "" && $regtype == 1;

	&FromChars($member{'regrealname'});
	$convertstr = $member{'regrealname'};
	$convertcut = 30;
	&CountChars;
	$member{'regrealname'} = $convertstr;
	&fatal_error("name_too_long") if $cliped;

	&fatal_error('invalid_character', "$register_txt{'38'} $register_txt{'241re'}") if $member{'regrealname'} =~ /[^ \w\x80-\xFF\[\]\(\)#\%\+,\-\|\.:=\?\@\^]/;

	if ($name_cannot_be_userid && lc $member{'regusername'} eq lc $member{'regrealname'}) { &fatal_error('name_is_userid'); }

	if (lc $member{'regusername'} eq lc &MemberIndex("check_exist", $member{'regusername'})) { &fatal_error("id_taken","($member{'regusername'})"); }
	if (lc $member{'email'} eq lc &MemberIndex("check_exist", $member{'email'})) { &fatal_error("email_taken","($member{'email'})"); }
	if (lc $member{'regrealname'} eq lc &MemberIndex("check_exist", $member{'regrealname'})) { &fatal_error("name_taken"); }

	&ToHTML($member{'email'});
	&ToHTML($member{'regrealname'});
	&ToChars($member{'regrealname'});

	if ($regtype == 1) {
		my $reason_len = $member{'reason'};
		$reason_len =~ s/[\r\n ]//ig;
		$reason_len =~ s/&#\d{3,}?\;/X/ig;
		if (length($reason_len) > $RegReasonSymbols) {
			$member{'reason'} = substr($member{'reason'}, 0, $RegReasonSymbols) . '...';
		}
		undef $reason_len;
		&ToHTML($member{'reason'});
		$member{'reason'} =~ s~\r~~g;
		$member{'reason'} =~ s~\n~<br \/>~g;
	}

	if ($regcheck) { &validation_check($member{'verification'}); }

	if ($emailpassword) {
		srand();
		$member{'passwrd1'} = int(rand(100));
		$member{'passwrd1'} =~ tr/0123456789/ymifxupbck/;
		$_ = int(rand(77));
		$_ =~ tr/0123456789/q8dv7w4jm3/;
		$member{'passwrd1'} .= $_;
		$_ = int(rand(89));
		$_ =~ tr/0123456789/y6uivpkcxw/;
		$member{'passwrd1'} .= $_;
		$_ = int(rand(188));
		$_ =~ tr/0123456789/poiuytrewq/;
		$member{'passwrd1'} .= $_;
		$_ = int(rand(65));
		$_ =~ tr/0123456789/lkjhgfdaut/;
		$member{'passwrd1'} .= $_;
	} else {
		&fatal_error("password_mismatch","($member{'regusername'})") if $member{'passwrd1'} ne $member{'passwrd2'};
		&fatal_error("no_password","($member{'regusername'})") if $member{'passwrd1'} eq '';
		&fatal_error("invalid_character","$register_txt{'36'} $register_txt{'241'}") if $member{'passwrd1'} =~ /[^\s\w!\@#\$\%\^&\*\(\)\+\|`~\-=\\:;'",\.\/\?\[\]\{\}]/;
	}
	&fatal_error("invalid_character","$register_txt{'69'} $register_txt{'241e'}") if $member{'email'} !~ /^[\w\-\.\+]+\@[\w\-\.\+]+\.\w{2,4}$/;
	&fatal_error("invalid_email") if $member{'email'} =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(\.$)/ || $member{'email'} !~ /\A.+@\[?(\w|[-.])+\.[a-zA-Z]{2,4}|[0-9]{1,4}\]?\Z/;
	fopen(BAN, "$vardir/ban_email.txt");
	@banned = <BAN>;
	fclose(BAN);
	foreach $curban (@banned) {
		if ($member{'email'} eq "$curban") { &fatal_error("banned","$register_txt{'678'}$register_txt{'430'}!"); }
	}

	fopen(RESERVE, "$vardir/reserve.txt") || &fatal_error("cannot_open","$vardir/reserve.txt", 1);
	@reserve = <RESERVE>;
	fclose(RESERVE);
	fopen(RESERVECFG, "$vardir/reservecfg.txt") || &fatal_error("cannot_open","$vardir/reservecfg.txt", 1);
	@reservecfg = <RESERVECFG>;
	fclose(RESERVECFG);
	for ($a = 0; $a < @reservecfg; $a++) {
		chomp $reservecfg[$a];
	}
	$matchword = $reservecfg[0] eq 'checked';
	$matchcase = $reservecfg[1] eq 'checked';
	$matchuser = $reservecfg[2] eq 'checked';
	$matchname = $reservecfg[3] eq 'checked';
	$namecheck = $matchcase eq 'checked' ? $member{'regusername'} : lc $member{'regusername'};
	$realnamecheck = $matchcase eq 'checked' ? $member{'regrealname'} : lc $member{'regrealname'};

	foreach $reserved (@reserve) {
		chomp $reserved;
		$reservecheck = $matchcase ? $reserved : lc $reserved;
		if ($matchuser) {
			if ($matchword) {
				if ($namecheck eq $reservecheck) { &fatal_error('id_reserved',"$reserved"); }
			} else {
				if ($namecheck =~ $reservecheck) { &fatal_error('id_reserved',"$reserved"); }
			}
		}
		if ($matchname) {
			if ($matchword) {
				if ($realnamecheck eq $reservecheck) { &fatal_error('name_reserved',"$reserved"); }
			} else {
				if ($realnamecheck =~ $reservecheck) { &fatal_error('name_reserved',"$reserved"); }
			}
		}
	}

	if ($default_template) { $new_template = $default_template; }
	else { $new_template = qq~Forum default~; }

	# check if user isn't already registered
	&fatal_error("id_taken") if (-e ("$memberdir/$member{'regusername'}.vars"));
	# check if user isn't already in pre-registration
	&fatal_error("already_preregged") if (-e ("$memberdir/$member{'regusername'}.pre"));
	&fatal_error("already_preregged") if (-e ("$memberdir/$member{'regusername'}.wait"));

	if ($new_template !~ m^\A[0-9a-zA-Z\_\(\)\ \#\%\-\:\+\?\$\&\~\.\,\@]+\Z^ && $new_template ne '') { &fatal_error('invalid_template'); }
	if ($member{'language'} !~ m^\A[0-9a-zA-Z\_\(\)\ \#\%\-\:\+\?\$\&\~\.\,\@]+\Z^ && $member{'language'} ne '') { &fatal_error('invalid_language'); }

	&ToHTML($member{'hideemail'});
	&ToHTML($member{'language'});

	$member{'joinmemgroup'} = '';
	if (($addmemgroup_enabled == 1 || $addmemgroup_enabled == 3) && $FORM{'joinmemgroup'} ne '') {
		foreach (keys %NoPost) {
			my $additional = (split /\|/, $NoPost{$_})[10];
			if ($additional == 1 && $FORM{'joinmemgroup'} eq $_) { $member{'joinmemgroup'} = $_; }
		}
		&ToHTML($member{'joinmemgroup'});
	}

	$reguser = $member{'regusername'};
	$registerdate = &timetostring($date);
	$language = $member{'language'};

	${$uid.$reguser}{'password'} = &encode_password($member{'passwrd1'});
	${$uid.$reguser}{'realname'} = $member{'regrealname'};
	${$uid.$reguser}{'email'} = lc($member{'email'});
	${$uid.$reguser}{'postcount'} = 0;
	${$uid.$reguser}{'regreason'} = $member{'reason'};
	${$uid.$reguser}{'usertext'} = $defaultusertxt; 
	${$uid.$reguser}{'userpic'} = "blank.gif";
	${$uid.$reguser}{'regdate'} = $registerdate;
	${$uid.$reguser}{'regtime'} = $date;
	${$uid.$reguser}{'timeselect'} = $timeselected;
	${$uid.$reguser}{'timeoffset'} = $timeoffset;
	${$uid.$reguser}{'dsttimeoffset'} = $dstoffset;
	${$uid.$reguser}{'lastips'} = $user_ip;
	${$uid.$reguser}{'hidemail'} = $member{'hideemail'};
	${$uid.$reguser}{'timeformat'} = qq~MM D+ YYYY @ HH:mm:ss*~;
	${$uid.$reguser}{'template'} = $new_template;
	${$uid.$reguser}{'language'} = $language;
	${$uid.$reguser}{'pageindex'} = qq~1|1|1|1~;
	${$uid.$reguser}{'addgroups'} = $member{'joinmemgroup'};

	if ($regtype == 1 || $regtype == 2) {
		my (@reglist,@x);
		# If a pre-registration list exists load it
		if (-e "$memberdir/memberlist.inactive") {
			fopen(INACT, "$memberdir/memberlist.inactive");
			@reglist = <INACT>;
			fclose(INACT);
		}
		# If a approve-registration list exists load it too
		if (-e "$memberdir/memberlist.approve") {
			fopen(APPROVE, "$memberdir/memberlist.approve");
			push(@reglist, <APPROVE>);
			fclose(APPROVE);
		}
		foreach (@reglist) {
			@x = split(/\|/, $_);
			if ($reguser eq $x[2]) { &fatal_error("already_preregged"); }
			if (lc $member{'email'} eq lc $x[4]) { &fatal_error("email_already_preregged"); }
		}

		# create pre-registration .pre file and write log and inactive list
		if ($regcheck) { &validation_code; }
		$activationcode = substr($sessionid, 0, 20);

		if ($extendedprofiles) {
			require "$sourcedir/ExtendedProfiles.pl";
			my $error = &ext_validate_submition($reguser,$reguser);
			if ($error ne "") { &fatal_error("extended_profiles_validation",$error); }
			&ext_saveprofile($reguser);
		}

		&UserAccount($reguser, "preregister");
		if ($do_scramble_id) { $cryptuser = &cloak($reguser); } else { $cryptuser = $reguser; }
		fopen(INACT, ">>$memberdir/memberlist.inactive", 1);
		print INACT "$date|$activationcode|$reguser|$member{'passwrd1'}|$member{'email'}|\n";
		fclose(INACT);

		fopen(REGLOG, ">>$vardir/registration.log", 1);
		print REGLOG "$date|N|$member{'regusername'}|\n";
		fclose(REGLOG);

		## send an e-mail to the user that registration is pending e-mail validation within the given timespan. ##
		my $templanguage = $language;
		$language = $member{'language'};
		&LoadLanguage('Email');
		&sendmail(${$uid.$reguser}{'email'}, "$mailreg_txt{'apr_result_activate'} $mbname", &template_email($preregemail, {'displayname' => $member{'regrealname'}, 'username' => $reguser, 'cryptusername' => $cryptuser, 'password' => $member{'passwrd1'}, 'activationcode' => $activationcode, 'preregspan' => $preregspan}),'',$emailcharset);
		$language = $templanguage;
		$yymain .= qq~
			<div class="bordercolor" style="width: 650px; margin-bottom: 8px; margin-left: auto; margin-right: auto;">
				<div class="bordercolor" style="width: 650px; margin-bottom: 8px; margin-left: auto; margin-right: auto;">
					<table cellpadding="4" cellspacing="1" border="0" width="100%" align="center">
						<tr><td class="titlebg"><img src="$imagesdir/register.gif" alt="$prereg_txt{'1a'}" title="$prereg_txt{'1a'}" border="0" /><b>$prereg_txt{'1a'}</b></td></tr>
						<tr><td class="windowbg" align="left">$prereg_txt{'1'}</td></tr>
					</table>
				</div>
			</div>~;
		$yytitle = "$prereg_txt{'1a'}";

	} else {
		if ($extendedprofiles) {
			require "$sourcedir/ExtendedProfiles.pl";
			my $error = &ext_validate_submition($reguser,$reguser);
			if ($error ne "") { &fatal_error("extended_profiles_validation",$error); }
			&ext_saveprofile($reguser);
		}
		&UserAccount($reguser, "register");
		&MemberIndex("add", $reguser);
		&FormatUserName($reguser);

		if ($send_welcomeim == 1) {
			# new format msg file:
			# messageid|(from)user|(touser(s))|(ccuser(s))|(bccuser(s))|subject|date|message|(parentmid)|reply#|ip|messagestatus|flags|storefolder|attachment
			$messageid = $^T . $$;
			fopen(IM, ">$memberdir/$member{'regusername'}.msg", 1);
			print IM "$messageid|$sendname|$member{'regusername'}|||$imsubject|$date|$imtext|$messageid|0|$ENV{'REMOTE_ADDR'}|s|u||\n";
			fclose(IM);
		}
		if ($new_member_notification) {
			my $templanguage = $language;
			$language = $lang;
			&LoadLanguage('Email');
			&sendmail($new_member_notification_mail, $mailreg_txt{'new_member_info'}, &template_email($newmemberemail, {'displayname' => $member{'regrealname'}, 'username' => $reguser, 'userip' => $user_ip, 'useremail' => ${$uid.$reguser}{'email'}}),'',$emailcharset);
			$language = $templanguage;
		}

		if ($emailpassword) {
			my $templanguage = $language;
			$language = $member{'language'};
			&LoadLanguage('Email');
			&sendmail(${$uid.$reguser}{'email'}, "$mailreg_txt{'apr_result_info'} $mbname", &template_email($passwordregemail, {'displayname' => $member{'regrealname'}, 'username' => $reguser, 'password' => $member{'passwrd1'}}),'',$emailcharset);
			$language = $templanguage;
			$yymain .= qq~
<div class="bordercolor" style="width: 650px; margin-bottom: 8px; margin-left: auto; margin-right: auto;">
		<table cellpadding="4" cellspacing="1" border="0" width="100%" align="center">
			<tr><td class="titlebg"><b>$register_txt{'97'}</b></td></tr>
			<tr><td class="windowbg" align="left">$register_txt{'703'}</td></tr>
			<tr><td class="windowbg2" align="left">$register_txt{'704'}</td></tr>
		</table>
</div>~;
		} else {
			if ($emailwelcome) {
				my $templanguage = $language;
				$language = $member{'language'};
				&LoadLanguage('Email');
				&sendmail(${$uid.$reguser}{'email'}, "$mailreg_txt{'apr_result_info'} $mbname", &template_email($welcomeregemail, {'displayname' => $member{'regrealname'}, 'username' => $reguser, 'password' => $member{'passwrd1'}}),'',$emailcharset);
				$language = $templanguage;
			}
			$yymain .= qq~
			<br /><br />
			<form action="$scripturl?action=login2" method="post">
			<table border="0" width="300" cellspacing="1" class="bordercolor" align="center">
			<tr>
			<td class="titlebg">
			<img src="$imagesdir/register.gif" alt="$register_txt{'97'}" title="$register_txt{'97'}" border="0" /> <span class="text1"><b>$register_txt{'97'}</b></span></td>
			</tr><tr>
			<td class="windowbg" align="center">
			<br />$register_txt{'431'}<br /><br />
			<input type="hidden" name="username" value="$member{'regusername'}" />
			<input type="hidden" name="passwrd" value="$member{'passwrd1'}" />
			<input type="hidden" name="cookielength" value="$Cookie_Length" />
			<input type="submit" value="$register_txt{'34'}" class="button" />
			</td>
			</tr>
			</table>
			</form>
			<br /><br />
			~;
		}
		$yytitle = "$register_txt{'245'}";
	}
	&template;
}

sub activation_check {
	$changed  = 0;
	$timer    = $date;
	$timespan = $preregspan * 3600;
	fopen(INACT, "$memberdir/memberlist.inactive");
	@actlist = <INACT>;
	fclose(INACT);

	# check if user is in pre-registration and check activation key
	foreach (@actlist) {
		($regtime, undef, $regmember, undef) = split(/\|/, $_, 4);
		$difftime = $timer - $regtime;
		if ($difftime > $timespan) {
			$changed = 1;
			unlink "$memberdir/$regmember.pre";

			# add entry to registration log
			fopen(REGLOG, ">>$vardir/registration.log", 1);
			print REGLOG "$timer|T|$regmember|\n";
			fclose(REGLOG);
		} else {
			# update non activate user list
			# write valid registration to the list again
			push(@outlist, $_);
		}
	}
	if ($changed) {
		# re-open inactive list for update if changed
		fopen(INACT, ">$memberdir/memberlist.inactive", 1);
		print INACT @outlist;
		fclose(INACT);
	}
}

sub user_activation {
	$changed       = 0;
	$reguser       = $_[0] || $INFO{'username'};
	$activationkey = $_[1] || $INFO{'activationkey'};
	&fatal_error('wrong_id') unless $reguser;
	if ($do_scramble_id) { $reguser = &decloak($reguser); }
	if (!-e "$memberdir/$reguser.pre" && -e "$memberdir/$reguser.vars") { &fatal_error("already_activated"); }
	if (!-e "$memberdir/$reguser.pre") { &fatal_error("prereg_expired"); }
	# If a pre-registration list exists load it
	if (-e "$memberdir/memberlist.inactive") {
		fopen(INACT, "$memberdir/memberlist.inactive");
		@reglist = <INACT>;
		fclose(INACT);
	} else {
		# add entry to registration log
		fopen(REGLOG, ">>$vardir/registration.log", 1);
		print REGLOG "$date|E|$reguser|\n";
		fclose(REGLOG);
		&fatal_error("prereg_expired");
	}
	if ($regtype == 1 && -e "$memberdir/memberlist.approve") {
		fopen(APR, "$memberdir/memberlist.approve");
		@aprlist = <APR>;
		fclose(APR);
	}

	# check if user is in pre-registration and check activation key
	foreach (@reglist) {
		($regtime, $testkey, $regmember, $regpassword, undef) = split(/\|/, $_, 5);

		if ($regmember ne $reguser) {
			push(@chnglist, $_); # update non activate user list
		} else {
			my $templanguage = $language;
			if ($activationkey ne $testkey) {
				fopen(REGLOG, ">>$vardir/registration.log", 1);
				print REGLOG "$date|E|$reguser|\n"; # add entry to registration log
				fclose(REGLOG);
				&fatal_error("wrong_code");

			} elsif ($regtype == 1) {
				# user is in list and the keys match, so move him/her for admin approval
				unshift(@aprlist, $_);

				rename("$memberdir/$reguser.pre", "$memberdir/$reguser.wait");

				# add entry to registration log
				if ($iamadmin || $iamgmod) { $actuser = $username; } else { $actuser = $reguser; }
				fopen(REGLOG, ">>$vardir/registration.log", 1);
				print REGLOG "$date|W|$reguser|$actuser\n";
				fclose(REGLOG);

				&LoadUser($reguser);
				$language = ${$uid.$reguser}{'language'};
				&LoadLanguage('Email');
				&sendmail(${$uid.$reguser}{'email'}, "$mailreg_txt{'apr_result_wait'} $mbname", &template_email($approveregemail, {'username' => $reguser, 'displayname' => ${$uid.$reguser}{'realname'}}),'',$emailcharset);

			} elsif ($regtype == 2) {
				&LoadUser($reguser);
				# ckeck if email is allready in active use
				if (lc ${$uid.$reguser}{'email'} eq lc &MemberIndex("check_exist", ${$uid.$reguser}{'email'})) {
					&fatal_error("email_taken", "(${$uid.$reguser}{'email'})");
				}

				# user is in list and the keys match, so let him/her in
				rename("$memberdir/$reguser.pre", "$memberdir/$reguser.vars");
				&MemberIndex("add", $reguser);

				if ($iamadmin || $iamgmod) { $actuser = $username; } else { $actuser = $reguser; }
				# add entry to registration log
				fopen(REGLOG, ">>$vardir/registration.log", 1);
				print REGLOG "$date|A|$reguser|$actuser\n";
				fclose(REGLOG);

				if ($emailpassword) {
					chomp $regpassword;
					$language = ${$uid.$reguser}{'language'};
					&LoadLanguage('Email');
					&sendmail(${$uid.$reguser}{'email'}, "$mailreg_txt{'apr_result_validate'} $mbname", &template_email($activatedpassregemail, {'displayname' => ${$uid.$reguser}{'realname'}, 'username' => $reguser, 'password' => $regpassword}),'',$emailcharset);
					$yymain .= qq~<br /><table border="0" width="100%" cellspacing="1" class="bordercolor" align="center">~;
					$sharedLogin_title = $register_txt{'97'};
					$sharedLogin_text  = $register_txt{'703'};
					$yymain .= qq~</table>~;

				} elsif ($emailwelcome) {
					chomp $regpassword;
					$language = ${$uid.$reguser}{'language'};
					&LoadLanguage('Email');
					&sendmail(${$uid.$reguser}{'email'}, "$mailreg_txt{'apr_result_validate'} $mbname", &template_email($activatedwelcomeregemail, {'displayname' => ${$uid.$reguser}{'realname'}, 'username' => $reguser, 'password' => $regpassword}),'',$emailcharset);
				}
			}

			if ($send_welcomeim == 1) {
				# new format msg file:
				# messageid|(from)user|(touser(s))|(ccuser(s))|(bccuser(s))|subject|date|message|(parentmid)|reply#|ip|messagestatus|flags|storefolder|attachment
				$messageid = $^T . $$;
				fopen(INBOX, ">$memberdir/$reguser.msg");
				print INBOX "$messageid|$sendname|$reguser|||$imsubject|$date|$imtext|$messageid|0|$ENV{'REMOTE_ADDR'}|s|u||\n";
				fclose(INBOX);
			}
			if ($new_member_notification) {
				$language = $lang;
				&LoadLanguage('Email');
				&sendmail($new_member_notification_mail, $mailreg_txt{'new_member_info'}, &template_email($newmemberemail, {'displayname' => ${$uid.$reguser}{'realname'}, 'username' => $reguser, 'userip' => $user_ip, 'useremail' => ${$uid.$reguser}{'email'}}),'',$emailcharset);
			}
			$language = $templanguage;
			$changed = 1;
		}
	}

	if ($changed) {
		# if changed write new inactive list
		fopen(INACT, ">$memberdir/memberlist.inactive");
		print INACT @chnglist;
		fclose(INACT);
		# update approval user list
		if ($regtype == 1) {
			fopen(APR, ">$memberdir/memberlist.approve");
			print APR @aprlist;
			fclose(APR);
		}
	} else {
		# add entry to registration log
		fopen(REGLOG, ">>$vardir/registration.log", 1);
		print REGLOG "$date|E|$reguser|\n";
		fclose(REGLOG);
		&fatal_error("wrong_id");
	}

	if ($regtype == 1) {
			$yymain .= qq~
				<div class="bordercolor" style="width: 650px; margin-bottom: 8px; margin-left: auto; margin-right: auto;">
					<div class="bordercolor" style="width: 650px; margin-bottom: 8px; margin-left: auto; margin-right: auto;">
						<table cellpadding="4" cellspacing="1" border="0" width="100%" align="center">
							<tr><td class="titlebg"><img src="$imagesdir/register.gif" alt="$prereg_txt{'1a'}" title="$prereg_txt{'1a'}" border="0" /><b>$prereg_txt{'1a'}</b></td></tr>
							<tr><td class="windowbg" align="left">$prereg_txt{'13'}</td></tr>
						</table>
					</div>
				</div>~;
			$yytitle = "$prereg_txt{'1b'}";

	} elsif ($regtype == 2) {
		$yymain .= qq~
		<br /><br />
		<table border="0" width="650" cellspacing="1" class="bordercolor" align="center">
		<tr>
		<td colspan="2" class="titlebg">
		<img src="$imagesdir/register.gif" alt="$prereg_txt{'1a'}" title="$prereg_txt{'1a'}" border="0" /> <span class="text1"><b>$prereg_txt{'1a'}</b></span></td>
		</tr><tr>
		<td colspan="2" class="windowbg" align="center">
		<br />$prereg_txt{'5'}~;
		$yymain .= $prereg_txt{'5a'} unless $emailpassword;
		$yymain .= qq~$prereg_txt{'5b'}<br /><br />~;
		if ($emailpassword) {
			$yymain .= qq~$register_txt{'703'}<br /> <br />~;
		}
		$yymain .= qq~
		</td>
		</tr>
		</table>
		~;

		if (!$iamadmin && !$iamgmod) {
			unless ($emailpassword) {
				$yymain .= qq~<div class="bordercolor" style="width: 650px; margin-bottom: 8px; margin-left: auto; margin-right: auto;">~;
				require "$sourcedir/LogInOut.pl";
				$yymain .= &sharedLogin;
				$yymain .= qq~</div>~;
			} else {
				$yymain .= qq~<br /><br />~;
			}
		}
		$yytitle = "$prereg_txt{'5'}";
	}

	if ($iamadmin || $iamgmod) {
		$yySetLocation = qq~$adminurl?action=view_reglog~;
		&redirectexit;
	} else {
		&template;
	}
}

1;