<?php
include "cfg.php";
include "ini.php";

$secret_hash = 'qh6w5g99C1eCqlDO1eVfTOoA';
$user = $_POST['label'];
$sum = $_POST['amount'];
$purse = $_POST['operation_id'];
$hash['yad'] = $_POST['sha1_hash'];
$hash['our'] = sha1($_POST['notification_type'] . '&' . $_POST['operation_id'] . '&' . $_POST['amount'] . '&' . $_POST['currency'] . '&' . $_POST['datetime'] . '&' . $_POST['sender'] . '&' . $_POST['codepro'] . '&' . $secret_hash . '&' . $_POST['label']);

mail('desvla@mail.ru', 'YandexMoney - BOMZ', 'DO!!! ' . serialize($_POST));

if ($hash['yad'] == $hash['our']) {

$depo=$sum;
			$get_info	= mysql_query("SELECT * FROM users WHERE id = '".$user."'  LIMIT 1");
			$row		= mysql_fetch_array($get_info);

			$date = date("d.m.Y");

			
				$lastdate = time();
				if(cfgSET('datestart') <= time()) {
				$nextdate = $lastdate + 3600 ;}else{
					$nextdate = cfgSET('datestart') + 3600;
				}
				
				$sql = "INSERT INTO `deposits` (username, user_id, date, plan, sum, paysys, lastdate, nextdate)
									VALUES ('".$row['login']."', '".$user."', ".time().", '2', ".$depo.", 'YandexMoney', ".$lastdate.", ".$nextdate.")";
				mysql_query($sql);
						
//реферальные начисления////
	$get_user = mysql_query( 'SELECT * FROM users WHERE id = \'' . $user . '\' LIMIT 1' );
	$row = mysql_fetch_array( $get_user );
	$uref = $row['ref'];
		// Начисляем нашим "любимым" рефералам
			if($uref) {

				// Подсчитываем кол-во уровней
				$countlvl = mysql_num_rows(mysql_query("SELECT * FROM reflevels"));

				if($countlvl) {
					$i		= 0;
					$uid	= $row['id'];;
					$query	= "SELECT * FROM reflevels ORDER BY id ASC";
					$result	= mysql_query($query);
					while($row = mysql_fetch_array($result)) {
						if($i < $countlvl) {
							$lvlperc = $row['sum'];		// Процент уровня
							$ps		 = sprintf("%01.2f", $sum / 100 * $lvlperc); // Сумма рефских

							if($uref) {

							

								mysql_query('UPDATE users SET lr_balance = lr_balance + '.$ps.', reftopr = reftopr + '.$ps.' WHERE id = '.$uref.' LIMIT 1');

								mysql_query('UPDATE users SET ref_moneyr = ref_moneyr + '.$ps.' WHERE id = '.$uid.' LIMIT 1');
								
								// Получаем данные следующего пользователя

								$get_ref	= mysql_query("SELECT id, ref FROM users WHERE id = ".intval($uref)." LIMIT 1");
								$rowref		= mysql_fetch_array($get_ref);
								$uref		= $rowref['ref'];
								$uid		= $rowref['id'];

							}

						}
						$i++;
					}
				}

			}
			// Закончили с рефералами
}		

