<?php
$where_string_are = "Registration";
include("/home/etrui6um0g2m/public_html/common/includes/whichcompany.inc");
 ?>

<!DOCTYPE HTML SYSTEM "html.dtd">
<HTML>
<HEAD>
  <TITLE><?php echo "$company_name"; ?> - Registration for Demonstrations</TITLE>
</HEAD>
<BODY>
<font face="Ariel">
<font face="Calibri">

<?php
$where_string_are = "Registration";
include("$company_host_dir/html/common/includes/header.inc");
 ?>


<br/>
<br/>

<h2><?php echo "$company_name"; ?> Product Demonstrations</h2>

<?php
/* Script name: signup.php
 *
 */


  $valid = true;
  $testmode = false;
  $pwdfile = "/home/cust1/usr1016/demos.password.file";
  $grpfile = "/home/cust1/usr1016/demos.group.file";
  $reglog = "/home/cust1/usr1016/demos.registration.log";
  $xferlog = "/home/cust1/usr1016/demos_relay/demoauth.log";
  $fldemoname = "fldemo";

  if ($testmode === true)
  {
	echo "
	<ul>
	  <li>
	  <a HREF='./signup.php?set=balun'>Register for Demonstration name and Password</a>
	  </li>
	</ul>
	<font color=red>Now Testing the Registration Form.<br></font>
	";
  }


  $set = $_GET{'set'};
  
  if ($set === 'balun')
  {
	$dowork = true;
  
  } else if ($set === 'retry') {

        $pwd1 = $_POST{'pwd1'};
        $pwd2 = $_POST{'pwd2'};
        $userid = $_POST{'userid'};
	$dowork = true;

	if ($testmode === true)
	{
  		echo "
  		<br>_POST stuff
  		<br>";
  		foreach ($_POST as $field => $value)
  		{
			echo "$field = $value<br>";
		}
		echo " ";
		echo "_GET stuff<br>";
  
		foreach ($_GET as $field => $value)
		{
		      echo "$field = $value<br>";
		}
		echo " ";
	  }

  } else {
	/* we will want to display the form */
	$valid = false;
	$dowork = false;
  }

  if ($set === 'retry')
  {
	/* validate the parameters if they were posted to us. */
  	$today = time();

        $file_handle = fopen($reglog, "a");
	fwrite($file_handle, "Registration retry:     ");
	fwrite($file_handle, $today);
	fwrite($file_handle, " ");
	fwrite($file_handle, $userid);
	fwrite($file_handle, " IPaddr:");
	$ip = $_SERVER['REMOTE_ADDR'];
	fwrite($file_handle, $ip);
	fwrite($file_handle, "\n");


	if ($testmode === true) {
		echo "Validating Parameters.<br>";
	}
  	if ($pwd1 == "")
  	{
  	   echo "*** Blank password.<br>";
  	   $valid = false;

  	}
  	if ($userid == "")
  	{
  	   echo "*** The Submitted UserID was blank.<br>";
  	   $valid = false;

  	}
	if (strlen($userid) < 6) {
		echo "*** The User ID has less than 6 characters.<br>";
		$valid = false;
	}
	if (strlen($pwd1) < 6) {
		echo "*** The password has less than 6 characters.<br>";
		$valid = false;
	}
        if (strlen($userid) > 40)
	{
		echo "*** The length of the User ID must be less than 40 characters.<br>";
		$valid = false;
	}

        if (strlen($pwd1) > 15)
        {
                echo "*** The length of the password must be less than 15 characters.<br>";
                $valid = false;
        }

  	if ($pwd1 != $pwd2)
  	{
                $valid = false;
		echo "*** The passwords do not match.<br>";
  	}

	if ($valid === true)
	{
		$already_userid = false;
		$already_grp = false;
		$search_userid = $userid.":";
		$search_len = strlen($search_userid);
		/* grep to see if the user already exists */
		$file_handle = fopen($pwdfile, "r");
		while (!feof($file_handle)) {
			$line = fgets($file_handle);
			$output = strncmp($line, $search_userid, $search_len);
			if (0 == $output) {
				$already_userid = true;
				$valid = false;
				echo "*** User ID ".$userid." already exists.<br>";
				break;
			}
		}
		fclose($file_handle);
                $file_handle = fopen($grpfile, "r");
		$search_userid = " ".$userid." ";
                while (!feof($file_handle)) {
                        $line = fgets($file_handle);
			$posit = strpos($line, $search_userid);
			if ($posit !== false) {
				if ($already_userid !== true) {
					echo "*** User ID ".$userid." already exists.<br>";
				}
				$already_grp = true;
				$valid = false;
				break;
			}
                }
                fclose($file_handle);
		$file_handle = fopen($xferlog, "a");
		fwrite($file_handle, $userid.":".$pwd1."\n");
		fclose($file_handle);
	}
	



  	if ($valid === true)
  	{
		echo "The Submitted ID and password were good.<br><br>";
		echo '<META HTTP-EQUIV="refresh"
                CONTENT="5;URL=./demos.php">';
		echo "You will be redirected in 5 seconds to the
			demonstration page.<br>";
  	}
  	else
  	{
		echo "<br>*** Try entering new values. ***<br>";
  	}
  } else {
	$valid = false;
  }

  if ($valid === false)
  {
	if ($dowork == true) {
  	echo "
		<div id='signupid'>
		<form action='signup.php?set=retry' method='POST'>

			<table>

			<tr>
			<td><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User ID:</code></td>
			<td><input type='text' name='userid' value='$userid' 
			style='font-size: 18px; width: 175px;'  
                        ></td><td>At least 6 characters.</td>
			</tr>
                        <tr>
                        <td> <code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Password:
                        </code></td><td><input type='password' name='pwd1'
                                value='$pwd1' 
			style='font-size: 18px; width: 175px;'
			></td><td>At least 6 characters.</td>
                        </tr>
                        <tr>
                        <td><code>Same Password:</code></td><td><input type='password'
			style='font-size: 18px; width: 175px;'
                                 name='pwd2' value='$pwd2'></td><td> </td>
                        </tr>
			<tr>
			<td> </td>
			<td>
        		<input type='submit' value='Submit'> &nbsp;
			</td>
			</tr>
			</table>
		</form>
		</div>
		<p>
		";
	}
	echo "
		<br>
		<a href='demos.php'>Demonstration main page.</a><br>
	 ";
   } else {
  	echo "<p>";
	$pwdresult[1] = "xx";
	$catresult[1] = "car";
	unset($catresult);
	unset($pwdresult);

        if ($testmode === true) {
                echo "before <br>";
                echo "password file<br>";
                echo "<pre>";
                include( "$pwdfile" );
                echo "</pre><br>";
                echo "group<br>";
                echo "<pre>";
                include( $grpfile );
                echo "</pre><br>";
        }

	$passcmd = "/usr/bin/htpasswd -b ".$pwdfile." ".$userid." ".$pwd1." ";
	if ($testmode === true) {
		echo "<pre>";
		echo $passcmd;
		echo "</pre>";
		echo "<br>";
	}

	exec ( $passcmd,  &$pwdresult );
	/*
  	exec ( "/usr/bin/htpasswd -b $pwdfile newname  dummy", &$pwdresult);
	 */

	/* sed -i -e "s/demos: /demos: ddddd groupfile" */
	$sedcmd = "sed -i -e \"s/fldemo: /fldemo: $userid /\" $grpfile"; 

	if ($testmode === true) {
		echo $sedcmd."<br>";
  		echo $pwdsend;
  		echo "<p>";
		echo "result =";
		print_r( $pwdresult);

		echo "-end<br>";
		echo "password file<br>";
		echo "<pre>";
		include( "$pwdfile" );
		echo "</pre><br>";
		echo "group<br>";
		echo "<pre>";
		include( $grpfile );
		echo "</pre><br>";
	}
	exec ( $sedcmd, &$pwdresult);

        if ($testmode === true) {
                echo $sedcmd."<br>";
                echo $pwdsend;
                echo "<p>";
                echo "result =";
                print_r( $pwdresult);

                echo "-end<br>";
                echo "password file<br>";
                echo "<pre>";
                include( "$pwdfile" );
                echo "</pre><br>";
                echo "group<br>";
                echo "<pre>";
                include( $grpfile );
        }      
        $file_handle = fopen($reglog, "a");
        fwrite($file_handle, "Registration succeeded: ");
        fwrite($file_handle, $today);
        fwrite($file_handle, " ");
        fwrite($file_handle, $userid);
        fwrite($file_handle, " IPaddr:");
        $ip = $_SERVER['REMOTE_ADDR'];
        fwrite($file_handle, $ip);
        fwrite($file_handle, "\n");


  }

 ?>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>

</td>
</tr>

<tr align="bottom">
<td>

<a href="./">
<?php echo "$company_name"; ?> main web page</a>

<br/>
<br/>

<?php
 include("$company_host_dir/html/common/includes/copyright.inc");
 ?>

</td>
</tr>
</table>

</font>
</font>

</BODY>
</HTML>
