<?php
/*
*----------------------------phpMyBitTorrent V 2.0.4---------------------------*
*--- The Ultimate BitTorrent Tracker and BMS (Bittorrent Management System) ---*
*--------------   Created By Antonio Anzivino (aka DJ Echelon)   --------------*
*-------------               http://www.p2pmania.it               -------------*
*------------ Based on the Bit Torrent Protocol made by Bram Cohen ------------*
*-------------              http://www.bittorrent.com             -------------*
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*
*--   This program is free software; you can redistribute it and/or modify   --*
*--   it under the terms of the GNU General Public License as published by   --*
*--   the Free Software Foundation; either version 2 of the License, or      --*
*--   (at your option) any later version.                                    --*
*--                                                                          --*
*--   This program is distributed in the hope that it will be useful,        --*
*--   but WITHOUT ANY WARRANTY; without even the implied warranty of         --*
*--   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          --*
*--   GNU General Public License for more details.                           --*
*--                                                                          --*
*--   You should have received a copy of the GNU General Public License      --*
*--   along with this program; if not, write to the Free Software            --*
*-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA --*
*--                                                                          --*
*------------------------------------------------------------------------------*
*------              ©2005 phpMyBitTorrent Development Team              ------*
*-----------               http://phpmybittorrent.com               -----------*
*------------------------------------------------------------------------------*
*-----------------   Sunday, September 14, 2008 9:05 PM   ---------------------*
*/
if (defined('IN_PMBT'))die ("You can't include this file");
define("IN_PMBT",true);
include("header.php");
include_once("language/mailtexts.php");
global $db, $db_prefix;
function validemail($email) {
    return preg_match('/^[\w.-]+@([\w.-]+\.)+[a-z]{2,6}$/is', $email);
}

function mksecret($len = 20) {
    $ret = "";
    for ($i = 0; $i < $len; $i++)
        $ret .= chr(mt_rand(0, 255));
    return $ret;
}

$sql=("SELECT COUNT(*) FROM ".$db_prefix."_users")or sql_error();
$res = $db->sql_query($sql) or btsqlerror($sql);
$arr = $db->sql_fetchrow($res);
if ($arr[0] >= $invites1)bterror(""._btsignup_limit_reached."(" . number_format($invites1) . ")"._btsignup_reached."",""._btsignup_limit."");
if($user->invites == 0)bterror(_btsignup_noinvites);
$mess= unesc($_POST["mess"]);
if (!$mess) bterror(_btsignup_message);
if (!mkglobal("email"))die();
if (!validemail($email))bterror(_btsignup_invalid_email);

// check if email addy is already in use
$a = ($db->sql_numrows($db->sql_query("select count(*) from ".$db_prefix."_users where email='$email'"))) or die(mysql_error());
if ($a[0] != 0)bterror(_bterremailexists);
$secret = mksecret();
$editsecret = mksecret();
$username = rand();
if($force_passkey){
                do {
                        $passkey = ", '".RandomAlpha(32)."'";
                        //Check whether passkey already exists
                        $sql = "SELECT passkey FROM ".$db_prefix."_users WHERE passkey = '".$passkey."';";
                        $res = $db->sql_query($sql);
                        $cnt = $db->sql_numrows($sql);
                        $db->sql_freeresult($res);
                } while ($cnt > 0);
				$passkeyrow = ', passkey';
				}else{
				$passkeyrow = NULL;
				$passkey = NULL;
				}
$act_key = RandomAlpha(32);
$ret = $db->sql_query("INSERT INTO ".$db_prefix."_users (username, password, email, active, act_key, invited_by, regdate" . $passkeyrow . ") VALUES ('" .$username. "', '". $secret ."', '" .$email ."', '0', '".$act_key."', '". $user->id ."', now() " . $passkey .")")or die(mysql_error());
$id = $db->sql_nextid();
$id2 = $user->id;
$invites = $user->invites -1;
$invitees = $user->invitees;
$invitees2 = "$id $invitees";
 $db->sql_query("UPDATE ".$db_prefix."_users SET invites='$invites', invitees='$invitees2' WHERE id = $id2");
$username = $user->name;

$psecret = md5($secret);

$message = ($html ? strip_tags($mess) : $mess);

$replace_markers = Array("**sitename**","**siteurl**","**inviter**","**toemail**","**message**","**newid**","**newsecrt**");
$replace_data = Array ($sitename,$siteurl,$username,$email,$message,$id,$psecret);

$confirm_mail = New eMail();
$confirm_mail->sender = $admin_email;
$confirm_mail->subject = $userinviteconfirmmailsub[$language];
$confirm_mail->body = str_replace($replace_markers,$replace_data,$inviteuser[$language]);
$confirm_mail->Add($email);
$confirm_mail->Send();

header("Refresh: 0; url=user.php?op=profile&id=".$user->id."&type=invite&email=" . urlencode($email));
?>