<?php require("variables.inc"); ?>
<?php require("functions.inc"); ?>
<?php require("classes.inc");

/* Register-specific functions */
function invalidChar($c,$low=32)
{
	if(($c < $low) || ($c == 127))
		return true;
	return false;
}

function checkDetails($username,$realname,$email)
   {
   global $memberpath;
   $password="auto-set";
   if ((isset($username)) && (trim($username) != ''))
      {
      if (getMemberNumber($username) != false)
         {
         $result = "<p>Sorry, but that username ($username) has already been taken.  Please press the back button on your browser and pick another.\n";
         }
      else
         {
         if ($password != "")
            {
            if ($realname != "")
               {
               if ($email != "")
                  {
         	        $result = 'valid';
 			for($i=0;$i<strlen($username);$i++)
				if(invalidChar($username{$i}))
					$result = "<p>Your chosen username contains invalid characters.\n";
			for($i=0;$i<strlen($realname);$i++)
				if(invalidChar($realname{$i}))
					$result = "<p>Your name contains invalid characters.\n";
			for($i=0;$i<strlen($email);$i++)
				if(invalidChar($email{$i},33))
					$result = "<p>Your email address contains invalid characters.\n";
			if(!strchr($email,"@") || !strchr($email,"."))
				$result = "<p>Invalid email address.\n";
                  }
               else
                  {
                  $result = "<p>You forgot to enter your email address! Please press the back button on your browser and try again.\n";
                  }
               }
            else
               {
               $result = "<p>You forgot to enter your name!  Please press the back button on your browser and try again.\n";
               }
            }
         else
            {
            $result = "<p>You forgot to enter a password! Please press the back button on your browser and try again.\n";
            }
         }
      }
   else
      {
      $result = "<p>You forgot to enter a username!  Please press the back button on your browser and try again.\n";
      }
   return $result;
   }

if (checkDetails($username,$realname,$email) == 'valid')
   {
   // Get number of files in member directory
   $nummembers = 0;
   $dafilename = $memberpath . $nummembers . ".txt";
   while (file_exists($dafilename))
      {
      $nummembers++;
      $dafilename = $memberpath . $nummembers . ".txt";
      }

   if ($website == "http://")
      {
      $website = "";
      }

   // Create file
   $password="";
   $alpha_upper = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
   for ($passloop = 0; $passloop < 8; $passloop++)
   {
    $pass_alpha_array_select = rand (0, 25);
    $password .= $alpha_upper[$pass_alpha_array_select];
   }
   $filename = $memberpath . $nummembers . ".txt";
   $memberdetails = "<member>\n <username>" . $username . "</username>\n <realname>" . $realname . "</realname>\n <password>" . $password . "</password>\n <number>" . $nummembers . "</number>\n <status>Member</status>\n <group>Member</group>\n <email>" . $email . "</email>\n <regemail>" . $email . "</regemail>\n <website>" . $website . "</website>\n <icq>" . $icq . "</icq>\n <totalposts>0</totalposts>\n <preferences></preferences>\n</member>\n";
   touch($filename);
   chmod($filename,0777) or die("<b>Error:</b> Unable to chmod new member file ($filename).  Please contact the <a href=\"mailto:$adminemail\">board administrator</a>.");
   $fd = fopen($filename,"w");
   fwrite($fd,$memberdetails);
   fclose($fd);

   // Add member to member index
   $fd = fopen($memberpath . "memberlist.txt","a");
   fwrite($fd,"$nummembers - $username|\n");
   fclose($fd);

   $registered = true;

   // Set cookies
//   $now = time();
//   $expireon = mktime(0,0,0,1,1,2015);
//   if ($cookiesavailabletoroot)
//      {
//      setcookie("cookieusername",$username,$expireon,"/",$availabletodomain);
//      setcookie("cookiepassword",$password,$expireon,"/",$availabletodomain);
//      }
//   else
//      {
//      setcookie("cookieusername",$username,$expireon);
//      setcookie("cookiepassword",$password,$expireon);
//      }
      $email_ok = @mail($email,
                        "$siteabbrev Forum Registration",
                        "Hi $realname,\n\nThanks for registering with $siteabbrev!\n\nYou details are as follows...\n\n    username: $username\n    password: $password\n\nGo to ${siteurl}alpha-board/login.php3 to enter your details, and welcome to $boardname!\n\nCheers,\n$sitename\n-- \n$siteabbrev: $siteurl\n",
                        "To: $realname <$email>\n".
                        "From: $siteabbrev <$adminemail>\n".
                        "BCC: $siteabbrev <$adminemail>\n");
   }

?>
<html>
<head>
 <title><?php echo $boardname; ?>: Register</title>
 <?php echo stylesheetLink(); ?>
</head>

