I've redone the invites code and it appears to work properly. It's integrated into signup instead of having an extra 3 or so files for invites.
invite.php:
invite.php:
<?php
require_once("backend/functions.php");
dbconn();
loggedinonly;
if (!$site_config["INVITEONLY"] && !$site_config["ENABLEINVITES"]) {
show_error_msg("Invites are disabled.", "Invites are disabled. Please use the register link.", 1);
}
$users = get_row_count("users", "WHERE enabled = 'yes'");
if ($users >= $site_config["maxusers_invites"]) {
print("Sorry, The current user account limit (" . number_format($site_config["maxusersinvites"]) . ") has been reached. Inactive accounts are pruned all the time, please check back again later...");
end_frame();
exit;
}
if ($CURUSER["invites"] == 0) {
show_error_msg("You have no invites", "You don't have any invites. Invites are given automatically based on your ratio.", 1);
}
if ($_GET["take"]) {
if (!validemail($email))
show_error_msg("Error", "That doesn't look like a valid email.", 1);
//check email isnt banned
$maildomain = (substr($email, strpos($email, "@") + 1));
$a = (@mysql_fetch_row(@mysql_query("select count(*) from email_bans where mail_domain='$email'"))) or die(mysql_error());
if ($a[0] != 0)
$message = "The e-mail address $email is Banned.";
$a = (@mysql_fetch_row(@mysql_query("select count(*) from email_bans where mail_domain='$maildomain'"))) or die(mysql_error());
if ($a[0] != 0)
$message = "The e-mail address $email is Banned.";
// check if email addy is already in use
if (get_row_count("users", "WHERE email='$email'"))
$message = "The email address '$email' is already in use.";
if ($message)
show_error_msg("Error", $message, 1);
$secret = mksecret();
$username = mksecret(40);
$ret = mysql_query("INSERT INTO users (username, secret, email, status, invited_by, added) VALUES (".
implode(",", array_map("sqlesc", array($username, $secret, $email, 'pending', $CURUSER["id"]))) . ",'" . get_date_time() . "')");
if (!$ret) {
// If username is somehow taken, keep trying
while (mysql_errno() == 1062) {
$username = mksecret(40);
$ret = mysql_query("INSERT INTO users (username, secret, email, status, invited_by, added) VALUES (".
implode(",", array_map("sqlesc", array($username, $secret, $email, 'pending', $CURUSER["id"]))) . ",'" . get_date_time() . "')");
}
show_error_msg("Error", "Database error. Please report this to an admin.", 1);
}
$id = mysql_insert_id();
$invitees = "$id $CURUSER[invitees]";
mysql_query("UPDATE users SET invites = invites - 1, invitees='$invitees' WHERE id = $CURUSER[id]");
$psecret = md5($secret);
$mess = strip_tags($mess);
$body = <<<EOD
You have been invited to $site_config[SITENAME] by $CURUSER[username]. They have specified this address ($email) as your email.
If you do not know this person, please ignore this email. Please do not reply.
Message:
-------------------------------------------------------------------------------
$mess
-------------------------------------------------------------------------------
This is a private site and you must agree to the rules before you can enter:
$site_config[SITEURL]/rules.php
$site_config[SITEURL]/faq.php
To confirm your invitation, you have to follow this link:
$site_config[SITEURL]/account-signup.php?invite=$id&secret=$psecret
After you do this, you will be able to use your new account. If you fail to
do this, your account will be deleted within a few days. We urge you to read
the RULES and FAQ before you start using $site_config[SITENAME].
EOD;
mail($email, "$site_config[SITENAME] user registration confirmation", $body, "From: $site_config[SITENAME] <$site_config[SITEEMAIL]>");
header("Refresh: 0; url=account-confirm-ok.php?type=invite&email=" . urlencode($email));
die;
}
stdhead("Invite");
begin_frame("Invite");
?>
<p>
<form method="post" action="invite.php?take=1">
<table border="0" cellspacing=0 cellpadding="3">
<tr valign=top><td align="right" class="heading"><B>Email Address:</B></td><td align=left><input type="text" size="40" name="email" />
<table width=250 border=0 cellspacing=0 cellpadding=0><tr><td class=embedded><font class=small>Please make sure this is a valid email address, the recipient will receive a confirmation email.</td></tr>
</font></td></tr></table>
<tr><td align="right" class="heading"><B>Message:</B></td><td align=left><textarea name="mess" rows="10" cols="80"></textarea>
</td></tr>
<tr><td colspan="2" align="center"><input type=submit value="Send Invite (PRESS ONLY ONCE)" style='height: 25px'></td></tr>
</table>
</form>
<?
end_frame();
stdfoot();
?>
account-signup.php:
<?
//
// TorrentTrader v2.x
// This file was last updated: 21/June/2007
//
// http://www.torrenttrader.org
//
//
require_once("backend/functions.php");
dbconn();
// Disable checks if we're signing up with an invite
if (!is_valid_id($_REQUEST["invite"]) || strlen($_REQUEST["secret"]) != 32) {
//invite only check
if ($site_config["INVITEONLY"]) {
show_error_msg("Invite only", "<br><br><center>Sorry this site has disabled user registration, the only way to register is via an invite from an existing member.<br><br></center>",1);
}
//get max members, and check how many users there is
$numsitemembers = get_row_count("users");
if ($numsitemembers >= $site_config["maxusers"])
show_error_msg("Sorry...", "The site is full!<br>The limit of ".number_format($site_config["maxusers"])." users have been reached.<br>HOWEVER, user accounts expire all the time so please check back again later!<BR><BR>There is currently ".number_format($numsitemembers)." members",1);
} else {
$res = mysql_query("SELECT id FROM users WHERE id = $_REQUEST[invite] AND MD5(secret) = ".sqlesc($_REQUEST["secret"])."");
$invite_row = mysql_fetch_array($res);
if (!$invite_row) {
show_error_msg("Error", "No invite found with those details. Unconfirmed accounts/invites expire after ".($site_config['signup_timeout']/86400)." days.", 1);
}
}
if ($takesignup == "1") {
$message == "";
function validusername($username) {
$allowedchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for ($i = 0; $i < strlen($username); ++$i)
if (strpos($allowedchars, $username[$i]) === false)
return false;
return true;
}
//Setup Error Messages
if (empty($wantpassword) || (empty($email) && !$invite_row) || empty($wantusername))
$message = "Don't leave any required field blank.";
elseif (strlen($wantusername) > 15)
$message = "Sorry, username is too long (max is 15 chars)";
elseif ($wantpassword != $passagain)
$message = "The passwords didn't match! Must've typoed. Try again.";
elseif (strlen($wantpassword) < 6)
$message = "Sorry, password is too short (min is 6 chars)";
elseif (strlen($wantpassword) > 40)
$message = "Sorry, password is too long (max is 40 chars)";
elseif ($wantpassword == $wantusername)
$message = "Sorry, password cannot be same as user name.";
elseif (!validusername($wantusername))
$message = "Invalid username.";
elseif (!$invite_row && !validemail($email))
$message = "That doesn't look like a valid email address.";
if ($message == "") {
// Certain checks must be skipped for invites
if (!$invite_row) {
//check email isnt banned
$maildomain = (substr($email, strpos($email, "@") + 1));
$a = (@mysql_fetch_row(@mysql_query("select count(*) from email_bans where mail_domain='$email'"))) or die(mysql_error());
if ($a[0] != 0)
$message = "The e-mail address $email is Banned.";
$a = (@mysql_fetch_row(@mysql_query("select count(*) from email_bans where mail_domain='$maildomain'"))) or die(mysql_error());
if ($a[0] != 0)
$message = "The e-mail address $email is Banned.";
// check if email addy is already in use
$a = (@mysql_fetch_row(@mysql_query("select count(*) from users where email='$email'"))) or die(mysql_error());
if ($a[0] != 0)
$message = "The e-mail address $email is already in use.";
}
//check username isnt in use
$a = (@mysql_fetch_row(@mysql_query("select count(*) from users where username='$wantusername'"))) or die(mysql_error());
if ($a[0] != 0)
$message = "The username $wantusername is already in use.";
$secret = mksecret(); //generate secret field
$wantpassword = md5($wantpassword);//md5 hash the password
}
if ($message != "")
show_error_msg("Signup Failed", $message, 1);
if ($message == "") {
if ($invite_row) {
mysql_query("UPDATE users SET username=".sqlesc($wantusername).", password=".sqlesc($wantpassword).", secret=".sqlesc($secret).", status='confirmed', added='".get_date_time()."' WHERE id=$invite_row[id]");
header("Refresh: 0; url=account-confirm-ok.php?type=confirm");
die;
}
if ($site_config["CONFIRMEMAIL"]) { //req confirm email true/false
$status = "pending";
}else{
$status = "confirmed";
}
//make first member admin
if ($numsitemembers == '0')
$signupclass = '7';
else
$signupclass = '1';
$ret = mysql_query("INSERT INTO users (username, password, secret, email, status, added, age, country, gender, client, stylesheet, language, class) VALUES (" .
implode(",", array_map("sqlesc", array($wantusername, $wantpassword, $secret, $email, $status, get_date_time(), $age, $country, $gender, $client, $site_config["default_theme"], $site_config["default_language"], $signupclass))).")");
$id = mysql_insert_id();
$psecret = md5($secret);
$thishost = $_SERVER["HTTP_HOST"];
$thisdomain = preg_replace('/^www\./is', "", $thishost);
//ADMIN CONFIRM
if ($site_config["ACONFIRM"]) {
$body = "Your account at ".$site_config['SITENAME']." has been created.\n\nYou will have to wait for the approval of an admin before you can use your new account.\n\n".$site_config['SITENAME']." Admin";
}else{//NO ADMIN CONFIRM, BUT EMAIL CONFIRM
$body = "Your account at ".$site_config['SITENAME']." has been : APPROVED\n\nTo confirm your user registration, you have to follow this link:\n\n ".$site_config['SITEURL']."/account-confirm.php?id=$id&secret=$psecret\n\nAfter you do this, you will be able to use your new account.\n\n If you fail to do this, your account will be deleted within a few days.\n\n".$site_config['SITENAME']." Admin";
}
if ($site_config["CONFIRMEMAIL"]){ //email confirmation is on
ini_set("sendmail_from", "");
mail($email, "Your ".$site_config['SITENAME']." User Account", $body, "From: ".$site_config['SITENAME']." <".$site_config['SITEEMAIL'].">");
header("Refresh: 0; url=account-confirm-ok.php?type=signup&email=" . urlencode($email));
}else{ //email confirmation is off
header("Refresh: 0; url=account-confirm-ok.php?type=noconf");
}
//send pm to new user
if ($site_config["WELCOMEPMON"]){
$dt = sqlesc(get_date_time());
$msg = sqlesc($site_config["WELCOMEPMMSG"]);
mysql_query("INSERT INTO messages (sender, receiver, added, msg, poster) VALUES(0, $id, $dt, $msg, 0)");
}
die;
}
}//end takesignup
stdhead("Signup");
begin_frame("Signup");
?>
<? echo "" . COOKIES . "";?>
<p>
<form method="post" action="account-signup.php?takesignup=1">
<?php if ($invite_row) { ?>
<input type="hidden" name="invite" value="<?php echo $_GET[invite]; ?>" />
<input type="hidden" name="secret" value="<?php echo $_GET[secret]; ?>" />
<?php } ?>
<table cellSpacing="0" cellPadding="2" border="0" >
<tr>
<td>Username: <font class="small"><font color="#FF0000">*</font></td>
<td><input type="text" size="40" name="wantusername" /></td>
</tr>
<tr>
<td>Password: <font class="small"><font color="#FF0000">*</font></td>
<td><input type="password" size="40" name="wantpassword" /></td>
</tr>
<tr>
<td>Confirm: <font class="small"><font color="#FF0000">*</font></td>
<td><input type="password" size="40" name="passagain" /></td>
</tr>
<?php if (!$invite_row) {?>
<tr>
<td>Email: <font class="small"><font color="#FF0000">*</font></td>
<td><input type="text" size="40" name="email"/></td>
</tr>
<?php } ?>
<tr>
<td>Age:</td>
<td><input type="text" size="40" name="age" maxlength="3" /></td>
</tr>
<tr>
<td>Country:</td>
<td>
<select name="country" size="1">
<?php
$countries = "<option value=\"0\">---- None selected ----</option>\n";
$ct_r = mysql_query("SELECT id,name,domain from countries ORDER BY name") or die;
while ($ct_a = mysql_fetch_array($ct_r)) {
$countries .= "\t\t\t\t\t\t<option value=\"$ct_a[id]\"";
if ($dom == $ct_a["domain"])
$countries .= " SELECTED";
$countries .= ">$ct_a[name]</option>\n";
}
?>
<?=$countries ?>
</select>
</td>
</tr>
<tr>
<td>Gender:</td>
<td>
<input type="radio" name="gender" value="Male">Male
<input type="radio" name="gender" value="Female">Female
</td>
</tr>
<tr>
<td>Preferred BitTorrent Client:</td>
<td><input type="text" size="40" name="client" maxlength="20" /></td>
</tr>
<tr>
<td align="middle" colSpan="2">
<input type="submit" value="Sign Up" />
</td>
</tr>
</table>
</form>
<?
end_frame();
stdfoot();
?>
blocks/invite_block.php:
<?
if (($site_config["INVITEONLY"] || $site_config["ENABLEINVITES"]) && $CURUSER) {
$invites = $CURUSER["invites"];
begin_block("" . INVITES . "");
?>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr><td align="center"><? print("" . YOUHAVE . "\n"); ?> <?=$invites?> <? print("" . INVITES . "\n"); ?><br></td></tr>
<?if ($invites > 0 ){?>
<tr><td align="center"><a href=invite.php><? print("" . SENDANINVITE . "\n"); ?></a><br></td></tr>
<?}?>
</table>
<?
end_block();
}
?>
add to backend/cleanup.php (outside do_cleanup function):
// Invite update function (Author: TorrentialStorm)
function autoinvites($interval, $minlimit, $maxlimit, $minratio, $invites, $maxinvites) {
$time = gmtime() - ($interval*86400);
$minlimit = $minlimit*1024*1024*1024;
$maxlimit = $maxlimit*1024*1024*1024;
$res = mysql_query("SELECT id, username, class, invites FROM users WHERE enabled = 'yes' AND downloaded >= $minlimit AND downloaded < $maxlimit AND uploaded / downloaded >= $minratio AND warned = 'no' AND enabled='yes' AND UNIX_TIMESTAMP(invitedate) <= $time") or die;
if (mysql_num_rows($res) > 0) {
while ($arr = mysql_fetch_assoc($res)) {
$maxninvites = $maxinvites[$arr['class']];
if ($arr['invites'] >= $maxninvites)
continue;
if (($maxninvites-$arr['invites']) < $invites)
$invites = $maxninvites - $arr['invites'];
mysql_query("UPDATE users SET invites = invites+$invites, invitedate = NOW() WHERE id=$arr[id]");
write_log("Gave $invites invites to '$arr[username]' - Class: ".get_user_class_name($arr['class'])."");
}
}
}
add before //OPTIMISE TABLES:
// START INVITES UPDATE
// SET INVITE AMOUNTS ACCORDING TO RATIO/GIGS ETC
// autoinvites(interval to give invites (days), min downloaded GB, max downloaded GB, min ratio, invites to give, max invites allowed (array))
// $maxinvites[CLASS ID] = max # of invites;
$maxinvites[1] = 5; // User
$maxinvites[2] = 10; // Power User
$maxinvites[3] = 20; // VIP
$maxinvites[4] = 25; // Uploader
$maxinvites[5] = 100; // Moderator
$maxinvites[6] = 100; // Super Moderator
$maxinvites[7] = 400; // Administrator
// Give 1 invite every 21 days to users with > 1GB downloaded AND < 4GB downloaded AND ratio > 0.50
autoinvites(21, 1, 4, 0.50, 1, $maxinvites);
autoinvites(14, 1, 4, 0.90, 2, $maxinvites);
autoinvites(14, 4, 7, 0.95, 2, $maxinvites);
$maxinvites[1] = 7; // User
autoinvites(14, 7, 10, 1.00, 3, $maxinvites);
$maxinvites[1] = 10; // User
autoinvites(14, 10, 100000, 1.05, 4, $maxinvites);
//END INVITES
add to backend/config.php:
$site_config['ENABLEINVITES'] = true; // Enable invites regardless of INVITEONLY setting
$site_config['maxusers'] = 20000; // Max # of enabled accounts
$site_config['maxusers_invites'] = $site_config['maxusers'] + 5000; // Max # of enabled accounts when inviting
account-details.php, find:
</tr>
<?
replace with:
<?php if ($CURUSER["edit_users"] == "yes") { ?>
<tr>
<td width="50%"><B>Staff Only Info</B></td>
</tr>
<tr valign="top">
<td align="left">
<?php
if ($user["invited_by"]) {
$res = mysql_query("SELECT username FROM users WHERE id=$user[invited_by]");
$row = mysql_fetch_array($res);
echo "<B>Invited by:</B> <a href=\"account-details.php?id=$user[invited_by]\">$row[username]</a><BR>";
}
echo "<B>Invites:</B> $user[invites]<BR>";
$invitees = array_reverse(explode(" ", $user["invitees"]));
$rows = array();
foreach ($invitees as $invitee) {
$res = mysql_query("SELECT id, username FROM users WHERE id='$invitee' and status='confirmed'");
if ($row = mysql_fetch_array($res)) {
$rows[] = "<a href=\"account-details.php?id=$row[id]\">$row[username]</a>";
}
}
if ($rows)
echo "<B>Invitees:</B> ".implode(", ", $rows)."<BR>";
?>
</td>
</tr>
<?php
}
account.php, find:
<? print("<B>Passkey:</b> " . $CURUSER["passkey"] . "\n"); ?><br>
add after:
<?php
if ($CURUSER["invited_by"]) {
$res = mysql_query("SELECT username FROM users WHERE id=$CURUSER[invited_by]");
$row = mysql_fetch_array($res);
echo "<B>Invited by:</B> <a href=\"account-details.php?id=$CURUSER[invited_by]\">$row[username]</a><BR>";
}
echo "<B>Invites:</B> $CURUSER[invites]<BR>";
$invitees = array_reverse(explode(" ", $CURUSER["invitees"]));
$rows = array();
foreach ($invitees as $invitee) {
$res = mysql_query("SELECT id, username FROM users WHERE id='$invitee' and status='confirmed'");
if ($row = mysql_fetch_array($res)) {
$rows[] = "<a href=\"account-details.php?id=$row[id]\">$row[username]</a>";
}
}
if ($rows)
echo "<B>Invitees:</B> ".implode(", ", $rows)."<BR>";
?>