#!/usr/local/bin/perl5 -w
#	DaProcess User Name and Password
#
require "cgi-lib.pl";
#
$passworderrorflag =0;
open(PASSWORDFILE,'<userlist') || ($passworderrorflag =1);
unless( $passworderrorflag )	{
	while($line=<PASSWORDFILE>)	{
		chomp($line);
		unless( $line =~ m%([^:]*):(.*)$% )	{
			$passworderrorflag = 2;
			last;
		}
		$user{$1}=$2;
	}
}
close(PASSWORDFILE);
$userdir = 'defaultuser';
$password ='';
$outputfile = 'wisdom2';
#
$statusofread =  &ReadParse(*formdata);
#
if( $statusofread )	{
	if( $formdata{'callingplace'} )	{
		$outputfile = $formdata{'callingplace'};
	}
	unless( $userdir = $formdata{'username'} )	{
		$passworderrorflag = 3;
	}
	else	{
		$anon = $userdir;
		$anon =~ tr/A-Z/a-z/;
		if( ($anon eq 'guest') || ($anon eq 'anon') || ($anon eq 'defaultuser') )	{
			$userdir = 'defaultuser';
		}
		else	{
			if( ($password = $formdata{'userpassword'}) )	{
				if(exists($user{$userdir}))	{
					unless(defined($user{$userdir}))	{
						$passworderrorflag = 5;
					}
					else	{
						unless( $user{$userdir} eq $password )	{
							$passworderrorflag = 6;
						}
					}
				}
				else	{
					$passworderrorflag = 7;
				}
			}
			else	{
				$passworderrorflag = 4;
			}
		}
	}
}
#
$today =`date`;
chomp($today);
#
$userdir = 'defaultuser' if( $passworderrorflag > 0 );
$filetologto = "/usr/local/archives/public/users/gcf/nobody/webwisdom/" .$userdir .'/hostdefaultlog';
$filelogerror =0;
if(-e $filetologto )    {
	open(FILETOLOGTO,'>>'.$filetologto) || ($filelogerror=1);
}
else    {
	open(FILETOLOGTO,'>'.$filetologto) || ($filelogerror=1);
	print FILETOLOGTO "Log of Parameter Save Actions\n";
}
unless($filelogerror)	{
	print FILETOLOGTO 'Password Check ',$today,' ',$userdir,' ',$passworderrorflag,' ',$ENV{'REMOTE_ADDR'},' ',$ENV{'REMOTE_HOST'},"\n";
	close(FILETOLOGTO);
}
else	{
	$passworderrorflag =8;
}
if( $passworderrorflag )	{ # Error of Some Sort
	if( $outputfile eq 'wisdom1' )	{
print <<EOF;
Content-Type: text/html

<html>
<head>
<title>Error in Setting Username/Password from WebWisdom HomePage </title>
</head>
<script language="JavaScript">
<!--scriptbegin
document.writeln('<body  bgcolor=' + top.bigboss.currentparameters.bgcolor + ' text=' +
top.bigboss.currentparameters.fgcolor + ' link=' +top.bigboss.currentparameters.linkcolor + ' vlink=' +
top.bigboss.currentparameters.vlinkcolor + ' alink=' + top.bigboss.currentparameters.alinkcolor +  '>');
document.writeln('<basefont size=' + top.bigboss.currentparameters.fontsize  + '>');
document.writeln('<font size=' + top.bigboss.currentparameters.fontsize + ' color=' +
top.bigboss.currentparameters.titlecolor + '>');
top.bigboss.foilsettitle(self,'Error in Setting UserName/Password from WebWisdom HomePage');
document.writeln('</font>');
//scriptend-->
</script>
<img src="http://www.npac.syr.edu/users/gcf/npacresources/webwisdom/maple-lgtyL.gif" >
<P><B>This is starting point to Web Wisdom -- a hierarchical tree of knowledge
built from JavaScript and aimed at Netscape 2.0 Navigators. Web Wisdom is
set up to handle any worlds built in terms of self defining Web Pages where
the header of documents have appropriate JavaScript calls to define hierarchical
structure recursively. This structure has natural applicability in education
and in Mail archives. Web Wisdom can handle several different worlds (limited
only by strength of JavaScript) and currently we have implemented foilworld
which archives foils produced by NPAC.</B><BR>
<h2>Here you can Set User Name and Password</h2>
Note this is only necessary if you need to undertake special actions
<UL>
<li>Save Parameter Sets
<li>Read Parameter Sets from anything but defaultuser (aka guest)
<li>Set up special audio file recording option
</ul>
<hr>
</form>
<form name="formtodump" ACTION="http://boss.npac.syr.edu:8080/cgi-bin/webwisdom/setusername.pl" METHOD="post"
ENCTYPE="application/x-www-form-urlencoded"  >
<input type="submit" name="List" value="Set User Name" >
<br>
<script language="JavaScript">
<!--scriptbegin
document.writeln('User Name<input name="username" type="text" value ="' + top.bigboss.username + '" ><br>');
document.writeln('User Password<input name="userpassword" type="password" value ="' + top.bigboss.userpassword +
'" >');
//scriptend-->
</script>
<br>
Label where called From<input name="callingplace" type="hidden"  value="wisdom1" >
</form>
<B>This system was originally built by Wojtek Furmanski and modified by Geoffrey
Fox. Here are the co-conspirators!</B></P>
<P><IMG SRC="http://www.npac.syr.edu/users/gcf/homepage/fox.jpg" WIDTH="280" HEIGHT="401" SGI_SETWIDTH
SGI_SETHEIGHT SGI_FULLPATH="/tmp_mnt/usr/local/archives/public/users/gcf/homepage/fox.jpg"><B></B></P>

</basefont>
</body>
</html>
EOF
		exit;
	}
	else	{
print <<EOF;
Content-Type: text/html

<html>
<head>
<title>Error in Set User and Password Sets</title>
<script language="JavaScript">
</script>
</head>
<script language="JavaScript">
<!--scriptbegin
document.writeln('<body background="' + top.bigboss.wisdomhomeurl + '../background/' + top.bigboss.currentparameters.bgfile + '"  >');
document.writeln('<font size=3 color="' + top.bigboss.currentparameters.imgfgcolor + '" >');
//scriptend-->
</script>
<h2>Error Found in Setting User Name and Password</h2>
<b>
Don't Despair!<br>
Either Try Again or keep with default</b>
<hr>
Note this is only necessary if you need to undertake special actions
<UL>
<li>Save Parameter Sets
<li>Read Parameter Sets from anything but defaultuser (aka guest)
<li>Set up special audio file recording option
</ul>
<hr>
</form>
<form name="formtodump" ACTION="http://boss.npac.syr.edu:8080/cgi-bin/webwisdom/setusername.pl" METHOD="post"
ENCTYPE="application/x-www-form-urlencoded"  >
<input type="submit" name="List" value="Set User Name" >
<br>
<script language="JavaScript">
<!--scriptbegin
document.writeln('User Name<input name="username" type="text" value ="' + top.bigboss.username + '" ><br>');
document.writeln('User Password<input name="userpassword" type="password" value ="' + top.bigboss.userpassword + '" >');
//scriptend-->
</script>
<br>
<input name="callingplace" type="hidden"  value="wisdom2" >
</form>
EOF
		exit;
	}
}	# End Error Processing
#
#	Succesful Setting of Password and UserName
        print <<EOF;
Content-Type: text/html

<html>
<head>
<title>Success in UserName/Password Setting</title>
<script language="JavaScript">
<!--scriptbegin
function setusername()     {
	top.bigboss.username = document.forms[0].elements[0].value;
	top.bigboss.userpassword = document.forms[0].elements[1].value;
}
//scriptend-->
</script>
</head>
<script language="JavaScript">
<!--scriptbegin
document.writeln('<body background="' + top.bigboss.wisdomhomeurl + '../background/' + top.bigboss.currentparameters.bgfile + '"  >');
document.writeln('<font size=3 color="' + top.bigboss.currentparameters.imgfgcolor + '" >');
//scriptend-->
</script>
<H2>User Name and Password Succesfully Set</H2>
Please Rejoice and Continue with WebWisdom<br>
<form name="goodusername" >
EOF
	print 'User Name<input name="username" type="text" value ="' . $userdir . '" ><br>',"\n";
	print 'User Password<input name="userpassword" type="password" value ="' . $password . '" >',"\n";
	print <<EOF;
</form>
<script language="JavaScript">
<!--scriptbegin
setusername();
//scriptend-->
</script>
</body>
</html>
EOF
#
1;

