<?
function user_inf($ref = NULL, $w = NULL){
global $dbs;
if (isset($ref)) $ref = check($ref);
if (!ctype_digit($ref)){
$lowuser = mb_strtolower($ref, 'UTF8');
$result = DB::$dbs->query("SELECT * FROM `blogger` WHERE `nick` = ? LIMIT 1",[$lowuser]);
} elseif (ctype_digit($ref)) {
$ref = abs(intval($ref));
$result = DB::$dbs->query("SELECT * FROM `blogger` WHERE `id` = ? LIMIT 1",[$ref]);
}
$nik = $result->fetch();
if ($w == NULL) return $nik['id'];
else return $nik[$w];
}
function timecount($var) {
$timediff= $var;
$oneMinute=60;
$oneHour=60*60;
$oneDay=60*60*24;
$dayfield=floor($timediff/$oneDay);
$hourfield=floor(($timediff-$dayfield*$oneDay)/$oneHour);
$minutefield=floor(($timediff-$dayfield*$oneDay-$hourfield*$oneHour)/$oneMinute);
$secondfield=floor(($timediff-$dayfield*$oneDay-$hourfield*$oneHour-$minutefield*$oneMinute));
$sDaysLeft=$dayfield;
$sDaysText = "дней";
$nDaysLeftLength =strlen($sDaysLeft);
$d_1=substr($sDaysLeft,-1,1);
if (substr($sDaysLeft,-2,1) != 1 && $nDaysLeftLength>1)
{
if ($d_1 == 2 || $d_1== 3 || $d_1== 4)
{
$sDaysText = "дня";
}
elseif ($d_1== 1)
{
$sDaysText = "день";
}
}
if ($nDaysLeftLength==1)
{
if ($d_1==2 || $d_1== 3 || $d_1== 4)
{
$sDaysText = "дня";
}
elseif ($d_1== 1)
{
$sDaysText = "день";
}
}
$sHoursLeft=$hourfield;
$sHoursText = "часов";
$nHoursLeftLength = strlen($sHoursLeft);
$h_1=substr($sHoursLeft,-1,1);
if (substr($sHoursLeft,-2,1) != 1 && $nHoursLeftLength>1)
{
if ($h_1== 2 || $h_1== 3 || $h_1== 4)
{
$sHoursText = "часа";
}
elseif ($h_1== 1)
{
$sHoursText = "час";
}
}
if ($nHoursLeftLength==1)
{
if ($h_1== 2 || $h_1== 3 || $h_1== 4)
{
$sHoursText = "часа";
}
elseif ($h_1== 1)
{
$sHoursText = "час";
}
}
$sMinsLeft =$minutefield;
$sMinsText = "минут";
$nMinsLeftLength = strlen($sMinsLeft);
$m_1=substr($sMinsLeft,-1,1);
if ($nMinsLeftLength>1 && substr($sMinsLeft,-2,1) != 1)
{
if ($m_1== 2 || $m_1== 3 || $m_1== 4)
{
$sMinsText = "минуты";
}
else if ($m_1== 1)
{
$sMinsText = "минута";
}
}
if ($nMinsLeftLength==1)
{
if ($m_1== 2 || $m_1==3 || $m_1== 4)
{
$sMinsText = "минуты";
}
elseif ($m_1== "1")
{
$sMinsText = "минута";
}
}
$displaystring=(($sDaysLeft)?$sDaysLeft.' '.
$sDaysText.',':NULL).' '.(($sHoursLeft)?
$sHoursLeft.' '.
$sHoursText.',':NULL).' '.(($sMinsLeft)?
$sMinsLeft.' '.
$sMinsText:NULL);
return $displaystring;
}

function Nick( $matches ) {
global $dbs;
$out = null;
if ( is_array( $matches ) ) {
$id = $matches[ 1 ];
} else {
$id = $matches;
}
$ban = DB::$dbs->queryFetch("SELECT * FROM ban WHERE user_ban = ? AND timeban > ? LIMIT 1", [$id,time()]);
$bans = ($ban > 0 ? '[<small><font color="red">Бан</font></small>]':NULL);
$u = DB::$dbs->queryFetch( "SELECT `id`,`nick`,`sex`,`last`,`mosh`,`level`,`nickcolor1`,`nickcolor2`,`icon`,`us_browser` FROM `blogger` WHERE (`id`='" . $id . "')" );
if ( $u != 0 ) {
$out .= '<a href="/users/' . $u['id'] . '"> <b>' . ( isset( $u[ 'nickcolor1' ], $u[ 'nickcolor2' ] ) ? gradient( $u[ 'nick' ], $u[ 'nickcolor1' ], $u[ 'nickcolor2' ] ) : $u[ 'nick' ] ) . '</b></a> '.$bans;
if ( $u[ 'mosh' ] != 0 ) {
$out .= ' <font color="green">[<font color="red"><b>мошенник</b></font>]</font>';
}
        if ($u['id'] == 1) { 
     // $out .= ' <span style="color:red;">[Тест]</span> '; 
        } 

        if ($u['level']!= 0) { 
       $out .= '<span data-toggle="tooltip" title="Команда '.home.'"><img src="/assets/img/support.png"style="width:16px;height:16px;"></span>'; 
        } 

} else {
$out = '<img src="/files/ico/mon.png" alt="*" style="width:18px;height:18px;"/><b>Гость</a></b>';
}
return $out;
}

