#!/usr/bin/perl


print "Content-type: TEXT/HTML\n\n";

$cr=chr(0x0D).chr(0x0A);
$cl=chr(0x0A);

$cs=$ENV{'QUERY_STRING'};
$CONTENT_LENGTH=$ENV{'CONTENT_LENGTH'};
if(length$cs<=0){read STDIN,$cs,$CONTENT_LENGTH;}

$cs=~s/\+/ /g;$cs=~s/\|\|/\/\//g;

@a=split(/&/,$cs);
for(@a){replacx($_)};


for$i(0..$#a)
{	($a1,$a2)=split(/=/,$a[$i],2);
	if($a1 eq 'registration'){$reg=$a2}
	if($a1 eq 'admin'){$admin=$a2}
	if($a1 eq 'pass'){$pass=$a2}
	if($a1 eq 'submit'){$submit=$a2}
	if($a1 eq 'text'){$text=$a2}
	if($a1 eq 'info'){$info=$a2}
	if($a1 eq 'uname'){$uname=$a2}
	if($a1 eq 'fid'){$fid=$a2}
}

if(length$submit){goto SUBMIT}
if(length$admin){goto ADMIN}



if(!length$cs)
{	print "<html><title>Registration</title><center>Registrations of White Tiger WWW Board</center><p>
<body bgcolor=#00dddd><form action=/cgi-bin/wtboardreg.cgi method=post><table width=100%>
<tr><td>Name for registration/Имя для регистрации:</td><td><input type=text name=uname size=30></td></tr>
<tr><td>Forum identificator (fid)/Идентификатор форума:</td><td><input type=text name=fid size=10></td></tr>
<tr><td>Other info?/Другая информация?</td><td><input type=text name=info size=30></td></tr>

</table><p><center><input type=submit name=submit value=\"Add\"></form></center>
<p>Введите имя для регистрации и идентификатор форума. Если идентификатор не
указан, считается равным 0. Поле &#171;Другая информация&#187; можно оставить
пустым. Одному форуму (fid) соответствует только один регистрационный ключ.
<p>Полученный регистрационный ключ именно в том виде, как он будет выдан,
поместите в качестве значения параметра <b>registration</b> или укажите через
администрацию: Администрирование/Основные настройки/Регистрационный ключ.

</body></html>";

	exit;
}



if(!length$reg){$str='<i>This forum works on <font color=$errvlcolor>unregistered</font> copy of White Tiger WWW Board.</i>';$noreg='nr'}
else
{	($a,$b)=split(/\/\//,$reg);
	$name=$a;

	$a=$fid.$a;
	$a=join('x',reverse(split(/[ a]/,$a)));
	$a=join('a',reverse(split(/i/,$a)));
	$a=join('z',reverse(split(/e/,$a)));
	$a=crypt($a,'WT');
	$a=~s/[\/\.]//g;
	for(split(//,$a)){$number+=((ord$_)**2)}

	open NAM,"../data/wtbregsts.txt";@b=<NAM>;close NAM;chomp @b;

	$noreg='nr';
	$ste=0;$str='<i>This forum works on <font color=$errvlcolor>unregistered</font> copy of White Tiger WWW Board.</i>';
	for(@b)
	{	@c=split(/;;/);
		if($c[0] eq $name){$str="<i>This forum works on White Tiger WWW Board registered by <font color=\$errvlcolor>$name</font> but not licensed.</i>";$noreg='nl';$ste=1;}
		if($c[0] eq $name && $b eq $a && ($fid eq $c[2] or $c[2] eq 'x')){$str="<i>This forum works on White Tiger WWW Board registered by <font color=\$errvlcolor>$name</font>, forum license <font color=\$errvlcolor>$c[3]</font>$c[4].</i>";$ste=2;$noreg='re';last}
	}
}


print "<registration><p><dd> $str </registration><reg>$ste</reg>";

if($noreg ne 're')
{	open NAM,'../data/wtbreglog.txt';@b=<NAM>;close NAM;chomp@b;
	for(@b){if($_ eq "$noreg=$fid,$ENV{'HTTP_REFERER'}"){$exist=1;last}}
	if(!$exist){open NAM,"+>>../data/wtbreglog.txt";print NAM "$noreg=$fid,$ENV{'HTTP_REFERER'}\n";close NAM}
}

exit;



ADMIN:


if($admin ne 'admin' xor $pass ne 'Richard'){print "<html><title>Error authorization</title><body bgcolor=#00dddd>Authorization error!";exit}

open NAM,"../data/wtbregsts.txt";@ini=<NAM>;close NAM;chomp@ini;
$a=join($cl,@ini);
$a=~s/&/&amp;/g;$a=~s/</&lt;/g;$a=~s/>/&gt;/g;$a=~s/\"/&quot;/g;
undef @ini;

print "<html><title>R</title><body bgcolor=#00dddd><center>Registrations of White Tiger WWW Board</center><p> <form action=/cgi-bin/wtboardreg.cgi method=post><table width=100%><tr><td><center>&nbsp;<textarea cols=70 rows=12 name=text>$a</textarea></td></tr></table><p><center>$hr<p><input type=hidden name=admin value=\"$admin\"><input type=hidden name=pass value=\"$pass\">";

print "<input type=submit name=submit value=\"Send\"></form><p></body></html>";

exit;


SUBMIT:


if($submit eq 'Send'){

if($admin ne 'admin' xor $pass ne 'Richard'){print "<html><title>Error authorization</title><body bgcolor=#00dddd>Authorization error!";exit}
rename "../data/wtbregsts.txt","../data/wtbregsts.txt.old";
if(length $text>0)
{	$text=~s/$cr/\n/g;
	open NAM,"+>../data/wtbregsts.txt";
	print NAM $text;close NAM;
}
print "ok";
if(-z "../data/wtbregsts.txt"==1){rename "../data/wtbregsts.txt.old","../data/wtbregsts.txt";}

exit;
}

if($submit eq 'Add')
{

open NAM,"../data/wtbregsts.txt";@ini=<NAM>;close NAM;chomp@ini;
for(@ini)
{	@a=split(/;;/);
	if($a[0] eq $uname && $a[2] eq $fid){print "<html><title>Error registration</title><body bgcolor=#00dddd>This license (<b>$uname, fid=$fid</b>) already exists, try another.";exit}
}

$a=$uname;
$a=$fid.$a;
$a=join('x',reverse(split(/[ a]/,$a)));
$a=join('a',reverse(split(/i/,$a)));
$a=join('z',reverse(split(/e/,$a)));
$a=crypt($a,'WT');
$a=~s/[\/\.]//g;
for(split(//,$a)){$number+=((ord$_)**2)}


if($info){$info=' ('.$info.')'}
open NAM,"+>>../data/wtbregsts.txt";
print NAM "$uname;;$a;;$fid;;$number;;$info;;\n";
close NAM;

print "<html><title>Registered</title><body bgcolor=#00dddd>Thanks to registering new forum. Your registration information:
<p>Name=$uname<br>FID=$fid<br>Key=$a<br>RegNumber=$number<br>Other info=$info.
<p>Set this registration string in Administration/Status of forum/RegInfo:
<p><center><font face=courier size=-1><b>$uname//$a</b></font>";

exit;
}
sub replacx
{$_[0]=~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;}

