<?php

session_start();

function unreg(){
global $user_data;
if($user_data){
header("Location: /404.php");
exit;
}
}

function reg(){
global $user_data;
if(!$user_data){
header("Location: /404.php");
exit;
}
}

function level($level){
global $user_data;
if($user_data AND $user_data['level'] < $level OR !$user_data){
header("Location: /404.php");
exit;
}
}

function slv1($str,$msg1,$msg2,$msg3) {

$str = (int)$str;
$str1 = abs($str) % 100;
$str2 = $str % 10;
if ($str1 > 10 && $str1 < 20) return $str .' '. $msg3;
if ($str2 > 1 && $str2 < 5) return $str .' '. $msg2;
if ($str2 == 1) return $str .' '. $msg1;
  
return $str .' '. $msg3;
}

function data($times=NULL){
$time = time();
if(($time-$times)<=60){
$timesp = slv1((($time-$times)),'секунду','секунды','секунд').' назад';
return $timesp;
} else if(($time-$times)<=3600){$timesp = slv1((($time-$times)/60),'минуту','минуты','минут').' назад';
return $timesp;
} else {
$today = date("j M Y", $time);
$today = date("j M Y", $time);
$yesterday = date("j M Y", strtotime("-1 day"));
$timesp=date("j M Y  в H:i", $times);
$timesp = str_replace($today, 'Сегодня', $timesp);
$timesp = str_replace($yesterday, 'Вчера', $timesp);
$timesp = strtr($timesp, array ("Jan" => "Янв","Feb" => "Фев","Mar" => "Марта","May" => "Мая","Apr" => "Апр","Jun" => "Июня","Jul" => "Июля","Aug" => "Авг","Sep" => "Сент","Oct" => "Окт","Nov" => "Ноября","Dec" => "Дек",));

return $timesp;}
}

function navigation($all,$napage,$page,$url,$total){
if(!($all<=$napage)){
if($page-3>0)$first=' <a href="'.$url.'page=1">1</a> [...]';
if($page-2>0)$page2left=' <a class="upage" href="'.$url.'page='.($page-2).'">'.($page-2).'</a> ';
if($page-1>0)$page1left=' <a class="upage" href="'.$url.'page='.($page-1).'">'.($page-1).'</a> ';
if($page+3<=$total)$last=' <a class="upage" href="'.$url.'page='.($total).'">'.($total).'</a>';
if($page+2<=$total)$page2right=' <a class="upage" href="'.$url.'page='.($page+2).'">'.($page + 2).'</a>';
if($page+1<=$total)$page1right=' <a class="upage" href="'.$url.'page='.($page+1).'">'.($page + 1).'</a>'; 
echo '<div class="topni" style="text-align: center;"><div class="topmenu n"> <span class="o"><b>'.$first.'</b></span>'.$page2left.$page1left.'<b class="tpage">'.$page.'</b>'.$page1right.$page2right.$last.'</center></div></div>';
}
}


function filter($t){
$t = mysql_real_escape_string(nl2br(htmlspecialchars(trim($t))));
return $t;
}

function browser(){
global $ua;
if (preg_match('/opera min/i', $ua)) $browser = 'Opera Mini';
else if (preg_match('/Chrome/i', $ua))$browser = 'Chrome';
else if (preg_match('/Opera/i', $ua)) $browser = 'Opera';
else if (preg_match('/MSIE/i', $ua)) $browser = 'IE';
else if (preg_match('/Mozilla/i', $ua)) $browser = 'Mozilla';
else if (preg_match('/ucweb/i', $ua)) $browser = 'UCWeb';
else $browser = 'Неизвестно';
return $browser;
}


function str2gradient($text,$from='', $to='', $mode="hex") {
if($mode=="hex") {
$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]);
}
if( empty($text) )
return '';
else
$levels=mb_strlen($text);
if (empty($from))
$from = array(0,0,255);
else
$from = explode(",", $from);
if (empty($to))
$to = array(255,0,0);
else
$to = explode(",", $to);
$output = "";
for ($i=1;$i<=$levels;$i++) {
for ($ii=0;$ii<4;$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]);
$rgb[$ii] = mb_strtoupper($rgb[$ii]);
if (mb_strlen($rgb[$ii]) < 2) $rgb[$ii] = "0$rgb[$ii]";
}
$output .= "<font color=\"".$rgb[0].$rgb[1].$rgb[2]."\">" . $text[$i -1 ] . "</font>";
}
return $output;
}

function buildTextGradient($str, $points, $mode="hex") {
if( count($points) >= 2 && !empty($str) ){
if( count($points) == 2 ){
return iconv("windows-1251", "UTF-8", str2gradient( iconv("UTF-8", "windows-1251", $str), $points[0], $points[1], $mode ) );
}else{
$str = iconv("UTF-8", "windows-1251", $str);
$parts_count = count($points) - 1;
$let_in_part = round( strlen($str) / $parts_count );
$result = '';
for($i = 0; $i < $parts_count; $i++){
$s = substr($str, $i * $let_in_part, ($i < $parts_count ? $let_in_part : strlen($str) - $i * $let_in_part) );
$result .= str2gradient( $s, $points[ $i ], $points[ $i + 1 ], $mode );
}
return iconv("windows-1251", "UTF-8", $result );
}}
return '';
}

function nick($id){
global $HOME;
$users = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '".$id."' LIMIT 1"));

$us=null;
if($users['level']==2) $us='<font color="red">[мд]</font>';
if($users['level']==3) $us='<font color="blue">[адм]</font>';

return (empty($users)?'[Удален]':'<img src="'.$HOME.'/images/user.png" alt="*"> <a href="'.$HOME.'/user/'.$users['id'].'"><b>'.$users['login'].'</b></a> '.$us.'');
  
}

?>