<?php
function checkEmail($email){
	return preg_match("|^[-0-9a-z_\.]+@[-0-9a-z_^\.]+\.[a-z]{2,6}$|i", $email);
}

function heshPass($str){
	return md5(md5($str)."rost");
}

function getIP(){
	if (!empty($_SERVER['HTTP_CLIENT_IP'])) 
 {
   $ip=$_SERVER['HTTP_CLIENT_IP'];
 }
 elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
 {
  $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
 }
 else
 {
   $ip=$_SERVER['REMOTE_ADDR'];
 }
 return $ip;
}

function make_signature($merchant_id, $out_amount, $secret_word, $order_id)
{
    return md5($merchant_id.":".$out_amount.":".$secret_word.":".$order_id);
}

function userRating($rayt){
	if($rayt < 10)
		return "<img align='absmiddle' src='/style/img/pogons/1.png' alt='Рядовой' title='Рядовой' />";
	elseif($rayt < 25)
		return "<img align='absmiddle' src='/style/img/pogons/2.png' alt='' title='' />";
	elseif($rayt < 40)
		return "<img align='absmiddle' src='/style/img/pogons/3.png' alt='' title='' />";
	elseif($rayt < 55)
		return "<img align='absmiddle' src='/style/img/pogons/4.png' alt='' title='' />";
	elseif($rayt < 75)
		return "<img align='absmiddle' src='/style/img/pogons/5.png' alt='' title='' />";
	elseif($rayt > 100)
		return "<img align='absmiddle' src='/style/img/pogons/6.png' alt='' title='' />";
	elseif($rayt < 115)
		return "<img align='absmiddle' src='/style/img/pogons/7.png' alt='' title='' />";
	elseif($rayt < 140)
		return "<img align='absmiddle' src='/style/img/pogons/8.png' alt='' title='' />";
	elseif($rayt < 170)
		return "<img align='absmiddle' src='/style/img/pogons/9.png' alt='' title='' />";
	elseif($rayt < 200)
		return "<img align='absmiddle' src='/style/img/pogons/10.png' alt='' title='' />";
	elseif($rayt < 250)
		return "<img align='absmiddle' src='/style/img/pogons/11.png' alt='' title='' />";
	elseif($rayt < 300)
		return "<img align='absmiddle' src='/style/img/pogons/12.png' alt='' title='' />";
	elseif($rayt < 350)
		return "<img align='absmiddle' src='/style/img/pogons/13.png' alt='' title='' />";
	elseif($rayt < 500)
		return "<img align='absmiddle' src='/style/img/pogons/14.png' alt='' title='' />";
	elseif($rayt < 700)
		return "<img align='absmiddle' src='/style/img/pogons/15.png' alt='' title='' />";
	else
		return "<img align='absmiddle' src='/style/img/pogons/16.png' alt='' title='' />";
}


function getPartnerPay($sum, $proc, $refback)
{
	return $sum/1000*($proc*((100-$refback)/100));
}
function getRefPay($sum, $proc, $refback)
{
	return $refback*$sum/1e5*$proc;
}

function getPartnerProc($rating)
{
	if($rating < 5000)
		return referer1;
	elseif($rating < 10000)
		return referer2;
	elseif($rating < 30000)
		return referer3;
	elseif($rating < 60000)
		return referer4;
	else return referer5;
}

function getLvl($rating){
	if($rating < 200)
		return 1;
	elseif($rating < 450)
		return 2;
	elseif($rating < 1000)
		return 3;
	elseif($rating < 2300)
		return 4;
	elseif($rating < 4800)
		return 5;
	elseif($rating < 10000)
		return 6;
	else return 7;
}
function getRefProc($rating, $bonus_lvl)
{
	$r_lvl = getLvl($rating);
	if($bonus_lvl > $r_lvl) 
	{
		$lvl = $bonus_lvl;
	}
	else $lvl = $r_lvl;
	switch ($lvl) {
		case 1:
			$proc = refback1;
			break;
		
		case 2:
			$proc = refback2;
			break;

		case 3:
			$proc = refback3;
			break;
			
		case 4:
			$proc = refback4;
			break;
			
		case 5:
			$proc = refback5;
			break;
		
		case 6:
			$proc = refback6;
			break;

		case 7:
			$proc = refback7;
			break;			
	}
	return $proc;
}

