<?php
	/*
	** File: ADD_USER
	** Description: Adds a user to the system
	** Version: $Revision: 1.13 $
	** Created: 06/08/1999
	** Author: Leon Atkinson, Shannon -jj Behrens	
	** Email: leon@clearink.com, jj@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/12/09 23:23:18 $
	**
	** Log : $Log: ADD_USER,v $
	** Log : Revision 1.13  1999/12/09 23:23:18  leon
	** Log : Changed ActionResults use so that it's an array instead of a
	** Log : continually-growing string.  Note the little function in standard_library
	** Log : for printing out all the action results.
	** Log :
	** Log : Revision 1.12  1999/12/06 21:17:19  leon
	** Log : ECML compliance
	** Log : Overhauled address functions to use ECML form field names.  Ditto with
	** Log : the billing fields on the checkout forms.  Note the change to build.sql.
	** Log : Yes, the address table changed to accomodate new fields.  The billing
	** Log : table changed too.
	** Log :
	** Log : Revision 1.11  1999/11/09 21:47:12  leon
	** Log : More localization.
	** Log :
	** Log : Revision 1.10  1999/11/08 22:32:18  leon
	** Log : Added prepareText() function to use everywhere we were using htmlentities.
	** Log :
	** Log : Revision 1.9  1999/11/04 21:35:29  leon
	** Log : Cleaned up the use of paths.  Paths are now all set in index.php3 as
	** Log : constants.  Example settings for Apache/Unix, Apache/Win32, and IIS.
	** Log :
	** Log : Revision 1.8  1999/09/09 22:15:35  jj
	** Log : No longer any need to use the variable $addressFunctionsDefined.
	** Log :
	** Log : Revision 1.7  1999/08/20 16:43:27  jj
	** Log : Found a bug in ADD_USER (need to declare $addressFunctionsDefined).
	** Log :
	** Log : Revision 1.6  1999/08/18 22:31:46  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.5  1999/08/18 16:19:30  jj
	** Log : All actions now use verifyAddress and insertAddress whenever appropriate.
	** Log :
	** Log : Revision 1.4  1999/08/17 17:57:23  jj
	** Log : Modularized everything to do with addresses. Please see include/addressFunctions
	** Log :
	** Log : Revision 1.3  1999/07/28 19:27:11  leon
	** Log : Mass update based on code donation from a live project
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	*/

	/* Has a function to insert an address into the database. */
	include(APPLICATION_ROOT . "/modules/include/addressFunctions");
	
	
	$ProceedWithInsert = TRUE;

	//pull address from environment
	$inputAddress = getAddressFromEnvironment("input");
	

	/*
	** Validate the input.  The defaults for Freetrade are to only require
	** a login and password.
	*/
	if(strlen($inputLogin) < 4)
	{
		$ActionResults[] =  L_ADDUSER_LOGINTOOSHORT . "<BR>\n";
		$ProceedWithInsert = FALSE;
	}
	if(strlen($inputPassword) < 4)
	{
		$ActionResults[] =  L_ADDUSER_PWDTOOSHORT . "<BR>\n";
		$ProceedWithInsert = FALSE;
	}
	
	/* 
	** Require first and last name
	*/
	if(verifyAddress(array("Name_First","Name_Last"), $inputAddress, &$ErrorDescription) == FALSE)
	{
		$ActionResults[] =  $ErrorDescription;
		$ProceedWithInsert = FALSE;
	}

	/* 
	** Make sure that the user has selected a unique login. 
	*/
	if($ProceedWithInsert)
	{
		$query = "SELECT ID FROM user WHERE Login = ";
		$query .= "'" . addslashes($inputLogin) . "'";

		/* Execute the query. */
		if ( !($queryResults = mysql_query($query, $DatabaseLink)) )
		{
			$ActionResults[] =  mysql_error() . ":" . mysql_error();
			$ProceedWithInsert = FALSE;
		}
		
		/* If the query was successful, make sure no rows were returned. */
		if (mysql_num_rows($queryResults))
		{
			$ActionResults[] =  L_ADDUSER_NEEDUNIQUE . prepareText($inputLogin);
			$ProceedWithInsert = FALSE;

			/* Suggest a different login. We'll come up with one randomly. */
			srand(time());
			for ($i = 0 ; $i<4 ; $i++) 
			{
				$inputLogin .= strval(rand() % 10);
			}
		}
	}

	

	/*
	** If everything is "kosher", we can proceed with the insert. First
	** we insert the user's address into the "address" table.
	*/
	if ($ProceedWithInsert)
	{	
		$userAddressPointer = insertAddress($inputAddress);
		
		/* Make sure that we were successful. */
		if (!$userAddressPointer)
		{
			$ActionResults[] =  mysql_error() . ":" . mysql_error();
			$ProceedWithInsert = FALSE;
		}
	}



	/*
	** If everything is "kosher", we can proceed with the insert. Now
	** we insert the rest of the user information into the "user" table.
	** Make sure to set up the "pointer" to the user's address.
	*/
	if ($ProceedWithInsert)
	{
		$query = "INSERT INTO user (Login, Password, Address) VALUES (";
		$query .= "'" . substr(addslashes($inputLogin),0,255) . "', ";
		$query .= "'" . substr(addslashes($inputPassword),0,255) . "', ";
		$query .= $userAddressPointer . ")";

		/* Execute the query. */
		if (!mysql_query($query, $DatabaseLink))
		{
			$ActionResults[] =  mysql_error() . ":" . mysql_error();
			$ProceedWithInsert = FALSE;
		}
	}



	/* 
	** If everything's ok so far, we can proceed with the insert. Now
	** we need to give the user permission to login. Make sure to set
	** up the "pointer" to the user's main record.
	*/
	if ($ProceedWithInsert)
	{
		$newUserID = mysql_insert_id($DatabaseLink);
		$query = "INSERT INTO user_permission VALUES (";
		$query .= $newUserID . ", 1)";

		/* Execute the query. */
		if (!mysql_query($query, $DatabaseLink))
		{
			$ActionResults[] =  mysql_error() . ":" . mysql_error();
			$ProceedWithInsert = FALSE;
		}
	}



	/* 
	** If everything's ok so far, we can proceed. Since the account 
	** should be completely set up by now, we can go ahead and automatically
	** login the user. 
	*/
	if ($ProceedWithInsert)
	{
		$query = "UPDATE session SET user="; 
		$query .= $newUserID . " ";
		$query .= "WHERE ID = ";
		$query .= "'" . addslashes($sid) . "'";

		/* Execute the query. */
		if (!mysql_query($query, $DatabaseLink))
		{
			$ActionResults[] =  mysql_error() . ":" . mysql_error();
			$ProceedWithInsert = FALSE;
		}
		/* This is copied straight out of "LOGIN_USER". */
		else
		{
			$UserInfo = GetUserInfo($newUserID);
		}			
	}		


	
	/*
	** If everything is okay, tell the user so. Otherwise, send him
	** back to the "create_account" screen.
	*/
	if ($ProceedWithInsert) 
	{
		$SCREEN = "contents";
		$ActionResults[] = L_ADDUSER_SUCCESS;
	}
	else
	{
		/* $ActionResults already has the error results in it. */ 
		$SCREEN = "create_account";
	}
	
?>
