<?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   ---------------------*
*-------------- A premium or business paypal account is required, this will NOT work with a personal account------------------*
*-------------------------------------- You can upgrade via the paypal site---------------------------------------------------*
*-----------------------------------------------------------------------------------------------------------------------------*
*-------------------------Remember to change this: yourpaypaladdres@here.com to your paypal address---------------------------*
*---------------------------------- Remember to change yoursite.com to your tracker URL---------------------------------------*
*/

if (defined('IN_PMBT'))die ("You can't include this file");
define("IN_PMBT",true);
include("include/config_lite.php");
function getip() {
   if (isset($_SERVER)) {
     if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && validip($_SERVER['HTTP_X_FORWARDED_FOR'])) {
       $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
     } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && validip($_SERVER['HTTP_CLIENT_IP'])) {
       $ip = $_SERVER['HTTP_CLIENT_IP'];
     } else {
       $ip = $_SERVER['REMOTE_ADDR'];
     }
   } else {
     if (getenv('HTTP_X_FORWARDED_FOR') && validip(getenv('HTTP_X_FORWARDED_FOR'))) {
       $ip = getenv('HTTP_X_FORWARDED_FOR');
     } elseif (getenv('HTTP_CLIENT_IP') && validip(getenv('HTTP_CLIENT_IP'))) {
       $ip = getenv('HTTP_CLIENT_IP');
     } else {
       $ip = getenv('REMOTE_ADDR');
     }
   }

   return $ip;
}
function logerror($message, $error = _btgenerror) {
        global $db, $db_prefix, $user;
                $ip = getip();
       $sql = "INSERT INTO ".$db_prefix."_log (action, results, ip, host, userid) VALUES ('".addslashes($error)."', '".addslashes($message)."', '".sprintf("%u",ip2long($ip))."',  '".gethostbyaddr($ip)."', '".$user->id."');";
        $db->sql_query($sql);
        return;
}
function gmtime()
{
    return strtotime(get_date_time());
}

function get_date_time($timestamp = 0)
{
  if ($timestamp)
    return date("Y-m-d H:i:s", $timestamp);
  else
    return gmdate("Y-m-d H:i:s");
}

// read the post from PayPal system and add 'cmd'
// Read the post from PayPal and add 'cmd' 
$req = 'cmd=_notify-validate'; 
if(function_exists('get_magic_quotes_gpc')) 
  { $get_magic_quotes_exits = true;} 
foreach ($_POST as $key => $value) 
  // Handle escape characters, which depends on setting of magic quotes 
  { if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) 
    {  $value = urlencode(stripslashes($value)); 
  } else { 
    $value = urlencode($value); 
  }  
  $req .= "&$key=$value";  
} 
logerror($req,_btpaypal_donation);
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);

// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
$clid = $_POST['payer_id'];
$gift = $_POST['custom'];
if($gift == 3 AND $payment_amount < 10){
$gift = 1;
$gift_message = _btpaypal_giftmsg;
}
$ftp_gift = false;
if($gift == 3){
$ftp_gift = true;
$gift_message = _btpaypal_ftp;
}
if($gift == 1)$gift_message = _btpaypal_giftmsg1;
if($gift == 2)$gift_message = _btpaypal_giftmsg2;
$donated = $payment_amount;
$days = $donated/0.322;
$time = 86400*$days;
$gig = 1025*1025*1025*$donated;//86400
$gig = $gig*10;

if (!$fp) {
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
$sql = "SELECT * FROM ".$db_prefix."_users WHERE id='$item_number';";
$row = $db->sql_query($sql);
$user = $db->sql_fetchrow($row);
if($user['dondate'] == '0000-00-00 00:00:00' OR get_date_time(gmtime() - $user['donator_tell']) > $user['dondate'])$donatedate = get_date_time();
else
$donatedate = $user['dondate'];
  $db->sql_query("UPDATE ".$db_prefix."_users SET uploaded = uploaded + '$gig', dondate = NOW(), donator_tell = UNIX_TIMESTAMP(donator_tell) + '$time',  `dongift` = $gift, donator = 'true', donated = donated + '$payment_amount' WHERE id='$item_number'");
  $db->sql_query("UPDATE ".$db_prefix."_paypal SET reseaved_donations = $donatein + '$payment_amount' ");
  $db->sql_query("INSERT INTO ".$db_prefix."_private_messages (sent, sender, recipient, subject, text) VALUES (NOW(), 0, $item_number, '"._btpaypal_donation."', '"._btpaypal_donation_thanks."$".$payment_amount."\n".$gift_message."')");
  if($ftp_gift)$db->sql_query("INSERT INTO ".$db_prefix."_private_messages (sent, sender, recipient, subject, text) VALUES (NOW(), 0, 145, '"._btpaypal_donation_ftp."', '".$item_number.""._btpaypal_donation_ftp_req."')");

}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation

}//header("Location: ".$siteurl."");
}
fclose ($fp);
}
?>