function getBonusSum($qt, $sum, $pos=1){

	switch ($pos) {
		case 1:
			if($qt == 0) return 0;
			if($qt == 1) return round($sum, 2);
			if($qt == 2) return round(($sum/100)*(bonus_first_place+(bonus_third_place/2)),2);
			else return round($sum/100*bonus_first_place, 2);
			break;
		case 2:
			if($qt == 0) return 0;
			if($qt == 1) return 0;
			if($qt == 2) return round(($sum/100)*(bonus_second_place+(bonus_third_place/2)),2);
			else return round($sum/100*bonus_second_place, 2);
			break;

		case 3:
			if($qt == 0) return 0;
			if($qt == 1) return 0;
			if($qt == 2) return 0;
			else return round($sum/100*bonus_third_place, 2);
			break;
	}
}

function getMonthSumm($qt, $sum, $pos=1){

	switch ($pos) {
		case 1:
			if($qt == 0) return 0;
			if($qt == 1) return round($sum, 2);
			if($qt == 2) return round(($sum/100)*(month_first_place+((month_third_place+month_fourth_place+(month_fifth_place*6))/2)),2);
			if($qt == 3) return round(($sum/100)*(month_first_place+((month_fourth_place+(month_fifth_place*6))/3)),2);
			if($qt == 4) return round(($sum/100)*(month_first_place+((month_fifth_place*6/4))),2);
			if($qt == 5) return round(($sum/100)*(month_first_place+(month_fifth_place)),2);
			if($qt == 6) return round(($sum/100)*(month_first_place+((month_fifth_place*4/6))),2);
			if($qt == 7) return round(($sum/100)*(month_first_place+((month_fifth_place*3/7))),2);
			if($qt == 8) return round(($sum/100)*(month_first_place+((month_fifth_place*2/8))),2);
			if($qt == 9) return round(($sum/100)*(month_first_place+((month_fifth_place/9))),2);
			else return round($sum/100*month_first_place, 2);
			break;
		case 2:
			if($qt == 0) return 0;
			if($qt == 1) return 0;
			if($qt == 2) return round(($sum/100)*(month_second_place+((month_third_place+month_fourth_place+(month_fifth_place*6))/2)),2);
			if($qt == 3) return round(($sum/100)*(month_second_place+((month_fourth_place+(month_fifth_place*6))/3)),2);
			if($qt == 4) return round(($sum/100)*(month_second_place+((month_fifth_place*6/4))),2);
			if($qt == 5) return round(($sum/100)*(month_second_place+(month_fifth_place)),2);
			if($qt == 6) return round(($sum/100)*(month_second_place+((month_fifth_place*4/6))),2);
			if($qt == 7) return round(($sum/100)*(month_second_place+((month_fifth_place*3/7))),2);
			if($qt == 8) return round(($sum/100)*(month_second_place+((month_fifth_place*2/8))),2);
			if($qt == 9) return round(($sum/100)*(month_second_place+((month_fifth_place/9))),2);
			else return round($sum/100*month_second_place, 2);
			break;

		case 3:
			if($qt == 0) return 0;
			if($qt == 1) return 0;
			if($qt == 2) return 0;
			if($qt == 3) return round(($sum/100)*(month_third_place+((month_fourth_place+(month_fifth_place*6))/3)),2);
			if($qt == 4) return round(($sum/100)*(month_third_place+((month_fifth_place*6/4))),2);
			if($qt == 5) return round(($sum/100)*(month_third_place+(month_fifth_place)),2);
			if($qt == 6) return round(($sum/100)*(month_third_place+((month_fifth_place*4/6))),2);
			if($qt == 7) return round(($sum/100)*(month_third_place+((month_fifth_place*3/7))),2);
			if($qt == 8) return round(($sum/100)*(month_third_place+((month_fifth_place*2/8))),2);
			if($qt == 9) return round(($sum/100)*(month_third_place+((month_fifth_place/9))),2);
			else return round($sum/100*month_third_place, 2);
			break;
		
		case 4:
			if($qt == 0) return 0;
			if($qt == 1) return 0;
			if($qt == 2) return 0;
			if($qt == 3) return 0;
			if($qt == 4) return round(($sum/100)*(month_fourth_place+((month_fifth_place*6/4))),2);
			if($qt == 5) return round(($sum/100)*(month_fourth_place+(month_fifth_place)),2);
			if($qt == 6) return round(($sum/100)*(month_fourth_place+((month_fifth_place*4/6))),2);
			if($qt == 7) return round(($sum/100)*(month_fourth_place+((month_fifth_place*3/7))),2);
			if($qt == 8) return round(($sum/100)*(month_fourth_place+((month_fifth_place*2/8))),2);
			if($qt == 9) return round(($sum/100)*(month_fourth_place+((month_fifth_place/9))),2);
			else return round($sum/100*month_fourth_place, 2);
			break;
		
		case 5:
			if($qt == 0) return 0;
			if($qt == 1) return 0;
			if($qt == 2) return 0;
			if($qt == 3) return 0;
			if($qt == 4) return 0;
			if($qt == 5) return round(($sum/100)*(month_fifth_place+(month_fifth_place)),2);
			if($qt == 6) return round(($sum/100)*(month_fifth_place+((month_fifth_place*4/6))),2);
			if($qt == 7) return round(($sum/100)*(month_fifth_place+((month_fifth_place*3/7))),2);
			if($qt == 8) return round(($sum/100)*(month_fifth_place+((month_fifth_place*2/8))),2);
			if($qt == 9) return round(($sum/100)*(month_fifth_place+((month_fifth_place/9))),2);
			else return round($sum/100*month_fifth_place, 2);
			break;

		case 6:
			if($qt == 0) return 0;
			if($qt == 1) return 0;
			if($qt == 2) return 0;
			if($qt == 3) return 0;
			if($qt == 4) return 0;
			if($qt == 5) return 0;
			if($qt == 6) return round(($sum/100)*(month_fifth_place+((month_fifth_place*4/6))),2);
			if($qt == 7) return round(($sum/100)*(month_fifth_place+((month_fifth_place*3/7))),2);
			if($qt == 8) return round(($sum/100)*(month_fifth_place+((month_fifth_place*2/8))),2);
			if($qt == 9) return round(($sum/100)*(month_fifth_place+((month_fifth_place/9))),2);
			else return round($sum/100*month_fifth_place, 2);
			break;

		case 7:
			if($qt == 0) return 0;
			if($qt == 1) return 0;
			if($qt == 2) return 0;
			if($qt == 3) return 0;
			if($qt == 4) return 0;
			if($qt == 5) return 0;
			if($qt == 6) return 0;
			if($qt == 7) return round(($sum/100)*(month_fifth_place+((month_fifth_place*3/7))),2);
			if($qt == 8) return round(($sum/100)*(month_fifth_place+((month_fifth_place*2/8))),2);
			if($qt == 9) return round(($sum/100)*(month_fifth_place+((month_fifth_place/9))),2);
			else return round($sum/100*month_fifth_place, 2);
			break;

		case 8:
			if($qt == 0) return 0;
			if($qt == 1) return 0;
			if($qt == 2) return 0;
			if($qt == 3) return 0;
			if($qt == 4) return 0;
			if($qt == 5) return 0;
			if($qt == 6) return 0;
			if($qt == 7) return 0;
			if($qt == 8) return round(($sum/100)*(month_fifth_place+((month_fifth_place*2/8))),2);
			if($qt == 9) return round(($sum/100)*(month_fifth_place+((month_fifth_place/9))),2);
			else return round($sum/100*month_fifth_place, 2);
			break;

		case 9:
			if($qt == 0) return 0;
			if($qt == 1) return 0;
			if($qt == 2) return 0;
			if($qt == 3) return 0;
			if($qt == 4) return 0;
			if($qt == 5) return 0;
			if($qt == 6) return 0;
			if($qt == 7) return 0;
			if($qt == 8) return 0;
			if($qt == 9) return round(($sum/100)*(month_fifth_place+((month_fifth_place/9))),2);
			else return round($sum/100*month_fifth_place, 2);
			break;

		case 10:
			if($qt == 0) return 0;
			if($qt == 1) return 0;
			if($qt == 2) return 0;
			if($qt == 3) return 0;
			if($qt == 4) return 0;
			if($qt == 5) return 0;
			if($qt == 6) return 0;
			if($qt == 7) return 0;
			if($qt == 8) return 0;
			if($qt == 9) return 0;
			else return round($sum/100*month_fifth_place, 2);
			break;

	}
}

