<?php

/*
* @package BANANA WARS
* @copyright: (c) 2017 G.P. aka Mvcshka (mvcshka@yandex.ru)
* @license: GNU General Public License version 2; see LICENSE.txt
* @files: freekassa.php
*
*/


session_start();
include($_SERVER['DOCUMENT_ROOT'].'/lib/connect.php');
include($_SERVER['DOCUMENT_ROOT'].'/lib/func.php');
include($_SERVER['DOCUMENT_ROOT'].'/lib/CofigFarm.php');

$merchant_id = $IdMerchantFreekassa;
$merchant_secret = $SecretKeyFreeKassa2;
function getIP() {
if(isset($_SERVER['HTTP_X_REAL_IP'])) return $_SERVER['HTTP_X_REAL_IP'];
   return $_SERVER['REMOTE_ADDR'];
}
if (!in_array(getIP(), array('5.9.72.245', '5.9.72.243', '5.9.29.230'))) {
    die("hacking attempt!");
}
$sign = md5($merchant_id.':'.$_REQUEST['AMOUNT'].':'.$merchant_secret.':'.$_REQUEST['MERCHANT_ORDER_ID']);
if ($sign != $_REQUEST['SIGN']) {
    die('wrong sign');
}


	$q = $mysql->prepare("SELECT * FROM db_enter WHERE Uid = ?");
	$q->execute(array($_REQUEST['MERCHANT_ORDER_ID']));
	
	if($q->rowCount() == 1)
	{
		$w = $q->fetch();
		if($w['Status'] == 0)
		{
			if(sprintf("%01.2f", $_REQUEST['AMOUNT'])==$w['Summa'])
			{
				$sumRub = $_REQUEST['AMOUNT'];
				$Serebro = (int)$sumRub * $SerebroPrice;
				$refId = GetUser('RefId', $w['UserId']);
				$lvl = GetUser('Level', $refId);
				$SumRef = intval($Serebro / 100 * (8 + $RefPercent[$lvl]));
				$e = $mysql->prepare("UPDATE db_users SET MoneyIn = MoneyIn + ?, ReferMoney = ReferMoney + ?, MoneyEnter = MoneyEnter + ? WHERE Uid = ?");
				$e->execute(array($Serebro, $SumRef, $sumRub, $w['UserId']));
				
				//$refId = GetUser('RefId', $w['UserId']);
				//$lvl = GetUser('Level', $refId);
				//$SumRef = intval($Serebro / 100 * (5 + $RefPercent[$lvl]);
				$r = $mysql->prepare("UPDATE db_users SET MoneyOut = MoneyOut + ?, RefMoney = RefMoney + ? WHERE Uid = ?");
				$r->execute(array($SumRef, $SumRef, $refId));
				
				$Reut = GetUser('Reuting', $refId);
				$LevelAcc = GetUser('Level', $refId);
				$UserMoney = GetUser('MoneyOut', $refId);
				$UserMoneyIn = GetUser('MoneyIn', $refId);
				$FullOpyt = $Lev[$LevelAcc];
				ReutingUser($refId, $LevelAcc, $Reut, $FullOpyt, $Serebro * 0.5);
				
				
				$UserMoney1 = GetUser('MoneyOut', $w['UserId']);
				$UserMoneyIn1 = GetUser('MoneyIn', $w['UserId']);
				$gg1 = $mysql->prepare("INSERT INTO db_log SET UserId = ?, DateAdd = ?, `Text` = ?, MoneyIn = ?, MoneyOut = ?");
				$gg1->execute(array($w['UserId'], time(), 'Пополнение баланса <img src="/images/moneyin.png"/><span class="title fs14">+ '.$Serebro.'</span>', $UserMoney1, $UserMoneyIn1));
				
				
				$gg = $mysql->prepare("INSERT INTO db_log SET UserId = ?, DateAdd = ?, `Text` = ?, MoneyIn = ?, MoneyOut = ?");
				$gg->execute(array($refId, time(), 'Пополнение рефералом <img src="/images/level.png"/><span class="title fs14">+ '. $Serebro * 0.5 .'</span><img src="/images/moneyout.png"/><span class="title fs14">+ '.$SumRef.'</span>', $UserMoney, $UserMoneyIn));
				
				$t = $mysql->prepare("UPDATE db_enter SET Status = ? WHERE Uid = ?");
				$t->execute(array('1', $_REQUEST['MERCHANT_ORDER_ID']));
				
				//Конкурс рефералов
				include("lib/cref.php");
				$c = new competition($mysql);
				$c->UpdatePoints($w['UserId'], $sumRub);
				//Конкурс инвесторов
				include("lib/cinv.php");
				$v = new competition($mysql);
				$v->UpdatePoints($w['UserId'], $sumRub);
			}
		}
		else
		{
			exit;
		}
	}
	else
	{
		exit;
	}
	
die('YES');
	
?>