<body bgcolor="#<?php echo $bgcolour; ?>" text="#<?php echo $textcolour; ?>" link="#<?php echo $linkcolour; ?>" vlink="#<?php echo $vlinkcolour; ?>" alink="#<?php echo $alinkcolour; ?>"<?php if (!$margins) { print " marginwidth=\"0\" marginheight=\"0\" leftmargin=\"0\" topmargin=\"0\""; } ?>>

<?php include("options/header.txt"); ?>

<div align="center">
<table border="0" width="98%" cellspacing="3">
 <tr valign="top">
  <td><a href="index.php3?serve=cached"><b><?php echo $boardname ?></b></a>: <b>Register</b><br><small><?php

if (isset($cookieusername))
   {
   print "Logged in as $cookieusername | <a href=\"logout.php3\">Logout</a>\n";
   }
else
   {
   print "<a href=\"login.php3\">Login</a> | <a href=\"register.php3\">Register</a>\n";
   }

?></small></td>
  <td align="right"><a href="post.php3">Post new topic</a> | <a href="register.php3">Register</a> | <a href="faq.php3">FAQ</a><?php

if (isset($cookieoldlastvisit))
   {
   print "<br><small>You last visited: ";
   echo date("H:i, j/n/Y",$cookieoldlastvisit);
   }

?></td>
 </tr>
</table>
</div>

<p>&nbsp;</p>

<?php


if (isset($submit))
   {
   if ($registered)
      {
      print "<ul>";
      if ($email_ok) {
        print "Email sent ok<br />\n";
      } else {
        print "<font color=\"#ff0000\"><b>Error!</b></font><br />\nMail send failure.  Please email <a href=\"$adminemail\">$adminemail</a> and let us know about it!<br />\n";
      }
      print "<p>Your registration was successful.  Check your mail for your password, and <a href=\"login.php3\">click here to Login</a>.  Once you log in you can change your password.<br /> <br />Click <a href=\"index.php3\">here</a> to return to the discussion forum.\n";
      print "</ul>";
      }
   else
      {
      echo checkDetails($username,$realname,$email);
      }
   }
else // Display forum and info
   {
     print "<p><center><h1>Registration</h1></center>\n";
     print "<form method=\"post\" action=\"$PHP_SELF\">\n\n";
     print "<div align=\"center\">\n";
     print "<br />Items in <b>bold</b> are required fields. A valid email address is necessary to inform you of your password.\n";
     print "<table border=\"0\" cellspacing=\"2\" cellpadding=\"3\" width=\"90%\">\n";
     print " <tr>\n";
     print "  <td align=\"right\"><b>Username:</b></td>\n";
     print "  <td bgcolor=\"#$secondary_tdbgcolor\"><input type=\"text\" name=\"username\" size=\"10\" maxlength=\"20\"></td>\n";
     print " </tr>\n";
   //print " <tr>\n";
   //print "  <td align=\"right\">Password:</td>\n";
   //print "  <td bgcolor=\"#$secondary_tdbgcolor\"><input type=\"password\" name=\"password\" size=\"10\" maxlength=\"10\"></td>\n";
   //print " </tr>\n";
     print " <tr>\n";
     print "  <td align=\"right\"><b>Your&nbsp;name:</b></td>\n";
     print "  <td bgcolor=\"#$secondary_tdbgcolor\"><input type=\"text\" name=\"realname\" size=\"30\"></td>\n";
     print " </tr>\n";
     print " <tr>\n";
     print "  <td align=\"right\"><b>Email&nbsp;address:</b></td>\n";
     print "  <td bgcolor=\"#$secondary_tdbgcolor\"><input type=\"text\" name=\"email\" size=\"30\">\n";
     print " </tr>\n";
     print " <tr>\n";
     print "  <td align=\"right\">ICQ:</td>\n";
     print "  <td bgcolor=\"#$secondary_tdbgcolor\"><input type=\"text\" name=\"icq\" size=\"8\">\n";
     print " </tr>\n";
     print " <tr>\n";
     print "  <td align=\"right\">Website:</td>\n";
     print "  <td bgcolor=\"#$secondary_tdbgcolor\"><input type=\"text\" name=\"website\" size=\"30\" value=\"http://\">\n";
     print " </tr>\n";
     print " <tr>\n";
     print "  <td>&nbsp;</td>\n";
     print "  <td>&nbsp;</td>\n";
     print " </tr>\n";
     print " <tr>\n";
     print "  <td bgcolor=\"#$secondary_tdbgcolor\">&nbsp;</td>\n";
     print "  <td bgcolor=\"#$secondary_tdbgcolor\"><input type=\"submit\" name=\"submit\" value=\"Register\"></td>\n";
     print " </tr>\n";
     print "</table>\n";
     print "</div>\n";
     print "</form></p>\n\n";
   }

?>

<p align="center"><small>Powered by alpha board version <?php echo $versionnum; ?>.<br>
&copy; <a href="http://www.tfountain.co.uk/">Tim Fountain</a>, 2000 - <?php echo $year ?>.</small></p>

<?php include("options/footer.txt"); ?>

</body>
</html>