function getUserInfo($login) {
	return db_assoc(db_query("SELECT `login`, `balance`, `rating`, `bonus_lvl`, `knb_bonus`, `orel_bonus`, `sapper_bonus`, `partner_bonus`, `month_bonus`, `rating` FROM `".DBpref."users` WHERE `login` = '".$login."'"));

}

function setUserInfo($login, $field, $value) {
	return db_query("UPDATE `".DBpref."users` SET `".$field."` = '".$value."' WHERE `login` = '".$login."'");

}

// КНБ
function getKnbTop() {
	$knb_query = db_query("SELECT `login`, `knb_bonus` FROM `".DBpref."users` ORDER BY `knb_bonus` DESC LIMIT 3");
	$knb_top['qt'] = 0;
	while ($data = mysqli_fetch_assoc($knb_query)) {
		$knb_top[] = $data;
		if($data['knb_bonus'] != 0) $knb_top['qt']++;
	}
	return $knb_top;
}

function getKnbId() {

	$knb_login = db_assoc(db_query("SELECT `login`, `knb_bonus` FROM `".DBpref."users` WHERE `login` = '".db_string($_SESSION["login"])."'"));
	$knb_user_rate = $knb_login['knb_bonus'];
	$knb_id_query = db_assoc(db_query("SELECT COUNT(*) FROM `".DBpref."users` WHERE `knb_bonus` >= ".$knb_user_rate));
	//return print_r($knb_id_query);
	return $knb_id_query["COUNT(*)"];
}

