<?php $root=$_SERVER['DOCUMENT_ROOT'];$wroot=dirname($_SERVER['SCRIPT_NAME']);while((strlen($wroot)>1)&&(!file_exists($root.$wroot.'/main')))$wroot=dirname($wroot);if($wroot=='/')$wroot='';require_once($root.$wroot.'/main/app/App.php');
	$error='';$ertype='';
	if ($app->getPost('join')){
		if (!$error){
			$img=$app->imageCatchlet('avatar');
			$rand=rand();$rand="/main/temp/$rand.png";
			if (!$img) copy($app->froot."/main/assets/icons/anonymous.png",$app->froot.$rand);
			else{
				$img=$app->imageGDSquare($img);
				imagepng($img,$app->froot.$rand);
				imagedestroy($img);
			}
		}
		if (!$error){
			$ertype='un';
			$un=addslashes($app->getPost('un'));
			if (!$un) $error='You must provide a username.';
			elseif (strlen($un)>31) $error='Your username must be less than 32 characters.';
			elseif (!ctype_alnum($un)) $error='Usernames must have alphanumeric characters only.';
			elseif ($app->findUser($un)) $error='Username already exists.';
		}
		if (!$error){
			$ertype='pw';
			$pw=$app->getPost('pw');
			$pc=$app->getPost('pc');
			if ($pw=='') $error='You need to provide a password.';
			else if ($pw!==$pc){$ertype='pc'; $error='Password confirmation doesn\'t match password.';}
			else $hash=password_hash($pw,PASSWORD_DEFAULT);
		}
		if (!$error){
			$ertype='un';
			if (!$app->addUser('associate',$un,$hash)) $error='Could not add user to database.';
			if (!$error){
				rename($app->froot.$rand,$app->froot."/main/users/$un/avatar.png");
				$app->login($un,$pw);Header("Location: {$app->fqwroot}/main/special/account.php");
			}
		}
 	}	
	$app->heads('join');
	$app->header();
?>
<section>
	<article>
		<h1 class='article'>Join Auranos.org</h1>
		<h3>Become and Associate</h3>
		<form class='tall' method='post' enctype='multipart/form-data'>
			<?php $app->imageDroplet('avatar'); ?>
			<input type='text' name='un' placeholder='Username' value=<?php echo "'{$_POST['un']}'"; ?> />
			<?php if ($ertype==='un') echo "<p class='error'>$error</p>"; ?>
			<input type='password' name='pw' placeholder='Password' value=<?php echo "'{$_POST['pw']}'"; ?> />
			<?php if ($ertype==='pw') echo "<p class='error'>$error</p>"; ?>
			<input type='password' name='pc' placeholder='Confirm Password' value=<?php echo "'{$_POST['pc']}'"; ?> />
			<?php if ($ertype==='pc') echo "<p class='error'>$error</p>"; ?>
			<input type='submit' value='Join' name='join' />
		</form>
	</article>
</section>
<?php
	$app->footer();
	$app->tails();
?>