<?php
/*
script: join.php
purpose: display the sign up form for new members and process their sign-ups.
copyright: copyright 2004 Php Image Host. All Rights Reserved. You may modify this file for use on your
					site running a licenced copy of the Php Image Host scripts.
					You must not distribute this file or derivations of it.
Nullified By CrAcKlOvE

*/

// include general config / intialization (eg. database) stuff

require_once(dirname(__FILE__).'/includes/config.inc.php');

// include site header

require_once(dirname(__FILE__).'/header.php');

if( $loggedin ){
	// if already logged in they don't need to join, so display the standard "logged in" page.
	require_once(dirname(__FILE__).'/includes/loggedin.inc.php');
}

if( !$config->allow_signups ){
	// if new sign ups have been disabled via the admin, display a message about this, then exit.
	$pagecontent->display('No New Members');
	require_once(dirname(__FILE__).'/footer.php');
	exit();
}elseif( $banned ){
	$pagecontent->display('IP Banned');
	require_once(dirname(__FILE__).'/footer.php');
	exit();
}

// initilize vars for the sign up form.

$username = $ace->getstr('username', $_POST);
$password = $ace->getstr('password', $_POST);
$rpassword = $ace->getstr('rpassword', $_POST);
$email = $ace->getstr('email', $_POST);
$remail = $ace->getstr('remail', $_POST);
$name = $ace->getstr('name', $_POST);

$joined = false;
$signuperrors = '';

// if the user has submitted the sign-up form, process it.
// the code checks that various fields are valid, and that there is not already a user
// with this username or email address.

if( $ace->getstr('join', $_POST) != '' ){
	if( !preg_match('/^[a-z0-9]{2,20}$/i', $username) ) $signuperrors.='<br />Your username must be between 6 and 20 alphanumeric (a-z0-9) characters long.';
	if( !preg_match('/^[a-z0-9]{6,20}$/i', $password) ) $signuperrors.='<br />Your password must be between 6 and 20 alphanumeric (a-z0-9) characters long.';
	if( $password != $rpassword ) $signuperrors .='<br />You must enter your password twice to confirm your choice.';
	if( $email == '' ){
		$signuperrors.='<br />You must enter your email address.';
	}elseif( !$ace->validateemail($email) ){
		$signuperrors.='<br />You must enter a valid email address.';
	}elseif( $email != $remail ){
		$signuperrors.='<br />You must confirm your email address by entering it twice.';
	}
	if( !preg_match('/[a-z]{2,}/i', $name) ) $signuperrors.='You must enter your name.';
	if( $signuperrors == '' ){
		$sql = "SELECT username, email FROM users WHERE username='".addslashes($username)."' OR email='".addslashes($email)."' ";
		$res = $ace->query($sql, 'Check for existing username or email');
		while( list($uname, $em) = mysql_fetch_row($res) ){
			if( strtolower($uname) == strtolower($username) ){
				$signuperrors.='<br />The username "'.$username.'" is already in our database. Please choose a different username.';
			}elseif( strtolower($em) == strtolower($email) ){
				$signuperrors.='<br />The email address "'.$email.'" is already in our database.';
			}
		}
		if( $ace->getint('agree', $_POST) == 0 ){
			$signuperrors.='<br />You must confirm that you have read and agree to our terms &amp; conditions.';
		}
		if( $signuperrors == '' ){

			// call the function that actually adds the user to the database.

			if( $users->adduser($username, $password, $email, $name) != 0 ){
				$joined = true;

				// send a confirmation email - if the admin requires new users to confirm their email address
				// then this includes instructions on how to do so.

				$from = $config->signup_email_from;
				$subject = $config->signup_email_subject;
				$message = $config->signup_email_template;
				if( $config->email_confirmation == 0 ){
					$message = preg_replace('/<confirm>.*?<\/confirm>/is', '', $message);
				}else{
					$message = preg_replace('/<[\/]{0,1}confirm>/is', '', $message);
				}
				$cid = substr(md5($email.$username.$config->email_confirmation_key),0,12);
				$message = str_replace(array('{confirmid}', '{name}', '{username}', '{password}', '{sitename}',
																		'{siteurl}'),
															array($cid, $name, $username, $password, $config->sitename, $config->siteurl),
															$message);
				$headers = "From: $from\r\nReply-To: $from\r\nReturn-Path: $from\r\nErrors-To: $from";
				@mail($email, $subject, $message, $headers);
			}else{
				$signuperrors.="<br />".join("<br />\n", $users->errors);
			}
		}
	}
}