function getKnbSum() {
	$knb_sum_query=db_assoc(db_query("SELECT SUM(knb_bonus) FROM `knb_users`")); 
	return $knb_sum_query["SUM(knb_bonus)"]/1e5;
}

// ОРЕл
function getOrelTop() {

	$orel_query = db_query("SELECT `login`, `orel_bonus` FROM `".DBpref."users` ORDER BY `orel_bonus` DESC LIMIT 3");
	$orel_top['qt'] = 0;
	while ($data = mysqli_fetch_assoc($orel_query)) {
		$orel_top[] = $data;
		if($data['orel_bonus'] != 0) $orel_top['qt']++;
	}
	return $orel_top;
}

function getOrelId() {
	$orel_login = db_assoc(db_query("SELECT `login`, `orel_bonus` FROM `".DBpref."users` WHERE `login` = '".db_string($_SESSION["login"])."'"));
	$orel_user_rate = $orel_login['orel_bonus'];
	$orel_id_query = db_assoc(db_query("SELECT COUNT(*) FROM `".DBpref."users` WHERE `orel_bonus` >= ".$orel_user_rate));
	//print_r($orel_id_query);
	return $orel_id_query["COUNT(*)"];
}

function getOrelSum() {
	$orel_sum_query = db_assoc(db_query("SELECT SUM(orel_bonus) FROM `".DBpref."users`")); 
	return $orel_sum_query["SUM(orel_bonus)"]/1e6;
}

