<?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: capcha.php
*
*/

function clean($var){
	$replace = array(
					'"' => '', 
					"'" => '', 
					'`' => '', 
					'{' => '', 
					'}' => '', 
					'<' => '', 
					'>' => '',
					'%' => '',
					'/' => '',
					'' => ''
					
					);
	
	return @htmlspecialchars(str_replace(array_keys($replace), array_values($replace), trim($var)));
	unset($var, $replace);
}

function mdHashPass($var)
{
	return md5('Powered By WmRush:'.$var);
}

function GetUser($var, $id)
{
	global $mysql;
	$a = $mysql->prepare("SELECT `$var` FROM db_users WHERE Uid = ?");
	$a->execute(array($id));
	$row = $a->fetch();
	foreach($row as $key => $value) {
		return($value);
	}
}


function ReutingUser($UserId, $UserLevel, $Reut, $CountLevel, $Reuting)
{
	global $mysql;
	include("CofigFarm.php");
	//$FullOpyt = $CountLevel;
	if(($CountLevel - $Reut) > $Reuting  and $UserLevel <= 12)
	{
		$s = $mysql->prepare("UPDATE db_users SET Reuting = Reuting + ? WHERE Uid = ?");
		$s->execute(array($Reuting, $UserId));
	}
	elseif(($CountLevel - $Reut) < $Reuting and $UserLevel <= 11)
	{
		$gg = $CountLevel - $Reut;
		$ggg = $Reuting - $gg;
		$f = $mysql->prepare("UPDATE db_users SET Reuting = ?, Level = Level + ? WHERE Uid = ?");
		$f->execute(array($ggg, 1, $UserId));
		
		for($i = 1; $i <= 12; $i++)
		{
			$ReutUser = GetUser("Reuting", $UserId);
			$LevUser = GetUser("Level", $UserId);
			if($Lev[$LevUser] < $ReutUser and $LevUser < 12)
			{
				$f = $mysql->prepare("UPDATE db_users SET Level = Level + ? WHERE Uid = ?");
				$f->execute(array(1, $UserId));
			}
		}
		
		
		
		
	}
	elseif(($CountLevel - $Reut) == $Reuting and $UserLevel <= 11)
	{
		$f = $mysql->prepare("UPDATE db_users SET Reuting = ?, Level = Level + ? WHERE Uid = ?");
		$f->execute(array(0, 1, $UserId));
	}
	else
	{
		$f = $mysql->prepare("UPDATE db_users SET Reuting = Reuting + ? WHERE Uid = ?");
		$f->execute(array($Reuting, $UserId));
	}
}


function validefloat($str) {

	return (isset($str)) ? filter_var($str, FILTER_VALIDATE_FLOAT) : false;
	
}


function CountRefUser($var)
{
	global $mysql;
	$q = $mysql->prepare("SELECT * FROM db_users WHERE RefId = ?");
	$q->execute(array(intval($var)));
	return $q->rowCount();
}

//Statistic


function FullUser()
{
	global $mysql;
	$q = $mysql->query("SELECT Uid FROM db_users");
	return $q->rowCount();
}

function FullUserEvery()
{
	global $mysql;
	$dd = time() - 86400;
	$q = $mysql->query("SELECT Uid FROM db_users WHERE DateReg >= '$dd'");
	return $q->rowCount();
}

function FullEnter()
{
	global $mysql;
	$e = $mysql->query("SELECT Uid FROM db_enter WHERE Status = '1'");
	if($e->rowCount() == 0)
	{
		return 0;
	}
	else
	{
		$q = $mysql->query("SELECT SUM(Summa) AS `Sum` FROM db_enter WHERE Status = '1'");
		$w = $q->fetch();
		return $w['Sum'];
	}
}


function FullVivod()
{
	global $mysql;
	$e = $mysql->query("SELECT Uid FROM db_vivod WHERE Status = '1'");
	if($e->rowCount() == 0)
	{
		return 0;
	}
	else
	{
		$q = $mysql->query("SELECT SUM(SummaRub) AS `Sum` FROM db_vivod WHERE Status = '1'");
		$w = $q->fetch();
		return $w['Sum'];
	}
}

function generate_password($number)
{
    $arr = array('a','b','c','d','e','f',
                 'g','h','i','j','k','l',
                 'm','n','o','p','r','s',
                 't','u','v','x','y','z',
                 'A','B','C','D','E','F',
                 'G','H','I','J','K','L',
                 'M','N','O','P','R','S',
                 'T','U','V','X','Y','Z',
                 '1','2','3','4','5','6',
                 '7','8','9','0','.',',',
                 '(',')','[',']','!','?',
                 '&','^','%','@','*','$',
                 '<','>','/','|','+','-',
                 '{','}','`','~');
    // Генерируем пароль
    $pass = "";
    for($i = 0; $i < $number; $i++)
    {
      // Вычисляем случайный индекс массива
      $index = rand(0, count($arr) - 1);
      $pass .= $arr[$index];
    }
    return $pass;
 }

?>