if( $joined ){

	// if the member has successfully joined, display a relevant message

	if( $config->email_confirmation ){
		// if the member needs to confirm their email address, display relevant message.
		$pagecontent->display('Signed Up - Unconfirmed');
	}else{
		// if the user doesn't need to confirm their email address, display relevant message.
		$pagecontent->display('Signed Up - Complete');
	}

}else{

	// if the form hasn't been submitted, or there were errors, display the sign-up page content and form.

	$pagecontent->display('Sign Up');

	// display any error messages

	if( $signuperrors != '' ){
?>
<div class="errors">Please correct the following errors:<br />
<?php echo $signuperrors;?>
</div>
<?php
	}

	// display the actual form itself.

?>
<form name="joinform" action="<?php echo $_SERVER['SCRIPT_NAME'];?>" method="post">
<div class="memberform">
<table align="center" class="membertable" cellspacing="1" cellpadding="0" border="0">
<tr>
	<td colspan="2" class="memberheading">Create An Account</td>
</tr>
<tr>
	<td class="memberlabel">Your Name:</td>
	<td class="memberfield"><input class="memberinput" type="text" name="name" value="<?php echo htmlspecialchars($name);?>" />
		<br />Enter the name you wish to be addressed by in any correspondence with this site.
	</td>
</tr>
<tr>
	<td class="memberlabel">Username:</td>
	<td class="memberfield"><input class="memberinput" type="text" name="username" value="<?php echo htmlspecialchars($username);?>" />
		<br />Choose a username. Your username must be between 6 and 20 characters long and can only contain
		alphanumeric characters (a-z and 0-9).
	</td>
</tr>
<tr>
	<td class="memberlabel">Email:</td>
	<td class="memberfield"><input class="memberinput" type="text" name="email" value="<?php echo htmlspecialchars($email);?>" />
		<br />Enter your email address. If you forget your password, we can send a reminder to this email address.
		<?php if( $config->email_confirmation == 1 ){ ?>
		We will send a confirmation email to this address containing instructions on how to finish creating your account.
		<?php } ?>
	</td>
</tr>
<tr>
	<td class="memberlabel">Repeat Email:</td>
	<td class="memberfield"><input class="memberinput" type="text" name="remail" value="<?php echo htmlspecialchars($remail);?>" />
		<br />Please repeat your email address here to confirm you have entered it correctly.
	</td>
</tr>
<tr>
	<td class="memberlabel">Password:</td>
	<td class="memberfield"><input class="memberinput" type="password" name="password" />
		<br />Choose a password to use when logging in to this site. Your password must be between 6 and 20 
		characters long and contain only alphanumeric characters (a-z and 0-9).
	</td>
</tr>
<tr>
	<td class="memberlabel">Repeat Password:</td>
	<td class="memberfield"><input class="memberinput" type="password" name="rpassword" />
		<br />Please repeat the password you have chosen to confirm its spelling.
	</td>
</tr>
<tr>
	<td class="memberlabel"><input type="checkbox" name="agree" value="1" /></td>
	<td class="memberfield">I have read and agree to the <a target="_blank" href="terms.php">terms and conditions</a> of this site.</td>
</tr>
<tr>
	<td class="memberlabel">&nbsp;</td>
	<td class="memberfield"><input class="memberinput" type="submit" name="join" value="Create Account" /></td>
</tr>
</table>
</div>
</form>
<script language="JavaScript" type="text/javascript">
<!--
document.forms.joinform['name'].focus();
//-->
</script>
<?php

}

// include the site footer

require_once(dirname(__FILE__).'/footer.php');

?>