function gen( $var ) {
return substr( str_shuffle( '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ), 0, $var );
}
// функция обработки чисел
function num($var) {
return abs((int)$var);
}
function gradient( $string, $from = '', $to = '' ) {
$out    = null;
$string = iconv( 'utf-8', 'windows-1251', $string );
$to     = [ hexdec( $to[ 0 ] . $to[ 1 ] ), hexdec( $to[ 2 ] . $to[ 3 ] ), hexdec( $to[ 4 ] . $to[ 5 ] ) ];
$from   = [ hexdec( $from[ 0 ] . $from[ 1 ] ), hexdec( $from[ 2 ] . $from[ 3 ] ), hexdec( $from[ 4 ] . $from[ 5 ] )];
$levels = strlen( $string );
for ( $i = 1; $i <= $levels; $i++ ) {
for ( $ii = 0; $ii < 3; $ii++ ) {
$tmp[ $ii ] = $from[ $ii ] - $to[ $ii ];
$tmp[ $ii ] = floor( $tmp[ $ii ] / $levels );
$rgb[ $ii ] = $from[ $ii ] - ( $tmp[ $ii ] * $i );
if ( $rgb[ $ii ] > 255 ) {
$rgb[ $ii ] = 255;
}
$rgb[ $ii ] = dechex( $rgb[ $ii ] );
if ( strlen( $rgb[ $ii ] ) < 2 ) {
$rgb[ $ii ] = '0' . $rgb[ $ii ];
}
}
$out .= '<span style="color: #' . $rgb[ 0 ] . $rgb[ 1 ] . $rgb[ 2 ] . '">' . $string[ $i - 1 ] . '</span>';
}
return iconv( 'windows-1251', 'utf-8', $out );
}
function NickN( $matches ) {
global $dbs;
$out = null;
if ( is_array( $matches ) ) {
$id = $matches[ 1 ];
} else {
$id = $matches;
}
$ban = DB::$dbs->queryFetch("SELECT * FROM ban WHERE user_ban = ? AND timeban > ? LIMIT 1", [$id,time()]);
$bans = ($ban > 0 ? '[<small><font color="red">Бан</font></small>]':NULL);
$u = DB::$dbs->queryFetch( "SELECT `id`,`nick`,`sex`,`last`,`mosh`,`level`,`nickcolor1`,`nickcolor2`,`icon`,`us_browser` FROM `blogger` WHERE (`id`='" . $id . "')" );
             
if ( $u != 0 ) {
$out .= ' <b>' . ( isset( $u[ 'nickcolor1' ], $u[ 'nickcolor2' ] ) ? gradient( $u[ 'nick' ], $u[ 'nickcolor1' ], $u[ 'nickcolor2' ] ) : $u[ 'nick' ] ) . '</b> '.$bans;
if ( $u[ 'mosh' ] != 0 ) {
$out .= ' <font color="green">[<font color="red"><b>мошенник</b></font>]</font>';
}

        if ($u['level']!= 0) { 
       $out .= '<span data-toggle="tooltip" title="Команда '.home.'"><img src="/assets/img/support.png"style="width:16px;height:16px;"></span>'; 
        } 

} else {
$out = '<img src="/files/ico/mon.png" alt="*" style="width:18px;height:18px;"/><b>Гость</a></b>';
}
return $out;
}
function check($var) {
return trim(htmlspecialchars($var, ENT_QUOTES, 'UTF-8'));
}

function err($var) {
if (isset($var)) echo ' <div class="alert alert-danger" role="alert">'.$var.'</div>';
}
function calc_age($data)
{
	$ex_age = explode('.', $data);
	$age = date('Y') - $ex_age[2];
	if (date('m') < $ex_age[1] || (date('m') == $ex_age[1] && date('d') < $ex_age[0])) {
		$age--;
	}
	$age = (int)$age;

     $q1 = abs($age) % 100;
     $q2 = $age % 10;
     if ($q1 > 10 && $q1 < 20) {
     	$result = 'лет';
     } elseif ($q2 > 1 && $q2 < 5) {
     	$result = 'года';
     } elseif ($q2 == 1) {
     	$result = 'год';
     } else {
     	$result = 'лет';
     }

	if ($age == date('Y')) $age = 0;
		return $age . ' ' . $result; 
}
?>