// Сапер
function getSapperTop() {
	$sapper_query = db_query("SELECT `login`, `sapper_bonus` FROM `".DBpref."users` ORDER BY `sapper_bonus` DESC LIMIT 3");
	$sapper_top['qt'] = 0;
	while ($data = mysqli_fetch_assoc($sapper_query)) {
		$sapper_top[] = $data;
		if($data['sapper_bonus'] != 0) $sapper_top['qt']++;
	}
	return $sapper_top;
}

function getSapperId() {
	$sapper_login = db_assoc(db_query("SELECT `login`, `sapper_bonus` FROM `".DBpref."users` WHERE `login` = '".db_string($_SESSION["login"])."'"));
	$sapper_user_rate = $sapper_login['sapper_bonus'];
	$sapper_id_query = db_assoc(db_query("SELECT COUNT(*) FROM `".DBpref."users` WHERE `sapper_bonus` >= ".$sapper_user_rate));
	return $sapper_id_query["COUNT(*)"];
}

function getSapperSum() {
	$sapper_sum_query=db_assoc(db_query("SELECT SUM(sapper_bonus) FROM `".DBpref."users`")); 
	return $sapper_sum_query["SUM(sapper_bonus)"]/1e6;
}


// Партнер
function getPartnerTop() {
	$partner_query = db_query("SELECT `login`, `partner_bonus` FROM `".DBpref."users` ORDER BY `partner_bonus` DESC LIMIT 3");
	$partner_top['qt'] = 0;
	while ($data = mysqli_fetch_assoc($partner_query)) {
		$partner_top[] = $data;
		if($data['partner_bonus'] != 0) $partner_top['qt']++;
	}
	return $partner_top;
}

function getPartnerId() {
	$partner_login = db_assoc(db_query("SELECT `login`, `partner_bonus` FROM `".DBpref."users` WHERE `login` = '".db_string($_SESSION["login"])."'"));
	$partner_user_rate = $partner_login['partner_bonus'];
	$partner_id_query = db_assoc(db_query("SELECT COUNT(*) FROM `".DBpref."users` WHERE `partner_bonus` >= ".$partner_user_rate));
	return $partner_id_query["COUNT(*)"];
}

function getPartnerSum() {
	$partner_sum_query=db_assoc(db_query("SELECT SUM(partner_bonus) FROM `".DBpref."users`")); 
	return $partner_sum_query["SUM(partner_bonus)"]/1e6;
}

// Месяц
function getMonthTop() {
	$month_query = db_query("SELECT `login`, `month_bonus` FROM `".DBpref."users` ORDER BY `month_bonus` DESC LIMIT 10");
	$month_top['qt'] = 0;
	while ($data = mysqli_fetch_assoc($month_query)) {
		$month_top[] = $data;
		if($data['month_bonus'] != 0) $month_top['qt']++;
	}
	return $month_top;
}

function getMonthId() {
	$month_login = db_assoc(db_query("SELECT `login`, `month_bonus` FROM `".DBpref."users` WHERE `login` = '".db_string($_SESSION["login"])."'"));
	$month_user_rate = $month_login['month_bonus'];
	$month_id_query = db_assoc(db_query("SELECT COUNT(*) FROM `".DBpref."users` WHERE `month_bonus` >= ".$month_user_rate));
	return $month_id_query["COUNT(*)"];
}

function getMonthSum() {
	$month_sum_query=db_assoc(db_query("SELECT SUM(month_bonus) FROM `".DBpref."users`")); 
	return $month_sum_query["SUM(month_bonus)"]/1e6;
}


?>
