<?

require_once("include/bittorrent.inc.php");

if (!mkglobal("wantusername:wantpassword:passagain:email:emailagain"))
	die();

function bark($msg) {
	genbark($msg, "Signup failed!");
}

if (empty($wantusername) || empty($wantpassword) || empty($email))
	bark("Don't leave any fields blank.");

if ($wantpassword != $passagain)
	bark("The passwords didn't match! Must've typoed. Try again.");

if (strtolower($email) != strtolower($emailagain))
	bark("The email addresses didn't match! Must've typoed. Try again.");

if (strlen($wantpassword) > 15)
	bark("Sorry, password is too long (max is 15 chars)");

if (!validemail($email))
	bark("That doesn't look like a valid email address.");

if (!preg_match('/^[a-z][\w.-]*$/is', $wantusername) || strlen($wantusername) > 15)
	bark("Invalid username. Must not be more than 15 characters long and no weird characters");

dbconn();

$secret = mksecret();

$ret = mysql_query("INSERT INTO users (username, password, secret, email, status, added) VALUES (" .
		implode(",", array_map("sqlesc", array($wantusername, $wantpassword, $secret, $email, 'pending'))) .
		", NOW())");

if (!$ret) {
	if (mysql_errno() == 1062)
		bark("Username already exists!");
	bark("borked");
}

$id = mysql_insert_id();
$psecret = md5($secret);

$body = <<<EOD
You have requested a new user account on $tracker_url and you have
specified this address ($email) as user contact.

If you did not do this, please ignore this email. The person who entered your
email address had the IP address {$_SERVER["REMOTE_ADDR"]}. Please do not reply.

To confirm your user registration, you have to follow this link:

$tracker_url/confirm.php/$id/$psecret

After you do this, you will be able to use your new account. If you fail to
do this, you account will be deleted within a few days.
EOD;

mail($email, "$thisdomain user registration confirmation", $body, "From: $tracker_url");

header("Refresh: 0; url=ok.php?type=signup&email=" . urlencode($email));

?>
